[Ethereum] How to find the block where a user enacted a certain event on a contract

blockchainexchange-apitokenstransactionsweb3.py

I'm trying to build a web3.py program to take in a given user's address, and return the amount of ETH traded since they added liquidity to a Uniswap exchange. Uniswap is a smart contract on the Ethereum platform.

The Uniswap contract has a method 'AddLiquidity' that allows users to add funds, and I want to find when a given user did this.

I looked through the documentation for how to do this. I'm accessing the blockchain via an infura node, and the docs seemed to say that on infura you can't use a filter.

On the contract documentation, it seemed like I could do this via the events object, but couldnt't get this to work. Think the eventslog may be important but as I say have struggled to get any of it to do what I was hoping.

Please let me know if you need any further info to help me and thanks!

Best Answer

Smart Contract

pragma solidity 0.5.1;


contract Uniswap {

   event Triggered(address indexed user);

   function addLiquidity() external {
       emit Triggered(msg.sender);
   }

}

Javascript Event Filtering

var sha3HashOfTheEvent = web3.sha3("event Triggered(address)");
var filterOptions = {
    fromBlock: 0,
    toBlock: 'latest',
    topics:[sha3HashOfTheEvent]
}
filter.watch(function(error, result){
    if(!error){
       console.log("The event has been triggered.......")
       filter.stopWatching();
    }
});

Hope it will help you.