首页 > 解决方案 > 关于节点 js 中触发事件的问题

问题描述

var events = require('events');

const express = require('express');
const router = express.Router();
const tokenbalance = require('./tokenbalance')

//const accCollection = require('../models/account.js');
const keythereum = require("keythereum");
const request = require("request");
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const config = require('../../config');
const log = require('log4js').getLogger("tokenadmin");
var erc20Abi = [
    {
        "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": "success",
                "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": "success",
                "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": "version",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],
        "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": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_spender",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            },
            {
                "name": "_extraData",
                "type": "bytes"
            }
        ],
        "name": "approveAndCall",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "_owner",
                "type": "address"
            },
            {
                "name": "_spender",
                "type": "address"
            }
        ],
        "name": "allowance",
        "outputs": [
            {
                "name": "remaining",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "fallback"
    },
    {
        "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": "_owner",
                "type": "address"
            },
            {
                "indexed": true,
                "name": "_spender",
                "type": "address"
            },
            {
                "indexed": false,
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "Approval",
        "type": "event"
    }
]
const Abiarray = erc20Abi;
console.log(Abiarray);
const web3 = new Web3(new Web3.providers.HttpProvider(config.rpcURL));



//event Transfer(myAddress,toAddress,amount);



let Tokenadmin ={
    tokenadmin :function(toAddress,myAddress,amount,contractAddress,callback) {
        //set from address,to address and token amount
        
        let tokena = web3.eth.contract(Abiarray).at(contractAddress);
        // console.log(tokena);
        console.log("bbbbbbbbbbbb");

         console.log(toAddress)
        console.log(myAddress)
         console.log(amount)
        console.log(contractAddress)
        amount1 = web3.toHex(amount)//To convert any given value to HEX
        console.log("Amount to transfer" +amount)
        
        tokena.balanceOf.call(myAddress, function (err, balance) {

         console.log("BALNCE B4 TRANSFER" +balance);
              
            })
            tokena.balanceOf.call(toAddress,function(err,result) {
                console.log("BALANCE B4 TRANSFER OF TOADDRESS"+result);
            })
        
    
               
        var privatekey = new Buffer('cc479124b293d2abd98ea4a57f2670ef7671c90d1eaa382258618d23440b12c2', 'hex')
       

        // //To get nonce
        // get transaction count, later will used as nonce
        web3.eth.getTransactionCount(myAddress,function(err,v) {
            console.log(v); 
            var count = v;
        
        
    
       console.log(toAddress,"aaaaaaaaaa", amount);
        var rawTransaction = {"from":myAddress, 
                              "gasPrice":web3.toHex(2 * 1e9),
                              "gasLimit":web3.toHex(210000),
                              "to": contractAddress,
                              "value":"0x0",
                              //"data" :tokena,
                              "data" : tokena.transfer.getData(toAddress,amount1),
                              "nonce":web3.toHex(count),
                              "chainid" : "0x04"
                              }
                              //console.log(rawTransaction);
        var transaction = new Tx(rawTransaction);
        transaction.sign(privatekey);
        
        let serializedTx = transaction.serialize();

        
        
    
        web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
            console.log("Response",err,hash);
            if (!err){
                console.log(hash);
                //filter.emit('Transfer','Event emitted')
                
                    }
                    
                    
            else
                console.log("HASHERROR"+err);
                return callback ({hash:hash})
                
        //})
        });
        

            let filter =web3.eth.filter('Transfer',{fromBlock:2383351,toBlock:'latest'})
            console.log("Filter" +filter);
             // watch for changes
            filter.watch(function(error, result){
            if (!error)
            console.log(result);
            });



        

        tokena.balanceOf.call(myAddress, function (err, balance) {

            console.log("BALANCE AFTER "+balance);
          
        })
        
   }) 
}

}
        module.exports =Tokenadmin;

我想在我的js文件中使用事件传输(erc20合约)来获取交易详情,以获取代币传输的交易详情。我在js代码中通过了erc20代币合约的abi。传递函数也在工作。

对于 Transfer 事件,我尝试使用:

  1. .getpastevents,
  2. .watch,
  3. .allevents,
  4. .web3.eth.filter

在我的节点 js 代码中。

无论如何,我可以看到事件登录myetherscan。erc20 合约 abi 中也有 Transfer 事件。但是在我的控制台和邮递员中看不到传输事件日志。

有人可以帮忙解决这个问题吗?

标签: javascriptdom-eventsethereumcryptocurrencyerc20

解决方案


我对问题进行了排序。这是因为 infura 不支持事件和过滤器。所以我使用了另一种使用 web3 的方法来解决这个问题。终于解决了。谢谢!阿努


推荐阅读