Web3js
如何通過 web3.js (0.20.6) 獲取令牌交易歷史
設置
node.js + web3.js (0.20.6 版本) + 並使用 infura 作為 httpProvider
我已經弄清楚並使用 web3.js 成功發送了 erc20 令牌,(經過大量Google搜尋……在這裡完成 noobie……)
我現在正試圖弄清楚如何獲取特定地址的令牌交易歷史。
我知道我需要觀看事件,對於令牌交易,這將是“轉移”事件。
所以我正在關注“契約事件”的 Javascript-API https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-events
下面是我正在執行的程式碼(或至少試圖)
var Web3 = require('web3'); var web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/MYINFURATOKEN')); var myaddress = "0x482d42bc7a5dd62f3fae88c3b4cde29a333cf913"; var contractAddress = "0x31a86307bfe81780a47fe98c192004ccdce8fcc0"; var abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"enableTokenTransfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"TransferAllowed","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"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":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"frozenAccount","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"disableTokenTransfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"target","type":"address"},{"name":"freeze","type":"bool"}],"name":"freezeAccount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"},{"name":"tokenName","type":"string"},{"name":"tokenSymbol","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"target","type":"address"},{"indexed":false,"name":"frozen","type":"bool"}],"name":"FrozenFunds","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"","type":"bool"}],"name":"TransferEnabled","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"","type":"bool"}],"name":"TransferDisabled","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"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"}]; var myContractInstance = web3.eth.contract(abi).at(contractAddress); var event = myContractInstance.Transfer({_from: myaddress}, {fromBlock:0, toBlock: 'latest'}); event.watch(function(error, result){ if (!error) console.log(result); });
然後返回
Filter { requestManager: RequestManager { provider: HttpProvider { host: 'https://rinkeby.infura.io/MYINFURATOKEN', timeout: 0, user: undefined, password: undefined, headers: undefined }, polls: {}, timeout: null }, options: { topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', null, null ], from: undefined, to: undefined, address: '0x31a86307bfe81780a47fe98c192004ccdce8fcc0', fromBlock: '0x0', toBlock: 'latest' }, implementation: { newFilter: { [Function: send] request: [Function: bound ], call: [Function: newFilterCall] }, uninstallFilter: { [Function: send] request: [Function: bound ], call: 'eth_uninstallFilter' }, getLogs: { [Function: send] request: [Function: bound ], call: 'eth_getFilterLogs' }, poll: { [Function: send] request: [Function: bound ], call: 'eth_getFilterChanges' } }, filterId: null, callbacks: [ [Function] ], getLogsCallbacks: [], pollFilters: [], formatter: [Function: bound ] }
我很確定這不是令牌交易歷史……還是我錯過了一個步驟?
我怎麼從這裡走?
先感謝您。賈斯汀。
你想要的是我相信的*過去事件。*
我在這里和你做同樣的事情。這確實向你展示了代幣交易歷史,問題是,它不會向你展示特定地址的代幣交易歷史。