Closed cmbothwell closed 2 years ago
With the introduction of the new useEthers hook via #11, this pattern is greatly simplified. For instance:
const [ethers, connected, address, contract, resolved] = useEthers(ContractNames.TokenFactory);
const getWalletTokens = async (contract) => {
const tokens: object[] = [];
const tokenFilter = contract.filters.TokenCreated(address, null, null);
await contract.queryFilter(tokenFilter).then(async result => {
result.forEach(event => tokens.push(
{
"owner": event.args[0],
"address": event.args[1],
"uri": event.args[2],
}
))
dispatch(setTokens(tokens))
});
}
useEffect(() => {
const setup = async () => {
if (resolved) {
await getWalletTokens(contract).then(() => console.log("Tokens successfully queried"));
const tokenFilter = contract.filters.TokenCreated(address, null, null);
contract.on(tokenFilter, () => getWalletTokens(contract));
}
}
setup().then()
}, [resolved])
I have unfortunately not been able to watch the TokenFactory events upon token creation in the front-end. The below code successfully queries the contract for events, but does not receive events upon the creation call: