Ape provides an interactive console with useful pre-defined locals to interact with your project.
Ape console - Unplugged
Ape console comes with a few fresh features out of the box but really comes alive when combined with ape plugins.
In this first section, we'll explore the unplugged version and later see how the power of ape console is magnified with plugins.
What Is Ape Console?
Ape console is an interactive Python shell with ape-specific features. The console can be activated from the command line to access a Python interactive environment, like IPython.
The --help or -h flag will display a message showing available options with usage and description information.
$ ape console --help
Ape Console Command Line
Let's start by exploring the ape console command line.
The --verbosity or -v flag is used to pass the message status level for the current session from the following values:
ERROR - most restrictive, only error messages
WARNING
SUCCESS
INFO
DEBUG - least restrictive; all messages
$ ape console --verbosity ERROR
$ ape console -v DEBUG
Ape Console Namespace - Pre-defined Locals
Ape console comes with pre-defined local variables, classes, and methods. In a more advanced tutorial, we'll look at how you can add your own locals to the ape console.
The chain variable provides access to the currently connected blockchain; it requires an active provider. This access is useful for development purposes, such as controlling the state of the blockchain. It's also handy for querying data about the chain.
The config variable provides access to manage configuration settings within the current project. Ape configuration settings are stored in the ape-config.yaml file but can be interactively accessed in the console.
The project variable providers access to the currently active project.
from ape import project # "project" is the ProjectManager for the active project
from ape import Project # Is a ProjectManager
# MyContractType (example) is contract type in the active project
contract_type = project.MyContactType
Ape's console, combined with the Alchemy and/or Infura provider plugins, provides access to the underlying service provider. Plugin installation is simple and straightforward once your API KEY is generated within the Alchemy and/or Infura website and set in your Python environment.
# Please see ape alchemy and ape infura for more details on provider plugins
$ ape plugins install alchemy infura -y
# INFO: Installing alchemy...
# SUCCESS: Plugin 'alchemy==0.2.0' has been installed.
# INFO: Installing infura...
# SUCCESS: Plugin 'infura==0.2.0' has been installed.
$ ape console --network :mainnet:alchemy
Now we can switch within the console between the two providers.
Ape's console, combined with the account plugin, provides access to an account manager for loading, creating, and interfacing with accounts.
# Please see ape accounts for more details on CLI accounts
# ape accounts generate [ALIAS_OF_ACCOUNT]
$ ape accounts generate demo_account
# Add extra entropy for key generation...:
# Create Passphrase:
# Repeat for confirmation:
# SUCCESS: A new account '0xB1b1A92A6f7435ba375e4cc470aED1e951e92fe5' has been added with the id 'demo_account'
If we use the CLI to generate an account, it will then be available in the console via the alias.
Ape's console, combined with the Vyper plugin, provides access to the compilers for the current project.
# Please ape vyper for more details on compilers
# ape plugins install [NAME_OF_PLUGIN]
$ ape plugins install vyper -y
# INFO: Installing vyper...
# SUCCESS: Plugin 'vyper==0.2.0' has been installed.
After we've installed the compiler plugin, we can access it in the console.
The Contract class provides a class for creating a contract instance. Combining this class with the Etherscan plugin allows a user to create instances of verified contracts from the blockchain.
$ ape plugins install etherscan alchemy -y
# INFO: Installing etherscan...
# SUCCESS: Plugin 'etherscan==0.2.0' has been installed.
# INFO: Installing alchemy...
# SUCCESS: Plugin 'alchemy==0.2.0' has been installed.
$ ape console --network :mainnet:alchemy
In the console, use the Contract class to create the instance.