Skip to content

Commit

Permalink
fix constant network pinging (#19)
Browse files Browse the repository at this point in the history
## Why
The `useEffect` hook was constantly pinging the network while looking
for changes in `playerGuesses` which lead excessive network requests.
## How
By changing the `useEffect` to watch only for address changes and
shifting the `playerGuess` update to `handleTryGuess`, which makes more
sense.
  • Loading branch information
rccsousa authored Nov 7, 2024
1 parent e6c342a commit dccfcd7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
24 changes: 15 additions & 9 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,30 @@ function App() {
// Gets the current account's WDT balance, eligibility to play, player attempts, guesses and alphabet.
// this is constantly pinging the network, so i'm unsure if this is the right way to go
useEffect(() => {

const getParams = async (address) => {
const balance = await getWDTBalance(address)
const alphabet = await getAlphabet(address)
const playerGuesses = await getPlayerGuesses(address)
if (alphabet.length > 0) {setIsPlaying(true)}
if (balance) setWdtBalance(balance)
if (alphabet) setAlphabet(alphabet)
if (playerGuesses) setPlayerGuesses(playerGuesses)
}

if (currentAddress) {
getWDTBalance(currentAddress).then(setWdtBalance)

getAlphabet(currentAddress).then(res => {
if (res.length === 0) setIsPlaying(false)
else setIsPlaying(true)
setAlphabet(res)
})
getPlayerGuesses(currentAddress).then(setPlayerGuesses)
getParams(currentAddress)
}
}, [currentAddress, playerGuesses])

}, [currentAddress])

// handle the guess input
const handleGuessChange = (e) => setGuess(e.target.value)

// handle new guess
const handleNewGuess = (account, guess) => {
tryGuess(account, guess)
setTimeout(() => getPlayerGuesses(account).then(setPlayerGuesses), 500)
}

const handleInitAttempts = async (account) => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/hooks/WordleHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ERC20ABI from '../abis/ERC20ABI.json'
// Get number of remaining attempts a player still has
export async function getAttempts(account) {
const attempts = await wordle.read.getPlayerAttempts([account])
return attempts.toString()
return formatEther(attempts)
}

// Get the current state of the player's alphabet hitmap
Expand Down

0 comments on commit dccfcd7

Please sign in to comment.