有关在节点js中触发事件的问题

时间:2018-06-01 06:28:18

标签: javascript-events ethereum cryptocurrency erc20

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。此外,传输功能正常工作。 对于转移事件,我尝试使用1).getpastevents, 2).watch, 3).allevents 4).web3.eth.filter在我的节点js代码中。 无论如何,我可以在myetherscan中看到事件日志。 还有erc20合同abi中的转移事件。 但无法在我的控制台和邮递员中看到转移事件日志。

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

1 个答案:

答案 0 :(得分:0)

我对问题进行了排序。因为infura不支持事件和过滤器。所以我使用另一种方法使用web3来解决这个问题。最后得到了解决。谢谢! ANU