def check_transfer(contract, sender, receiver, amount): sender_balance = call_function(contract, 'balanceOf', [sender]) receiver_balance = call_function(contract, 'balanceOf', [receiver]) tx_hash = transact_function(sender, contract, 'transfer', [receiver, amount]) contract.web3.eth.waitForTransactionReceipt(tx_hash) new_sender_balance = call_function(contract, 'balanceOf', [sender]) new_receiver_balance = call_function(contract, 'balanceOf', [receiver]) # check that balances are correct after transfer if sender != receiver: assert new_sender_balance == sender_balance - amount, \ wrong('balance of sender {} after transfer', new_sender_balance, sender_balance - amount) assert new_receiver_balance == receiver_balance + amount, \ wrong('balance of receiver {} after transfer'.format(receiver), new_receiver_balance, receiver_balance + amount) else: assert sender_balance == new_sender_balance, \ wrong('balance of address {} after transferring to itself'.format(sender), new_sender_balance, sender_balance) # check that Transfer event is emitted correctly check_event(contract, tx_hash, 'Transfer', { 'from': sender, 'to': receiver, 'tokens': amount })
def test_initialized_correctly(contract, owner): print('check that contract is initialized correctly: ', end='') got_owner = call_function(contract, 'owner') assert got_owner == owner, wrong('owner', got_owner, owner) print('SUCCESS')
def test_initialized_correctly(contract, name, symbol): print('check that contract is initialized correctly: ', end='') got_name = call_function(contract, 'name') assert got_name == name, wrong('name', got_name, name) got_symbol = call_function(contract, 'symbol') assert got_symbol == symbol, wrong('symbol', got_symbol, symbol) total_supply = call_function(contract, 'totalSupply') assert total_supply == 0, wrong('initial supply', 0, total_supply) for token_number in [0, 1, 2, 5, 10, 25, 50]: assert reverts(call_function, [contract, 'tokenByIndex', [token_number]]), \ 'should not be able to access token #{}'.format(token_number) print('SUCCESS')
def test_initial_balances(contract, accounts): print('check that balances are initialized correctly: ', end='') owner = call_function(contract, 'owner') owner_balance = call_function(contract, 'balanceOf', [owner]) total_supply = call_function(contract, 'totalSupply') assert owner_balance == total_supply, wrong('balance of owner', owner_balance, total_supply) for address in accounts: if address != owner: balance = call_function(contract, 'balanceOf', [address]) assert balance == 0, wrong('balance of {}'.format(address), balance, 0) print('SUCCESS')
def check_transfer_from(contract, approver, spender, receiver, amount): approver_balance = call_function(contract, 'balanceOf', [approver]) receiver_balance = call_function(contract, 'balanceOf', [receiver]) spender_balance = call_function(contract, 'balanceOf', [spender]) allowance = call_function(contract, 'allowance', [approver, spender]) tx_hash = transact_function(spender, contract, 'transferFrom', [approver, receiver, amount]) contract.web3.eth.waitForTransactionReceipt(tx_hash) new_approver_balance = call_function(contract, 'balanceOf', [approver]) new_receiver_balance = call_function(contract, 'balanceOf', [receiver]) new_spender_balance = call_function(contract, 'balanceOf', [spender]) new_allowance = call_function(contract, 'allowance', [approver, spender]) assert new_allowance == allowance - amount, \ wrong('{} allowance to {}'.format(approver, spender), new_allowance, allowance - amount) # check that balances are correct after transferFrom if approver != receiver: assert new_approver_balance == approver_balance - amount, \ wrong('balance of approver {} after transfer', new_approver_balance, approver_balance - amount) assert new_receiver_balance == receiver_balance + amount, \ wrong('balance of receiver {} after transfer'.format(receiver), new_receiver_balance, receiver_balance + amount) else: assert new_approver_balance == approver_balance, \ wrong('balance of approver {} after transfer to itself'.format(approver), new_approver_balance, approver_balance) if spender != receiver and spender != spender: assert new_spender_balance == spender_balance, \ wrong('balance of spender {} after transfer', new_spender_balance, spender_balance) # check that Transfer event is emitted correctly check_event(contract, tx_hash, 'Transfer', { 'from': approver, 'to': receiver, 'tokens': amount })
def test_initial_allowances(contract, accounts): print('check that allowances are initialized correctly: ', end='') owner = call_function(contract, 'owner') for address in accounts: allowance = call_function(contract, 'allowance', [owner, address]) assert allowance == 0, wrong('owner allowance for {}'.format(address), allowance, 0) allowance = call_function(contract, 'allowance', [address, owner]) assert allowance == 0, wrong('{} allowance for owner'.format(address), allowance, 0) for sender in accounts: for receiver in accounts: allowance = call_function(contract, 'allowance', [sender, receiver]) assert allowance == 0, wrong( '{} allowance for {}'.format(sender, receiver), allowance, 0) print('SUCCESS')
def test_initialized_correctly(contract, owner, name, symbol, decimals, initial_supply): print('check that contract is initialized correctly: ', end='') got_owner = call_function(contract, 'owner') assert got_owner == owner, wrong('owner', got_owner, owner) got_name = call_function(contract, 'name') assert got_name == name, wrong('name', got_name, name) got_symbol = call_function(contract, 'symbol') assert got_symbol == symbol, wrong('symbol', got_symbol, symbol) got_decimals = call_function(contract, 'decimals') assert got_decimals == decimals, wrong('decimals', got_decimals, decimals) total_supply = call_function(contract, 'totalSupply') assert total_supply == initial_supply, wrong('initial supply', total_supply, initial_supply) print('SUCCESS')
def check_approve(contract, approver, spender, amount): tx_hash = transact_function(approver, contract, 'approve', [spender, amount]) contract.web3.eth.waitForTransactionReceipt(tx_hash) allowance = call_function(contract, 'allowance', [approver, spender]) assert allowance == amount, wrong( '{} allowance for {}'.format(approver, spender), allowance, amount) # check that Approval event is emitted correctly check_event(contract, tx_hash, 'Approval', { 'tokenOwner': approver, 'spender': spender, 'tokens': amount })
def check_adjust_approval(contract, approver, spender, amount): allowance = call_function(contract, 'allowance', [approver, spender]) if amount >= 0: function_name = 'increaseApproval' else: function_name = 'decreaseApproval' tx_hash = transact_function(approver, contract, function_name, [spender, abs(amount)]) contract.web3.eth.waitForTransactionReceipt(tx_hash) new_allowance = call_function(contract, 'allowance', [approver, spender]) assert new_allowance == allowance + amount, wrong( '{} allowance for {}'.format(approver, spender), allowance, amount) # check that Approval event is emitted correctly check_event(contract, tx_hash, 'Approval', { 'tokenOwner': approver, 'spender': spender, 'tokens': new_allowance })