Python web3.eth api调用

Python web3.eth api调用

1.简介

web3.eth api主要提供erc20链内查询方法

2.常用web3.eth API

eth.hashrate
链节点当前时间每秒产生HX数量

'''导入Web3模块'''
from  web3 import Web3


'''连接rpc环境'''
web3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7890'))


web3.eth.hashrate
101

eth.gas_price
查询以wei为单位的当前gas

from  web3 import Web3


'''连接rpc环境'''
web3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7890'))

web3.eth.gas_price
20000000000

eth.blockNumber
查询当前最新区块

from  web3 import Web3


'''连接rpc环境'''
web3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7890'))

web3.eth.blockNumber
2206939

eth.chain_id
查询当前链的链id

from  web3 import Web3


'''连接rpc环境'''
web3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7890'))

web3.eth.chain_id
90

eth.getBalance
查询地址主币数量

from  web3 import Web3


'''连接rpc环境'''
web3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7890'))

web3.eth.getBalance('0xd97F0D2537800BFBb78D9d7Cf087f4A397CbCc3B')

eth.getBlock
根据块高度查询块信息

from web3 import Web3

'''导入geth_poa_middleware'''
from web3.middleware import geth_poa_middleware

web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

'''解决查询块信息数据过长报错问题'''
web3.middleware_onion.inject(geth_poa_middleware, layer=0)

'''查询块信息,不包括完成的交易信息'''
web3.eth.get_block(block_identifier=111,full_transactions=False)
{
'blockExtraData': '0x',
'difficulty': 1, 
'extDataHash': '0x0000000000000000000000000000000000000000000000000000000000000000', 
'proofOfAuthorityData': HexBytes('0xd883010916846765746888676f312e31332e38856c696e757833c10d4e05aed2e565806c09c230894c47daa2d18b278a2905045a55c61516c7'), 
'gasLimit': 89722406, 
'gasUsed': 21000, 
'hash': HexBytes('0x94ec6c0142cbf074457234c7a6fa6661f818a6859efcbae166598b91da81c045'), 
'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'miner': '0x0100000000000000000000000000000000000000',
'mixHash': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000'), 
'nonce': HexBytes('0x0000000000000000'), 
'number': 111, 
'parentHash': HexBytes('0x3e5460ee05e93d4025681cede036d5876f60404140915104c10284a563a97dc0'), 
'receiptsRoot': HexBytes('0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2'),
'sha3Uncles': HexBytes('0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'), 
'size': 718, 
'stateRoot': HexBytes('0x83f5f7feb20c5716668ff5867729148af33a3e895a607fe495ec5af91d2e78bd'), 
'timestamp': 1600930023,
'totalDifficulty': 111, 
'transactions': [HexBytes('0xa0755b4f899841a58ff675c381994e5e36858920ac9a037f5dbfe8343b055ca8')],
'transactionsRoot': HexBytes('0x659f84685620333b164372d7bee9f103641f64ff45a4f8888de5bf3c70a16d5f'), 
'uncles': []}
     
     
'''查询块信息,包括完成的交易信息'''
web3.eth.get_block(block_identifier=111,full_transactions=False)
{
'blockExtraData': '0x', 
'difficulty': 1, 
'extDataHash': '0x0000000000000000000000000000000000000000000000000000000000000000', 
'proofOfAuthorityData': HexBytes('0xd883010916846765746888676f312e31332e38856c696e757833c10d4e05aed2e565806c09c230894c47daa2d18b278a2905045a55c61516c7'), 
'gasLimit': 89722406,
'gasUsed': 21000, 
'hash': HexBytes('0x94ec6c0142cbf074457234c7a6fa6661f818a6859efcbae166598b91da81c045'), 
'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 
'miner': '0x0100000000000000000000000000000000000000',
'mixHash': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000'),
'nonce': HexBytes('0x0000000000000000'),
'number': 111, 
'parentHash': HexBytes('0x3e5460ee05e93d4025681cede036d5876f60404140915104c10284a563a97dc0'), 
'receiptsRoot': HexBytes('0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2'),
'sha3Uncles': HexBytes('0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'),
'size': 718, 'stateRoot': HexBytes('0x83f5f7feb20c5716668ff5867729148af33a3e895a607fe495ec5af91d2e78bd'),
'timestamp': 1600930023, 
'totalDifficulty': 111, 
'transactions': 
      [
          AttributeDict({
              'blockHash': HexBytes('0x94ec6c0142cbf074457234c7a6fa6661f818a6859efcbae166598b91da81c045'), 
              'blockNumber': 111, 'from': '0x572f4D80f10f663B5049F789546f25f70Bb62a7F',
               'gas': 21000, 
               'gasPrice': 500000000000, 
               'hash': HexBytes('
                   0xa0755b4f899841a58ff675c381994e5e36858920ac9a037f5dbfe8343b055ca8'
                   ), 'input': '0x', 
               'nonce': 92, 
               'to': '0xF1400D80F913a82D12a4d5313c9A6e4a0f56Cbb7',
               'transactionIndex': 0, 
               'value': 200000000000000000, 
               'type': '0x0', 
               'chainId': '0xa869',
               'v': 86262, 
               'r': HexBytes('0x01845c893909c494746f9057ca61f68035c8ea5d0cd0dfc12295458ad38b37c2'), 
               's': HexBytes('0x59b07ac872d42e67e5eff8c47beca688a5c2a316b29426b604d537f9665f61dc')})], 
               'transactionsRoot': HexBytes('0x659f84685620333b164372d7bee9f103641f64ff45a4f8888de5bf3c70a16d5f'), 
               'uncles': []}) 

