Etherscan查询区块 / 学习智能合约#7


etherscan.jpg

https://ropsten.etherscan.io/tx/0x7a319aaa29afdf997fcd7f5fc313faf362d6fef97241cd0ea016ed8aa18929cc

除了用web3去查询区块外,Etherscan也提供具体的查询区块的方法,功能更加全面!

Etherscan查询区块主要是查询合约中的事件日志,先复习下上一篇文章《区块链如何实现溯源?用日志吧》,用它就可以查询出合约中的所有操作,以实现存储功能,或是溯源的作用。

手册 |
github |
参考

cnpm install etherscan-api --save
const api = require('etherscan-api').init('YourApiKey','rinkeby', '3000')
Parameters:
apiKey (string) (optional) Your Etherscan APIkey
chain (string) (optional) Testnet chain keys [ropsten, rinkeby, kovan]
timeout (number) (optional) Timeout in milliseconds for requests, default 10000
let blockNumber = ethapi.proxy.eth_getBlockByNumber('0x10d4f') //promise
getLogs(address: any, fromBlock: string, toBlock: string, topic0: string, topic0_1_opr: string, topic1: string, topic1_2_opr: string, topic2: string, topic2_3_opr: string, topic3: string, topic0_2_opr: string)

eg:
const ethapi = require('etherscan-api').init('DYX3xxxxx','ropsten', '3000')
let logs = await _this.ethapi.log.getLogs('0x98C59036C71Ef10684b3A4a3ffA85b280fe7b402', 0, 'latest')
//results
{status: "1", message: "OK", result: Array(3)}

//另一种直接使用的方法
https://api-ropsten.etherscan.io/api?module=logs&action=getLogs&fromBlock=0&toBlock=latest&address=0xF1484DB8e5adf717E83c105cc0aD1D9668Cc076c&apikey=DYX3xxxxx

主网的Host是 api.etherscan.io
//目前支持的测试网:
api-ropsten.etherscan.io
api-kovan.etherscan.io
api-rinkeby.etherscan.io

getLogs(){
let _this = this
async function main() {
let res = await _this.axios.request({
method: 'get',
url: 'https://api-ropsten.etherscan.io/api?module=logs\&' +
'action=getLogs&fromBlock=0\&' +
'toBlock=latest\&' +
'address=0xF1484DB8e5adf717E83c105cc0aD1D9668Cc076c\&' +
'apikey=DYX36xxxxxxxx',
})
console.log(999, res)
console.log(99911, res.data.result)
}
main()
}


Comments 0