Getting Started with Ape
The Ape Framework is an easy-to-use Web3 development tool. Developers can compile, test, and interact with smart contracts all in one command line session. With its modular plugin system, Ape supports multiple contract languages and chains including Rootstock.
In this guide, we will learn about the Ape Framework and its benefits for smart contract development, how to setup your development environment, create a Ape project and execute a deployment script for Rootstock.
Prerequisites
To get started with Ape, ensure the following tools are installed:
- Linux or macOS
- Python 3.9 up to 3.12
- Windows: Install Windows Subsystem Linux (WSL
- Check the python version in a terminal with python3 --version.
Create a Ape project
To start a new project with Ape, install Ape and then create a new one:
-
Create a directory for the project
mkdir ape && cd ape -
Install pipx
Only neccessary if you don't have pipx installed:
python3 -m pip install --user pipx
python3 -m pipx ensurepath
-
Install Ape using pipx
pipx install eth-ape -
Create an empty project
ape init -
Enter a name for your project
ape init
Please enter project name: ape-rootstock-demo
SUCCESS: ape-rootstock-demo is written in ape-config.yaml
ls
ape-config.yaml contracts scripts tests
A common project structure looks like this:
project # The root project directory
├── contracts/ # Project source files, such as '.sol' or '.vy' files
│ └── smart_contract_example.sol # Sample of a smart contract
├── tests/ # Project tests, ran using the 'ape test' command
│ └── test_sample.py # Sample of a test to run against your sample contract
├── scripts/ # Project scripts, such as deploy scripts, ran using the 'ape run <`name>' command
│ └── deploy.py # Sample script to automate a deployment of an ape project
└── ape-config.yaml # The ape project configuration file
You can configure the ape project using the ape-config.yaml file. See the configuration guide for a more detailed explanation of the settings to adjust.
Create an Account
We will create an account and send funds to it before we can deploy a smart contract or interact with previously deployed contracts from the Ape project. Run the following command to generate an account.
ape accounts generate <ALIAS>
Use
devto replace ALIAS.
You will be prompted to add random input to enhance the security and add a password to encrypt the account.
ape accounts generate dev
Enhance the security of your account by adding additional random input:
Show mnemonic? [Y/n]: n
Create Passphrase to encrypt account:
Repeat for confirmation:
SUCCESS: A new account '0x260C915483943bf65596c298D2b46b8D67fF2FE5' with HDPath m/44'/60'/0'/0/0 has been added with the id 'dev'
If you do not want to see your mnemonic, select n. Alternatively, use the --hide-mnemonic option to skip the prompt.
Don't forget to add funds to the account generated. To get tRBTC, use the Rootstock Faucet. Additional faucet options include; Thirdweb and Blast Faucets.
To import an existing account check Importing Existing Accounts documentation.
Write your first contract
As an example, You can use the following Box contract to store and retrieve a value.
Fist create a file named Box.solinside the contracts directory:
touch contracts/Box.sol
Open the file and add the following contract to it:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
contract Box {
uint256 private value;
event ValueChanged(uint256 newValue);
function store(uint256 newValue) public {
value = newValue;
emit ValueChanged(newValue);
}
function retrieve() public view returns (uint256) {
return value;
}
}
Compile the contract
Before compiling the Solidity, ensure to install the Solidity compiler plugin. Running the following command will install the latest version of the plugin:
ape plugins install solidity
To use a specific version of Solidity or a specific EVM version, modify the ape-config.yaml file as follows:
solidity:
version: INSERT_VERSION
evm_version: INSERT_VERSION
For more information about the Solidity plugin, check ape-solidity
After installation, compile the contract using the following command:
ape compile
Result:
ape compile
INFO: Compiling using Solidity compiler '0.8.25+commit.b61c2a91'.
Input:
contracts/Box.sol
SUCCESS: 'local project' compiled.
After compilation, you can find the bytecode and ABI for your contracts in the .build directory.