简介: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 获取以太坊地址来往交易列表:
语句:
http://api.etherscan.io/api?module=account&action=tokentx&address={填入查询地址}&startblock=0&endblock=999999999&sort=asc&apikey={填入你的ApiKey}
当然,不使用 apikey
也是可以查询的:
http://api.etherscan.io/api?module=account&action=tokentx&address={填入查询地址}&startblock=0&endblock=999999999&sort=asc
如果需要获取分页结果,设置一下page
和offset
即可:
https://api.etherscan.io/api?module=account&action=tokentx&address={填入查询地址}&page={页码}&offset={每页结果数}&sort=asc&apikey={填入你的ApiKey}
如果需要指定代币合约查询交易记录,增加contractaddress
参数就行:
https://api.etherscan.io/api?module=account&action=tokentx&contractaddress={填入查询代币合约地址}&address={填入查询地址}&page=1&offset=100&sort=asc&apikey={填入你的ApiKey}
Node.js 代码示例:
const fetch = require('node-fetch');
fetch('http://api.etherscan.io/api?module=account&action=tokentx&address={填入查询地址}&startblock=0&endblock=999999999&sort=asc&apikey={填入你的ApiKey}', {
method: 'get',
}).then(response => response.json()
.then(data => console.log(data)));
返回的 JSON 示例:
{
"status": "1",
"message": "OK",
"result": [
{
"blockNumber": "2228258",
"timeStamp": "1473433992",
"hash": "0x5f2cd76fd3656686e356bc02cc91d8d0726a16936fd08e67ed30467053225a86",
"nonce": "10",
"blockHash": "0x466dbced0a98e1b0490f787b51570850c8347960a4e92bd89acf85299e4cb9dc",
"from": "0x4e83362442b8d1bec281594cea3050c8eb01311c",
"contractAddress": "0xecf8f87f810ecf450940c9f60066b4a7a501d6a7",
"to": "0xac75b73394c329376c214663d92156afa864a77f",
"value": "101000000000000000000",
"tokenName": "\u0001",
"tokenSymbol": "\u0001",
"tokenDecimal": "1",
"transactionIndex": "10",
"gas": "1000000",
"gasPrice": "20000000000",
"gasUsed": "93657",
"cumulativeGasUsed": "684715",
"input": "0x2ac9bf090000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000005a34a38fc00a00000000000000000000000000000000000000000000000000005386e53c7de1e0000",
"confirmations": "5219883"
}
]
}
Etherscan API 官方文档:https://etherscan.io/apis
Etherscan API 思维导图:
Etherscan API.png我们有一个区块链知识星球,做区块链前沿资料的归纳整理以方便大家检索查询使用,也是国内顶尖区块链技术社区,欢迎感兴趣的朋友加入。如果你对上面内容有疑问,也可以加入知识星球提问我:
区块链社群 知识星球
网友评论