A simple example to show how to connect python with blockchain smart contracts.
- Infura: Account and Key to access to a blockchain's node.
- Alchemy: Alternative to infura.
-
Clone this repository with git
-
Install nodeJs, npm and python
-
Create an Infura or Alchemy Accounts
-
Install truffle framework (npm install -g truffle)
-
Install python dependencies (pip install web3)
-
Install dependencies (npm i)
-
Compile and migrate contracts
truffle develop
After truffle console start:
compile
migrate
Those steps will create the smart contracts binaries inside the ./compiled/contracts
folder. You could change that in trufle-config.js.
In this repo, only we could create the smart contracts binaries and run tests over them. Inside truffle console (truffle develop):
truffle develop
test
To deploy to test network.
# replace <NET> with desire network: rinkeby, goerli, mainnet
truffle migrate --reset --network <NET>
python3 example.py -r <Provider> -e <providerKeyEth> -p <providerKeyPolygon> -a <addressAcount> -k <addressAccountKey> -n <networkToUse>
# Provider: INFURA or ALCHEMY
# Provider Key Eth: Your Key (in Infura is your projectId)
# Provider Key Polygon: Your Key
# Address Account: An Address to use with contract
# Address Key: Key from Address Account
# Network To Use: RINKEBy or POLYGON (if you need other network, you have yo change example code in example.py)
These are the networks and addresses where contract is deployed:
- Ethereum testnet (rinkeby): 0xb497D1572210AAF11D0C39b8307dC73EAB480cfC
- Polygon testnet (mumbai): 0xf05eD004881fD8B0eeeAed908d53FDdD43525C12