Package: ezeth
This repository is a python client to access ethereum network.RequirementsWeb3.pypy-solc-xUsageInstantiate clientCheck connectionConnect to new nodeCreate new ethereum accountGet account instanceGet account propertiesChange account passwordTransfer balance to another
accountCompile smart contractDeploy smart contractGet contractModify contract’s storageParse contract event logCall contractCancel transactionGet blockchain dataInstantiate clientInitialize the clientfromezethimportETHClientnode_host='localhost'node_port=8545node_connection_type='http'node_consensus='PoW'client=ETHClient(node_host=node_host,node_port=node_port,node_connection_type=node_connection_type,node_consensus=node_consensus)Check connectionprint(client.isConnected)# TrueConnect to new nodenode_host='localhost'node_port=8546node_connection_type='http'node_consensus='PoW'client.connect(node_host=node_host,node_port=node_port,node_connection_type=node_connection_type,node_consensus=node_consensus)Create new ethereum accountThe returned object is dictionary with 2 keys,"account", contains the instance ofLocalAccount"encrypted_key", contains the dictionary that contains address
and private key of the account encrypted with password input from the
parameterimportjsonfrometh_account.signers.localimportLocalAccountpassword='pass123'new_account=client.create_account(password)print(isinstance(new_account,LocalAccount))# Truewithopen('account_data.json','w')asf:json.dump(new_account['encrypted_key'],f)Get account instanceTo get account instance from encrypted private key, use this methodimportjsonfrometh_account.signers.localimportLocalAccountpassword='pass123'withopen('account_data.json')asf:encrypted_key=json.load(f)account=client.get_account(password,encrypted_key)print(isinstance(account,LocalAccount))# TrueTo get account instance from private key, use this method# don't use this private key in developmentprivate_key='0xd69ff3bd9e6a4455c13974be6ac741996c94eedf9725ad3c7fbccb833d3fae79'account=client.get_account_from_key(private_key)print(isinstance(account,LocalAccount))# TrueGet account propertiesThe returned object is dictionary with 3 keys,"address", the address of the account"balance", the balance of the account at current network"nonce", the current nonce (number of transactions) of the
account at current networkaccount_address=account.addressaccount_properties=client.get_account_properties(account_address)print(account_properties)# {'address': '0xf3cCa25419069bcd6B94bE3876Ac3400070E4796', 'balance': 0, 'nonce': 0}Change account passwordTo change password of the encrypted private key, use this methodimportjsonold_password='pass123'new_password='newpasss123'withopen('account_data.json')asf:encrypted_key=json.load(f)new_encrypted_key=client.change_account_password(old_password,new_password,encrypted_key)withopen('new_account_data.json','w')asf:json.dump(new_encrypted_key,f)Transfer balance to another accountreceiver_address='0xf3cCa25419069bcd6B94bE3876Ac3400070E4796'value=10message='here is the money'transaction=client.transfer(receiver_address,value,message,account=account)To estimate the cost to transfer, use thiscost=client.estimate_transfer_price(value,message)print(cost)# {'cost': 664680000000000, 'value': 10, 'total': 664680000000010}Compile smart contractThe returned object is dictionary with keys in format<filename>:<contractname>(ex."Storage.sol:Storage") and the
value is dictionary with 2 keys,"abi", contains ABI of the contract"bin", contains bytecode of the contractsolc_version='0.8.11'sol_file='Storage.sol'compiled_contract=client.compile_contract(sol_file,solc_version)Here is example of contract// SPDX-License-Identifier: GPL-3.0// Storage.solpragma solidity>=0.4.16<0.9.0;contractStorage{uintstoredData;eventValueModified(uintoldValue,uintnewValue);constructor(uintinitValue){storedData=initValue;}functionset(uintnewValue)public{emitValueModified(storedData,newValue);storedData=newValue;}functionget()publicviewreturns(uint){returnstoredData;}}Deploy smart contractstorage_contract=compiled_contract['Storage.sol:Storage']contract_bytecode=storage_contract['bin']contract_abi=storage_contract['abi']# for contract constructorinit_value=10deployed_contract_data=client.deploy_contract(contract_bytecode,contract_abi,account=account,initValue=init_value)To estimate the cost to deploy smart contract, use thiscost=client.estimate_deploy_contract_price(contract_bytecode,contract_abi,account_address=account.address,initValue=init_value)print(cost)# {'cost': 5166480000000000, 'value': 0, 'total': 5166480000000000}The account which deployed the smart contract must have a sufficient
balance to estimate the cost if the constructor is payable method.Get contractTo get the deployed smart contract address, use thistransaction_hash=deployed_contract_data['transaction_hash']contract_address=client.get_contract_address(transaction_hash)print(contract_address)# 0x7c0ce101E6712DD4E447CE2af81AAD5f8fbF34D0To get the instance of the deployed smart contract, use thiscontract=client.get_contract(contract_address,contract_abi)Modify contract’s storageTo modify contract storage by contract method, use thiscontract_method='set'new_value=13transaction=client.contract_method(contract_method,contract=contract,account=account,newValue=new_value)To test contract method locally without sending transaction to network, use this# will raise exception if something wrong, else return Trueclient.contract_method_test(contract_method,contract=contract,account_address=account.address,newValue=new_value)To estimate the cost, use thiscost=client.estimate_contract_method_price(contract_method,contract=contract,account_address=account.address,newValue=new_value)print(cost)# {'cost': 811560000000000, 'value': 0, 'total': 811560000000000}The account which modify the smart contract’s storage must have a
sufficient balance to estimate the cost if the method is payable
method.Parse contract event logTo parse event log that is emitted from modified smart contract, use
thisevent_name='ValueModified'transaction_hash=transaction['hash']event_log=client.parse_contract_event_log(event_name,transaction_hash,contract=contract)Call contractTo call contract’spureandviewmethods, use thiscontract_method='get'currentValue=client.contract_call(contract_method,contract=contract)print(currentValue)# 13Cancel transactionTo cancel any transaction from account, use thistransaction_hash=transaction['hash']new_transaction=client.cancel_transaction(transaction_hash,account=account)To estimate the cost, use thiscost=client.estimate_cancel_transaction_price(transaction_hash)print(cost)# {'cost': 811590000000000, 'value': 0, 'total': 811590000000000}Transaction that already verified or mined can’t be canceled. The way
the transaction canceled is by sending new empty transaction with the
same nonce but higher gas price so the empty transaction will be
mined and the old one will be discarded.Get blockchain dataTo get detailed transaction from transaction hash, use thistransaction=client.get_transaction(transaction_hash)To get transaction receipt (the prove that transaction is verified or
mined), use thisreceipt=client.get_transaction_receipt(transaction_hash)To get detailed block, use thisblock=client.get_block('latest')