AnswerBun.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!

Related Questions

Peering over a direct L2 link without IP addresses

1  Asked on November 18, 2021 by moustafa-ali

     

What is the maximum size of a struct?

0  Asked on November 18, 2021 by user3316323

 

Web3js can not touch smart contract, always return 0

1  Asked on November 17, 2021 by will_z

   

Self-destributing token contract template

2  Asked on November 15, 2021 by vesa

 

error message when transfer TX

1  Asked on November 15, 2021 by mohammed-jumaa

 

What is a blockchain oracle?

1  Asked on November 15, 2021 by keenan-olsen

 

Unknown network ganache

1  Asked on November 12, 2021 by cloudtech-support

 

Where authorities in PoA are stored?

2  Asked on November 10, 2021 by maroodb

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir