Ape
Using Ape to Deploy To Phron
Introduction
Ape is an Ethereum development environment that helps Python developers manage and automate the recurring tasks inherent to building smart contracts and DApps. Ape can directly interact with Phron's Ethereum API, so you can also use Ape to deploy smart contracts on Phron.
This guide will walk you through using Ape to compile, deploy, and interact with Ethereum smart contracts on the Phron TestNet.
Checking Prerequisites
To get started, ensure you have the following:
MetaMask installed and connected to Phron
An account with funds. You can get DEV tokens for testing on Phron once every 24 hours from the Phron Faucet
To test out the examples in this guide on Phron, you will need to have your own endpoint and API key, which you can get from one of the supported Endpoint Providers
Creating an Ape Project
If you don't already have an Ape project, you must install Ape and create a new one. You can follow the steps below to get started and create an empty project:
Create a directory for your project
If you don't have
pipx
installed, install itCreate a project
Enter a name for your project
Your Ape project contains a bare-bones ape-config.yaml
file for customizing specific settings and the following empty directories:
contracts
- an empty directory for storing smart contractsscripts
- an empty directory for storing Python scripts, such as deployment scripts and scripts to interact with your deployed contractstests
- an empty directory for pytest testing scripts
Configure Accounts
You'll need to import an account before you can deploy smart contracts or interact with previously deployed contracts from your Ape project. You can run the following command, which will import your account and give it a name:
You'll then be prompted to enter your private key and add a password to encrypt your account.
NoteIf you wish to use a mnemonic instead, you can append the
--use-mnemonic
option to the import command.
Create Smart Contracts
Now that you have set up your account, you can start writing smart contracts. As a basic example, you can use the following Box
contract to store a value you can retrieve later.
Start by creating a file named Box.sol
inside the contracts directory:
Open the file and add the following contract to it:
You can store any additional contracts in the contracts
directory.
Compile the Solidity Contract
Before compiling the Solidity, you must install the Solidity compiler plugin. Running the following command will install the latest version of the plugin:
To use a specific version of Solidity or a specific EVM version, you can modify your ape-config.yaml
file as follows:
For more information on the Solidity plugin, please refer to the README of the ape-solidity
repository on GitHub.
With the Solidity plugin installed, the next step is to compile the smart contract:
After compilation, you can find the bytecode and ABI for your contracts in the .build
directory.
Test the Contract
Before you deploy your contract, you can test it out directly inside your Ape project using the pytest framework to make sure it works as you expect.
You should already have a tests
directory where you'll create your tests, but if not, please create one, as all tests must be located in a directory named tests
. Additionally, each test file name must start with test_
and end with .py
. So, first, you can create a test file for the Box.sol
contract:
In addition to the test file, you can create a conftest.py
file that will define a couple of essential fixtures. Fixtures allow you to define functions that set up the necessary environment or resources to run your tests. Note that while the Box.sol
contract is simple, incorporating fixtures into your testing process is good practice.
To create the file, you can run the following command:
Since your tests will rely on the injection of the fixtures, you must define the fixtures first. When defining fixtures, you need to apply the pytest.fixture
decorator above each function. For this example, you'll create two fixtures: one that defines the owner of the contract and one that deploys the contract from the owner's account.
The owner
fixture will use the built-in accounts
fixture to take the first account in the list of test accounts provided by Ape and return it. The box
fixture will deploy the Box
contract type using the built-in project
fixture, you simply have to provide the name of the contract and use the owner
fixture to deploy it.
tests/conftest.py
Now that you've created the fixtures, you can start creating your tests. Each test function name must start with test_
and describe what the test does. For this example, you can use test_store_value
, as you'll create a test for the store
function. The test will store a value and then retrieve it, asserting that the retrieved value is equal to the value passed into the store
function.
To use the owner
and box
fixtures, you must pass them into your test function, which will inject the fixtures automatically for you to use. The owner
account will be used to call the store
function of the box
contract instance.
tests/test_box.py
And that's it! That's all you'll need inside your test file. You can use the following command to run the test:
The results of running the test will be printed to the terminal.
Now that you have confidence in your contract, the next step is to deploy it.
Deploy the Contract
To deploy your contracts, create a deployment script named deploy.py
inside of the scripts
directory:
Next, you'll need to write the deployment script. You'll need to load the account you will use to deploy the contract and access it by its name using the project manager.
scripts/deploy.py
Now you're ready to deploy the Box
contract! To configure your project for Phron, you will need to have your own endpoint and API key, which you can get from one of the supported Endpoint Providers.
Take the following steps to initiate and send the deployment transaction:
Run the deployment script using the
ape run deploy
command
Review the transaction details and enter y to sign the transaction
Enter your passphrase for your account
Enter y to leave your account unlocked or n to lock it
After you follow the prompts and submit the transaction, the transaction hash, total fees paid, and contract address will be displayed in the terminal.
Congratulations! Your contract is live! Save the address to interact with your contract in the following section.
Interact with the Contract
You can interact with contracts using the Ape console for quick debugging and testing, or write a script.
Using The Ape Console
To interact with your newly deployed contract, you can launch the Ape console by running:
Next, you'll need to create a contract instance using the contract's address:
Now, you can interact with your contract instance! For example, you can set the variable to be stored in the Box
contract using the following commands:
Call the
store
method by passing in a value to store and the account you want to use to send the transaction:Review the transaction details and enter y to sign the transaction
If you previously locked your account, you must enter your passphrase to unlock it. Otherwise, Ape will use the cached key for your account
If you unlocked your account in the previous step, you'll be asked if you want to leave your account unlocked. You can enter y to leave it unlocked or n to lock it
After you follow the prompts and submit the transaction, the transaction hash and total fees paid will be displayed in the terminal.
Then, you can retrieve the stored value by calling the retrieve
method:
The number you just stored in the previous steps will be printed to the console.
contract.retrieve() 4
Using a Script
You can also write a script to interact with your newly deployed contract. To get started, you can create a new file in the scripts
directory:
Next, you can write a script that stores and retrieves a value. Note that when creating a contract instance to interact with, you must pass in the address of the deployed contract.
scripts/store-and-retrieve.py
Now, you can run the script to set the stored value and retrieve it:
Run the script
Review the transaction details and enter y to sign the transaction
If you previously locked your account, you must enter your passphrase to unlock it. Otherwise, Ape will use the cached key for your account
If you unlocked your account in the previous step, you'll be asked if you want to leave your account unlocked. You can enter y to leave it unlocked or n to lock it
Once completed, you should see a transaction hash and a value of 4
printed to the console.
Congratulations! You have successfully deployed and interacted with a contract using Ape!
This tutorial is for educational purposes only. As such, any contracts or code created in this tutorial should not be used in production.The information presented herein has been provided by third parties and is made available solely for general information purposes. Phron does not endorse any project listed and described on the Phron Doc Website (https://docs.Phron.ai/). Phron does not warrant the accuracy, completeness or usefulness of this information. Any reliance you place on such information is strictly at your own risk. Phron disclaims all liability and responsibility arising from any reliance placed on this information by you or by anyone who may be informed of any of its contents. All statements and/or opinions expressed in these materials are solely the responsibility of the person or entity providing those materials and do not necessarily represent the opinion of Phron. The information should not be construed as professional or financial advice of any kind. Advice from a suitably qualified professional should always be sought in relation to any particular matter or circumstance. The information herein may link to or integrate with other websites operated or content provided by third parties, and such other websites may link to this website. Phron has no control over any such other websites or their content and will have no liability arising out of or related to such websites or their content. The existence of any such link does not constitute an endorsement of such websites, the content of the websites, or the operators of the websites. These links are being provided to you only as a convenience and you release and hold Phron harmless from any and all liability arising from your use of this information or the information provided by any third-party website or service.
Last updated