def open_channel(w3, account, contract, *args): transactor = contract.functions["openChannel"](args[0], args[1], args[2]) tx_hash = transact_function(w3, account, transactor) result = wait_event(w3, contract, tx_hash, "ChannelOpened") return result[0]['args']['channel_identifier'], result[0]['args'][ 'participant1'], result[0]['args']['participant2'], result[0]['args'][ 'settle_timeout']
def register_secret(w3, account, contract, *args): transactor = contract.functions["registerSecret"](args[0], args[1], args[2], args[3]) tx_hash = transact_function(w3, account, transactor) result = wait_event(w3, contract, tx_hash, "SecretRevealed") return result[0]['args']['secrethash'], result[0]['args'][ 'secret'], result[0]['args']['endTime']
def test_disable_transfers(contract, accounts, approvers, spenders): print('check that disabling transfers works correctly: ', end='') # non owner account should not be able to enable transfers owner = call_function(contract, 'owner') not_owners = [account for account in accounts if account != owner] for account in not_owners: assert reverts(transact_function, [account, contract, 'disableTransfers']), \ 'non owner account {} should not be able to disable transfers'.format(account) # owner should be able to enable transfers tx_hash = transact_function(owner, contract, 'disableTransfers') check_event(contract, tx_hash, 'TransfersDisabled') # check that transfer reverts senders = accounts[:2] receivers = accounts[1:3] for sender in senders: for receiver in receivers: assert reverts(transact_function, [sender, contract, 'transfer', [receiver, 1]]), \ '{} should not be able to transfer when transferring is disabled'.format(sender) # check that transferFrom reverts for approver in approvers: for spender in spenders: receiver = accounts[3] assert reverts( transact_function, [spender, contract, 'transferFrom', [approver, receiver, 1]]) # check that triggering disableTransfers one more time fails assert reverts(transact_function, [owner, contract, 'disableTransfers']), \ 'owner should not be able to disable transfers when it is already disabled' print('SUCCESS')
def test_enable_transfers(contract, accounts, approvers, spenders): print('check that enabling transfers works correctly: ', end='') # non owner account should not be able to enable transfers owner = call_function(contract, 'owner') not_owners = [account for account in accounts if account != owner] for account in not_owners: assert reverts(transact_function, [account, contract, 'enableTransfers']), \ 'non owner account {} should not be able to enable transfers'.format(account) # owner should be able to enable transfers tx_hash = transact_function(owner, contract, 'enableTransfers') check_event(contract, tx_hash, 'TransfersEnabled') # check that transfer works after enabling transfers senders = accounts[:2] receivers = accounts[1:3] for sender in senders: for receiver in receivers: check_transfer(contract, sender, receiver, 1) # check that transferFrom works after enabling transfers for approver in approvers: for spender in spenders: receiver = accounts[3] check_transfer_from(contract, approver, spender, receiver, 1) # check that triggering enableTransfers one more time fails assert reverts(transact_function, [owner, contract, 'enableTransfers']), \ 'owner should not be able to enable transfers when it is already enabled' print('SUCCESS')
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_duplicate(w3, account, contract, items): print('RBT rejects duplicate values: {}: '.format(items), end='') tx_hashes = [] for item in items: add_nonce = int(w3.txpool.status.pending, 16) tx_hash = transact_function(w3, account, contract, 'insert', item, add_nonce) tx_hashes.append(tx_hash) min_gas_used = 100000000000 max_gas_used = -10000000000 total_gas_used = 0 for i in range(len(tx_hashes)): tx_receipt = w3.eth.waitForTransactionReceipt(tx_hashes[i]) gas_used = tx_receipt['gasUsed'] min_gas_used = min(gas_used, min_gas_used) max_gas_used = max(gas_used, max_gas_used) total_gas_used += gas_used assert tx_receipt[ 'status'] == 0, 'succeeded to insert duplicate element {} to RBT'.format( items[i]) if len(items) > 1: print( 'minimum gas: {}, maximum gas: {}, total gas: {}, average gas: {}: SUCCESS' .format(min_gas_used, max_gas_used, total_gas_used, int(total_gas_used / len(tx_hashes)))) else: print('gas used: {}: SUCCESS'.format(total_gas_used))
def close_channel(w3, account, contract, *args): transactor = contract.functions["closeChannel"](args[0], args[1], args[2], args[3], args[4], args[5], args[6]) tx_hash = transact_function(w3, account, transactor) result = wait_event(w3, contract, tx_hash, "ChannelClosed") return result[0]['args']['channel_identifier'], result[0]['args'][ 'closing_participant'], result[0]['args']['nonce']
def settle_channel(w3, account, contract, *args): transactor = contract.functions["settleChannel"](args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]) tx_hash = transact_function(w3, account, transactor) result = wait_event(w3, contract, tx_hash, "ChannelSettled") return result[0]['args']['channel_identifier'], result[0]['args'][ 'participant1_amount'], result[0]['args']['participant2_amount']
def update_NonClosingBalanceProof(w3, account, contract, *args): transactor = contract.functions["updateNonClosingBalanceProof"]( args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]) tx_hash = transact_function(w3, account, transactor) result = wait_event(w3, contract, tx_hash, "NonClosingBalanceProofUpdated") print("test", result) return result[0]['args']['channel_identifier'], result[0]['args'][ 'closing_participant'], result[0]['args']['nonce']
def set_deposit(w3, account, contract, n1, n2, channel_identifier, deposit): transactor = contract.functions["setTotalDeposit"](channel_identifier, n1, deposit, n2) tx_hash = transact_function(w3, account, transactor) temp = wait_event(w3, contract, tx_hash, "ChannelNewDeposit") result = temp[0]['args']['channel_identifier'], temp[0]['args'][ 'participant'], temp[0]['args']['total_deposit'] return result
def unlock(w3, account, contract, *args): transactor = contract.functions["unlock"](args[0], args[1], args[2], args[3]) tx_hash = transact_function(w3, account, transactor) result = wait_event(w3, contract, tx_hash, "ChannelUnlocked") print("test : ", result) return result[0]['args']['channel_identifier'], result[0]['args']['participant'], result[0]['args']['partner']\ , result[0]['args']['locksroot'], result[0]['args']['unlocked_amount'], result[0]['args']['unsettled_amount']\ , result[0]['args']['unsettled_merkle_root'], result[0]['args']['unsettled_merkle_layer'], result[0]['args']['returned_tokens']
def create_ERC20Token_network(w3, account, contract, *args): contract_interface = compile_contract('../contracts/TokenNetwork.sol', 'TokenNetwork') transactor = contract.functions["createERC20TokenNetwork"](args[0], args[1], args[2]) tx_hash = transact_function(w3, account, transactor) result = wait_event(w3, contract, tx_hash, "TokenNetworkCreated") contract = get_contract(w3, result[0]['args']['token_network_address'], contract_interface['abi']) return contract
def set_frames_crowdsale(owner, royalty_crowdsale_contract, crowdsale): tx_hash = transact_function(owner, royalty_crowdsale_contract, 'setFrameCrowdsaleContract', [crowdsale]) w3 = royalty_crowdsale_contract.web3 tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash) assert tx_receipt['status'] == 1, 'transaction failed' set_crowdsale = call_function(royalty_crowdsale_contract, 'crowdsaleContract') assert set_crowdsale == crowdsale, 'crowdsaleContract not set' print("SUCCESS: Frames Crowdsale set on Royalty Crowdsale: {}".format( set_crowdsale))
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 set_initial_deposit(w3, account, contract, channel, initial_deposit): k, result = 0, [] for i in channel: for j in range(1, 3): if j == 1: k += 1 transactor = contract.functions["setTotalDeposit"]( channel[i][0], channel[i][j], initial_deposit[k], channel[i][3 - j]) tx_hash = transact_function(w3, account[k], transactor) temp = wait_event(w3, contract, tx_hash, "ChannelNewDeposit") result.append((temp[0]['args']['channel_identifier'], temp[0]['args']['participant'], temp[0]['args']['total_deposit'])) return result
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_add_to_whitelist(w3, owner, whitelist_contract, addresses): print('add addresses: {}: to whitelist at address: {}: ' .format(prettify_args(addresses), whitelist_contract.address), end='') tx_hash = transact_function(owner, whitelist_contract, 'add', [addresses]) tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash) assert tx_receipt['status'] == 1, 'transaction failed' events = get_event(whitelist_contract, tx_hash, 'AccountListed') event_args = [events[i]['args'] for i in range(len(events))] accounts = [event_arg['account'] for event_arg in event_args] assert sorted(accounts) == sorted(addresses), \ 'wrong accounts listed in events, should be: {}, got: {}'.format(sorted(addresses), sorted(accounts)) for event_arg in event_args: assert event_arg['status'] == True, 'wrong status, should be: {}, got: {}'.format(False, event_arg['status']) in_whitelist = call_function(whitelist_contract, 'isInWhiteList', [event_arg['account']]) assert in_whitelist, 'address not added to whitelist: {}'.format(event_arg['account']) print('gas used: {}: SUCCESS'.format(tx_receipt['gasUsed']))
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 })
def remove_from_list(w3, account, contract, addresses): return transact_function(w3, account, contract, 'remove', addresses)
def update_account(contract, account): tx_hash = transact_function(account, contract, 'updateAccount', [account]) gas_used = wait_transaction(contract.web3, tx_hash) print("SUCCESS: Account Updated: {} Gas used: {}".format( short_address(account), gas_used))
def withdraw_dividends(contract, account): tx_hash = transact_function(account, contract, 'withdrawDividends') withdraw_event = wait_event(contract, tx_hash, 'WithdrawalDividends') print("SUCCESS: Withdrew Dividends: {} Event: {}".format( short_address(account), withdraw_event))
def delegate(w3, account, contract, to): transactor = contract.functions["delegate"](to) tx_hash = transact_function(w3, account, transactor) w3.eth.waitForTransactionReceipt(tx_hash)
def unlock_account(owner, contract, account): tx_hash = transact_function(owner, contract, 'unlockAccount', [account]) gas_used = wait_transaction(contract.web3, tx_hash) print("SUCCESS: Account Unlocked: {} Gas used: {}".format( short_address(account), gas_used))
def giveRightToVote(w3, account, contract, voter): transactor = contract.functions["giveRightToVote"](voter) tx_hash = transact_function(w3, account, transactor) w3.eth.waitForTransactionReceipt(tx_hash)
def vote(w3, account, contract, proposal): transactor = contract.functions["vote"](proposal) tx_hash = transact_function(w3, account, transactor) w3.eth.waitForTransactionReceipt(tx_hash)
def add_controller(w3, account, contract, address): return transact_function(w3, account, contract, 'addController', address)
def ERC20_transfer(w3, account, contract, *args): transactor = contract.functions["transfer"](args[0], args[1]) tx_hash = transact_function(w3, account, transactor) result = wait_event(w3, contract, tx_hash, "Transfer") return result[0]['args']['to'], result[0]['args']['value']
def remove_controller(w3, account, contract, address): return transact_function(w3, account, contract, 'removeController', address)
def send(w3, account, contract, receiver, amount): transactor = contract.functions["send"](receiver, amount) tx_hash = transact_function(w3, account, transactor) result = wait_event(w3, contract, tx_hash, "Sent") return result[0]['args']['from'], result[0]['args']['to'], result[0][ 'args']['amount']
def mint(w3, account, contract, receiver, amount): transactor = contract.functions["mint"](receiver, amount) tx_hash = transact_function(w3, account, transactor) w3.eth.waitForTransactionReceipt(tx_hash)