TransWikia.com

getting the latest holders address:balance for a smart contract

Ethereum Asked by Lucas Rodriguez Benitez on November 21, 2020

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;

One Answer

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;

Answered by Lucas Rodriguez Benitez on November 21, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP