Which is better to listen events from smart contract, Contract.on or response.wait()?

Viewed 60

Hi I am now trying to create a simple dApp to learn Solidity and interaction with it.

I am wondering which is a better way for listening events from smart contracts and applying state changes from transactions to the front end, using on method from Contract instance or wait from TransactionResponse.

I am using ethers.js, and it gives you something like this to listen events from smart contracts:

const myContract = new Contract(address, abi, provider);
myContract.on("eventName", (...args) => { // do something here}

Also calling the transaction method from the contract instance like the above, it returns Promise<ethers.ContractTransaction> type. The returned object has wait method and it waits until the transaction has been mined like below:

myContract.someFunction(...args).then(async (response: ContractTransaction) => {
  await response.wait();
  // do something after transaction has been mined
}

**ContractTransaction is extended from TransactionResponse

Considering two ways to handle values after transactions have been mined, is one better the other? Or is there even another way better than these?

I would imagine "better" depends on how I structure components (I am using React) and other things. But I would like to know your preference at least :)

Thanks!

0 Answers
Related