eth.get_block_transaction_count
根据块高度或者块HX查询块的交易笔数

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

web3.eth.get_block_transaction_count(111)
1

web3.eth.get_block_transaction_count('0x659f84685620333b164372d7bee9f103641f64ff45a4f8888de5bf3c70a16d5f')
1

eth.get_transaction_receipt
通过交易HX查询交易详情

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))



web3.eth.get_transaction_receipt('0x0e3713a018e74149d00ad0ccf30271af4f9209bc888a34e18f09a4ed6d0032a9')
{
'blockHash': HexBytes('0x1ebff86c8b86ee3acda22bd3d8de51703b02c7fc6b9f56e1461d11c808cf4d99'), 
'blockNumber': 19700539, 
'contractAddress': None, 
'cumulativeGasUsed': 70727, 
'effectiveGasPrice': 25000000000, 
'from': '0xd97F0D2537800BFBb78D9d7Cf087f4A397CbCc3B', 
'gasUsed': 70727, 
'logs': [
    AttributeDict({
        'address': '0xD986fE4b11c73B63d72EFF7207858e5785b2752d', 
        'topics': [
            HexBytes('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'), 
            HexBytes('0x0000000000000000000000005fa7d55f978781e8a4b183ca910136f5242e5073'), 
            HexBytes('0x000000000000000000000000d97f0d2537800bfbb78d9d7cf087f4a397cbcc3b')], 
            'data': '0x000000000000000000000000000000000000000000000000000000000006ddd0', 
            'blockNumber': 19700539, 
            'transactionHash': HexBytes('0x0e3713a018e74149d00ad0ccf30271af4f9209bc888a34e18f09a4ed6d0032a9'), 
            'transactionIndex': 0, 
            'blockHash': HexBytes('0x1ebff86c8b86ee3acda22bd3d8de51703b02c7fc6b9f56e1461d11c808cf4d99'), 
            'logIndex': 0, 
            'removed': False
            }), 
    AttributeDict({
        'address': '0x5fA7D55F978781e8A4B183cA910136f5242e5073', 
        'topics': [
            HexBytes('0x0c187edcd92269145104ed94ee618c57f91d45da8dbe5b91a2f950cabb9c6c99')
            ], 
        'data': '0x0000000000000000000000000000000000000000000000000000000000000430000000000000000000000000000000000000000000000000000000000006ddd0', 
        'blockNumber': 19700539, 
        'transactionHash': HexBytes('0x0e3713a018e74149d00ad0ccf30271af4f9209bc888a34e18f09a4ed6d0032a9'), 
        'transactionIndex': 0, 
        'blockHash': HexBytes('0x1ebff86c8b86ee3acda22bd3d8de51703b02c7fc6b9f56e1461d11c808cf4d99'), 
        'logIndex': 1, 
        'removed': False
        })
        ], 
'logsBloom': HexBytes('0x00000000000000000002000200000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000800000000000000800000000000000000008000000000000000000000000000080000000000000000000002000000000000000000000000000000000000000000010000000100210000000000000000040000000000000000000004000000000000000000000000000000000400020000000000000000000000000000000000000000000040000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 
'status': 1, 
'to': '0x5fA7D55F978781e8A4B183cA910136f5242e5073', 
'transactionHash': HexBytes('0x0e3713a018e74149d00ad0ccf30271af4f9209bc888a34e18f09a4ed6d0032a9'), 
'transactionIndex': 0, 
'type': '0x0'
}

eth.get_transaction_by_block
根据块高和 下标查询交易详情
block_identifier : 区块高度
Index :第几笔交易

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

web3.eth.get_transaction_by_block(19700539,0)
{
    'blockHash': HexBytes('0x1ebff86c8b86ee3acda22bd3d8de51703b02c7fc6b9f56e1461d11c808cf4d99'),
    'blockNumber': 19700539,
    'from': '0xd97F0D2537800BFBb78D9d7Cf087f4A397CbCc3B', 
    'gas': 71392, 
    'gasPrice': 25000000000, 
    'hash': HexBytes('0x0e3713a018e74149d00ad0ccf30271af4f9209bc888a34e18f09a4ed6d0032a9'), 
    'input': '0x455c0d4a000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000430', 
    'nonce': 42267, 
    'to': '0x5fA7D55F978781e8A4B183cA910136f5242e5073', 
    'transactionIndex': 0, 
    'value': 0, 
    'type': '0x0', 
    'chainId': '0xa869', 
    'v': 86262, 
    'r': HexBytes('0xd9a79bd6e1971dbf9f6692dec3f1dac6f7bc29c977d9dd75245f857cf7f14a8b'), 
    's': HexBytes('0x0f7e3123938f773cd390403502a725f14da5f4261e06cb4690868f48111528da')
}

eth.wait_for_transaction_receipt
轮询链上交易HX,找到返回交易详情,未找到返回错误
transaction_hash:必填
timeout:非必填,默认两分钟(单位秒)

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

web3.eth.wait_for_transaction_receipt("0x0e3713a018e74149d00ad0ccf30271af4f9209bc888a34e18f09a4ed6d0032a9")
{
'blockHash': HexBytes('0x1ebff86c8b86ee3acda22bd3d8de51703b02c7fc6b9f56e1461d11c808cf4d99'), 
'blockNumber': 19700539, 
'contractAddress': None, 
'cumulativeGasUsed': 70727, 
'effectiveGasPrice': 25000000000, 
'from': '0xd97F0D2537800BFBb78D9d7Cf087f4A397CbCc3B', 
'gasUsed': 70727, 
'logs': [
    AttributeDict({
        'address': '0xD986fE4b11c73B63d72EFF7207858e5785b2752d', 
        'topics': [
            HexBytes('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'), 
            HexBytes('0x0000000000000000000000005fa7d55f978781e8a4b183ca910136f5242e5073'), 
            HexBytes('0x000000000000000000000000d97f0d2537800bfbb78d9d7cf087f4a397cbcc3b')
            ], 
        'data': '0x000000000000000000000000000000000000000000000000000000000006ddd0', 
        'blockNumber': 19700539, 
        'transactionHash': HexBytes('0x0e3713a018e74149d00ad0ccf30271af4f9209bc888a34e18f09a4ed6d0032a9'), 
        'transactionIndex': 0, 'blockHash': HexBytes('0x1ebff86c8b86ee3acda22bd3d8de51703b02c7fc6b9f56e1461d11c808cf4d99'), 
        'logIndex': 0, 
        'removed': False
        }), 
    AttributeDict({
        'address': '0x5fA7D55F978781e8A4B183cA910136f5242e5073', 
        'topics': [
            HexBytes('0x0c187edcd92269145104ed94ee618c57f91d45da8dbe5b91a2f950cabb9c6c99')
            ], 'data': '0x0000000000000000000000000000000000000000000000000000000000000430000000000000000000000000000000000000000000000000000000000006ddd0', 
        'blockNumber': 19700539, 
        'transactionHash': HexBytes('0x0e3713a018e74149d00ad0ccf30271af4f9209bc888a34e18f09a4ed6d0032a9'), 
        'transactionIndex': 0, 
        'blockHash': HexBytes('0x1ebff86c8b86ee3acda22bd3d8de51703b02c7fc6b9f56e1461d11c808cf4d99'), 
        'logIndex': 1, 'removed': False
        })
    ], 
'logsBloom': HexBytes('0x00000000000000000002000200000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000800000000000000800000000000000000008000000000000000000000000000080000000000000000000002000000000000000000000000000000000000000000010000000100210000000000000000040000000000000000000004000000000000000000000000000000000400020000000000000000000000000000000000000000000040000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 
'status': 1, 
'to': '0x5fA7D55F978781e8A4B183cA910136f5242e5073', 
'transactionHash': HexBytes('0x0e3713a018e74149d00ad0ccf30271af4f9209bc888a34e18f09a4ed6d0032a9'), 
'transactionIndex': 0, 
'type': '0x0'
}

eth.estimate_gas
预估手续费

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

web3.eth.estimate_gas({'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601', 'from':'0xd3CdA913deB6f67967B99D67aCDFa1712C293601', 'value': 12345})
21000

eth.getTransactionCount
查询nonce


from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

acct =web33.eth.account.privateKeyToAccount(privateKey)

web3.eth.getTransactionCount('0x2837331a85c77138D843250692e0bc3cF7C6B8D5')
3304

eth.account.signTransaction
签名交易

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

key = 'c89866c1a5541cf325d8b161e37b12926ec093bf85fb8996e41c942f409f8e40'
web3.eth.account.signTransaction(transaction,key)
gas = web3.eth.estimateGas({'from': acct.address, 'to': acct.address, 'value': 1})
tr = {
    'from': acct.address,
    'to': acct.address,
    'nonce': web3.eth.getTransactionCount(acct.address),
    'gasPrice': web3.eth.gas_price,
    'gas': gas,
    'value': 1,
    'data': '',
    'chainId': web3.eth.chainId
}
sig = web3.eth.account.signTransaction(tr, key)
web3.eth.sendRawTransaction(sig.rawTransaction)

SignedTransaction(rawTransaction=HexBytes('0xf869820ce88505d21dba00825208942837331a85c77138d843250692e0bc3cf7c6b8d50180830150f6a0fbc7f8f55f6473479d5c1f2bd0bd10d21e12c7886dd0ec40fc21b060aae07419a04f18d6eea278ef9b611eee8029b1addef1542e07c70c81c7fa1891c63fc61f0c'), hash=HexBytes('0x3c5a61afad21f34ed58493754217e987bc87995fae3e182c68174aa19028d2ec'), r=113883845808617690044871202117477804700152330397016477591268504947707468608537, s=35776602774928587288456033015730457511495579849041010202069064808800658530060, v=86262)

sendRawTransaction
广播交易

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

key = 'c89866c1a5541cf325d8b161e37b12926ec093bf85fb8996e41c942f409f8e40'
web3.eth.account.signTransaction(transaction,key)
gas = web3.eth.estimateGas({'from': acct.address, 'to': acct.address, 'value': 1})
tr = {
    'from': acct.address,
    'to': acct.address,
    'nonce': web3.eth.getTransactionCount(acct.address),
    'gasPrice': web3.eth.gas_price,
    'gas': gas,
    'value': 1,
    'data': '',
    'chainId': web3.eth.chainId
}
web3.eth.account.signTransaction(tr, key)
b'\x18,h\x17J\xa1\x90(\xd2\xec'

eth.account.privateKeyToAccount/from_key
通过私钥返回一个加密对象
address:合约地址
abi :合约abi

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

'''新版'''
web3.eth.account.from_key('c89866c1a5541cf325d8b161e37b12926ec093bf85fb8996e41c942f409f8e40')
<eth_account.signers.local.LocalAccount object at 0x7fae76d75130>

'''旧版'''
web3.eth.account.privateKeyToAccount('c89866c1a5541cf325d8b161e37b12926ec093bf85fb8996e41c942f409f8e40')
<eth_account.signers.local.LocalAccount object at 0x7fec04f8f160>

.address
换算地址

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))


