diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c2b17bf --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.vy linguist-language=Python diff --git a/tests/conftest.py b/tests/conftest.py index 0f0eafe..47c19de 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,16 @@ import os import pytest - -from ethereum.tools import tester -from ethereum import utils as ethereum_utils -import vyper +import logging +import eth_tester +import web3 + +from vyper import compiler +from functools import wraps +from eth_tester import EthereumTester +from eth_tester.exceptions import TransactionFailed +from web3.providers.eth_tester import EthereumTesterProvider +from web3 import Web3 +from web3.contract import ConciseContract, ConciseMethod ''' run tests with: python -m pytest -v @@ -14,68 +21,129 @@ ERC20_CODE = open(os.path.join(OWN_DIR, os.pardir, 'contracts/ERC20.vy')).read() FACTORY_CODE = open(os.path.join(OWN_DIR, os.pardir, 'contracts/uniswap_factory.vy')).read() +class VyperMethod(ConciseMethod): + ALLOWED_MODIFIERS = {'call', 'estimateGas', 'transact', 'buildTransaction'} -@pytest.fixture -def t(): - tester.s = tester.Chain() - return tester + def __call__(self, *args, **kwargs): + return self.__prepared_function(*args, **kwargs) + def __prepared_function(self, *args, **kwargs): + if not kwargs: + modifier, modifier_dict = 'call', {} + fn_abi = [x for x in self._function.contract_abi if x['name'] == self._function.function_identifier].pop() + modifier_dict.update({'gas': fn_abi.get('gas', 0) + 50000}) # To make tests faster just supply some high gas value. + elif len(kwargs) == 1: + modifier, modifier_dict = kwargs.popitem() + if modifier not in self.ALLOWED_MODIFIERS: + raise TypeError( + "The only allowed keyword arguments are: %s" % self.ALLOWED_MODIFIERS) + else: + raise TypeError("Use up to one keyword argument, one of: %s" % self.ALLOWED_MODIFIERS) -# @pytest.fixture(scope="module") -@pytest.fixture() -def chain(): - s = tester.Chain() - s.head_state.gas_limit = 10**9 - return s + return getattr(self._function(*args), modifier)(modifier_dict) -@pytest.fixture -def utils(): - return ethereum_utils +class VyperContract(ConciseContract): + def __init__(self, classic_contract, method_class=VyperMethod): + super().__init__(classic_contract, method_class) -@pytest.fixture -def get_contract(chain): - def get_contract(source_code, *args, **kwargs): - return chain.contract(source_code, language="vyper", *args, **kwargs) - return get_contract +############ +# PATCHING # +############ -@pytest.fixture -def assert_tx_failed(t): - def assert_tx_failed(function_to_test, exception=tester.TransactionFailed): - initial_state = t.s.snapshot() - with pytest.raises(exception): - function_to_test() - # t.s.revert(initial_state) - return assert_tx_failed +setattr(eth_tester.backends.pyevm.main, 'GENESIS_GAS_LIMIT', 10**9) +setattr(eth_tester.backends.pyevm.main, 'GENESIS_DIFFICULTY', 1) + + +def set_evm_verbose_logging(): + logger = logging.getLogger('evm') + logger.setLevel('TRACE') + + +# Useful options to comment out whilst working: +# set_evm_verbose_logging() +# vdb.set_evm_opcode_debugger() + + +@pytest.fixture(autouse=True) +def patch_log_filter_remove(monkeypatch): + + def Filter_remove(self, *values): + + def get_key(v): + return v.get('transaction_hash'), v.get('log_index'), v.get('transaction_index') + + values_to_remove = set([ + get_key(value) + for value in values + ]) + + queued_values = self.get_changes() + self.values = [ + value + for value + in self.get_all() + if get_key(value) not in values_to_remove + ] + for value in queued_values: + if get_key(value) in values_to_remove: + continue + self.queue.put_nowait(value) + + monkeypatch.setattr(eth_tester.utils.filters.Filter, 'remove', Filter_remove) + + +@pytest.fixture(autouse=True) +def patch_is_encodeable_for_fixed(monkeypatch): + original_is_encodable = web3.utils.abi.is_encodable + + def utils_abi_is_encodable(_type, value): + from eth_utils import is_integer + from eth_abi.abi import process_type + try: + base, sub, arrlist = _type + except ValueError: + base, sub, arrlist = process_type(_type) + + if not arrlist: + if base == 'fixed' and not arrlist: + return True + elif base == 'int': + if not is_integer(value): + return False + exp = int(sub) + if value < -1 * 2**(exp - 1) or value > 2**(exp - 1) + 1: + return False + return True + + # default behaviour + return original_is_encodable(_type, value) + + monkeypatch.setattr(web3.utils.abi, 'is_encodable', utils_abi_is_encodable) @pytest.fixture -def get_logs(): - def get_logs(receipt, contract, event_name=None): - contract_log_ids = contract.translator.event_data.keys() # All the log ids contract has - # All logs originating from contract, and matching event_name (if specified) - logs = [log for log in receipt.logs - if log.topics[0] in contract_log_ids and - log.address == contract.address and - (not event_name or - contract.translator.event_data[log.topics[0]]['name'] == event_name)] - assert len(logs) > 0, "No logs in last receipt" +def tester(): + return EthereumTester() - # Return all events decoded in the receipt - return [contract.translator.decode_event(log.topics, log.data) for log in logs] - return get_logs + +def zero_gas_price_strategy(web3, transaction_params=None): + return 0 # zero gas price makes testing simpler. @pytest.fixture -def get_last_log(get_logs): - def get_last_log(tester, contract, event_name=None): - receipt = tester.s.head_state.receipts[-1] # Only the receipts for the last block - # Get last log event with correct name and return the decoded event - print(get_logs(receipt, contract, event_name=event_name)) - return get_logs(receipt, contract, event_name=event_name)[-1] - return get_last_log +def w3(tester): + w3 = Web3(EthereumTesterProvider(tester)) + w3.eth.setGasPriceStrategy(zero_gas_price_strategy) + return w3 + + +@pytest.fixture +def keccak(): + return Web3.sha3 + # @pytest.fixture # def bytes_helper(): @@ -83,6 +151,7 @@ def get_last_log(tester, contract, event_name=None): # return bytes(str, 'utf-8') + bytearray(length - len(str)) # return bytes_helper + @pytest.fixture def pad_bytes32(): def pad_bytes32(instr): @@ -91,47 +160,171 @@ def pad_bytes32(instr): return bstr + (32 - len(bstr)) * b'\x00' return pad_bytes32 + +def _get_contract(w3, source_code, *args, **kwargs): + abi = compiler.mk_full_signature(source_code) + bytecode = '0x' + compiler.compile(source_code).hex() + contract = w3.eth.contract(abi=abi, bytecode=bytecode) + + value = kwargs.pop('value', 0) + value_in_eth = kwargs.pop('value_in_eth', 0) + value = value_in_eth * 10**18 if value_in_eth else value # Handle deploying with an eth value. + gasPrice = kwargs.pop('gasPrice', 0) + deploy_transaction = { + 'from': w3.eth.accounts[0], + 'data': contract._encode_constructor_data(args, kwargs), + 'value': value, + 'gasPrice': gasPrice, + } + tx = w3.eth.sendTransaction(deploy_transaction) + address = w3.eth.getTransactionReceipt(tx)['contractAddress'] + contract = w3.eth.contract(address, abi=abi, bytecode=bytecode, ContractFactoryClass=VyperContract) + # Filter logs. + contract._logfilter = w3.eth.filter({ + 'fromBlock': w3.eth.blockNumber - 1, + 'address': contract.address + }) + return contract + + +@pytest.fixture +def get_contract(w3): + def get_contract(source_code, *args, **kwargs): + return _get_contract(w3, source_code, *args, **kwargs) + return get_contract + + +def get_compiler_gas_estimate(code, func): + if func: + return compiler.gas_estimate(code)[func] + 22000 + else: + return sum(compiler.gas_estimate(code).values()) + 22000 + + +def check_gas_on_chain(w3, tester, code, func=None, res=None): + gas_estimate = get_compiler_gas_estimate(code, func) + gas_actual = tester.get_block_by_number('latest')['gas_used'] + # Computed upper bound on the gas consumption should + # be greater than or equal to the amount of gas used + if gas_estimate < gas_actual: + raise Exception("Gas upper bound fail: bound %d actual %d" % (gas_estimate, gas_actual)) + + print('Function name: {} - Gas estimate {}, Actual: {}'.format( + func, gas_estimate, gas_actual) + ) + + +def gas_estimation_decorator(w3, tester, fn, source_code, func): + def decorator(*args, **kwargs): + @wraps(fn) + def decorated_function(*args, **kwargs): + result = fn(*args, **kwargs) + if 'transact' in kwargs: + check_gas_on_chain(w3, tester, source_code, func, res=result) + return result + return decorated_function(*args, **kwargs) + return decorator + + +def set_decorator_to_contract_function(w3, tester, contract, source_code, func): + func_definition = getattr(contract, func) + func_with_decorator = gas_estimation_decorator( + w3, tester, func_definition, source_code, func + ) + setattr(contract, func, func_with_decorator) + + +@pytest.fixture +def get_contract_with_gas_estimation(tester, w3): + def get_contract_with_gas_estimation(source_code, *args, **kwargs): + + contract = _get_contract(w3, source_code, *args, **kwargs) + for abi in contract._classic_contract.functions.abi: + if abi['type'] == 'function': + set_decorator_to_contract_function( + w3, tester, contract, source_code, abi['name'] + ) + return contract + return get_contract_with_gas_estimation + + +@pytest.fixture +def get_contract_with_gas_estimation_for_constants(w3): + def get_contract_with_gas_estimation_for_constants( + source_code, + *args, **kwargs): + return _get_contract(w3, source_code, *args, **kwargs) + return get_contract_with_gas_estimation_for_constants + + +@pytest.fixture +def assert_tx_failed(tester): + def assert_tx_failed(function_to_test, exception=TransactionFailed): + snapshot_id = tester.take_snapshot() + with pytest.raises(exception): + function_to_test() + tester.revert_to_snapshot(snapshot_id) + return assert_tx_failed + + +@pytest.fixture +def assert_compile_failed(): + def assert_compile_failed(function_to_test, exception=Exception): + with pytest.raises(exception): + function_to_test() + return assert_compile_failed + + +@pytest.fixture +def get_logs(w3): + def get_logs(tx_hash, c, event_name): + tx_receipt = w3.eth.getTransactionReceipt(tx_hash) + logs = c._classic_contract.events[event_name]().processReceipt(tx_receipt) + return logs + return get_logs + + +@pytest.fixture +def exchange_abi(): + return compiler.mk_full_signature(EXCHANGE_CODE) + + @pytest.fixture -def exchange_abi(chain): - chain.mine() - return tester.languages['vyper'].mk_full_signature(EXCHANGE_CODE) +def exchange_bytecode(): + return '0x' + compiler.compile(EXCHANGE_CODE).hex() @pytest.fixture -def exchange_template(chain): - chain.mine() - return chain.contract(EXCHANGE_CODE, language='vyper') +def exchange_template(get_contract): + return get_contract(EXCHANGE_CODE) @pytest.fixture -def omg_token(chain): - chain.mine() - return chain.contract(ERC20_CODE, language='vyper', args=["OMG Token", "OMG", 18, 100000*10**18]) +def omg_token(get_contract): + return get_contract(ERC20_CODE, *[b"OMG Token", b"OMG", 18, 100000*10**18]) @pytest.fixture -def dai_token(chain): - chain.mine() - return chain.contract(ERC20_CODE, language='vyper', args=["DAI Token", "DAI", 18, 100000*10**18]) +def dai_token(get_contract): + return get_contract(ERC20_CODE, *[b"DAI Token", b"DAI", 18, 100000*10**18]) @pytest.fixture -def exchange_factory(chain, exchange_template, assert_tx_failed): - chain.mine() - factory_contract = chain.contract(FACTORY_CODE, language='vyper') - factory_contract.initializeFactory(exchange_template.address) +def exchange_factory(get_contract, exchange_template): + factory_contract = get_contract(FACTORY_CODE) + factory_contract.initializeFactory(exchange_template.address, transact={}) return factory_contract @pytest.fixture -def omg_exchange(t, chain, exchange_factory, exchange_abi, omg_token): - chain.mine() - omg_exchange_address = exchange_factory.createExchange(omg_token.address) - return t.ABIContract(chain, exchange_abi, omg_exchange_address) +def omg_exchange(w3, exchange_abi, exchange_bytecode, exchange_factory, omg_token): + exchange_factory.createExchange(omg_token.address, transact={}) + address = exchange_factory.getExchange(omg_token.address) + return w3.eth.contract(address, abi=exchange_abi, bytecode=exchange_bytecode, ContractFactoryClass=VyperContract) @pytest.fixture -def dai_exchange(t, chain, exchange_factory, exchange_abi, dai_token): - chain.mine() - dai_exchange_address = exchange_factory.createExchange(dai_token.address) - return t.ABIContract(chain, exchange_abi, dai_exchange_address) +def dai_exchange(w3, exchange_abi, exchange_bytecode, exchange_factory, dai_token): + exchange_factory.createExchange(dai_token.address, transact={}) + address = exchange_factory.getExchange(dai_token.address) + return w3.eth.contract(address, abi=exchange_abi, bytecode=exchange_bytecode, ContractFactoryClass=VyperContract) diff --git a/tests/exchange/test_ERC20.py b/tests/exchange/test_ERC20.py index 0983bb5..ac3af4b 100644 --- a/tests/exchange/test_ERC20.py +++ b/tests/exchange/test_ERC20.py @@ -1,9 +1,10 @@ -def test_ERC20(t, omg_token, pad_bytes32): +def test_ERC20(w3, omg_token, pad_bytes32): + a0, a1 = w3.eth.accounts[:2] assert omg_token.name() == pad_bytes32('OMG Token') assert omg_token.symbol() == pad_bytes32('OMG') assert omg_token.decimals() == 18 assert omg_token.totalSupply() == 100000*10**18 - assert omg_token.balanceOf(t.a0) == 100000*10**18 - omg_token.transfer(t.a1, 1*10**18, startgas=51875) - assert omg_token.balanceOf(t.a0) == 100000*10**18 - 1*10**18 - assert omg_token.balanceOf(t.a1) == 1*10**18 + assert omg_token.balanceOf(a0) == 100000*10**18 + omg_token.transfer(a1, 1*10**18, transact={}) + assert omg_token.balanceOf(a0) == 100000*10**18 - 1*10**18 + assert omg_token.balanceOf(a1) == 1*10**18 diff --git a/tests/exchange/test_eth_to_token.py b/tests/exchange/test_eth_to_token.py index 18bc3bd..f8afbb1 100644 --- a/tests/exchange/test_eth_to_token.py +++ b/tests/exchange/test_eth_to_token.py @@ -1,107 +1,111 @@ -def test_swap_default(t, chain, omg_token, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.approve(omg_exchange.address, 10*10**18) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) - # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 +def test_swap_default(w3, omg_token, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) + # # Starting balances of UNI exchange + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # BUYER converts ETH to UNI - chain.tx(to=omg_exchange.address, startgas=67388, value=1*10**18, sender=t.k1) - # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 6*10**18 + w3.eth.sendTransaction({'to': omg_exchange.address, 'gas': 67122, 'value': 1*10**18, 'from': a1}) + # # Updated balances of UNI exchange + assert w3.eth.getBalance(omg_exchange.address) == 6*10**18 assert omg_token.balanceOf(omg_exchange.address) == 8337502084375521096 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1662497915624478904 - assert chain.head_state.get_balance(t.a1) == 1*10**24 - 1*10**18 + assert omg_token.balanceOf(a1) == 1662497915624478904 + assert w3.eth.getBalance(a1) == 1*10**24 - 1*10**18 -def test_swap(t, chain, omg_token, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.approve(omg_exchange.address, 10*10**18) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) +def test_swap(w3, omg_token, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # BUYER converts ETH to UNI - # assert omg_exchange.ethToTokenSwap(1, deadline, value=1*10**18, startgas=62707, sender=t.k1) == 1662497915624478904 # Cost 1 - omg_exchange.ethToTokenSwap(1, deadline, value=1*10**18, startgas=67907, sender=t.k1) + omg_exchange.ethToTokenSwap(1, deadline, transact={'gas': 66929, 'value': 1*10**18, 'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 6*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 6*10**18 assert omg_token.balanceOf(omg_exchange.address) == 8337502084375521096 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1662497915624478904 - assert chain.head_state.get_balance(t.a1) == 1*10**24 - 1*10**18 + assert omg_token.balanceOf(a1) == 1662497915624478904 + assert w3.eth.getBalance(a1) == 1*10**24 - 1*10**18 -def test_transfer(t, chain, omg_token, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.approve(omg_exchange.address, 10*10**18) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) +def test_transfer(w3, omg_token, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Starting balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(a2) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.ethToTokenTransfer(1, deadline, t.a2, value=1*10**18, startgas=68750, sender=t.k1) + omg_exchange.ethToTokenTransfer(1, deadline, a2, transact={'gas': 68397, 'value': 1*10**18, 'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 6*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 6*10**18 assert omg_token.balanceOf(omg_exchange.address) == 8337502084375521096 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 - 1*10**18 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 - 1*10**18 # Updated balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 1662497915624478904 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 1662497915624478904 + assert w3.eth.getBalance(a2) == 1*10**24 -def test_swap_exact(t, chain, omg_token, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.approve(omg_exchange.address, 10*10**18) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) +def test_swap_exact(w3, omg_token, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.ethToTokenSwapExact(1662497915624478904, deadline, value=2*10**18, startgas=77000, sender=t.k1) + omg_exchange.ethToTokenSwapExact(1662497915624478904, deadline, transact={'gas': 76394, 'value': 2*10**18, 'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 6*10**18 - 1 + assert w3.eth.getBalance(omg_exchange.address) == 6*10**18 - 1 assert omg_token.balanceOf(omg_exchange.address) == 8337502084375521096 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1662497915624478904 - assert chain.head_state.get_balance(t.a1) == 1*10**24 - 1*10**18 + 1 + assert omg_token.balanceOf(a1) == 1662497915624478904 + assert w3.eth.getBalance(a1) == 1*10**24 - 1*10**18 + 1 -def test_transfer_exact(t, chain, omg_token, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.approve(omg_exchange.address, 10*10**18) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) +def test_transfer_exact(w3, omg_token, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Starting balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(a2) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.ethToTokenTransferExact(1662497915624478904, deadline, t.a2, value=2*10**18, startgas=78000, sender=t.k1) + omg_exchange.ethToTokenTransferExact(1662497915624478904, deadline, a2, transact={'gas': 77862, 'value': 2*10**18, 'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 6*10**18 - 1 + assert w3.eth.getBalance(omg_exchange.address) == 6*10**18 - 1 assert omg_token.balanceOf(omg_exchange.address) == 8337502084375521096 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 - 1*10**18 + 1 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 - 1*10**18 + 1 # Updated balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 1662497915624478904 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 1662497915624478904 + assert w3.eth.getBalance(a2) == 1*10**24 diff --git a/tests/exchange/test_factory.py b/tests/exchange/test_factory.py index 6b947a3..123c239 100644 --- a/tests/exchange/test_factory.py +++ b/tests/exchange/test_factory.py @@ -1,23 +1,58 @@ -def test_factory(t, chain, utils, exchange_abi, exchange_template, omg_token, exchange_factory, assert_tx_failed, pad_bytes32): +from web3.contract import ( + ConciseContract, + ConciseMethod +) + +class VyperMethod(ConciseMethod): + ALLOWED_MODIFIERS = {'call', 'estimateGas', 'transact', 'buildTransaction'} + + def __call__(self, *args, **kwargs): + return self.__prepared_function(*args, **kwargs) + + def __prepared_function(self, *args, **kwargs): + if not kwargs: + modifier, modifier_dict = 'call', {} + fn_abi = [x for x in self._function.contract_abi if x['name'] == self._function.function_identifier].pop() + modifier_dict.update({'gas': fn_abi.get('gas', 0) + 50000}) # To make tests faster just supply some high gas value. + elif len(kwargs) == 1: + modifier, modifier_dict = kwargs.popitem() + if modifier not in self.ALLOWED_MODIFIERS: + raise TypeError( + "The only allowed keyword arguments are: %s" % self.ALLOWED_MODIFIERS) + else: + raise TypeError("Use up to one keyword argument, one of: %s" % self.ALLOWED_MODIFIERS) + + return getattr(self._function(*args), modifier)(modifier_dict) + + +class VyperContract(ConciseContract): + + def __init__(self, classic_contract, method_class=VyperMethod): + super().__init__(classic_contract, method_class) + +def test_factory(w3, exchange_template, omg_token, exchange_factory, pad_bytes32, assert_tx_failed, exchange_bytecode, exchange_abi): + a0, a1 = w3.eth.accounts[:2] # Factory initial state - assert utils.remove_0x_head(exchange_factory.exchangeTemplate()) == exchange_template.address.hex() + assert exchange_factory.exchangeTemplate() == exchange_template.address + assert exchange_factory.getExchange(omg_token.address) == None # Create Exchange for UNI Token - exchange_address = exchange_factory.createExchange(omg_token.address) - omg_exchange = t.ABIContract(chain, exchange_abi, exchange_address) - assert exchange_factory.getExchange(omg_token.address) == exchange_address - assert utils.remove_0x_head(exchange_factory.getToken(omg_exchange.address)) == omg_token.address.hex() - # Can't call initializeFactory on factory twice - assert_tx_failed(lambda: exchange_factory.initializeFactory(omg_token.address)) - # Exchange already exists - assert_tx_failed(lambda: exchange_factory.createExchange(omg_token.address)) - # Can't call setup on exchange - assert_tx_failed(lambda: omg_exchange.setup(exchange_factory.address)) - # Exchange initial state + exchange_factory.createExchange(omg_token.address, transact={}) + omg_exchange_address = exchange_factory.getExchange(omg_token.address) + assert omg_exchange_address != None + omg_exchange = w3.eth.contract(omg_exchange_address, abi=exchange_abi, bytecode=exchange_bytecode, ContractFactoryClass=VyperContract) + assert exchange_factory.getToken(omg_exchange.address) == omg_token.address + # # Can't call initializeFactory on factory twice + # assert_tx_failed(lambda: exchange_factory.initializeFactory(omg_token.address)) + # # Exchange already exists + # assert_tx_failed(lambda: exchange_factory.createExchange(omg_token.address)) + # # Can't call setup on exchange + # assert_tx_failed(lambda: omg_exchange.setup(exchange_factory.address)) + # # Exchange initial state assert omg_exchange.name() == pad_bytes32('Uniswap Exchange') assert omg_exchange.symbol() == pad_bytes32('UNI') assert omg_exchange.decimals() == 18 assert omg_exchange.totalSupply() == 0 - assert utils.remove_0x_head(omg_exchange.tokenAddress()) == omg_token.address.hex() - assert utils.remove_0x_head(omg_exchange.factoryAddress()) == exchange_factory.address.hex() - assert chain.head_state.get_balance(omg_exchange.address) == 0 + assert omg_exchange.tokenAddress() == omg_token.address + assert omg_exchange.factoryAddress() == exchange_factory.address + assert w3.eth.getBalance(omg_exchange.address) == 0 assert omg_token.balanceOf(omg_exchange.address) == 0 diff --git a/tests/exchange/test_liquidity_pool.py b/tests/exchange/test_liquidity_pool.py index bb2382f..f659d56 100644 --- a/tests/exchange/test_liquidity_pool.py +++ b/tests/exchange/test_liquidity_pool.py @@ -1,76 +1,71 @@ -def test_add_liquidity(t, chain, utils, omg_token, exchange_factory, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 +def test_add_liquidity(w3, omg_token, exchange_factory, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 assert exchange_factory.getExchange(omg_token.address) == omg_exchange.address - assert utils.remove_0x_head(omg_exchange.tokenAddress()) == omg_token.address.hex() - assert utils.remove_0x_head(omg_exchange.factoryAddress()) == exchange_factory.address.hex() - omg_token.approve(omg_exchange.address, 100*10**18) - # Can't add liquidity without tokens - assert_tx_failed(lambda: omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18, sender=t.k1)) - chain.mine() - # msg.value can't be 0 - assert_tx_failed(lambda: omg_exchange.addLiquidity(10*10**18, deadline)) - chain.mine() - # Token value can't be 0 - assert_tx_failed(lambda: omg_exchange.addLiquidity(0, deadline, value=5*10**18)) - chain.mine() - # Throw exception if not enough gas is provided - assert_tx_failed(lambda: omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18, startgas=25000)) + assert omg_exchange.tokenAddress() == omg_token.address + assert omg_exchange.factoryAddress() == exchange_factory.address + omg_token.approve(omg_exchange.address, 100*10**18, transact={}) + # # Can't add liquidity without tokens + # assert_tx_failed(lambda: omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18, sender=t.k1)) + # # msg.value can't be 0 + # assert_tx_failed(lambda: omg_exchange.addLiquidity(10*10**18, deadline)) + # # Token value can't be 0 + # assert_tx_failed(lambda: omg_exchange.addLiquidity(0, deadline, value=5*10**18)) + # # Throw exception if not enough gas is provided + # assert_tx_failed(lambda: omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18, startgas=25000)) # Liquidity provider (t.a0) adds liquidity - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 assert omg_exchange.totalSupply() == 5*10**18 - assert omg_exchange.balanceOf(t.a0) == 5*10**18 + assert omg_exchange.balanceOf(a0) == 5*10**18 -def test_liquidity_pool(t, chain, utils, omg_token, exchange_factory, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 10*10**18) - omg_token.approve(omg_exchange.address, 100*10**18) - omg_token.approve(omg_exchange.address, 10*10**18, sender=t.k1) +def test_liquidity_pool(w3, omg_token, exchange_factory, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 100*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={'from': a1}) # First liquidity provider (t.a0) adds liquidity - omg_exchange.addLiquidity(2*10**18, deadline, value=1*10**18) + omg_exchange.addLiquidity(2*10**18, deadline, transact={'value': 1*10**18}) assert omg_exchange.totalSupply() == 1*10**18 - assert omg_exchange.balanceOf(t.a0) == 1*10**18 - assert omg_exchange.balanceOf(t.a1) == 0 - assert omg_exchange.balanceOf(t.a2) == 0 - assert omg_token.balanceOf(t.a1) == 10*10**18 - assert omg_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(omg_exchange.address) == 1*10**18 + assert omg_exchange.balanceOf(a0) == 1*10**18 + assert omg_exchange.balanceOf(a1) == 0 + assert omg_exchange.balanceOf(a2) == 0 + assert omg_token.balanceOf(a1) == 10*10**18 + assert omg_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(omg_exchange.address) == 1*10**18 assert omg_token.balanceOf(omg_exchange.address) == 2*10**18 - # Second liquidity provider (t.a1) adds liquidity - omg_exchange.addLiquidity(1, deadline, value=5*10**18, sender=t.k1) + # Second liquidity provider (a1) adds liquidity + omg_exchange.addLiquidity(1, deadline, transact={'value': 5*10**18, 'from': a1}) assert omg_exchange.totalSupply() == 6*10**18 - assert omg_exchange.balanceOf(t.a0) == 1*10**18 - assert omg_exchange.balanceOf(t.a1) == 5*10**18 - assert omg_exchange.balanceOf(t.a2) == 0 - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(omg_exchange.address) == 6*10**18 + assert omg_exchange.balanceOf(a0) == 1*10**18 + assert omg_exchange.balanceOf(a1) == 5*10**18 + assert omg_exchange.balanceOf(a2) == 0 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(omg_exchange.address) == 6*10**18 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 - # Can't divest more liquidity than owned - assert_tx_failed(lambda: omg_exchange.removeLiquidity((5*10**18 + 1), 1, 1, deadline, sender=t.k2)) - # Mine block - chain.mine() - # Second liquidity provider (t.a1) transfers liquidity to third liquidity provider (t.a2) - omg_exchange.transfer(t.a2, 2*10**18, sender=t.k1) - assert omg_exchange.balanceOf(t.a0) == 1*10**18 - assert omg_exchange.balanceOf(t.a1) == 3*10**18 - assert omg_exchange.balanceOf(t.a2) == 2*10**18 - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(omg_exchange.address) == 6*10**18 + # # Can't divest more liquidity than owned + # assert_tx_failed(lambda: omg_exchange.removeLiquidity((5*10**18 + 1), 1, 1, deadline, transact={'from': a2}) + # Second liquidity provider (a1) transfers liquidity to third liquidity provider (a2) + omg_exchange.transfer(a2, 2*10**18, transact={'from': a1}) + assert omg_exchange.balanceOf(a0) == 1*10**18 + assert omg_exchange.balanceOf(a1) == 3*10**18 + assert omg_exchange.balanceOf(a2) == 2*10**18 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(omg_exchange.address) == 6*10**18 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 - # Mine block - chain.mine() # First, second and third liquidity providers remove their remaining liquidity - omg_exchange.removeLiquidity(1*10**18, 1, 1, deadline) - omg_exchange.removeLiquidity(3*10**18, 1, 1, deadline, sender=t.k1) - omg_exchange.removeLiquidity(2*10**18, 1, 1, deadline, sender=t.k2) + omg_exchange.removeLiquidity(1*10**18, 1, 1, deadline, transact={}) + omg_exchange.removeLiquidity(3*10**18, 1, 1, deadline, transact={'from': a1}) + omg_exchange.removeLiquidity(2*10**18, 1, 1, deadline, transact={'from': a2}) assert omg_exchange.totalSupply() == 0 - assert omg_exchange.balanceOf(t.a0) == 0 - assert omg_exchange.balanceOf(t.a1) == 0 - assert omg_exchange.balanceOf(t.a2) == 0 - assert omg_token.balanceOf(t.a1) == 6*10**18 - assert omg_token.balanceOf(t.a2) == 4*10**18 - assert chain.head_state.get_balance(omg_exchange.address) == 0 + assert omg_exchange.balanceOf(a0) == 0 + assert omg_exchange.balanceOf(a1) == 0 + assert omg_exchange.balanceOf(a2) == 0 + assert omg_token.balanceOf(a1) == 6*10**18 + assert omg_token.balanceOf(a2) == 4*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 0 assert omg_token.balanceOf(omg_exchange.address) == 0 # Can add liquidity again after all liquidity is divested - omg_exchange.addLiquidity(2*10**18, deadline, value=1*10**18) + omg_exchange.addLiquidity(2*10**18, deadline, transact={'value': 1*10**18}) diff --git a/tests/exchange/test_token_to_eth.py b/tests/exchange/test_token_to_eth.py index c85fbf3..a620cb9 100644 --- a/tests/exchange/test_token_to_eth.py +++ b/tests/exchange/test_token_to_eth.py @@ -1,95 +1,99 @@ -def test_swap(t, chain, omg_token, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 2*10**18) - omg_token.approve(omg_exchange.address, 10*10**18) - omg_token.approve(omg_exchange.address, 2*10**18, sender=t.k1) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) +def test_swap(w3, omg_token, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 2*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 2*10**18, transact={'from': a1}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 2*10**18 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 2*10**18 + assert w3.eth.getBalance(a1) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.tokenToEthSwap(2*10**18, 1, deadline, sender=t.k1) + omg_exchange.tokenToEthSwap(2*10**18, 1, deadline, transact={'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 4168751042187760548 + assert w3.eth.getBalance(omg_exchange.address) == 4168751042187760548 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + 831248957812239452 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 + 831248957812239452 -def test_transfer(t, chain, omg_token, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 2*10**18) - omg_token.approve(omg_exchange.address, 10*10**18) - omg_token.approve(omg_exchange.address, 2*10**18, sender=t.k1) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) +def test_transfer(w3, omg_token, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 2*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 2*10**18, transact={'from': a1}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 2*10**18 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 2*10**18 + assert w3.eth.getBalance(a1) == 1*10**24 # Starting balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(a2) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.tokenToEthTransfer(2*10**18, 1, deadline, t.a2, sender=t.k1) + omg_exchange.tokenToEthTransfer(2*10**18, 1, deadline, a2, transact={'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 4168751042187760548 + assert w3.eth.getBalance(omg_exchange.address) == 4168751042187760548 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Updated balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + 831248957812239452 + assert omg_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(a2) == 1*10**24 + 831248957812239452 -def test_swap_exact(t, chain, omg_token, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 3*10**18) - omg_token.approve(omg_exchange.address, 10*10**18) - omg_token.approve(omg_exchange.address, 3*10**18, sender=t.k1) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) +def test_swap_exact(w3, omg_token, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 3*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 3*10**18, transact={'from': a1}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 3*10**18 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 3*10**18 + assert w3.eth.getBalance(a1) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.tokenToEthSwapExact(831248957812239452, 3*10**18, deadline, sender=t.k1) + omg_exchange.tokenToEthSwapExact(831248957812239452, 3*10**18, deadline, transact={'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 4168751042187760548 + assert w3.eth.getBalance(omg_exchange.address) == 4168751042187760548 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 - 3 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1*10**18 + 3 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + 831248957812239452 + assert omg_token.balanceOf(a1) == 1*10**18 + 3 + assert w3.eth.getBalance(a1) == 1*10**24 + 831248957812239452 -def test_transfer_exact(t, chain, omg_token, omg_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 3*10**18) - omg_token.approve(omg_exchange.address, 10*10**18) - omg_token.approve(omg_exchange.address, 3*10**18, sender=t.k1) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) +def test_transfer_exact(w3, omg_token, omg_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 3*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 3*10**18, transact={'from': a1}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 3*10**18 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 3*10**18 + assert w3.eth.getBalance(a1) == 1*10**24 # Starting balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(a2) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.tokenToEthTransferExact(831248957812239452, 3*10**18, deadline, t.a2, sender=t.k1) + omg_exchange.tokenToEthTransferExact(831248957812239452, 3*10**18, deadline, a2, transact={'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 4168751042187760548 + assert w3.eth.getBalance(omg_exchange.address) == 4168751042187760548 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 - 3 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1*10**18 + 3 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 1*10**18 + 3 + assert w3.eth.getBalance(a1) == 1*10**24 # Updated balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + 831248957812239452 + assert omg_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(a2) == 1*10**24 + 831248957812239452 diff --git a/tests/exchange/test_token_to_exchange.py b/tests/exchange/test_token_to_exchange.py index f3531b7..42622db 100644 --- a/tests/exchange/test_token_to_exchange.py +++ b/tests/exchange/test_token_to_exchange.py @@ -1,77 +1,79 @@ -def test_transfer(t, chain, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 3*10**18) - omg_token.approve(omg_exchange.address, 10*10**18) - omg_token.approve(omg_exchange.address, 3*10**18, sender=t.k1) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) - dai_token.approve(dai_exchange.address, 20*10**18) - dai_exchange.addLiquidity(20*10**18, deadline, value=5*10**18) +def test_transfer(w3, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 3*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 3*10**18, transact={'from': a1}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) + dai_token.approve(dai_exchange.address, 20*10**18, transact={}) + dai_exchange.addLiquidity(20*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5*10**18 + assert w3.eth.getBalance(dai_exchange.address) == 5*10**18 assert dai_token.balanceOf(dai_exchange.address) == 20*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 3*10**18 - assert dai_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 3*10**18 + assert dai_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Starting balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert dai_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert dai_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(a2) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.tokenToExchangeTransfer(2*10**18, 1, deadline, t.a2, dai_exchange.address, startgas=119000, sender=t.k1) + omg_exchange.tokenToExchangeTransfer(2*10**18, 1, deadline, a2, dai_exchange.address, transact={'gas': 119000, 'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 4168751042187760548 + assert w3.eth.getBalance(omg_exchange.address) == 4168751042187760548 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 # Updated balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5831248957812239452 + assert w3.eth.getBalance(dai_exchange.address) == 5831248957812239452 assert dai_token.balanceOf(dai_exchange.address) == 17156321784165919403 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1*10**18 - assert dai_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 1*10**18 + assert dai_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Updated balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert dai_token.balanceOf(t.a2) == 2843678215834080597 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert dai_token.balanceOf(a2) == 2843678215834080597 + assert w3.eth.getBalance(a2) == 1*10**24 -def test_transfer_exact(t, chain, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 3*10**18) - omg_token.approve(omg_exchange.address, 10*10**18) - omg_token.approve(omg_exchange.address, 3*10**18, sender=t.k1) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) - dai_token.approve(dai_exchange.address, 20*10**18) - dai_exchange.addLiquidity(20*10**18, deadline, value=5*10**18) +def test_transfer_exact(w3, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 3*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 3*10**18, transact={'from': a1}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) + dai_token.approve(dai_exchange.address, 20*10**18, transact={}) + dai_exchange.addLiquidity(20*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5*10**18 + assert w3.eth.getBalance(dai_exchange.address) == 5*10**18 assert dai_token.balanceOf(dai_exchange.address) == 20*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 3*10**18 - assert dai_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 3*10**18 + assert dai_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Starting balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert dai_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert dai_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(a2) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.tokenToExchangeTransferExact(2843678215834080597, 3*10**18, deadline, t.a2, dai_exchange.address, startgas=130000, sender=t.k1) + omg_exchange.tokenToExchangeTransferExact(2843678215834080597, 3*10**18, deadline, a2, dai_exchange.address, transact={'gas': 130000, 'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 4168751042187760548 + assert w3.eth.getBalance(omg_exchange.address) == 4168751042187760548 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 - 3 # Updated balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5831248957812239452 + assert w3.eth.getBalance(dai_exchange.address) == 5831248957812239452 assert dai_token.balanceOf(dai_exchange.address) == 17156321784165919403 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1*10**18 + 3 - assert dai_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 1*10**18 + 3 + assert dai_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Updated balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert dai_token.balanceOf(t.a2) == 2843678215834080597 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert dai_token.balanceOf(a2) == 2843678215834080597 + assert w3.eth.getBalance(a2) == 1*10**24 diff --git a/tests/exchange/test_token_to_token.py b/tests/exchange/test_token_to_token.py index 38850e3..599bb6b 100644 --- a/tests/exchange/test_token_to_token.py +++ b/tests/exchange/test_token_to_token.py @@ -1,140 +1,143 @@ -def test_swap(t, chain, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 3*10**18) - omg_token.approve(omg_exchange.address, 10*10**18) - omg_token.approve(omg_exchange.address, 3*10**18, sender=t.k1) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) - dai_token.approve(dai_exchange.address, 20*10**18) - dai_exchange.addLiquidity(20*10**18, deadline, value=5*10**18) +def test_swap(w3, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 3*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 3*10**18, transact={'from': a1}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) + dai_token.approve(dai_exchange.address, 20*10**18, transact={}) + dai_exchange.addLiquidity(20*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5*10**18 + assert w3.eth.getBalance(dai_exchange.address) == 5*10**18 assert dai_token.balanceOf(dai_exchange.address) == 20*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 3*10**18 - assert dai_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 3*10**18 + assert dai_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.tokenToTokenSwap(2*10**18, 1, deadline, dai_token.address, startgas=118048, sender=t.k1) - # omg_exchange.tokenToTokenSwap(dai_token.address, 2*10**18, 1, deadline, startgas=108048, sender=t.k1) + omg_exchange.tokenToTokenSwap(2*10**18, 1, deadline, dai_token.address, transact={'gas': 117974, 'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 4168751042187760548 + assert w3.eth.getBalance(omg_exchange.address) == 4168751042187760548 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 # Updated balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5831248957812239452 + assert w3.eth.getBalance(dai_exchange.address) == 5831248957812239452 assert dai_token.balanceOf(dai_exchange.address) == 17156321784165919403 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1*10**18 - assert dai_token.balanceOf(t.a1) == 2843678215834080597 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 1*10**18 + assert dai_token.balanceOf(a1) == 2843678215834080597 + assert w3.eth.getBalance(a1) == 1*10**24 -def test_transfer(t, chain, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 3*10**18) - omg_token.approve(omg_exchange.address, 10*10**18) - omg_token.approve(omg_exchange.address, 3*10**18, sender=t.k1) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) - dai_token.approve(dai_exchange.address, 20*10**18) - dai_exchange.addLiquidity(20*10**18, deadline, value=5*10**18) +def test_transfer(w3, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 3*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 3*10**18, transact={'from': a1}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) + dai_token.approve(dai_exchange.address, 20*10**18, transact={}) + dai_exchange.addLiquidity(20*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5*10**18 + assert w3.eth.getBalance(dai_exchange.address) == 5*10**18 assert dai_token.balanceOf(dai_exchange.address) == 20*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 3*10**18 - assert dai_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 3*10**18 + assert dai_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Starting balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert dai_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert dai_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(a2) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.tokenToTokenTransfer(2*10**18, 1, deadline, t.a2, dai_token.address, startgas=119550, sender=t.k1) + omg_exchange.tokenToTokenTransfer(2*10**18, 1, deadline, a2, dai_token.address, transact={'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 4168751042187760548 + assert w3.eth.getBalance(omg_exchange.address) == 4168751042187760548 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 # Updated balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5831248957812239452 + assert w3.eth.getBalance(dai_exchange.address) == 5831248957812239452 assert dai_token.balanceOf(dai_exchange.address) == 17156321784165919403 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1*10**18 - assert dai_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 1*10**18 + assert dai_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Updated balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert dai_token.balanceOf(t.a2) == 2843678215834080597 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert dai_token.balanceOf(a2) == 2843678215834080597 + assert w3.eth.getBalance(a2) == 1*10**24 -def test_swap_exact(t, chain, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 3*10**18) - omg_token.approve(omg_exchange.address, 10*10**18) - omg_token.approve(omg_exchange.address, 3*10**18, sender=t.k1) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) - dai_token.approve(dai_exchange.address, 20*10**18) - dai_exchange.addLiquidity(20*10**18, deadline, value=5*10**18) +def test_swap_exact(w3, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 3*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 3*10**18, transact={'from': a1}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) + dai_token.approve(dai_exchange.address, 20*10**18, transact={}) + dai_exchange.addLiquidity(20*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5*10**18 + assert w3.eth.getBalance(dai_exchange.address) == 5*10**18 assert dai_token.balanceOf(dai_exchange.address) == 20*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 3*10**18 - assert dai_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 3*10**18 + assert dai_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.tokenToTokenSwapExact(2843678215834080597, 3*10**18, deadline, dai_token.address, sender=t.k1) + omg_exchange.tokenToTokenSwapExact(2843678215834080597, 3*10**18, deadline, dai_token.address, transact={'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 4168751042187760548 + assert w3.eth.getBalance(omg_exchange.address) == 4168751042187760548 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 - 3 # Updated balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5831248957812239452 + assert w3.eth.getBalance(dai_exchange.address) == 5831248957812239452 assert dai_token.balanceOf(dai_exchange.address) == 17156321784165919403 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1*10**18 + 3 - assert dai_token.balanceOf(t.a1) == 2843678215834080597 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 1*10**18 + 3 + assert dai_token.balanceOf(a1) == 2843678215834080597 + assert w3.eth.getBalance(a1) == 1*10**24 -def test_transfer_exact(t, chain, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): - deadline = chain.head_state.timestamp + 300 - omg_token.transfer(t.a1, 3*10**18) - omg_token.approve(omg_exchange.address, 10*10**18) - omg_token.approve(omg_exchange.address, 3*10**18, sender=t.k1) - omg_exchange.addLiquidity(10*10**18, deadline, value=5*10**18) - dai_token.approve(dai_exchange.address, 20*10**18) - dai_exchange.addLiquidity(20*10**18, deadline, value=5*10**18) +def test_transfer_exact(w3, omg_token, dai_token, omg_exchange, dai_exchange, assert_tx_failed): + a0, a1, a2 = w3.eth.accounts[:3] + deadline = w3.eth.getBlock(w3.eth.blockNumber).timestamp + 300 + omg_token.transfer(a1, 3*10**18, transact={}) + omg_token.approve(omg_exchange.address, 10*10**18, transact={}) + omg_token.approve(omg_exchange.address, 3*10**18, transact={'from': a1}) + omg_exchange.addLiquidity(10*10**18, deadline, transact={'value': 5*10**18}) + dai_token.approve(dai_exchange.address, 20*10**18, transact={}) + dai_exchange.addLiquidity(20*10**18, deadline, transact={'value': 5*10**18}) # Starting balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 5*10**18 + assert w3.eth.getBalance(omg_exchange.address) == 5*10**18 assert omg_token.balanceOf(omg_exchange.address) == 10*10**18 # Starting balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5*10**18 + assert w3.eth.getBalance(dai_exchange.address) == 5*10**18 assert dai_token.balanceOf(dai_exchange.address) == 20*10**18 # Starting balances of BUYER - assert omg_token.balanceOf(t.a1) == 3*10**18 - assert dai_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 3*10**18 + assert dai_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Starting balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert dai_token.balanceOf(t.a2) == 0 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert dai_token.balanceOf(a2) == 0 + assert w3.eth.getBalance(a2) == 1*10**24 # BUYER converts ETH to UNI - omg_exchange.tokenToTokenTransferExact(2843678215834080597, 3*10**18, deadline, t.a2, dai_token.address, startgas=150000, sender=t.k1) + omg_exchange.tokenToTokenTransferExact(2843678215834080597, 3*10**18, deadline, a2, dai_token.address, transact={'gas': 130749,'from': a1}) # Updated balances of UNI exchange - assert chain.head_state.get_balance(omg_exchange.address) == 4168751042187760548 + assert w3.eth.getBalance(omg_exchange.address) == 4168751042187760548 assert omg_token.balanceOf(omg_exchange.address) == 12*10**18 - 3 # Updated balances of SWAP exchange - assert chain.head_state.get_balance(dai_exchange.address) == 5831248957812239452 + assert w3.eth.getBalance(dai_exchange.address) == 5831248957812239452 assert dai_token.balanceOf(dai_exchange.address) == 17156321784165919403 # Updated balances of BUYER - assert omg_token.balanceOf(t.a1) == 1*10**18 + 3 - assert dai_token.balanceOf(t.a1) == 0 - assert chain.head_state.get_balance(t.a1) == 1*10**24 + assert omg_token.balanceOf(a1) == 1*10**18 + 3 + assert dai_token.balanceOf(a1) == 0 + assert w3.eth.getBalance(a1) == 1*10**24 # Updated balances of RECIPIENT - assert omg_token.balanceOf(t.a2) == 0 - assert dai_token.balanceOf(t.a2) == 2843678215834080597 - assert chain.head_state.get_balance(t.a2) == 1*10**24 + assert omg_token.balanceOf(a2) == 0 + assert dai_token.balanceOf(a2) == 2843678215834080597 + assert w3.eth.getBalance(a2) == 1*10**24