def test_local_accounts_register_key(): # given # [that address is not recognized by ganache, this way we can be sure it's the local account being used for signing] web3 = Web3(HTTPProvider("http://localhost:8555")) web3.eth.defaultAccount = Address( '0x13314e21cd6d343ceb857073f3f6d9368919d1ef').address # and keyfile_path = pkg_resources.resource_filename( __name__, "accounts/4_0x13314e21cd6d343ceb857073f3f6d9368919d1ef.json") passfile_path = pkg_resources.resource_filename(__name__, "accounts/pass") register_key(web3, f"key_file={keyfile_path},pass_file={passfile_path}") # and # [as ganache does not know this address, we need to send some ETH to it first] eth_transfer(web3, Address(web3.eth.defaultAccount), Wad.from_number(100)) \ .transact(from_address=Address(web3.eth.accounts[0])) # when # [we deploy some test contract and mint some tokens] token = DSToken.deploy(web3, 'XYZ') token.mint(Wad.from_number(150000)).transact() # then # [these operations were successful] assert token.balance_of(Address( web3.eth.defaultAccount)) == Wad.from_number(150000)
def test_eth_transfer(self): # given initial_balance = eth_balance(self.web3, self.second_address) # when eth_transfer(self.web3, self.second_address, Wad.from_number(1.5)).transact() # then assert eth_balance(self.web3, self.second_address) == initial_balance + Wad.from_number(1.5)
def on_block(self): block = web3.eth.blockNumber logging.info(f"Found block; web3.eth.blockNumber={block}") if run_transactions and block % 3 == 0: # dummy transaction: send 0 ETH to ourself eth_transfer(web3=web3, to=our_address, amount=Wad(0)).transact( from_address=our_address, gas=21000, gas_strategy=gas_strategy) if our_address: logging.info(f"Eth balance is {eth.balance_of(our_address)}")
def test_eth_transfer_from_other_account(self): # given initial_balance_second_address = eth_balance(self.web3, self.second_address) initial_balance_third_address = eth_balance(self.web3, self.third_address) # when eth_transfer(self.web3, self.third_address, Wad.from_number(1.5)).transact(from_address=self.second_address) # then assert eth_balance(self.web3, self.second_address) < initial_balance_second_address assert eth_balance(self.web3, self.third_address) == initial_balance_third_address + Wad.from_number(1.5)
def main(self): print( f"Connected to {os.environ['ETH_RPC_URL']} at block {web3.eth.get_block('latest').number}" ) pending_txes = get_pending_transactions(web3, our_address) if our_address: print( f"{our_address} TX count is {web3.eth.getTransactionCount(our_address.address, block_identifier='pending')}" ) pprint(list(map(lambda t: f"{t.name()}", pending_txes))) if transact and len(pending_txes) > 0: # User would implement their own cancellation logic here, which could involve waiting before # submitting subsequent cancels. for tx in pending_txes: if tx.gas_price < 100 * GWEI: tx.cancel() else: print( f"Gas for TX with nonce={tx.nonce} is too high; leaving alone" ) if transact and stuck_txes_to_submit: logging.info( f"Submitting {stuck_txes_to_submit} transactions with low gas" ) for i in range(1, stuck_txes_to_submit + 1): self._run_future( eth_transfer(web3=web3, to=our_address, amount=Wad(i * 10)).transact_async( gas_strategy=low_gas)) time.sleep(2) # Give event loop a chance to send the transactions
def test_eth_transfer_from_other_account(self): # given assert eth_balance(self.web3, self.second_address) == Wad.from_number(1000000) assert eth_balance(self.web3, self.third_address) == Wad.from_number(1000000) # when eth_transfer( self.web3, self.third_address, Wad.from_number(1.5)).transact(from_address=self.second_address) # then assert eth_balance(self.web3, self.second_address) < Wad.from_number(1000000) assert eth_balance(self.web3, self.third_address ) == Wad.from_number(1000000) + Wad.from_number(1.5)
def test_multiple_local_accounts(): # given local_account_1 = Address('0x13314e21cd6d343ceb857073f3f6d9368919d1ef') local_account_2 = Address('0x176087fea5c41fc370fabbd850521bc4451690ca') # and # [that address is not recognized by ganache, this way we can be sure it's the local account being used for signing] web3 = Web3(HTTPProvider("http://localhost:8555")) web3.eth.defaultAccount = local_account_1.address # and keyfile_path = pkg_resources.resource_filename( __name__, "accounts/4_0x13314e21cd6d343ceb857073f3f6d9368919d1ef.json") passfile_path = pkg_resources.resource_filename(__name__, "accounts/pass") register_key_file(web3, keyfile_path, passfile_path) # and keyfile_path = pkg_resources.resource_filename( __name__, "accounts/5_0x176087fea5c41fc370fabbd850521bc4451690ca.json") passfile_path = pkg_resources.resource_filename(__name__, "accounts/pass") register_key_file(web3, keyfile_path, passfile_path) # and # [as ganache does not know these addresses, we need to send some ETH to it first] eth_transfer(web3, local_account_1, Wad.from_number(100)).transact( from_address=Address(web3.eth.accounts[0])) eth_transfer(web3, local_account_2, Wad.from_number(100)).transact( from_address=Address(web3.eth.accounts[0])) # when # [we execute some test scenario involving two addresses] token = DSToken.deploy(web3, 'XYZ') token.mint(Wad.from_number(150000)).transact() token.transfer(local_account_2, Wad.from_number(60000)).transact() token.transfer( local_account_1, Wad.from_number(10000)).transact(from_address=local_account_2) # then # [these operations were successful] assert token.balance_of(local_account_1) == Wad.from_number(100000) assert token.balance_of(local_account_2) == Wad.from_number(50000)