Last active
June 15, 2019 22:02
-
-
Save robrobbins/d51b955b64d5455ae404c12dabfad269 to your computer and use it in GitHub Desktop.
Using the Computable.py library with IPython for an Ethereum TestNet Session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # NOTE: In this example we expect that you have a locally running geth (full) node broadcasting to a (private) test network. | |
| # That being the case, we assume you have started geth with the correct commands and have access to a `geth.ipc` | |
| # you'll need a web3.py instance first as computable.py works with it | |
| import web3 | |
| from web3 import Web3 | |
| # the assumption of a test network means PoA | |
| from web3.middleware import geth_poa_middleware | |
| # tells web3 what to connect to and how | |
| provider = web3.IPCProvider('path_to_your/geth.ipc') | |
| w3 = Web3(provider) | |
| # inject the poa middleware | |
| w3.middleware_stack.inject(geth_poa_middleware, layer=0) | |
| # now you should be able to talk to your node | |
| w3.eth.getBlock('latest') | |
| -> ... | |
| # you may need to import an account into your node, check your local accounts first | |
| w3.eth.accounts | |
| -> [...] # if you don't have any known accounts on your local node, use the code below | |
| # to import an acct we can use the web3 importRawKey. you'll need 2 things. the privateKey for the account and the passphrase | |
| # used when making it. | |
| w3.personal.importRawKey(the_private_key, the_passphrase) | |
| -> ... | |
| # if you have a keystore file, and need to extract the private_key from it you can do this | |
| keyfile = open('path_to/keyfile') | |
| encrypted = keyfile.read() | |
| keyfile.close() | |
| the_private_key = w3.eth.account.decrypt(encrypted, the_passphrase) # obv you have to know the passphrase yourself, now do importRawKey... | |
| # with those steps performed you should see the account | |
| w3.eth.accounts | |
| -> [your_account] | |
| # with an account you could check its balance | |
| w3.eth.getBalance(your_account) | |
| -> ... | |
| # i like to divide the wei numbers by 1e18 to see them in ETH | |
| ONE_ETH = 1000000000000000000 | |
| w3.eth.getBalance(your_account) / ONE_ETH | |
| -> much_better | |
| # computable.py works with web3, not attempting to replace it or do anything it does. some methods in the library will | |
| # expect a web3 instance as an argument. As such it's handy to set the default account to the one you are using | |
| w3.eth.defaultAccount = your_account | |
| # let's say we'd like to interact with a deployed Computable EtherToken, first import it | |
| from computable.contracts import EtherToken | |
| # All Computable Higher-Order-Contracts are constructed with whatever account you are using | |
| ether_token = EtherToken(your_account) | |
| # you can always see what account an HOC will default to using | |
| ether_token.account | |
| -> your_account | |
| # before we can use the HOC to communicate with the deployed contract we must tell it where it is | |
| ether_token.at(w3, address_of_the_deployed_ether_token) # notice we pass the w3 instance with this | |
| # now it should be set | |
| ether_token.address | |
| -> address_of_the_deployed_ether_token | |
| # computable.py exposes helpers for call and transact type methods | |
| from computable.helpers.transaction import call, transact | |
| # we also expose helpers for locking and unlocking your account | |
| from computable.helpers.account import lock, unlock | |
| # NOTE: in this example we are working with a local geth node you are connecting to via IPC for a test network so | |
| # the use of `unlock` is safe. We also expose helpers for offline signing safely when on main-net. That will be covered | |
| # in another gist. | |
| # to make a call you don't need an unlocked account. say you want to check your ether_token balance | |
| call(ether_token.balance_of(your_account)) / ONE_ETH | |
| -> ... | |
| # before we can call any state changing methods, unlock your account for 5 min (the default in the library) | |
| unlock(w3, the_passphrase) | |
| -> True | |
| # let's deposit some ETH to the ether_token. With our account unlocked we can simply use the `transact` helper | |
| tx_hash = transact(ether_token.deposit(SOME_AMOUNT_OF_WEI, {'gasPrice': Web3.toWei(N, 'gwei')})) | |
| # note that we saved the transaction hash as the var | |
| tx_hash | |
| -> Hexbytes(...) | |
| # we can use that tx_hash to get a transaction receipt | |
| rct = w3.eth.getTransactionReceipt(tx_hash) | |
| -> AttributeDict({...}) | |
| # we could fetch any logs (events) from that receipt | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment