简介:Etherscan 大多数朋友都比较熟悉了,它是主流以太坊区块浏览器。Etherscan 有面向开发者提供 API 服务以方便其检索以太坊区块链信息。本文示例如何使用 Etherscan API 获取以太坊事件日志。
Etherscan 大多数朋友都比较熟悉了,它是主流以太坊区块浏览器。Etherscan 有面向开发者提供 API 服务以方便其检索以太坊区块链信息。
Etherscan API 在没有密钥的情况下,支持每秒最多五次请求。有更多请求需求可以在这里申请密钥:https://etherscancom.freshdesk.com/support/solutions/articles/35000022163-i-need-an-api-key
使用 Etherscan API 获取以太坊事件日志:
事件日志 API 旨在提供本机 eth_getLogs 的替代方案。以下是支持的过滤器参数列表:
fromBlock
, toBlock
, address
topic0
, topic1
, topic2
, topic3
(每个topic
32字节)
topic0_1_opr
(and|or between topic0
& topic1
), topic1_2_opr
(and|or between topic1
& topic2
), topic2_3_opr
(and|or between topic2
& topic3
),topic0_2_opr
(and|or between topic0
& topic2
), topic0_3_opr
(and|or between topic0
& topic3
), topic1_3_opr
(and|or between topic1
& topic3
)
fromBlock
& toBlock
接受区块号(整数,非十六进制)或者 'latest'(earliest & pending 暂不支持)作为参数;
topicX_X_opr
接受 'and' 或者 'or',且仅限上面列出的几种;
fromBlock
& toBlock
参数为必填;
必须使用 address
和/或 topic(X)
参数,当使用多个 topic(X)
参数时,需要使用
topicX_X_opr
。
示例:
从块号379224获取事件日志到“最新”块,其中日志地址为
0x33990122638b9132ca29c723bdf037f1a891a70c,topic
地址为 0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545:
语句:
https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock
= 379224
&toBlock = latest
&address = 0x33990122638b9132ca29c723bdf037f1a891a70c
&topic0 = 0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545
&apikey = YourApiKeyToken
当然,不使用 apikey
也是可以查询的:
https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock
= 379224
&toBlock = latest
&address = 0x33990122638b9132ca29c723bdf037f1a891a70c
&topic0 = 0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545
Node.js 代码示例:
const fetch = require('node-fetch');
fetch('https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock
= 379224&toBlock = latest&address = 0x33990122638b9132ca29c723bdf037f1a891a70c&topic0 = 0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545', {
method: 'get',
}).then(response => response.json()
.then(data => console.log(data)));
返回的 JSON 示例:
{
"status": "1",
"message": "OK",
"result": [
{
"address": "0x33990122638b9132ca29c723bdf037f1a891a70c",
"topics": [
"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
"0x72657075746174696f6e00000000000000000000000000000000000000000000",
"0x000000000000000000000000d9b2f59f3b5c7b3c67047d2f03c3e8052470be92"
],
"data": "0x",
"blockNumber": "0x5c958",
"timeStamp": "0x561d688c",
"gasPrice": "0xba43b7400",
"gasUsed": "0x10682",
"logIndex": "0x",
"transactionHash": "0x0b03498648ae2da924f961dda00dc6bb0a8df15519262b7e012b7d67f4bb7e83",
"transactionIndex": "0x"
},
{
"address": "0x33990122638b9132ca29c723bdf037f1a891a70c",
"topics": [
"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
"0x6c6f747465727900000000000000000000000000000000000000000000000000",
"0x0000000000000000000000001f6cc3f7c927e1196c03ac49c5aff0d39c9d103d"
],
"data": "0x",
"blockNumber": "0x5c965",
"timeStamp": "0x561d6930",
"gasPrice": "0xba43b7400",
"gasUsed": "0x105c2",
"logIndex": "0x",
"transactionHash": "0x8c72ea19b48947c4339077bd9c9c09a780dfbdb1cafe68db4d29cdf2754adc11",
"transactionIndex": "0x"
}
]
}
从块号379224获取事件日志到块400000,其中日志地址为0x33990122638b9132ca29c723bdf037f1a891a70c,主题为0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545和0x72657075746174696f6e000000000000000000000000000000000000000000000000:
语句:
https://api.etherscan.io/api?module=logs&action=getLogs
&fromBlock=379224
&toBlock=400000
&address=0x33990122638b9132ca29c723bdf037f1a891a70c
&topic0=0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545&
topic0_1_opr=and
&topic1=0x72657075746174696f6e00000000000000000000000000000000000000000000
&apikey=YourApiKeyToken
当然,不使用 apikey
也是可以查询的:
https://api.etherscan.io/api?module=logs&action=getLogs
&fromBlock=379224
&toBlock=400000
&address=0x33990122638b9132ca29c723bdf037f1a891a70c
&topic0=0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545&
topic0_1_opr=and
&topic1=0x72657075746174696f6e00000000000000000000000000000000000000000000
Node.js 代码示例:
const fetch = require('node-fetch');
fetch('https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=379224&toBlock=400000&address=0x33990122638b9132ca29c723bdf037f1a891a70c&topic0=0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545&
topic0_1_opr=and&topic1=0x72657075746174696f6e00000000000000000000000000000000000000000000&apikey=YourApiKeyToken', {
method: 'get',
}).then(response => response.json()
.then(data => console.log(data)));
返回的 JSON 示例:
{
"status": "1",
"message": "OK",
"result": [
{
"address": "0x33990122638b9132ca29c723bdf037f1a891a70c",
"topics": [
"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
"0x72657075746174696f6e00000000000000000000000000000000000000000000",
"0x000000000000000000000000d9b2f59f3b5c7b3c67047d2f03c3e8052470be92"
],
"data": "0x",
"blockNumber": "0x5c958",
"timeStamp": "0x561d688c",
"gasPrice": "0xba43b7400",
"gasUsed": "0x10682",
"logIndex": "0x",
"transactionHash": "0x0b03498648ae2da924f961dda00dc6bb0a8df15519262b7e012b7d67f4bb7e83",
"transactionIndex": "0x"
},
{
"address": "0x33990122638b9132ca29c723bdf037f1a891a70c",
"topics": [
"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
"0x6c6f747465727900000000000000000000000000000000000000000000000000",
"0x0000000000000000000000001f6cc3f7c927e1196c03ac49c5aff0d39c9d103d"
],
"data": "0x",
"blockNumber": "0x5c965",
"timeStamp": "0x561d6930",
"gasPrice": "0xba43b7400",
"gasUsed": "0x105c2",
"logIndex": "0x",
"transactionHash": "0x8c72ea19b48947c4339077bd9c9c09a780dfbdb1cafe68db4d29cdf2754adc11",
"transactionIndex": "0x"
}
]
}
Etherscan API 官方文档:https://etherscan.io/apis
Etherscan API 思维导图:
Etherscan API.png我们有一个区块链知识星球,做区块链前沿资料的归纳整理以方便大家检索查询使用,也是国内顶尖区块链技术社区,欢迎感兴趣的朋友加入。如果你对上面内容有疑问,也可以加入知识星球提问我:
区块链社群 知识星球
网友评论