I am using the useDapp react Library to call a function on a Solidity using the useContractFunction.
const { state, send, events } = useContractFunction(CreateERC20Contract, 'createToken', { transactionName: 'create a new erc20 token' })
I want to listen to events created by the smart contract in the React front-end using useDapp or any other library I can integrate with useDapp. I will be grateful if anyone has an Idea on how solidity contract events can be listened to using useDapp.
UseDapp has a recent addition for this, a hook named useLogs.
I invite you to take a look and see if it works for your use case.
This will download all Transfer events from an instance of a token contract, and print out transaction hash and data for every event.
const logs = useLogs(
{
contract: token,
event: 'Transfer',
args: [],
},
{
fromBlock: 0,
toBlock: 'latest',
}
)
logs?.value?.forEach((log) => {
console.log(log.transactionHash)
console.log(log.data)
})
It's best to narrow down the amount of blocks you inspect using the fromBlock and toBlock parameters otherwise the query might get too big for the RPC provider. You can also use blockHash to target specific block.