Brownie
Brownie is a Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine. In this tutorial, we will guide you through configuring Brownie (or eth-brownie) for Conflux eSpace and demonstrate how to use Brownie scripts to deploy contracts on Conflux eSpace.
安装 Brownie
执行以下命令安装 Brownie:
pip install eth-brownie # or pip3 install eth-brownie
添加 Conflux eSpace 网络
To add the Conflux eSpace networks to Brownie, execute these commands:
brownie networks add "Conflux eSpace" conflux-espace-main name=Mainnet host=https://evm.confluxrpc.com explorer=https://evm.confluxscan.io chainid=1030
brownie networks add "Conflux eSpace" conflux-espace-test name=Testnet host=https://evmtestnet.confluxrpc.com explorer=https://evmtestnet.confluxscan.io chainid=71
Alternatively, import the Conflux eSpace networks to Brownie using a yaml
file:
live:
- name: Conflux eSpace
networks:
- chainid: 1030
explorer: https://evm.confluxscan.io
host: https://evm.confluxrpc.com
id: conflux-espace-main
name: Mainnet
- chainid: 71
explorer: https://evmtestnet.confluxscan.io
host: https://evmtestnet.confluxrpc.com
id: conflux-espace-test
name: Testnet
然后,运行以下命令将其添加到您的网络配置:
brownie networks import ./network-config.yaml
Upon successful addition, the networks will appear in the Brownie network list, as shown by running brownie networks list
:
......
Conflux eSpace
├─Testnet: conflux-espace-test
└─Mainnet: conflux-espace-main
......
Generate Template Token Project
To generate a template token project, run brownie bake token
. The project will be created in the current folder. For this tutorial, we will place the project directly under the user directory:
cd ~
brownie bake token
cd token
Generate/Import Your Account
You have the option to either generate a new account or import an existing one for Brownie.
Generate a New Account
To create a new account via the command line:
brownie accounts generate <id>
You will be prompted to set a password for the account. Brownie will then generate a random private key and make the account accessible as <id>
. The address of the new account will be displayed in the terminal, which you will need in subsequent steps.
Here is an example output of the command:
Brownie v1.14.5 - Python development framework for Ethereum
Generating a new private key...
mnemonic: 'park service pull home hedgehog soul grief food people uncle will series'
Enter the password to encrypt this account with:
SUCCESS: A new account '0x960ecb222F296C1D75a111D33094Cb393ab17b09' has been generated with the id 'new'
Import Your Account
If you already have an account, you can import it. To import your private key, run:
# id is the identifier of your account, used to specify the account in scripts
brownie accounts new <id>
# for example, brownie accounts new dev
Brownie will prompt you to enter your secret key and password:
Brownie v1.14.5 - Python development framework for Ethereum
Enter the private key you wish to add: *******************
Enter the password to encrypt this account with: **************
SUCCESS: A new account 'xxxxxxxxxxxx' has been generated with the id 'dev'
It's also possible to import your keystore using:
brownie accounts import <id> <path>
For more information, refer to account management.
Fund Your Account
To send transactions, you need to fund your account. The Conflux's eSpace faucet can assist with this.
After entering your account address, your account will receive funds on the eSpace testnet.
Modify Deployment Script
The default scripts/token.py
in the template token
project is not directly usable for the Conflux eSpace testnet. An extra line needs to be added to the default script:
#!/usr/bin/python3
from brownie import Token, accounts
def main():
accounts.load("dev") # Specify which account to load here
return Token.deploy("Test Token", "TST", 18, 1e21, {'from': accounts[0]})