-1

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;

I want to use event Transfer (erc20 contract) in my js file to get the transaction details for getting the transaction details of token transfer. I have passed the abi of erc20 token contract in the js code. Also transfer function is working.

For the Transfer event, I tried using:

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

in my node js code.

Anyways I could see the event log in myetherscan. Also Transfer event is there in the erc20 contract abi. But could not see the transfer event log in my console and postman.

Can anybody please help to solve this issue?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anu Surya
  • 11
  • 2

1 Answers1

0

I sorted the issue.Its because infura wont support events and filters .So I used another method using web3 to solve this issue.And finally got it solved.Thanks! Anu

Anu Surya
  • 11
  • 2