def wait_for_transaction_receipt(self, txid): LOG.info("Waiting for transaction receipt from the DID sidechain...") try: w3 = Web3(Web3.HTTPProvider(self.sidechain_rpc)) tx_receipt = w3.eth.wait_for_transaction_receipt(txid, timeout=30, poll_latency=0.1) return { "tx_receipt": json.loads(Web3.toJSON(tx_receipt)), "err_type": None, "err_message": None } except TimeExhausted as e: LOG.info( f"Timed out while sending transactions to the DID sidechain: {str(e)}" ) return { "tx_receipt": {}, "err_type": "TimeExhausted", "err_message": str(e) } except Exception as e: LOG.info( f"Error while sending transactions to the DID sidechain: {str(e)}" ) return { "tx_receipt": {}, "err_type": "Exception", "err_message": str(e) }
def __init__(self, ipc_path: str = DEFAULT_IPC_PATH, timeout: int = TIMEOUT, testnet: bool = False): super().__init__() self.w3 = Web3(provider=IPCProvider(ipc_path=ipc_path, timeout=timeout) ) # TODO: Unify with clients or build error handling self.ipc_path = ipc_path self.testnet = testnet
def get_block_count(self) -> int: LOG.info("Get block count...") try: w3 = Web3(Web3.HTTPProvider(self.sidechain_rpc)) currentBlock = w3.eth.get_block_number() LOG.info("Actual block number: " + str(currentBlock)) return currentBlock except Exception as e: LOG.info(f"Error while getting block count: {str(e)}") return None
def get_raw_transaction(self, txid): LOG.info(f"Retrieving transaction {txid} from the DID sidechain...") try: w3 = Web3(Web3.HTTPProvider(self.sidechain_rpc)) tx = w3.eth.get_transaction_receipt(txid) return json.loads(Web3.toJSON(tx)) except Exception as e: LOG.info( f"Error while getting raw transaction for a txid: {str(e)}") return None
def send_raw_transaction(self, signed_transaction): LOG.info("Sending transaction to the DID sidechain...") try: w3 = Web3(Web3.HTTPProvider(self.sidechain_rpc)) tx = w3.eth.send_raw_transaction(signed_transaction.rawTransaction) return {"tx_id": tx.hex(), "error": None} except Exception as e: LOG.info( f"Error while sending transactions to the DID sidechain: {str(e)}" ) return {"tx_id": None, "error": str(e)}
def get_balance(self, address): LOG.info( f"Retrieving current balance on DID sidechain for address {address}" ) balance = 0 try: w3 = Web3(Web3.HTTPProvider(self.sidechain_rpc)) balance = float(w3.eth.get_balance( Web3.toChecksumAddress(address))) balance = balance / 1000000000000000000.0 except Exception as e: LOG.info(f"Error while retrieving balance for an address: {e}") return balance
def web3(): provider = EthereumTesterProvider() w3 = Web3(provider) # Delete this whole block after eth-account has passed security audit try: w3.eth.account except AttributeError: pass else: raise AssertionError("Unaudited features must be disabled by default") w3.eth.enable_unaudited_features() return w3
def get_web3(network_url: str) -> Web3: """ Return a web3 instance connected via the given network_url. Adds POA middleware when connecting to the Rinkeby Testnet. A note about using the `rinkeby` testnet: Web3 py has an issue when making some requests to `rinkeby` - the issue is described here: https://github.com/ethereum/web3.py/issues/549 - and the fix is here: https://web3py.readthedocs.io/en/latest/middleware.html#geth-style-proof-of-authority """ provider = get_web3_connection_provider(network_url) web3 = Web3(provider) if web3.eth.chain_id == 4: web3.middleware_onion.inject(geth_poa_middleware, layer=0) return web3
def __init__(self, requester: Requester, ethClient: EthClientInterface, s3manager: S3Manager, bucketName: str): self.requester = requester self.ethClient = ethClient self.s3manager = s3manager self.bucketName = bucketName self.w3 = Web3() with open('./contracts/IERC721Metadata.json') as contractJsonFile: erc721MetadataContractJson = json.load(contractJsonFile) self.erc721MetadataContractAbi = erc721MetadataContractJson['abi'] self.erc721MetadataUriFunctionAbi = [ internalAbi for internalAbi in self.erc721MetadataContractAbi if internalAbi.get('name') == 'tokenURI' ][0] self.erc721MetadataNameFunctionAbi = [ internalAbi for internalAbi in self.erc721MetadataContractAbi if internalAbi.get('name') == 'name' ][0] with open('./contracts/CryptoPunksMetadata.json') as contractJsonFile: contractJson = json.load(contractJsonFile) self.cryptoPunksContract = self.w3.eth.contract( address='0x16F5A35647D6F03D5D3da7b35409D65ba03aF3B2', abi=contractJson['abi']) self.cryptoPunksAttributesFunctionAbi = [ internalAbi for internalAbi in self.cryptoPunksContract.abi if internalAbi.get('name') == 'punkAttributes' ][0] self.cryptoPunksImageSvgFunctionAbi = [ internalAbi for internalAbi in self.cryptoPunksContract.abi if internalAbi.get('name') == 'punkImageSvg' ][0] with open('./contracts/IERC1155MetadataURI.json') as contractJsonFile: erc1155MetadataContractJson = json.load(contractJsonFile) self.erc1155MetadataContractAbi = erc1155MetadataContractJson['abi'] self.erc1155MetadataUriFunctionAbi = [ internalAbi for internalAbi in self.erc1155MetadataContractAbi if internalAbi.get('name') == 'uri' ][0] #self.erc1155MetadataNameFunctionAbi = [internalAbi for internalAbi in self.erc1155MetadataContractAbi if internalAbi.get('name') == 'name'][0] with open('./contracts/IERC165.json') as contractJsonFile: erc165MetadataContractJson = json.load(contractJsonFile) self.erc165MetadataContractAbi = erc165MetadataContractJson['abi'] self.erc165SupportInterfaceUriFunctionAbi = [ internalAbi for internalAbi in self.erc165MetadataContractAbi if internalAbi.get('name') == 'supportsInterface' ][0]
def web3(): provider = EthereumTesterProvider() return Web3(provider)
def w3_strict_abi(): w3 = Web3(EthereumTesterProvider()) w3.enable_strict_bytes_type_checking() return w3
##run ganache-cli with ganache-cli --acctKeys ganache-accounts.json ## ganache-cli --acctKeys ganache-accounts.json ##this will generate ganache-accounts.json which will have addresses dict with each key as private key ##and private_keys key which is a list of private keys import json from web3.auto import w3 from eth_account.messages import encode_defunct from eth_account.datastructures import AttributeDict from web3.main import Web3 import sys, os filepath = os.path.dirname(os.path.abspath(__file__)) w3 = Web3() def read_keys(): _path = os.path.join(os.path.dirname(filepath), "ganache-accounts.json") f = open(_path, "r") data = json.loads(f.read()) return data["addresses"], data["private_keys"] def to_32byte_hex(val): return Web3.toHex(Web3.toBytes(val).rjust(32, b'\0')) def ec_revoverable_message(msg, private_key): signed_message = sign_message(msg, private_key)