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_transfer_from(contract, accounts, approvers, spenders): print('check that transferFrom works correctly: ') receivers = accounts[3:4] amount = 1 for approver in approvers: for spender in spenders: for receiver in receivers: print("Approver: {} Spender: {} Receiver: {}".format( approver, spender, receiver)) check_transfer_from(contract, approver, spender, receiver, amount) # should not send more than balance of approver approver_balance = call_function(contract, 'balanceOf', [approver]) assert reverts(transact_function, [spender, contract, 'transferFrom', [approver, receiver, approver_balance + 1]]), \ 'spender should not be able to send more tokens than balance of approver' # should not send more than allowance allowance = call_function(contract, 'allowance', [approver, spender]) assert reverts(transact_function, [spender, contract, 'transferFrom', [approver, receiver, allowance + 1]]), \ 'spender should not be able to send more tokens than allowance' # spender should not be able to transfer tokens to zero address assert reverts(transact_function, [spender, contract, 'transferFrom', [approver, ZERO_ADDRESS, 1]]), \ 'transferFrom to 0x0 should revert' print('SUCCESS')
def print_dividend_account(contract, account): print("Dividends for Account: {}".format(account)) print("getLastEthPoints: {}".format( call_function(contract, 'getLastEthPoints', [account]))) print("dividendsOwingEth: {}".format( call_function(contract, 'dividendsOwing', [account]))) print("unclaimedDividendByAccount: {}".format( call_function(contract, 'unclaimedDividendByAccount', [account])))
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 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_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_transfer(contract, accounts): print('check that tokens are transferred correctly: ', end='') senders = accounts[:3] receivers = accounts[2:5] amount = 10 for sender in senders: for receiver in receivers: check_transfer(contract, sender, receiver, amount) # should not send more than balance sender_balance = call_function(contract, 'balanceOf', [sender]) assert reverts(transact_function, [sender, contract, 'transfer', [receiver, sender_balance + 1]]), \ 'owner should not be able to send more tokens than his balance' # should be able to send 0 tokens check_transfer(contract, sender, receiver, 0) # account should be able to transfer to itself check_transfer(contract, sender, sender, amount) # account should not be able to transfer tokens to zero address -> Not for BTTS token #assert reverts(transact_function, [sender, contract, 'transfer', [ZERO_ADDRESS, 1]]), \ # 'transfer to 0x0 should revert' print('SUCCESS')
def test_implements_interfaces(contract, interfaces): print('check that contract implements interfaces: {}: '.format( [interface['name'] for interface in interfaces]), end='') implements_zero_id = call_function(contract, 'supportsInterface', ['0xffffffff']) assert not implements_zero_id, 'should not implement 0xffffffff' for interface in interfaces: implements = call_function(contract, 'supportsInterface', [interface['id']]) assert implements, 'does not implement interface: {}'.format( interface['name']) 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 fund_accounts(contract, accounts, amount): print('funding accounts: ', end='') owner = call_function(contract, 'owner') for account in accounts: check_transfer(contract, owner, account, amount) 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 run(self): try: ta_module = self._import_module(self.ta_module_name) student_module = self._import_module(self.student_module_name) ta_func = getattr(ta_module, self.function_name) student_func = getattr(student_module, self.function_name) except Exception as e: return ProgramCrash(e) (_, student_output) = call_function(student_func, self.test_input) (_, ta_output) = call_function(ta_func, self.test_input) if student_output == None: return ProgramCrash("An error occurred running {}".format( str(self))) else: ignore_whitespace_cmp = lambda x, y: ' '.join(x.split( )) == ' '.join(y.split()) return compare_outputs(student_output, ta_output, ignore_whitespace_cmp)
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 test(w3, accounts, contract_path, contract_name, crowdsale, max_royalty_frames): crowdsale_contract = deploy(w3, accounts, contract_path, contract_name, crowdsale, max_royalty_frames) owner = call_function(crowdsale_contract, 'owner') test_initialized_correctly(crowdsale_contract, owner) # AG: Test if frame tokens are generated correctly # AG: Test hardcap limit return crowdsale_contract
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 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 run(self): try: client_module = self._import_module(self.client_module_name) func = getattr(client_module, self.function_name) except Exception as e: return ProgramCrash(e) (_, student_output) = call_function(func, self.test_input) if student_output is not None: student_output = ' '.join(student_output.strip().split()) ta_output = ' '.join(self.ta_output.strip().split()) return compare_outputs(student_output, ta_output) else: return ProgramCrash("Got no results from function call.")
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 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_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 test_empty(account, contract): print('RBT is empty: ', end='') root = call_function(account, contract, 'root') expected = 0 assert root == 0, 'root should be: {}, got: {}'.format(expected, root) first = call_function(account, contract, 'first') expected = 0 assert first == expected, 'first should be: {}, got: {}'.format( expected, first) last = call_function(account, contract, 'last') expected = 0 assert last == expected, 'last should be: {}, got: {}'.format( expected, last) next_123 = call_function(account, contract, 'next', 123) expected = 0 assert next_123 == expected, 'next(123) should be: {}, got: {}'.format( expected, next_123) prev_123 = call_function(account, contract, 'prev', 123) expected = 0 assert prev_123 == expected, 'prev(123) should be: {}, got: {}'.format( expected, prev_123) exists_123 = call_function(account, contract, 'exists', 123) expected = False assert exists_123 == expected, 'exists(123) should be: {}, got: {}'.format( expected, exists_123) node_123 = call_function(account, contract, 'getNode', 123) expected = [0, 0, 0, 0, False] assert node_123 == expected, 'getNode(123) should be: {}, got: {}'.format( expected, node_123) parent_123 = call_function(account, contract, 'parent', 123) expected = 0 assert parent_123 == expected, 'parent(123) should be: {}, got: {}'.format( expected, parent_123) grandparent_123 = call_function(account, contract, 'grandparent', 123) expected = 0 assert grandparent_123 == expected, 'grandparent(123) should be: {}, got: {}'.format( expected, grandparent_123) sibling_123 = call_function(account, contract, 'sibling', 123) expected = 0 assert sibling_123 == expected, 'sibling(123) should be: {}, got: {}'.format( expected, sibling_123) uncle_123 = call_function(account, contract, 'uncle', 123) expected = 0 assert uncle_123 == expected, 'uncle(123) should be: {}, got: {}'.format( expected, uncle_123) print('SUCCESS')
def print_dividend_contract(contract): print_break("Dividend Contract: {}".format(contract.address)) print("Symbol: {}".format(call_function(contract, 'symbol'))) print("Name: {}".format(call_function(contract, 'name')))
def get_last_eth_points(contract, account): last_eth_points = call_function(contract, 'getLastEthPoints', [account]) print("Last Eth Points for {}: {}".format(short_address(account), last_eth_points))
def get_dividends_owing(contract, account): dividends_owing = call_function(contract, 'dividendsOwing', [account]) print("Dividends owed for {}: {}".format(short_address(account), dividends_owing))
def get_unclaimed_dividends(contract): unclaimed_dividends = call_function(contract, 'totalUnclaimedDividends') print("Total Unclaimed Dividends: {}".format(unclaimed_dividends))
def get_total_dividend_points(contract): total_dividend_points = call_function(contract, 'totalDividendPoints') print("Total Dividend Points: {}".format(total_dividend_points))