Geth API
根据 EIP 1474 ,对象web3.geth
公开了模块,使你能够与 Geth 支持的 JSON-RPC 端点交互,这些端点没有在以太坊 JSONRPC 端点的标准集中定义。
GethAdmin API
以下方法在web3.geth.admin
名称空间中可用。
web3.geth.admin
对象公开了与 Geth 客户端支持的admin_
名称空间下的 RPC APIs 进行交互的方法。
web3.geth.admin.datadir()
- 委托给
admin_datadir
RPC 方法
返回节点数据目录的系统路径。
>>> web3.geth.admin.datadir()
'/Users/piper/Library/Ethereum'
web3.geth.admin.node_info()
- 委托给
admin_nodeInfo
RPC 方法
返回当前运行节点的信息。
>>> web3.geth.admin.node_info()
{
'enode': 'enode://e54eebad24dce1f6d246bea455ffa756d97801582420b9ed681a2ea84bf376d0bd87ae8dd6dc06cdb862a2ca89ecabe1be1050be35b4e70d62bc1a092cb7e2d3@[::]:30303',
'id': 'e54eebad24dce1f6d246bea455ffa756d97801582420b9ed681a2ea84bf376d0bd87ae8dd6dc06cdb862a2ca89ecabe1be1050be35b4e70d62bc1a092cb7e2d3',
'ip': '::',
'listenAddr': '[::]:30303',
'name': 'Geth/v1.4.11-stable-fed692f6/darwin/go1.7',
'ports': {'discovery': 30303, 'listener': 30303},
'protocols': {
'eth': {
'difficulty': 57631175724744612603,
'genesis': '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3',
'head': '0xaaef6b9dd0d34088915f4c62b6c166379da2ad250a88f76955508f7cc81fb796',
'network': 1,
},
},
}
web3.geth.admin.nodeInfo()
警告
已弃用:此方法已弃用,取而代之的是 node_info()
web3.geth.admin.peers()
- 委托给
admin_peers
RPC 方法
返回节点连接到的当前对等点。
>>> web3.geth.admin.peers()
[
{
'caps': ['eth/63'],
'id': '146e8e3e2460f1e18939a5da37c4a79f149c8b9837240d49c7d94c122f30064e07e4a42ae2c2992d0f8e7e6f68a30e7e9ad31d524349ec9d17effd2426a37b40',
'name': 'Geth/v1.4.10-stable/windows/go1.6.2',
'network': {
'localAddress': '10.0.3.115:64478',
'remoteAddress': '72.208.167.127:30303',
},
'protocols': {
'eth': {
'difficulty': 17179869184,
'head': '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3',
'version': 63,
},
}
},
{
'caps': ['eth/62', 'eth/63'],
'id': '76cb6cd3354be081923a90dfd4cda40aa78b307cc3cf4d5733dc32cc171d00f7c08356e9eb2ea47eab5aad7a15a3419b859139e3f762e1e1ebf5a04f530dcef7',
'name': 'Geth/v1.4.10-stable-5f55d95a/linux/go1.5.1',
'network': {
'localAddress': '10.0.3.115:64784',
'remoteAddress': '60.205.92.119:30303',
},
'protocols': {
'eth': {
'difficulty': 57631175724744612603,
'head': '0xaaef6b9dd0d34088915f4c62b6c166379da2ad250a88f76955508f7cc81fb796',
'version': 63,
},
},
},
...
]
web3.geth.admin.add_peer(*node_url*)
- 委托给
admin_addPeer
RPC 方法
请求将新的远程节点添加到被跟踪的静态节点列表中。
>>> web3.geth.admin.add_peer('enode://e54eebad24dce1f6d246bea455ffa756d97801582420b9ed681a2ea84bf376d0bd87ae8dd6dc06cdb862a2ca89ecabe1be1050be35b4e70d62bc1a092cb7e2d3@52.71.255.237:30303')
True
web3.geth.admin.addPeer(*node_url*)
警告
已弃用:此方法已弃用,取而代之的是 add_peer()
web3.geth.admin.start_rpc(*host='localhost'*, *port=8545*, *cors=""*, *apis="eth*, *net*, *web3"*)
- 委托给
admin_startRPC
RPC 方法
在指定的host
和port
上启动基于 HTTP 的 JSON RPC API webserver,将rpccorsdomain
设置为提供的cors
值,并启用由apis
指定的 API。返回关于服务器是否成功启动的布尔值。
>>> web3.geth.admin.start_rpc()
True
web3.geth.admin.startRPC(*host='localhost'*, *port=8545*, *cors=""*, *apis="eth*, *net*, *web3"*)
警告
已弃用:此方法已弃用,取而代之的是 start_rpc()
web3.geth.admin.start_ws(*host='localhost'*, *port=8546*, *cors=""*, *apis="eth*, *net*, *web3"*)
- 委托给
admin_startWS
RPC 方法
在指定的host
和port
上启动基于 Websocket 的 JSON RPC API webserver,将rpccorsdomain
设置为提供的cors
值,并启用由apis
指定的 API。返回关于服务器是否成功启动的布尔值。
>>> web3.geth.admin.start_ws()
True
web3.geth.admin.startWS(*host='localhost'*, *port=8546*, *cors=""*, *apis="eth*, *net*, *web3"*)
警告
已弃用:此方法已弃用,取而代之的是 start_ws()
web3.geth.admin.stop_rpc()
- 委托给
admin_stopRPC
RPC 方法
停止基于 HTTP 的 JSON RPC 服务器。
>>> web3.geth.admin.stop_rpc()
True
web3.geth.admin.stopRPC()
警告
已弃用:此方法已弃用,取而代之的是 stop_rpc()
web3.geth.admin.stop_ws()
- 委托给
admin_stopWS
RPC 方法
停止基于 Websocket 的 JSON RPC 服务器。
>>> web3.geth.admin.stop_ws()
True
web3.geth.admin.stopWS()
警告
已弃用:此方法已弃用,取而代之的是 stop_ws()
GethPersonal API
以下方法在web3.geth.personal
名称空间中可用。
web3.geth.personal.list_accounts()
- 委托给
personal_listAccounts
RPC 方法
返回已知帐户的列表。
>>> web3.geth.personal.list_accounts()
['0xd3CdA913deB6f67967B99D67aCDFa1712C293601']
web3.geth.personal.listAccounts()
警告
已弃用:此方法已弃用,取而代之的是 list_accounts()
web3.geth.personal.list_wallets()
- 委托给
personal_listWallets
RPC 方法
返回 Geth 管理的钱包列表。
>>> web3.geth.personal.list_wallets()
[{
accounts: [{
address: "0x44f705f3c31017856777f2931c2f09f240dd800b",
url: "keystore:///path/to/keystore/UTC--2020-03-30T23-24-43.133883000Z--44f705f3c31017856777f2931c2f09f240dd800b"
}],
status: "Unlocked",
url: "keystore:///path/to/keystore/UTC--2020-03-30T23-24-43.133883000Z--44f705f3c31017856777f2931c2f09f240dd800b"
}]
web3.geth.personal.import_raw_key(*self*, *private_key*, *passphrase*)
- 委托给
personal_importRawKey
RPC 方法
将给定的private_key
添加到节点的钥匙串中,用给定的passphrase
加密。返回导入帐户的地址。
>>> web3.geth.personal.import_raw_key(some_private_key, 'the-passphrase')
'0xd3CdA913deB6f67967B99D67aCDFa1712C293601'
web3.geth.personal.importRawKey()
警告
已弃用:此方法已弃用,取而代之的是 import_raw_key()
web3.geth.personal.new_account(*self*, *passphrase*)
- 委托给
personal_newAccount
RPC 方法
在节点的钥匙串中生成一个用给定的passphrase
加密的新帐户。返回创建的帐户的地址。
>>> web3.geth.personal.new_account('the-passphrase')
'0xd3CdA913deB6f67967B99D67aCDFa1712C293601'
web3.geth.personal.newAccount()
警告
已弃用:此方法已弃用,取而代之的是 new_account()
web3.geth.personal.lock_account(*self*, *account*)
- 委托给
personal_lockAccount
RPC 方法
锁定给定的account
。
>>> web3.geth.personal.lock_account('0xd3CdA913deB6f67967B99D67aCDFa1712C293601')
web3.geth.personal.lockAccount()
警告
已弃用:此方法已弃用,取而代之的是 lock_account()
web3.geth.personal.unlock_account(*self*, *account*, *passphrase*, *duration=None*)
- 委托给
personal_unlockAccount
RPC 方法
解锁给定的account
达duration
秒。如果duration
是None
,那么账户将在 Geth 设置的当前默认持续时间内保持解锁状态。如果duration
设置为0
,账户将无限期保持解锁状态。返回一个boolean
,表示账户是否解锁成功。
>>> web3.geth.personal.unlock_account('0xd3CdA913deB6f67967B99D67aCDFa1712C293601', 'wrong-passphrase')
False
>>> web3.geth.personal.unlock_account('0xd3CdA913deB6f67967B99D67aCDFa1712C293601', 'the-passphrase')
True
web3.geth.personal.unlockAccount()
警告
已弃用:此方法已弃用,取而代之的是 unlock_account()
web3.geth.personal.send_transaction(*self*, *transaction*, *passphrase*)
- 委托给
personal_sendTransaction
RPC 方法
发送事务处理。
web3.geth.personal.sendTransaction()
警告
已弃用:此方法已弃用,取而代之的是 send_transaction()
GethTxPool API
web3.geth.txpool
对象公开了与txpool_
名称空间下的 RPC APIs 交互的方法。这些方法只在geth
命名空间下公开,因为它们不是标准的,也不支持奇偶校验。
以下方法在web3.geth.txpool
名称空间中可用。
TxPool.inspect()
- 委托给
txpool_inspect
RPC 方法
返回当前待包含在下一个块中的所有事务以及计划将来执行的事务的文本摘要。
>>> web3.geth.txpool.inspect()
{
'pending': {
'0x26588a9301b0428d95e6Fc3A5024fcE8BEc12D51': {
31813: ["0x3375Ee30428b2A71c428afa5E89e427905F95F7e: 0 wei + 500000 × 20000000000 gas"]
},
'0x2a65Aca4D5fC5B5C859090a6c34d164135398226': {
563662: ["0x958c1Fa64B34db746925c6F8a3Dd81128e40355E: 1051546810000000000 wei + 90000 × 20000000000 gas"],
563663: ["0x77517B1491a0299A44d668473411676f94e97E34: 1051190740000000000 wei + 90000 × 20000000000 gas"],
563664: ["0x3E2A7Fe169c8F8eee251BB00d9fb6d304cE07d3A: 1050828950000000000 wei + 90000 × 20000000000 gas"],
563665: ["0xAF6c4695da477F8C663eA2D8B768Ad82Cb6A8522: 1050544770000000000 wei + 90000 × 20000000000 gas"],
563666: ["0x139B148094C50F4d20b01cAf21B85eDb711574dB: 1048598530000000000 wei + 90000 × 20000000000 gas"],
563667: ["0x48B3Bd66770b0D1EeceFCe090daFeE36257538aE: 1048367260000000000 wei + 90000 × 20000000000 gas"],
563668: ["0x468569500925D53e06Dd0993014aD166fD7Dd381: 1048126690000000000 wei + 90000 × 20000000000 gas"],
563669: ["0x3DcB4C90477a4b8Ff7190b79b524773CbE3bE661: 1047965690000000000 wei + 90000 × 20000000000 gas"],
563670: ["0x6DfeF5BC94b031407FFe71ae8076CA0FbF190963: 1047859050000000000 wei + 90000 × 20000000000 gas"]
},
'0x9174E688d7dE157C5C0583Df424EAAB2676aC162': {
3: ["0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413: 30000000000000000000 wei + 85000 × 21000000000 gas"]
},
'0xb18F9d01323e150096650ab989CfecD39D757Aec': {
777: ["0xcD79c72690750F079ae6AB6ccd7e7aEDC03c7720: 0 wei + 1000000 × 20000000000 gas"]
},
'0xB2916C870Cf66967B6510B76c07E9d13a5D23514': {
2: ["0x576f25199D60982A8f31A8DfF4da8aCB982e6ABa: 26000000000000000000 wei + 90000 × 20000000000 gas"]
},
'0xBc0CA4f217E052753614d6B019948824d0d8688B': {
0: ["0x2910543Af39abA0Cd09dBb2D50200b3E800A63D2: 1000000000000000000 wei + 50000 × 1171602790622 gas"]
},
'0xea674fdde714fd979de3edf0f56aa9716b898ec8': {
70148: ["0xe39c55ead9f997f7fa20ebe40fb4649943d7db66: 1000767667434026200 wei + 90000 × 20000000000 gas"]
}
},
'queued': {
'0x0F6000De1578619320aBA5e392706b131FB1dE6f': {
6: ["0x8383534d0bcd0186d326C993031311c0Ac0D9B2d: 9000000000000000000 wei + 21000 × 20000000000 gas"]
},
'0x5b30608c678e1ac464A8994C3B33E5CdF3497112': {
6: ["0x9773547e27f8303C87089dc42D9288aa2B9d8F06: 50000000000000000000 wei + 90000 × 50000000000 gas"]
},
'0x976A3Fc5d6f7d259EBfb4cc2Ae75115475E9867C': {
3: ["0x346FB27dE7E7370008f5da379f74dd49F5f2F80F: 140000000000000000 wei + 90000 × 20000000000 gas"]
},
'0x9B11bF0459b0c4b2f87f8CEBca4cfc26f294B63A': {
2: ["0x24a461f25eE6a318BDef7F33De634A67bb67Ac9D: 17000000000000000000 wei + 90000 × 50000000000 gas"],
6: ["0x6368f3f8c2B42435D6C136757382E4A59436a681: 17990000000000000000 wei + 90000 × 20000000000 gas", "0x8db7b4e0ecb095fbd01dffa62010801296a9ac78: 16998950000000000000 wei + 90000 × 20000000000 gas"],
7: ["0x6368f3f8c2B42435D6C136757382E4A59436a681: 17900000000000000000 wei + 90000 × 20000000000 gas"]
}
}
}
TxPool.status()
- 委托给
txpool_status
RPC 方法
返回当前待包含在下一个块中的所有事务以及计划将来执行的事务的文本摘要。
{
pending: 10,
queued: 7,
}
TxPool.content()
- 委托给
txpool_content
RPC 方法
返回所有挂起或排队的事务的确切详细信息。
>>> web3.geth.txpool.content()
{
'pending': {
'0x0216D5032f356960Cd3749C31Ab34eEFF21B3395': {
806: [{
'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
'blockNumber': None,
'from': "0x0216D5032f356960Cd3749C31Ab34eEFF21B3395",
'gas': "0x5208",
'gasPrice': None,
'hash': "0xaf953a2d01f55cfe080c0c94150a60105e8ac3d51153058a1f03dd239dd08586",
'input': "0x",
'maxFeePerGas': '0x77359400',
'maxPriorityFeePerGas': '0x3b9aca00',
'nonce': "0x326",
'to': "0x7f69a91A3CF4bE60020fB58B893b7cbb65376db8",
'transactionIndex': None,
'value': "0x19a99f0cf456000"
}]
},
'0x24d407e5A0B506E1Cb2fae163100B5DE01F5193C': {
34: [{
'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
'blockNumber': None,
'from': "0x24d407e5A0B506E1Cb2fae163100B5DE01F5193C",
'gas': "0x44c72",
'gasPrice': None,
'hash': "0xb5b8b853af32226755a65ba0602f7ed0e8be2211516153b75e9ed640a7d359fe",
'input': "0xb61d27f600000000000000000000000024d407e5a0b506e1cb2fae163100b5de01f5193c00000000000000000000000000000000000000000000000053444835ec580000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
'maxFeePerGas': '0x77359400',
'maxPriorityFeePerGas': '0x3b9aca00',
'nonce': "0x22",
'to': "0x7320785200f74861B69C49e4ab32399a71b34f1a",
'transactionIndex': None,
'value': "0x0"
}]
}
},
'queued': {
'0x976A3Fc5d6f7d259EBfb4cc2Ae75115475E9867C': {
3: [{
'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
'blockNumber': None,
'from': "0x976A3Fc5d6f7d259EBfb4cc2Ae75115475E9867C",
'gas': "0x15f90",
'gasPrice': None,
'hash': "0x57b30c59fc39a50e1cba90e3099286dfa5aaf60294a629240b5bbec6e2e66576",
'input': "0x",
'maxFeePerGas': '0x77359400',
'maxPriorityFeePerGas': '0x3b9aca00',
'nonce': "0x3",
'to': "0x346FB27dE7E7370008f5da379f74dd49F5f2F80F",
'transactionIndex': None,
'value': "0x1f161421c8e0000"
}]
},
'0x9B11bF0459b0c4b2f87f8CEBca4cfc26f294B63A': {
2: [{
'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
'blockNumber': None,
'from': "0x9B11bF0459b0c4b2f87f8CEBca4cfc26f294B63A",
'gas': "0x15f90",
'gasPrice': None,
'hash': "0x3a3c0698552eec2455ed3190eac3996feccc806970a4a056106deaf6ceb1e5e3",
'input': "0x",
'maxFeePerGas': '0x77359400',
'maxPriorityFeePerGas': '0x3b9aca00',
'nonce': "0x2",
'to': "0x24a461f25eE6a318BDef7F33De634A67bb67Ac9D",
'transactionIndex': None,
'value': "0xebec21ee1da40000"
}],
6: [{
'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
'blockNumber': None,
'from': "0x9B11bF0459b0c4b2f87f8CEBca4cfc26f294B63A",
'gas': "0x15f90",
'gasPrice': None,
'hash': "0xbbcd1e45eae3b859203a04be7d6e1d7b03b222ec1d66dfcc8011dd39794b147e",
'input': "0x",
'maxFeePerGas': '0x77359400',
'maxPriorityFeePerGas': '0x3b9aca00',
'nonce': "0x6",
'to': "0x6368f3f8c2B42435D6C136757382E4A59436a681",
'transactionIndex': None,
'value': "0xf9a951af55470000"
}, {
'blockHash': "0x0000000000000000000000000000000000000000000000000000000000000000",
'blockNumber': None,
'from': "0x9B11bF0459b0c4b2f87f8CEBca4cfc26f294B63A",
'gas': "0x15f90",
'gasPrice': None,
'hash': "0x60803251d43f072904dc3a2d6a084701cd35b4985790baaf8a8f76696041b272",
'input': "0x",
'maxFeePerGas': '0x77359400',
'maxPriorityFeePerGas': '0x3b9aca00',
'nonce': "0x6",
'to': "0x8DB7b4e0ECB095FBD01Dffa62010801296a9ac78",
'transactionIndex': None,
'value': "0xebe866f5f0a06000"
}],
}
}
}