Replies: 7 comments 16 replies
-
I've found the following way to solve this, but I'm still very interested in other/better solutions: const [provider, setProvider] = useState()
useEffect(() => {
if (isWeb3Enabled) {
updateUI()
const raffleContract = new ethers.Contract(raffleAddress, abi, provider.web3)
raffleContract.on("WinnerPicked", async () => {
console.log("Winner picked!")
updateUI()
})
}
Moralis.onWeb3Enabled((provider) => {
setProvider(provider)
})
}, [isWeb3Enabled]) |
Beta Was this translation helpful? Give feedback.
-
You can simplify it further: whenever our dependency array changes it updates the UI. useEffect(() => {
if (isWeb3Enabled) {
updateUI()
}
}, [isWeb3Enabled]) Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
@holmesec I am trying to do the same thing and was trying similar solutions but yours is slicker. I just tried it with local hardhat so far so I used this kind of config
and event triggers like it should, with one caveat and that is that actual value of winner is not updated, everything is triggered as it should be but when I logg what is happening in updateUI, value I get is still old. So
Anyway, I dont have something smart to add at this point but I am also trying to figure this out and working on it. So hoping we somehow get to bottom of it :) |
Beta Was this translation helpful? Give feedback.
-
Hallo guys, I think I have got a suitable solution, which neither requires to set the provider manually nor forces you to set up a moralis server. The following code should give you a solution. const { chainId: chainIdHex, isWeb3Enabled, web3 } = useMoralis();
useEffect(() => {
if(isWeb3Enabled){
updateUI();
listenForWinnerToBePicked();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isWeb3Enabled, numOfPlayers])
async function listenForWinnerToBePicked(){
const lottery = new ethers.Contract(lotteryAddress!, ABI, web3)
console.log("Waiting for a winner ...");
await new Promise<void>((resolve, reject) => {
lottery.once("WinnerPicked", async() => {
console.log("We got a winner!");
try {
await updateUI();
resolve();
} catch (error) {
console.log(error);
reject(error);
}
});
});
} This useEffect allows to remove the updateUI call from the handleSuccess function shown in the tutorial. I hope this solution satisfies your needs... :) if you want to see my solution, see my repo: https://github.com/Piwo96/nextjs-smartcontract-lottery.git Have a nice day and happy coding! |
Beta Was this translation helpful? Give feedback.
-
very useful |
Beta Was this translation helpful? Give feedback.
-
Hi guys, here is my solution. useEffect(() => { if (isWeb3Enabled) { updateUI(); if (contractAddress) { const contract = new ethers.Contract(contractAddress, abi, provider); contract.on("WinnerPick", () => { updateUI(); console.log("We've got a winner!"); }); } } }, [isWeb3Enabled, chainId]); I first tried using |
Beta Was this translation helpful? Give feedback.
-
Hey guys, i am not sure is it stupid solution but i did an easy way to solve it.
I only defined a new variable and added to the useEffect dependency array. It's working fine for now. If somebody sees a mistake, i am waiting to hear about it. Thanks. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there!👋
In lesson 10 Patrick challenges us to set up a listener to auto update the UI once a winner is picked from the lottery (https://www.youtube.com/watch?v=gyMwXuJrbJQ&t=65003s). I've been trying to do so, but without luck so far.
Looking at Moralis, the way to tackle this seems to be to setup a Moralis server which syncs with the contract's events and then subscribe to the server from the frontend (https://docs.moralis.io/moralis-dapp/automatic-transaction-sync/smart-contract-events). This feels a bit 'over the top' and I was wondering if anyone found a simpler way to solve this challenge. Maybe just using ethers.js?
Beta Was this translation helpful? Give feedback.
All reactions