0

For context, I'm trying to display the balance of a staking pool on my UI. Specifically, I want the balance to be updated every time there is a successful deposit made. I'm not sure how I actually go about creating the filter for the ethers js event filter.

This is what my smart contract event looks like:

  event DepositMade(address indexed capitalProvider, uint256 amount, uint256 indexed blockNumber);

This is what my react js looks like:

const checkEvents = async() => {
  const provider = new ethers.providers.Web3Provider(window.ethereum)
  const startBlockNumber = await provider.getBlockNumber();
  let juniorPool = new ethers.Contract(juniorPoolAddress, JuniorPool.abi, provider)


  juniorPool.on("DepositMade",  (capitalProvider, amount, blockNumber)=>{

    console.log(usdcBalance);
    console.log(parseInt(amount));
    setusdcBalance(parseInt(`${usdcBalance}`) + parseInt(amount));
  })
}

I've been told that I use contract.filters.EVENT_NAME( ...args ) ⇒ Filter to filter through events, but I'm still confused on how to manipulate the actual filter to exhibit the behavior I want.

1 Answer 1

0

you should use a web socket provider

here you can see how to use Alchemy

const provider = new ethers.providers.AlchemyProvider.getWebSocketProvider(
"goerli",
 alchemyApiKey
 );

another option:

new ethers.providers.WebSocketProvider( [ url [ , network ] ] )

example:

var const provider = new ethers.providers.WebSocketProvider("wss://goerli.infura.io/ws/v3/idhere", "goerli");

and then you can use your provider:

const contract = new ethers.Contract(juniorPoolAddress, JuniorPool.abi, provider);

contract.on("DepositMade", (capitalProvider, amount, blockNumber) => {
   //your code here
});

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.