EVM
A Python wrapper around an Embedded Rust based Ethereum Virtual Machine.
Constructor
- class PyEvm
Create and return an instance of the EVM that uses an
in-memorydatabase.
Example:
>>> from simular import PyEvm
>>> evm = PyEvm()
Methods
- static PyEvm.from_fork(url: str, blocknumber: int = None)
Create and return an instance of the EVM that will pull state from a remote Ethereum node.
- Parameters:
url – the url (
https://...) to a remote Ethereum node with JSON-RPC supportblocknumber – (optional) the specific blocknumber to pull state at. If
None, the latest block will be used.
- Returns:
an instance of the EVM
Example:
>>> from simular import PyEvm
>>> evm = PyEvm.from_fork('http://...', blocknumber=195653)
- static PyEvm.from_snapshot(snapshot: str)
Create and return an instance of the EVM from a previously created
snapshot. Seecreate_snapshotbelow.- Parameters:
snapshot – a (str) serialized snapshot
- Returns:
an instance of the EVM
Example:
Assume hello_snap.json is a file from a saved snapshot.
>>> from simular import PyEvm
# load the json file
>>> with open('hello_snap.json') as f:
... snap = f.read()
>>> evm = PyEvm.from_snapshot(snap)
- create_snapshot()
Create a JSON formatted snapshot of the current state of the EVM.
- Returns:
(str) the serialized state
Example:
>>> evm = PyEvm()
# do stuff with the EVM
>>> snap = evm.create_snapshot()
- create_account(address: str, balance=None)
Create an account
Note
See
utils.create_account- Parameters:
address – (str) a valid, hex-encoded Ethereum address
balance – (int) and optional balance in
weito set for the account. If None, balance = 0
- get_balance(address: str)
Get the balance of the given address
- Parameters:
address – (str) a valid, hex-encoded Ethereum address
- Returns:
the balance in
wei
- transfer(caller: str, to: str, amount: int)
Transfer
amountinweifromcaller -> to- Parameters:
caller – (str) a valid, hex-encoded Ethereum address
to – (str) a valid, hex-encoded Ethereum address
amount – (int) the amount to transfer
Warning
This will fail if the
callerdoes not have a sufficient balance to transfer
Example:
>>> from similar import PyEvm, create_account
>>> evm = PyEvm()
# create an account for Bob with 1 Ether
>>> bob = create_account(evm, value=int(1e18))
# create an account for Alice with no balance
>>> alice = create_account(evm)
# transfer 1 Ether from Bob to Alice
>>> evm.transfer(bob, alice, int(1e18))
# check Alice's balance
>>> evm.get_balance(alice)
1000000000000000000
- advance_block(interval=None)
This method provides the ability to simulate the mining of blocks. It will advance block.number and block.timestamp.
It’s not necessary to call this method. However, some contracts may have logic that need this information.
- Parameters:
interval – (int) optional. set the time in seconds between blocks. Default is 12 seconds
Example:
>>> evm.advance_block()