簡(jiǎn)介:Etherscan 大多數(shù)朋友都比較熟悉了蝴乔,它是主流以太坊區(qū)塊瀏覽器插佛。Etherscan 有面向開(kāi)發(fā)者提供 API 服務(wù)以方便其檢索以太坊區(qū)塊鏈信息对竣。本文示例如何使用 Etherscan API 獲取以太坊已驗(yàn)證源碼的合約ABI愈腾。
Etherscan 大多數(shù)朋友都比較熟悉了笆怠,它是主流以太坊區(qū)塊瀏覽器蔬浙。Etherscan 有面向開(kāi)發(fā)者提供 API 服務(wù)以方便其檢索以太坊區(qū)塊鏈信息猪落。
Etherscan API 在沒(méi)有密鑰的情況下,支持每秒最多五次請(qǐng)求畴博。有更多請(qǐng)求需求可以在這里申請(qǐng)密鑰:https://etherscancom.freshdesk.com/support/solutions/articles/35000022163-i-need-an-api-key
使用 Etherscan API 獲取以太坊已驗(yàn)證源碼的合約ABI:
語(yǔ)句:
https://api.etherscan.io/api?module=contract&action=getabi&address={填入合約地址}&apikey={填入你的ApiKey}
當(dāng)然笨忌,不使用 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
提取并去除轉(zhuǎn)義后得:
[{
"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 以與合約進(jìn)行交互的簡(jiǎn)單示例:
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 思維導(dǎo)圖:
我們有一個(gè)區(qū)塊鏈知識(shí)星球,做區(qū)塊鏈前沿資料的歸納整理以方便大家檢索查詢使用俱病,也是國(guó)內(nèi)頂尖區(qū)塊鏈技術(shù)社區(qū)官疲,歡迎感興趣的朋友加入。如果你對(duì)上面內(nèi)容有疑問(wèn)亮隙,也可以加入知識(shí)星球提問(wèn)我: