如何獲取交易類型(發送/接收/批准/交換)和該交易涉及的 ERC20 代幣?
我’,了解乙太坊技術(菜鳥級別)。到目前為止,通過免費的 API(etherscan.io、ethplorer.io、coingecko),我已經能夠建構一個“手錶錢包”。它看起來像這樣:
但我似乎找不到給我有關給定錢包中所有交易的更多詳細資訊的 API。API 確實給了我雜湊、塊、gas、狀態、地址(從/到)等……但我不知道這些交易中涉及的令牌(資產),我不知道採取了哪些行動地點(發送/接收/交換/批准…)
這是我從 api 獲得的原始資訊(圖 2),有人可以指導我下一步如何提取這些詳細數據嗎?
首先,您沒有查詢正確的端點。如果要獲取 ERC-20 事件,則必須執行以下查詢:
https://api.etherscan.io/api?module=account&action=tokentx&address=0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a&startblock=0&endblock=99999999&sort=asc&apikey=YourApiKeyToken
請注意,不幸的是,
input
欄位值為deprecated
. 這是您應該用來確定事件的(Trade
,Approval
,Send
…)幸運的是,我們可以呼叫另一個方法來獲取這些數據。讓我們獲取最後一筆交易的輸入數據,所以交易與雜湊
0xdceb6a76cc83802ddabf8efc0b0458dfa9150866df3ad826686e47b303259cd8
https://api.etherscan.io/api?module=proxy&action=eth_getTransactionByHash&txhash=0xdceb6a76cc83802ddabf8efc0b0458dfa9150866df3ad826686e47b303259cd8&apikey=YourApiKeyToken
然後,您可以獲得編碼的輸入數據:
0xa9059cbb000000000000000000000000ddbd2b932c763ba5b1b7ae3b362eac3e8d40121a00000000000000000000000000000000000000000000000000005af3107a4000
要對其進行解碼,您可以使用abi-decoder。您必須向 abi-decoder 提供 ERC-20 合約的 abi:
const erc20abistr = '[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]'
然後做這樣的事情:
import abiDecoder from "abi-decoder" const erc20Abi = JSON.parse(erc20abistr); let decodedData = abiDecoder.decodeMethod(input); console.log(input);
注意:不要用 etherscan api 弄髒你的手,你應該嘗試使用web3js