def test_build_sign_send_transaction(w3, pk, user, ether_token): """ transactions may be broken apart into each individual step in order to facilitate various signing methods (like crypto sticks for instance) """ lock(w3, user) # assure the account is locked other_user = w3.eth.accounts[1] # we'll send some tokens to this target other_user_bal = tx_helpers.call(ether_token.balance_of(other_user)) assert other_user_bal == 0 # Let's deposit 1 ether token in user's account tup = ether_token.deposit(Web3.toWei(1, 'gwei'), {'from': user}) tx = tx_helpers.send(w3, pk.to_bytes(), tup) rct = w3.eth.waitForTransactionReceipt(tx) user_bal = tx_helpers.call(ether_token.balance_of(user)) assert user_bal == Web3.toWei(1, 'gwei') # all computable.py HOC methods return a tuple in the form: (tx, opts) tup = ether_token.transfer(other_user, Web3.toWei(1, 'gwei'), {'from': user}) # these may be passed directly to build... built_tx = tx_helpers.build_transaction(w3, tup) assert built_tx['from'] == user # sign it signed_tx = tx_helpers.sign_transaction(w3, pk.to_bytes(), built_tx) assert signed_tx.hash is not None assert signed_tx.r is not None assert signed_tx.s is not None assert signed_tx.v is not None # a signed transaction can then be broadcast final_tx = tx_helpers.send_raw_transaction(w3, signed_tx) rct = w3.eth.waitForTransactionReceipt(final_tx) other_user_new_bal = tx_helpers.call(ether_token.balance_of(other_user)) assert other_user_new_bal == Web3.toWei(1, 'gwei')
def test_deposit(w3, ether_token): user = w3.eth.accounts[2] user_bal = call(ether_token.balance_of(user)) assert user_bal == 0 # in normal use cases the user's account would be set, but here we'll just send it transact(ether_token.deposit(Web3.toWei(1, 'gwei'), {'from': user})) new_user_bal = call(ether_token.balance_of(user)) assert new_user_bal == Web3.toWei(1, 'gwei')
def test_set_privileged(w3, voting, parameterizer, datatrust, listing): # Check privilege set correctly priv = call(voting.has_privilege(parameterizer.address)) assert priv == True priv = call(voting.has_privilege(datatrust.address)) assert priv == True priv = call(voting.has_privilege(listing.address)) assert priv == True
def test_increase_allowance(w3, ether_token): spender = w3.eth.accounts[1] old_allowed = call( ether_token.allowance(ether_token.account, w3.eth.accounts[1])) transact(ether_token.increase_allowance(spender, Web3.toWei(1, 'kwei'))) new_allowed = call( ether_token.allowance(ether_token.account, w3.eth.accounts[1])) assert new_allowed == old_allowed + Web3.toWei(1, 'kwei')
def test_withdraw(w3, ether_token): user = w3.eth.accounts[2] user_bal = call(ether_token.balance_of(user)) tx = transact(ether_token.withdraw(user_bal, {'from': user})) new_user_bal = call(ether_token.balance_of(user)) assert new_user_bal == 0 rct = w3.eth.getTransactionReceipt(tx) logs = ether_token.deployed.events.Withdrawn().processReceipt(rct) assert logs[0]['args']['to'] == user assert logs[0]['args']['amount'] == user_bal
def initialize_datatrust(self): """ Confirm or create role as datatrust backend in protocol """ log.info('Getting current datatrust address from network') self.datatrust = Datatrust(self.datatrust_wallet) self.datatrust.at(self.w3, self.datatrust_contract) self.voting = Voting(self.datatrust_wallet) self.voting.at(self.w3, self.voting_contract) backend = call(self.datatrust.get_backend_address()) datatrust_hash = self.w3.sha3(text=self.datatrust_host) if backend == self.datatrust_wallet: log.info('This server is the datatrust host. Resolving registration') resolve = send( self.w3, self.datatrust_key, self.datatrust.resolve_registration( self.w3.sha3(text=self.datatrust_host) ) ) log.info(f'Resolved, transaction id: {resolve}') else: # backend not set, or is set to a different host datatrust_url = call(self.datatrust.get_backend_url()) is_candidate = call(self.voting.is_candidate(datatrust_hash)) candidate_is = call(self.voting.candidate_is(datatrust_hash, constants.PROTOCOL_REGISTRATION)) if datatrust_url == self.datatrust_host: log.info('Server has been registered as datatrust, but not voted in') elif is_candidate and candidate_is: log.info('This datatrust is a candidate but has not been voted in') poll_status = call(self.voting.poll_closed(datatrust_hash)) if poll_status: log.info('This datatrust was a candidate, but was not voted in before the poll closed') resolve = send( self.w3, self.datatrust_key, self.datatrust.resolve_registration(datatrust_hash) ) log.info(f'Resolved any prior registration, transaction id: {resolve.hex()}') self.wait_for_mining(resolve) register = self.register_host() log.info(f'Datatrust has been registered.') else: log.info('This datatrust is a candidate. Voting polls are still open.') else: log.info('No backend or different host set. Resolving prior registrations and Submitting this one for voting') resolve = send( self.w3, self.datatrust_key, self.datatrust.resolve_registration(datatrust_hash) ) log.info(f'Resolved any prior registration, transaction id: {resolve.hex()}') self.wait_for_mining(resolve) register = self.register_host()
def test_get_candidate(w3, voting, parameterizer): user = w3.eth.accounts[2] hash = call(parameterizer.get_hash(PLURALITY, 51)) stake = call(parameterizer.get_stake()) vote_by = call(parameterizer.get_vote_by()) tup = call(voting.get_candidate(hash)) assert tup[0] == REPARAM assert tup[1] == user assert tup[2] == stake # the p11r vote by is simply the delta, returned val is actual end date assert tup[3] > vote_by # should pass even at a 0 vote_by assert tup[4] == 0 assert tup[5] == 0
def send_data_hash(self, listing, data_hash): """ On a successful post to the API db, send the data hash to protocol """ datatrust_hash = self.w3.sha3(text=self.datatrust_host) is_candidate = call(self.voting.is_candidate(datatrust_hash)) candidate_is = call(self.voting.candidate_is(datatrust_hash, constants.PROTOCOL_APPLICATION)) if is_candidate and candidate_is: receipt = send(self.w3, self.datatrust_key, self.datatrust.set_data_hash(listing, data_hash)) return receipt else: log.critical('This server is not the datatrust, unable to send data hash') raise ValueError('Server is not the datatrust, unable to send data hash')
def test_transfer_from(w3, ether_token): user = w3.eth.accounts[2] user_bal = call(ether_token.balance_of(user)) owner_bal = call(ether_token.balance_of(ether_token.account)) amt = Web3.toWei(1, 'mwei') # this user needs to approve the 'spender' (the owner in this test case) transact(ether_token.approve(ether_token.account, amt, {'from': user})) # with the allowance, that acct may transfer. transact(ether_token.transfer_from(user, ether_token.account, amt)) new_user_bal = call(ether_token.balance_of(user)) new_owner_bal = call(ether_token.balance_of(ether_token.account)) assert new_user_bal == user_bal - amt assert new_owner_bal == owner_bal + amt # should have 'zeroed' the allowance allowed = call(ether_token.allowance(user, ether_token.account))
def balance_of(self, owner=None): """ balance method is simply balance_of the stated owner, or default to env user """ if owner == None: owner = self.contract.account return call(self.contract.balance_of(owner))
def allowance(self, owner=None, spender=None): """ Given that we know the 'owner' of an allowance return the correct one for a given 'spender' """ if owner == None: owner = self.contract.account return call(self.contract.allowance(owner, spender))
def test_send_some_ether_token(w3, pk, user, ether_token): user_eth_bal = w3.eth.getBalance(user) assert user_eth_bal > 0 user_bal = tx_helpers.call(ether_token.balance_of(user)) assert user_bal == 0 # Let's deposit 1 ether token in user's account tup = ether_token.deposit(Web3.toWei(1, 'gwei'), {'from': user}) tx = tx_helpers.send(w3, pk.to_bytes(), tup) rct = w3.eth.waitForTransactionReceipt(tx) new_user_bal = tx_helpers.call(ether_token.balance_of(user)) assert new_user_bal == Web3.toWei(1, 'gwei') # Let's send this to the default account tup = ether_token.transfer(w3.eth.defaultAccount, Web3.toWei(1, 'gwei'), {'from': user}) tx2 = tx_helpers.send(w3, pk.to_bytes(), tup) final_user_bal = tx_helpers.call(ether_token.balance_of(user)) ##assert new_user_bal == Web3.toWei(1, 'gwei') assert final_user_bal == 0
def test_transfer(w3, ether_token): user = w3.eth.accounts[3] other = w3.eth.accounts[4] user_bal = call(ether_token.balance_of(user)) other_bal = call(ether_token.balance_of(other)) assert user_bal == 0 assert other_bal == 0 # Let's deposit 1 ether token in user's account transact(ether_token.deposit(Web3.toWei(1, 'gwei'), {'from': user})) # Let's send this to other tx = transact( ether_token.transfer(other, Web3.toWei(1, 'gwei'), {'from': user})) new_user_bal = call(ether_token.balance_of(user)) new_other_bal = call(ether_token.balance_of(other)) assert new_user_bal == 0 assert new_other_bal == Web3.toWei(1, 'gwei') # event published... rct = w3.eth.getTransactionReceipt(tx) logs = ether_token.deployed.events.Transfer().processReceipt(rct) assert logs[0]['args']['amount'] == Web3.toWei(1, 'gwei')
def test_send(w3, pk, user, ether_token): """ we do provide an all-in-one convenience method for offline signing """ other_user = w3.eth.accounts[2] # we'll send some tokens to this target other_user_bal = tx_helpers.call(ether_token.balance_of(other_user)) assert other_user_bal == 0 # Let's deposit 1 ether token in user's account tup = ether_token.deposit(Web3.toWei(1, 'gwei'), {'from': user}) tx = tx_helpers.send(w3, pk.to_bytes(), tup) rct = w3.eth.waitForTransactionReceipt(tx) user_bal = tx_helpers.call(ether_token.balance_of(user)) assert user_bal == Web3.toWei(1, 'gwei') tx = tx_helpers.send( w3, pk.to_bytes(), ether_token.transfer(other_user, Web3.toWei(1, 'gwei'), {'from': user})) rct = w3.eth.waitForTransactionReceipt(tx) other_user_new_bal = tx_helpers.call(ether_token.balance_of(other_user)) assert other_user_new_bal == Web3.toWei(1, 'gwei')
def get_withdrawal_proceeds(self, addr=None): # we can default to the env key if omitted if addr == None: addr = self.contract.account try: ret = call(self.contract.get_withdrawal_proceeds(addr)) except Exception: ret = 0 return ret
def wait_for_vote(self): """ Check if this backend is registered as the datatrust """ is_host = False while is_host == False: backend = call(self.datatrust.get_backend_url()) if backend != self.datatrust_host: print('backend not voted in yet, waiting for votes...') sleep(30) else: is_host = True return True
def test_approve_and_allowance(w3, ether_token): # exact gas amounts are known gas = ether_token.get_gas('approve') assert gas == 37763 + GAS_BUFFER # looked up manually in the abi spender = w3.eth.accounts[1] # passing empty opts assures all defaults are used tx = transact(ether_token.approve(spender, Web3.toWei(1, 'gwei'))) allowed = call( ether_token.allowance(ether_token.account, w3.eth.accounts[1])) assert allowed == Web3.toWei(1, 'gwei') # check the event rct = w3.eth.getTransactionReceipt(tx) logs = ether_token.deployed.events.Approval().processReceipt(rct) assert logs[0]['args']['amount'] == Web3.toWei(1, 'gwei')
def register_host(self): """ Register a host as the datatrust in protocol """ log.info('****Registering host****') register = send(self.w3, self.datatrust_key, self.datatrust.register(self.datatrust_host)) self.wait_for_mining(register) voting = Voting(self.datatrust_wallet) voting.at(self.w3, self.voting_contract) datatrust_hash = self.w3.sha3(text=self.datatrust_host) is_registered = call(voting.is_candidate(datatrust_hash)) if is_registered: log.info(f'Backend registered. Voting is now open.') else: log.error('Host attempted to register but did not succeed')
def test_withdraw(w3, ether_token, market_token, reserve, listing): # Set market token privileges before this test priv = call(market_token.has_privilege(reserve.address)) # It's possible the privilege may have been set already if priv == False: tx = transact( market_token.set_privileged(reserve.address, listing.address)) # wait for this to be mined rct = w3.eth.waitForTransactionReceipt(tx) # Check mining succeeded assert rct['status'] == 1 priv = call(market_token.has_privilege(reserve.address)) assert priv == True # Use a new user user = w3.eth.accounts[3] tx = transact(ether_token.deposit(Web3.toWei(1, 'ether'), {'from': user})) rct = w3.eth.waitForTransactionReceipt(tx) new_user_bal = call(ether_token.balance_of(user)) assert new_user_bal == Web3.toWei(1, 'ether') # Approve the spend tx = transact( ether_token.approve(reserve.address, Web3.toWei(1, 'ether'), opts={'from': user})) rct = w3.eth.waitForTransactionReceipt(tx) assert rct['status'] == 1 # Call support tx = transact( reserve.support(new_user_bal, opts={ 'gas': 1000000, 'from': user })) rct = w3.eth.waitForTransactionReceipt(tx) assert rct['status'] == 1 cmt_user_bal = call(market_token.balance_of(user)) # This is around one milliether, but not quite due to spread assert cmt_user_bal > 0 # Call withdraw tx = transact(reserve.withdraw(opts={'gas': 1000000, 'from': user})) rct = w3.eth.waitForTransactionReceipt(tx) assert rct['status'] == 1 cmt_user_bal = call(market_token.balance_of(user)) # This should be 0 assert cmt_user_bal == 0 cet_user_bal = call(ether_token.balance_of(user)) # Should be a little under 1 eth assert cet_user_bal > 0
def test_support(w3, ether_token, market_token, reserve, listing): # Set market token privileges before this test priv = call(market_token.has_privilege(reserve.address)) # May have been set previously if priv == False: tx = transact( market_token.set_privileged(reserve.address, listing.address)) # wait for this to be mined rct = w3.eth.waitForTransactionReceipt(tx) # Check mining succeeded assert rct['status'] == 1 priv = call(market_token.has_privilege(reserve.address)) assert priv == True user = w3.eth.accounts[2] user_bal = call(ether_token.balance_of(user)) assert user_bal == 0 # Deposit ETH in EtherToken tx = transact(ether_token.deposit(Web3.toWei(1, 'ether'), {'from': user})) rct = w3.eth.waitForTransactionReceipt(tx) new_user_bal = call(ether_token.balance_of(user)) assert new_user_bal == Web3.toWei(1, 'ether') assert rct['status'] == 1 # Approve the spend old_allowance = call(ether_token.allowance(user, reserve.address)) assert old_allowance == 0 tx = transact( ether_token.approve(reserve.address, Web3.toWei(1, 'ether'), opts={'from': user})) rct = w3.eth.waitForTransactionReceipt(tx) assert rct['status'] == 1 new_allowance = call(ether_token.allowance(user, reserve.address)) assert new_allowance == Web3.toWei(1, 'ether') # Perform pre-checks for support support_price = call(reserve.get_support_price()) assert new_user_bal >= support_price assert new_allowance >= new_user_bal minted = (new_user_bal // support_price) * 10**9 assert minted == 10**6 * Web3.toWei(1, 'gwei') priv = call(market_token.has_privilege(reserve.address)) assert priv == True total_supply = call(market_token.total_supply()) assert total_supply == Web3.toWei(2, 'ether') # Call support tx = transact( reserve.support(new_user_bal, opts={ 'gas': 1000000, 'from': user })) rct = w3.eth.waitForTransactionReceipt(tx) assert rct['status'] == 1 logs = reserve.deployed.events.Supported().processReceipt(rct) cmt_user_bal = call(market_token.balance_of(user)) assert cmt_user_bal == Web3.toWei(1, 'milliether') new_supply = call(market_token.total_supply()) assert new_supply == total_supply + cmt_user_bal
def get_decimals(self): return call(self.contract.get_decimals())
def test_get_withdrawal_proceeds(reserve): proceeds = call(reserve.get_withdrawal_proceeds(reserve.account)) assert proceeds == 0
def test_get_support_price(reserve): support_price = call(reserve.get_support_price()) assert support_price == Web3.toWei(1, 'microether')
def test_decimals(market_token): decimals = call(market_token.get_decimals()) assert decimals == 18
def get_support_price(self): return call(self.contract.get_support_price())
def test_symbol(market_token): symbol = call(market_token.get_symbol()) assert symbol == "CMT"
def is_listed(self, hash): return call(self.contract.is_listed(hash))
def get_symbol(self): return call(self.contract.get_symbol())
def get_listing(self, hash): return call(self.contract.get_listing(hash))
def total_supply(self): return call(self.contract.total_supply())