Skip to content

Instantly share code, notes, and snippets.

@dylanjw
Created April 6, 2018 04:40
Show Gist options
  • Save dylanjw/cac15da222b933616e1a6cd0ad5515af to your computer and use it in GitHub Desktop.
Save dylanjw/cac15da222b933616e1a6cd0ad5515af to your computer and use it in GitHub Desktop.
import time
import web3
from solc import compile_source
from web3 import Web3, EthereumTesterProvider
contract_source = '''
pragma solidity ^0.4.18;
contract WETH9 {
string public name = "Wrapped Ether";
string public symbol = "WETH";
uint8 public decimals = 18;
event Approval(address indexed src, address indexed guy, uint wad);
event Transfer(address indexed src, address indexed dst, uint wad);
event Deposit(address indexed dst, uint wad);
event Withdrawal(address indexed src, uint wad);
mapping (address => uint) public balanceOf;
mapping (address => mapping (address => uint)) public allowance;
function() public payable {
deposit();
}
function deposit() public payable {
balanceOf[msg.sender] += msg.value;
Deposit(msg.sender, msg.value);
}
function withdraw(uint wad) public {
require(balanceOf[msg.sender] >= wad);
balanceOf[msg.sender] -= wad;
msg.sender.transfer(wad);
Withdrawal(msg.sender, wad);
}
function totalSupply() public view returns (uint) {
return this.balance;
}
function approve(address guy, uint wad) public returns (bool) {
allowance[msg.sender][guy] = wad;
Approval(msg.sender, guy, wad);
return true;
}
function transfer(address dst, uint wad) public returns (bool) {
return transferFrom(msg.sender, dst, wad);
}
function transferFrom(address src, address dst, uint wad)
public
returns (bool)
{
require(balanceOf[src] >= wad);
if (src != msg.sender && allowance[src][msg.sender] != uint(-1)) {
require(allowance[src][msg.sender] >= wad);
allowance[src][msg.sender] -= wad;
}
balanceOf[src] -= wad;
balanceOf[dst] += wad;
Transfer(src, dst, wad);
return true;
}
}
'''
def wait_on_tx_receipt(tx_hash):
start_time = time.time()
while True:
if start_time + 60 < time.time():
raise TimeoutError("Timeout occurred waiting for tx receipt")
if w3.eth.getTransactionReceipt(tx_hash):
return w3.eth.getTransactionReceipt(tx_hash)
compiled_sol = compile_source(contract_source)
contract_interface = compiled_sol['<stdin>:WETH9']
contract_abi = contract_interface['abi']
contract_code = contract_interface['bin']
w3 = Web3(EthereumTesterProvider())
contract = w3.eth.contract(
abi=contract_abi,
bytecode=contract_code)
tx_hash = contract.constructor().transact(
transaction={
'from': w3.eth.accounts[0],
'gas': 410000})
tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
contract_address = tx_receipt['contractAddress']
# Instatiate contract
WETH9 = w3.eth.contract(address=contract_address, abi=contract_interface['abi'])
# Create log filter instance
_filter = WETH9.events.Deposit.createFilter(fromBlock=0)
# Event emitting transaction
tx_hash = WETH9.functions.deposit().transact()
# Wait for transaction to be mined
receipt = wait_on_tx_receipt(tx_hash)
# Poll filter
print(_filter.get_new_entries())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment