【发布时间】:2019-02-12 19:41:14
【问题描述】:
EtherScan 提供了一个用于交易详情的 API,它是 Geth/Parity 代理 API 的一部分,名称为 eth_getTransactionByHash,但我无法获取信息,哪些 ERC20 代币被转移以及有多少。
【问题讨论】:
标签: php ethereum web3js erc20 etherscan
EtherScan 提供了一个用于交易详情的 API,它是 Geth/Parity 代理 API 的一部分,名称为 eth_getTransactionByHash,但我无法获取信息,哪些 ERC20 代币被转移以及有多少。
【问题讨论】:
标签: php ethereum web3js erc20 etherscan
您使用了错误的 API。
要获取 ERC20 转账信息,您需要交易收据,因为转账信息记录在转账事件日志中。你应该使用eth_getTransactionReceipt。
这会给你一个这样的回应,this tx:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"blockHash": "0xc5e5a515898983d1370d40b03fc05ae08be861af746a1577796153a149a1bb20",
"blockNumber": "0x5ff5dd",
"contractAddress": null,
"cumulativeGasUsed": "0xe85fb",
"from": "0xd7afd4441fccc118b9207b0e136f4ef9319b3c79",
"gasUsed": "0x9034",
"logs": [
{
"address": "0x0d8775f648430679a709e98d2b0cb6250d2887ef",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x000000000000000000000000d7afd4441fccc118b9207b0e136f4ef9319b3c79",
"0x00000000000000000000000069d9e9aff57ec73582ad1ce441726dba7ea78fe0"
],
"data": "0x0000000000000000000000000000000000000000000001054aefee8ba6d00000",
"blockNumber": "0x5ff5dd",
"transactionHash": "0x3265c1461d3f167c756fbc062ae3a2dc279b44a9c3ca2194271d4251cd0c1655",
"transactionIndex": "0x1b",
"blockHash": "0xc5e5a515898983d1370d40b03fc05ae08be861af746a1577796153a149a1bb20",
"logIndex": "0xa",
"removed": false
}
],
"logsBloom": "0x04000000002000000200000000000000002000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000008000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000200010000000000000000000000000000000000000000000000100000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": "0x1",
"to": "0x0d8775f648430679a709e98d2b0cb6250d2887ef",
"transactionHash": "0x3265c1461d3f167c756fbc062ae3a2dc279b44a9c3ca2194271d4251cd0c1655",
"transactionIndex": "0x1b"
}
}
其中,这个日志部分很重要。
ERC20 传输日志的格式为Transfer(address from, address to, uint256 value)。当您为Transfer(address,address,uint256) 获取keccak 哈希时,您将获得主题的0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef,如上面的响应所示。
该日志中接下来的两个值分别是from 和to 地址,对于 ETH 是正常打包的(零填充直到 32 个字节)。最后,日志中的data 是转移的 ERC20 代币的值(本例中为 BAT)。
发出日志的address,在本例中为0x0d8775f648430679a709e98d2b0cb6250d2887ef,是代币合约。然后,您可以使用eth_call API 从该合约中读取代币符号、名称和小数,以读取代币信息。
【讨论】: