Ape provides an interactive console with useful pre-defined locals to interact with your project.
Ape console - Unplugged
Ape console comes with a few features fresh 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, much like IPython.
The --help or -h flag will display a message showing available options with usage and description information.
Ape Console Command Line
Let's start by exploring the ape console command line.
The --network flag is used to pass the name of an ecosystem, a network, and a provider for the current session.
The format for this value is [ecosystem-name]:[network-name]:[provider-name].
The default ecosystem is ethereum.
To see a list of available networks use the network list command below.
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
- DEBUG - least restrictive, all messages
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 accounts variable provides access to manage accounts, both aliased personal accounts (see Ape Console - Plugged In) and pre-funded test accounts.
The chain variable provides access to the currently connected blockchain; 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 compilers variable provides access to a compiler manager.
The convert method provides a conversion utility function.
The networks variable provides access to a network manager for the current project.
The project variable providers access to the currently active project.
The Project class provides the ability to load a project from a given path.
Ape console - Plugged in
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.
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.
If we use the CLI to generate an account, it will then be available in the console via the alias.
To return to our previous state, we can delete the account.
Ape's console combined with the Vyper plugin provides access to the compilers for the current project.
After we've installed the compiler plugin, we can access it in the console.
To return to our previous state, we can uninstall the Vyper plugin.
The config variable allows loading Projects dynamically.
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.
In the console, use the Contract class to create the instance.