Skip to content

pyethereum Cheat Sheet

Ren Zhang edited this page Jul 7, 2017 · 1 revision

Adapted from Using pyethereum.tester.

All the following commands assume you already did:

from ethereum.tools import tester
from ethereum import utils
s = tester.Chain()

Convenience accounts

The tester module generates ten private keys: tester.k0 ... tester.k9 and their corresponding addresses tester.a0 ... tester.a9.

An address of an account is the truncated hash result of the public key corresponding with the private key, which is 20 bytes or an 160-bit integer. One benefit of elliptic curve cryptography is that the public key can be computed from the private key. You can compute the address from the private key in pyethereum:

tester.a0 == utils.privtoaddr(tester.k0)

Interacting with the blockchain

  • tester.Chain() creates a new test-blockchain with a genesis block.
    • Returns the chain.
  • s.contract(code, language='evm') creates a contract with the given code, in the given language. Acceptable arguments are 'evm', 'serpent', 'solidity' and 'viper'. If any of the latter three are used, returns an ABIContract object, of which you can then call functions.
  • Assuming x is an ABIContract object, you can use x.address as the address of the contract on the blockchain, which is 20 bytes. To get the 160-bit integer version, use
xAddressInt = util.coerce_to_int(x.address)
  • x.foo(parameters, value=numberInWei, sender=privateKey) calls a function foo in ABIContract x. The transaction is signed with privateKey, sending value numberInWei. The value must be an integer in the unit of wei, 10^18 wei = 1 ether. When not relevant, value and sender can be omitted.
    • Returns the return value of the transaction execution. As all inputs and outputs in serpent are treated as 32-byte words (or 256-bit integer), when multiple outputs are expected, the number of outputs needs to be specified with outsz:
# in your serpent code, specify you have multiple outputs!
def ask(key):
  return([self.registry[key].owner, self.registry[key].value], items=2)
# end of serpent code
...
# in your pyethereum code:
o = x.ask('a', outsz=2) # assuming the ask function takes 'a' as an input and returns two values
print(str(o[0])) # print the first output
print(str(o[1])) # print the second output
  • s.tx(private_key, to, value, data=b'') sends a transaction using the given private key to the given address with the given value and data.
    • Returns the return value of the transaction execution.
    • See Examples/2.mul3.py for a minimum working example.
  • s.mine(n=1, coinbase=tester.a0) pretend-mines n times, with coinbase the blockmaker. When not relevant, both the miner and the number of blocks can be omitted.
    • Returns nothing.
  • s.snapshot() returns a snapshot object.
  • s.revert(snapshot) takes a snapshot as input and reverts to that snapshot.

Getting data out of the state

  • s.head_state returns the State object at the head. You can then access various methods of this including get_code(addr), get_balance(addr), get_nonce(addr) and get_storage_at(addr, key) and also various variables including , block_coinbase, block_difficulty, block_number, timestamp, prev_headers[0].hash, and:
    • s.head_state.receipts returns the receipts of transactions executed in the current block.
    • s.head_state.gas_used returns how much gas are consumed by transactions in the current block.
    • s.head_state.gas_limit returns the current gas limit. if gas_used is about to exceeds gas_limit, you cannot process more transactions in this block.
  • s.block.transactions returns the transactions included in the block