Using Web3.py will clarify everything behind the scene when using frameworks like Hardhat or Brownie
$ ganache
-> this launch the local blockchain development environment$ pip install -r requirements.txt
$ python3 deploy.py
TelephoneDirectory.sol
: all contracts code heredeploy.py
: deploy python script using Web3.py.env
: store all environment variables (testing addresses and private keys)export PRIVATE_KEY=0xabcde...
(need to set up.gitignore
- it’s volatile and dangerous, not recommended)- use
python-dotenv
package to pull the env var directly in python.
.gitignore
: make sure to put.env
requirements.txt
: python dependencies
- a local simulated blockchain environment for development purpose (similar to Rinkeby testnet, but local)
- has both GUI and CLI
- npm install ganache
- simulated accounts with balance, address, private key and mnemonics
- Read contract file
- Compile contract file (
pip install py-solc-x
) - Connect to Ganache
- require bytecode and ABI
- Connect to Ganache
- RPC server: :
- Network ID: 1337 (for Ganache)
- An account address to deploy from (with private key)
- Create the contract in Python
- Get nonce (for the latest transaction)
- Deploy to blockchain → build a transaction cause we are making a state change to the blockchain
- Build the contract deploy transaction
- Sign the transaction
- Send the transaction
- Wait for the transaction to be mined (confirmation) and see the transaction receipt.
- Interact and work with the deployed contract
- ALWAYS requires 2 things:
Contract Address
andABI
- 2 ways of interaction
Call
: does NOT make any state changes; only get return value; only get info from blockchain e.g., view functionsTransact
: will attempt to (and may or may not) make state changes to the blockchain
- Making a transaction is similar to deploy a contract (since they are both making state changes to the blockchain), so it also follows the steps from 6.a to 6.d
- ALWAYS requires 2 things:
- use blockchain endpoint provided by centralized services
- Infura, alchemy → these services will give you an endpoint (RPC server address) to the blockchain
- only need to change the config in step 3.b (RPC server address, Network ID and account address)
- run your own blockchain node
- go-ethereum (geth)
- To check and see the actual transaction on either mainnet or testnet → etherscan.io