acct = web3.eth.account.from_key('c89866c1a5541cf325d8b161e37b12926ec093bf85fb8996e41c942f409f8e40')

acct.address
0x2837331a85c77138D843250692e0bc3cF7C6B8D5

.key/privateKey
换算私钥

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))


acct = web3.eth.account.from_key('c89866c1a5541cf325d8b161e37b12926ec093bf85fb8996e41c942f409f8e40')

'''旧'''
acct.privateKey
b'\xc8\x98f\xc1\xa5T\x1c\xf3%\xd8\xb1a\xe3{\x12\x92n\xc0\x93\xbf\x85\xfb\x89\x96\xe4\x1c\x94/@\x9f\x8e@'

'''新版'''
acct.key
b'\xc8\x98f\xc1\xa5T\x1c\xf3%\xd8\xb1a\xe3{\x12\x92n\xc0\x93\xbf\x85\xfb\x89\x96\xe4\x1c\x94/@\x9f\x8e@'

contract
连接合约
eth.contract

from web3 import Web3


web33 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))
contracts = web33.eth.contract(address='0xd0fABb17BD2999A4A9fDF0F05c2386e7dF6519bb', abi='[{"anonymous":false,"inputs":[{"indexed":true,"intern.............')

functions
调用合约中的方法

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

