0

I managed to get all the transfer events from any given smart contract, but now I need to reduce that to current holder any idea how I would do this?

Here is my code so far:

import ERC20ABI from './blockchain/ERC20ABI.js';
import ethweb3 from './blockchain/ethweb3.js';
import _ from 'lodash';


var addresses = [];
var choppedAdrresses =[];
export function parseAddress(_smartcontract){

     var contractObj = new ethweb3.eth.Contract(ERC20ABI,_smartcontract);
       contractObj.getPastEvents(
           'Transfer' || 'allEvents',
           {
               fromBlock: 0,
               toBlock: 'latest'
           },
           function(err,res){
               console.log(err,res.length);

               for(var i = 0; i < res.length; i++) {
                 addresses.push([res[i].returnValues.from,res[i].returnValues.value]);
               }

               choppedAdrresses=_.chunk(addresses, 100);
               //just to shw how to access data
               console.log(choppedAdrresses);
               console.log(addresses.length)
               console.log(addresses[0])

           }
         );
         return
   }
export default choppedAdrresses;
0

Well I managed to solve this in a semi elegant way.

I retrieved all of the transactions added them into individual array and then created an object with keys: address value.

import ERC20ABI from './blockchain/ERC20ABI.js';
import ethweb3 from './blockchain/ethweb3.js';
import _ from 'lodash';


var addressesValues = [];
var uniqueAddresses = [];
var values = [];
var choppedAdrresses =[];
var map = {};

export function parseAddress(_smartcontract){

     var contractObj = new ethweb3.eth.Contract(ERC20ABI,_smartcontract);
       contractObj.getPastEvents(
           'Transfer' || 'allEvents',
           {
               fromBlock: 0,
               toBlock: 'latest'
           },
           function(err,res){
               //console.log(err,res.length);
                for(var i = 0; i< res.length;i++){
                     if (!uniqueAddresses.includes(res[i].returnValues.from)){
                       uniqueAddresses[i] = res[i].returnValues.from;
                       values[i] = parseInt(0);
                       map[uniqueAddresses[i]] = values[i];
                     }
                     if (!uniqueAddresses.includes(res[i].returnValues.to)){
                       uniqueAddresses[i] = res[i].returnValues.to;
                       values[i] = parseInt(0);
                       map[uniqueAddresses[i]] = values[i];
                     }
                }

                for(var j = 0; j< res.length; j++){
                    map[res[j].returnValues.from] -= parseInt(res[j].returnValues.value);
                    console.log("parseInt(res[j].returnValues.value) returns the value:" + map[res[j].returnValues.from])
                    map[res[j].returnValues.to] += parseInt(res[j].returnValues.value);
              }

           }
         );
   }
   export default choppedAdrresses;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.