0

just started learning this and trying to get all addresses that have been interacted with a contract, I am planning to pull out all events, and then get the addresses from there? any advice if this will work? thanks

async function getUsers() 
    {
        var allTransactionsHash = myContract.events.allEvents.transactionHash();
        var curBlock = await web3.eth.getBlockNumber();
        var allUsers = []; 
    
        while (curBlock !== GENESIS)
        {
          var txns = web3.eth.getTransaction (allTransactionsHash);
            for (var j = 0; j < txns.length; j++) 
            {
            var txn = txns[j];
            allUsers.push(txn.from.toLowerCase(), txn.to.toLowerCase());
            }
        }
        return allUsers;
    }
1
  • I think you are confusing block with transaction. You could iterate a block's transactions but you cannot iterate a transaction. A few thing you may want to consider: 1/ a contract doesn't need to emit an event, 2/ a contract could execute another contract with an "internal transactin" that you couldn't retrieve with web3.eth.getTransaction().
    – Ismael
    Commented Oct 15, 2022 at 21:13

1 Answer 1

0

The best solution would be to add events that should be emitted only for the specific functions in the contract, which you would want to track. You should always keep I'm mind that the code must be as simple and as short as possible in order to be gas-efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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