简介:Etherscan 大多数朋友都比较熟悉了,它是主流以太坊区块浏览器。Etherscan 有面向开发者提供 API 服务以方便其检索以太坊区块链信息。本文示例如何使用 Etherscan API 获取以太坊已验证源码的合约ABI。
Etherscan 大多数朋友都比较熟悉了,它是主流以太坊区块浏览器。Etherscan 有面向开发者提供 API 服务以方便其检索以太坊区块链信息。
Etherscan API 在没有密钥的情况下,支持每秒最多五次请求。有更多请求需求可以在这里申请密钥:https://etherscancom.freshdesk.com/support/solutions/articles/35000022163-i-need-an-api-key
使用 Etherscan API 获取以太坊已验证源码的合约ABI:
语句:
https://api.etherscan.io/api?module=contract&action=getabi&address={填入合约地址}&apikey={填入你的ApiKey}
当然,不使用 apikey
也是可以查询的:
https://api.etherscan.io/api?module=contract&action=getabi&address={填入合约地址}
Node.js 代码示例:
const fetch = require('node-fetch');
fetch('https://api.etherscan.io/api?module=contract&action=getabi&address={填入合约地址}&apikey={填入你的ApiKey}', {
method: 'get',
}).then(response => response.json()
.then(data => console.log(data)));
返回的 JSON 示例:
{
"status": "1",
"message": "OK",
"result": "[{\"constant\": true, \"inputs\": [{ \"name\": \"\", \"type\": \"uint256\" }], \"name\": \"proposals\", \"outputs\": [{ \"name\": \"recipient\", \"type\": \"address\" }, { \"name\": \"amount\", \"type\": \"uint256\" }, { \"name\": \"description\", \"type\": \"string\" }, { \"name\": \"votingDeadline\", \"type\": \"uint256\" }, { \"name\": \"open\", \"type\": \"bool\" }, { \"name\": \"proposalPassed\", \"type\": \"bool\" }, { \"name\": \"proposalHash\", \"type\": \"bytes32\" }, { \"name\": \"proposalDeposit\", \"type\": \"uint256\" }, { \"name\": \"newCurator\", \"type\": \"bool\" }, { \"name\": \"yea\", \"type\": \"uint256\" }, { \"name\": \"nay\", \"type\": \"uint256\" }, { \"name\": \"creator\", \"type\": \"address\" }], \"type\": \"function\" }]"
}
result
提取并去除转义后得:
[{
"constant": true,
"inputs": [{
"name": "",
"type": "uint256"
}],
"name": "proposals",
"outputs": [{
"name": "recipient",
"type": "address"
}, {
"name": "amount",
"type": "uint256"
}, {
"name": "description",
"type": "string"
}, {
"name": "votingDeadline",
"type": "uint256"
}, {
"name": "open",
"type": "bool"
}, {
"name": "proposalPassed",
"type": "bool"
}, {
"name": "proposalHash",
"type": "bytes32"
}, {
"name": "proposalDeposit",
"type": "uint256"
}, {
"name": "newCurator",
"type": "bool"
}, {
"name": "yea",
"type": "uint256"
}, {
"name": "nay",
"type": "uint256"
}, {
"name": "creator",
"type": "address"
}],
"type": "function"
}]
这是使用 Web3.js 和 Jquery 检索合约 ABI 以与合约进行交互的简单示例:
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider());
var version = web3.version.api;
$.getJSON('http://api.etherscan.io/api?module=contract&action=getabi&address=0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359', function (data) {
var contractABI = "";
contractABI = JSON.parse(data.result);
if (contractABI != ''){
var MyContract = web3.eth.contract(contractABI);
var myContractInstance = MyContract.at("0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359");
var result = myContractInstance.memberId("0xfe8ad7dd2f564a877cc23feea6c0a9cc2e783715");
console.log("result1 : " + result);
var result = myContractInstance.members(1);
console.log("result2 : " + result);
} else {
console.log("Error" );
}
});
Etherscan API 官方文档:https://etherscan.io/apis
Etherscan API 思维导图:
Etherscan API.png我们有一个区块链知识星球,做区块链前沿资料的归纳整理以方便大家检索查询使用,也是国内顶尖区块链技术社区,欢迎感兴趣的朋友加入。如果你对上面内容有疑问,也可以加入知识星球提问我:
区块链社群 知识星球
网友评论