def transaction_to_dict(transaction: SignedTransactionAPI) -> Dict[str, str]: return { 'hash': encode_hex(transaction.hash), 'nonce': hex(transaction.nonce), 'gas': hex(transaction.gas), 'gasPrice': hex(transaction.gas_price), 'from': to_checksum_address(transaction.sender), 'to': apply_formatter_if(is_address, to_checksum_address, encode_hex(transaction.to)), 'value': hex(transaction.value), 'input': encode_hex(transaction.data), 'r': hex(transaction.r), 's': hex(transaction.s), 'v': hex(transaction.v), }
def test_apply_formatter_if(condition, formatter, value, expected): assert eth_utils.apply_formatter_if(condition, formatter, value) == expected # must be able to curry conditional_formatter = apply_formatter_if(condition, formatter) assert conditional_formatter(value) == expected
def to_receipt_response(receipt: ReceiptAPI, transaction: SignedTransactionAPI, index: int, header: BlockHeaderAPI, tx_gas_used: int) -> RpcReceiptResponse: if transaction.to == CREATE_CONTRACT_ADDRESS: contract_address = encode_hex( generate_contract_address(transaction.sender, transaction.nonce) ) else: contract_address = None block_hash = encode_hex(header.hash) block_number = hex(header.block_number) receipt_and_transaction_index = hex(index) transaction_hash = encode_hex(transaction.hash) return { "blockHash": block_hash, "blockNumber": block_number, "contractAddress": contract_address, "cumulativeGasUsed": hex(receipt.gas_used), "from": encode_hex(transaction.sender), 'gasUsed': hex(tx_gas_used), "logs": [ { "address": encode_hex(log.address), "data": encode_hex(log.data), "blockHash": block_hash, "blockNumber": block_number, "logIndex": receipt_and_transaction_index, # We only serve receipts from transactions that ended up in the canonical chain # which means this can never be `True` "removed": False, "topics": [ encode_hex(int_to_big_endian(topic)) for topic in log.topics ], "transactionHash": transaction_hash, "transactionIndex": receipt_and_transaction_index, } for log in receipt.logs ], "logsBloom": format_bloom(receipt.bloom), "root": encode_hex(receipt.state_root), "to": apply_formatter_if( is_address, to_checksum_address, encode_hex(transaction.to) ), "transactionHash": transaction_hash, "transactionIndex": receipt_and_transaction_index, }