|
| 1 | +# Python ContractKit |
| 2 | + |
| 3 | +Celo's ContractKit is a library to help developers and validators to interact with the celo-blockchain. |
| 4 | + |
| 5 | +ContractKit supports the following functionality: |
| 6 | + |
| 7 | +- Connect to a node |
| 8 | +- Access web3 object to interact with node's Json RPC API |
| 9 | +- Send Transaction with celo's extra fields: (feeCurrency) |
| 10 | +- Simple interface to interact with CELO and cUSD |
| 11 | +- Simple interface to interact with Celo Core contracts |
| 12 | +- Utilities |
| 13 | + |
| 14 | +## User Guide |
| 15 | + |
| 16 | +### Getting Started |
| 17 | + |
| 18 | +To install: |
| 19 | + |
| 20 | +```bash |
| 21 | +pip install . |
| 22 | +``` |
| 23 | + |
| 24 | +You need in Python version 3.8 or higher. |
| 25 | + |
| 26 | +To start working with python contractkit you need a `Kit` instance: |
| 27 | + |
| 28 | +```python |
| 29 | +from celo_sdk.kit import Kit |
| 30 | + |
| 31 | +kit = Kit('https://alfajores-forno.celo-testnet.org') |
| 32 | +``` |
| 33 | + |
| 34 | +To access web3: |
| 35 | + |
| 36 | +```python |
| 37 | +kit.w3.eth.getBalance(some_address) |
| 38 | +``` |
| 39 | + |
| 40 | +### Setting Default Tx Options |
| 41 | + |
| 42 | +`Kit` allows you to set default transaction options: |
| 43 | + |
| 44 | +```python |
| 45 | +from celo_sdk.kit import Kit |
| 46 | + |
| 47 | +kit = Kit('https://alfajores-forno.celo-testnet.org') |
| 48 | +currency_address = kit.base_wrapper.registry.load_contract_by_name('StableToken')['address'] |
| 49 | +kit.wallet_fee_currency = currency_address |
| 50 | +``` |
| 51 | + |
| 52 | +### Interacting with CELO & cUSD |
| 53 | + |
| 54 | +celo-blockchain has two initial coins: CELO and cUSD (stableToken). |
| 55 | +Both implement the ERC20 standard, and to interact with them is as simple as: |
| 56 | + |
| 57 | +```python |
| 58 | +gold_token = kit.base_wrapper.create_and_get_contract_by_name('GoldToken') |
| 59 | +balance = gold_token.balance_of(address) |
| 60 | +``` |
| 61 | + |
| 62 | +To send funds: |
| 63 | + |
| 64 | +```python |
| 65 | +one_gold = kit.w3.toWei(1, 'ether') |
| 66 | +tx_hash = gold_token.transfer(address, one_gold) |
| 67 | +``` |
| 68 | + |
| 69 | +To interact with cUSD, is the same but with a different contract: |
| 70 | + |
| 71 | +```python |
| 72 | +stable_token = kit.base_wrapper.create_and_get_contract_by_name('StableToken') |
| 73 | +``` |
| 74 | + |
| 75 | +If you would like to pay fees in cUSD, set the gas price manually: |
| 76 | + |
| 77 | +```python |
| 78 | +stable_token = kit.base_wrapper.create_and_get_contract_by_name('StableToken') |
| 79 | +gas_price_contract = kit.base_wrapper.create_and_get_contract_by_name('GasPriceMinimum') |
| 80 | +gas_price_minimum = gas_price_contract.get_gas_price_minimum(stable.address) |
| 81 | +gas_price = gas_price_minimum * 1.3 # Wiggle room if gas price minimum changes before tx is sent |
| 82 | +kit.wallet_fee_currency = stable_token.address # Default to paying fees in cUSD |
| 83 | +kit.wallet_gas_price = gas_price |
| 84 | + |
| 85 | +tx = stable_token.transfer(recipient, wei_transfer_amount) |
| 86 | +``` |
| 87 | + |
| 88 | +### Interacting with Other Contracts |
| 89 | + |
| 90 | +Apart from GoldToken and StableToken, there are many core contracts. |
| 91 | + |
| 92 | +For the moment, we have contract wrappers for: |
| 93 | + |
| 94 | +- Accounts |
| 95 | +- Attestations |
| 96 | +- BlockchainParameters |
| 97 | +- DoubleSigningSlasher |
| 98 | +- DowntimeSlasher |
| 99 | +- Election |
| 100 | +- LockedGold |
| 101 | +- Escrow |
| 102 | +- Exchange |
| 103 | +- Freezer |
| 104 | +- GasPriceMinimum |
| 105 | +- GoldToken |
| 106 | +- Governance |
| 107 | +- MultiSig |
| 108 | +- Reserve |
| 109 | +- ReleaseGold |
| 110 | +- SortedOracles |
| 111 | +- StableToken |
| 112 | +- Validators |
| 113 | + |
| 114 | +## A Note About Contract Addresses |
| 115 | + |
| 116 | +Celo Core Contracts addresses, can be obtained by looking at the `Registry` contract. |
| 117 | +That's actually how `BaseWrapper` obtain them. |
| 118 | + |
| 119 | +We expose the registry api, which can be accessed by: |
| 120 | + |
| 121 | +```python |
| 122 | +gold_token_address = kit.base_wrapper.registry.load_contract_by_name('GoldToken')['address'] |
| 123 | +``` |
| 124 | + |
| 125 | +## Adding new keys to the wallet |
| 126 | + |
| 127 | +Wallet object by defaut generate some private key |
| 128 | + |
| 129 | +But of course you can add your own already generated private key or generate new one |
| 130 | + |
| 131 | +To add your own: |
| 132 | + |
| 133 | +```python |
| 134 | +from celo_sdk.kit import Kit |
| 135 | + |
| 136 | +kit = Kit('https://alfajores-forno.celo-testnet.org') |
| 137 | +kit.wallet_add_new_key = '0xf2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257d' |
| 138 | +``` |
| 139 | + |
| 140 | +To generate new one: |
| 141 | + |
| 142 | +```python |
| 143 | +new_key = kit.generate_new_key() |
| 144 | +kit.wallet_add_new_key = new_key |
| 145 | +``` |
| 146 | + |
| 147 | +To see all the wallet accounts: |
| 148 | + |
| 149 | +```python |
| 150 | +accounts = kit.wallet.accounts |
| 151 | +``` |
| 152 | + |
| 153 | +And to switch between accounts: |
| 154 | + |
| 155 | +```python |
| 156 | +kit.wallet_change_account = existing_account_address # address of account has to be in wallet.__accounts dict |
| 157 | +``` |
| 158 | + |
| 159 | +## Signing messages with wallet |
| 160 | + |
| 161 | +In addition to signing the transaction, the wallet can sign messages: |
| 162 | + |
| 163 | +```python |
| 164 | +from celo_sdk.kit import Kit |
| 165 | + |
| 166 | +kit = Kit('https://alfajores-forno.celo-testnet.org') |
| 167 | +signature = kit.wallet.sign_message(b'some message') |
| 168 | +``` |
0 commit comments