contracts = web33.eth.contract(address='0xd0fABb17BD2999A4A9fDF0F05c2386e7dF6519bb', abi='[{"anonymous":false,"inputs":[{"indexed":true,"intern.............')

'''通过functions调用合约的transfer方法,打包交易'''
transaction = contracts.functions.transfer(to_address,amount).buildTransaction({
    'from': acct.address,
    'nonce': node,
    'gasPrice': int(web33.eth.gas_price * 1.2)})  # 21000000000

3.其他方法

hex()
bytes转hx格式

hx = b'\xc8\x98f\xc1\xa5T\x1c\xf3%\xd8\xb1a\xe3{\x12\x92n\xc0\x93\xbf\x85\xfb\x89\x96\xe4\x1c\x94/@\x9f\x8e@'
hx.hex()
c89866c1a5541cf325d8b161e37b12926ec093bf85fb8996e41c942f409f8e40

buildTransactions
传递数据生成字典格式

from web3 import Web3


web3 = Web3(Web3.HTTPProvider('https://api.avax-test.network/ext/bc/C/rpc'))

contracts = web33.eth.contract(address='0xd0fABb17BD2999A4A9fDF0F05c2386e7dF6519bb', abi='[{"anonymous":false,"inputs":[{"indexed":true,"intern.............')

'''通过functions调用合约的transfer方法,常用数据要生成字典格式,打包交易'''
transaction = contracts.functions.transfer(to_address,amount).buildTransaction({
    'from': acct.address,
    'nonce': node,
    'gasPrice': int(web33.eth.gas_price * 1.2)})  # 21000000000

你可能感兴趣的:(python,web3,开发语言)