From 27b6eca1ee15142d7179260df828c49fa8bc11a5 Mon Sep 17 00:00:00 2001 From: Lulox Date: Tue, 4 Jul 2023 12:43:34 -0300 Subject: [PATCH] Made code cleaner, minor bugs remain --- packages/nextjs/pages/index.tsx | 86 ++++++------------- .../nextjs/pages/sportsbook/ChallengeCard.tsx | 51 ++++++----- 2 files changed, 56 insertions(+), 81 deletions(-) diff --git a/packages/nextjs/pages/index.tsx b/packages/nextjs/pages/index.tsx index 80962bc..ca8b4ab 100644 --- a/packages/nextjs/pages/index.tsx +++ b/packages/nextjs/pages/index.tsx @@ -134,7 +134,7 @@ const Home: NextPage = () => { challengeId: event.args[0].toString(), proposingTeam: event.args[1], newReferee: event.args[2], - })); + })) as UpdateRefereeRequestProps[]; setUpdateRefereeRequestHistory(mappedHistory); } }, [UpdateRefereeRequestHistory]); @@ -145,7 +145,7 @@ const Home: NextPage = () => { challengeId: event.args[0].toString(), newReferee: event.args[1], updateAccepted: event.args[2], - })); + })) as UpdateRefereeResponseProps[]; setUpdateRefereeResponseHistory(mappedHistory); } }, [UpdateRefereeResponseHistory]); @@ -155,21 +155,15 @@ const Home: NextPage = () => { contractName: "Sportsbook", eventName: "ChallengeCreated", listener: (challengeId, team1, team2, referee, bet) => { - setChallengeHistory(previousHistory => { - const newChallengeId = parseInt(challengeId.toString()); - if (previousHistory.some(challenge => challenge.challengeId === newChallengeId)) { - return previousHistory; - } - - const newChallenge: ChallengeCreatedProps = { - challengeId: newChallengeId, + setChallengeHistory(indexedHistory => { + const newChallengeCreated: ChallengeCreatedProps = { + challengeId: parseInt(challengeId.toString()), team1, team2, referee, bet: parseInt(bet.toString()), }; - - return [newChallenge, ...previousHistory]; + return [newChallengeCreated, ...indexedHistory]; }); }, }); @@ -178,19 +172,13 @@ const Home: NextPage = () => { contractName: "Sportsbook", eventName: "ChallengeAccepted", listener: (challengeId, team1, team2) => { - setChallengeAcceptedHistory(previousHistory => { - const newChallengeId = parseInt(challengeId.toString()); - if (previousHistory.some(challenge => challenge.challengeId === newChallengeId)) { - return previousHistory; - } - - const newChallenge: ChallengeAcceptedProps = { - challengeId: newChallengeId, + setChallengeAcceptedHistory(indexedHistory => { + const newChallengeAccepted: ChallengeAcceptedProps = { + challengeId: parseInt(challengeId.toString()), team1, team2, }; - - return [newChallenge, ...previousHistory]; + return [newChallengeAccepted, ...indexedHistory]; }); }, }); @@ -199,20 +187,14 @@ const Home: NextPage = () => { contractName: "Sportsbook", eventName: "ChallengeStarted", listener: (challengeId, referee, team1, team2) => { - setChallengeStartedHistory(previousHistory => { - const newChallengeId = parseInt(challengeId.toString()); - if (previousHistory.some(challenge => challenge.challengeId === newChallengeId)) { - return previousHistory; - } - - const newChallenge: ChallengeStartedProps = { - challengeId: newChallengeId, + setChallengeStartedHistory(indexedHistory => { + const newChallengeStarted: ChallengeStartedProps = { + challengeId: parseInt(challengeId.toString()), team1, team2, referee, }; - - return [newChallenge, ...previousHistory]; + return [newChallengeStarted, ...indexedHistory]; }); }, }); @@ -221,21 +203,15 @@ const Home: NextPage = () => { contractName: "Sportsbook", eventName: "ChallengeResult", listener: (challengeId, team1, team2, team1Result, team2Result) => { - setChallengeResultHistory(previousHistory => { - const newChallengeId = parseInt(challengeId.toString()); - if (previousHistory.some(challenge => challenge.challengeId === newChallengeId)) { - return previousHistory; - } - - const newChallenge: ChallengeResultProps = { - challengeId: newChallengeId, + setChallengeResultHistory(indexedHistory => { + const newChallengeResult: ChallengeResultProps = { + challengeId: parseInt(challengeId.toString()), team1, team2, team1Result, team2Result, }; - - return [newChallenge, ...previousHistory]; + return [newChallengeResult, ...indexedHistory]; }); }, }); @@ -244,18 +220,12 @@ const Home: NextPage = () => { contractName: "Sportsbook", eventName: "ChallengeCanceled", listener: (challengeId, canceledBy) => { - setChallengeCanceledHistory(previousHistory => { - const newChallengeId = parseInt(challengeId.toString()); - if (previousHistory.some(challenge => challenge.challengeId === newChallengeId)) { - return previousHistory; - } - - const newChallenge: ChallengeCanceledProps = { - challengeId: newChallengeId, + setChallengeCanceledHistory(indexedHistory => { + const newChallengeCanceled: ChallengeCanceledProps = { + challengeId: parseInt(challengeId.toString()), canceledBy: canceledBy, }; - - return [newChallenge, ...previousHistory]; + return [newChallengeCanceled, ...indexedHistory]; }); }, }); @@ -264,13 +234,13 @@ const Home: NextPage = () => { contractName: "Sportsbook", eventName: "UpdateRefereeRequest", listener: (challengeId, proposingTeam, newReferee) => { - setUpdateRefereeRequestHistory(previousHistory => { - const newChallenge: UpdateRefereeRequestProps = { + setUpdateRefereeRequestHistory(indexedHistory => { + const newUpdateRefereeRequest: UpdateRefereeRequestProps = { challengeId: parseInt(challengeId.toString()), proposingTeam, newReferee, }; - return [newChallenge, ...previousHistory]; + return [newUpdateRefereeRequest, ...indexedHistory]; }); }, }); @@ -279,13 +249,13 @@ const Home: NextPage = () => { contractName: "Sportsbook", eventName: "UpdateRefereeResponse", listener: (challengeId, newReferee, updateAccepted) => { - setUpdateRefereeResponseHistory(previousHistory => { - const newChallenge: UpdateRefereeResponseProps = { + setUpdateRefereeResponseHistory(indexedHistory => { + const newUpdateRefereeResponse: UpdateRefereeResponseProps = { challengeId: parseInt(challengeId.toString()), newReferee, updateAccepted, }; - return [newChallenge, ...previousHistory]; + return [newUpdateRefereeResponse, ...indexedHistory]; }); }, }); diff --git a/packages/nextjs/pages/sportsbook/ChallengeCard.tsx b/packages/nextjs/pages/sportsbook/ChallengeCard.tsx index 77a107d..682c78c 100644 --- a/packages/nextjs/pages/sportsbook/ChallengeCard.tsx +++ b/packages/nextjs/pages/sportsbook/ChallengeCard.tsx @@ -17,39 +17,38 @@ const ChallengeCard = ({ updateRefereeRequest, updateRefereeAccepted, }: ChallengeCardProps) => { + const [refereeAddress, setRefereeAddress] = useState(challenge.referee); const [updateRefereeAddress, setUpdateRefereeAddress] = useState(""); + const [updateRefereeResponse, setUpdateRefereeResponse] = useState(undefined); const [completeChallengeTeam1Score, setCompleteChallengeTeam1Score] = useState(""); const [completeChallengeTeam2Score, setCompleteChallengeTeam2Score] = useState(""); - const [refereeAddress, setRefereeAddress] = useState(challenge.referee); - - const [challengeIdArg, setChallengeIdArg] = useState(challenge.challengeId.toString()); const { address } = useAccount(); const { writeAsync: acceptChallenge } = useScaffoldContractWrite({ contractName: "Sportsbook", functionName: "acceptChallenge", - args: [challengeIdArg ? BigNumber.from(challengeIdArg) : BigNumber.from(0)], + args: [challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : BigNumber.from(0)], value: challenge.bet ? ethers.utils.formatEther(challenge.bet.toString()) : undefined, }); const { writeAsync: deleteChallenge } = useScaffoldContractWrite({ contractName: "Sportsbook", functionName: "deleteChallenge", - args: [challengeIdArg ? BigNumber.from(challengeIdArg) : BigNumber.from(0)], + args: [challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : BigNumber.from(0)], }); const { writeAsync: startChallenge } = useScaffoldContractWrite({ contractName: "Sportsbook", functionName: "startChallenge", - args: [challengeIdArg ? BigNumber.from(challengeIdArg) : undefined], + args: [challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : undefined], }); const { writeAsync: completeChallenge } = useScaffoldContractWrite({ contractName: "Sportsbook", functionName: "completeChallenge", args: [ - challengeIdArg ? BigNumber.from(challengeIdArg) : undefined, + challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : undefined, completeChallengeTeam1Score ? parseInt(completeChallengeTeam1Score) : undefined, completeChallengeTeam2Score ? parseInt(completeChallengeTeam2Score) : undefined, ], @@ -58,19 +57,19 @@ const ChallengeCard = ({ const { writeAsync: updateReferee } = useScaffoldContractWrite({ contractName: "Sportsbook", functionName: "updateReferee", - args: [challengeIdArg ? BigNumber.from(challengeIdArg) : undefined, updateRefereeAddress], - }); - - const { writeAsync: answerNoToUpdateReferee } = useScaffoldContractWrite({ - contractName: "Sportsbook", - functionName: "answerUpdateReferee", - args: [challengeIdArg ? BigNumber.from(challengeIdArg) : undefined, false], + args: [ + challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : undefined, + updateRefereeAddress, + ], }); - const { writeAsync: answerYesToUpdateReferee } = useScaffoldContractWrite({ + const { writeAsync: answerToUpdateReferee } = useScaffoldContractWrite({ contractName: "Sportsbook", functionName: "answerUpdateReferee", - args: [challengeIdArg ? BigNumber.from(challengeIdArg) : undefined, true], + args: [ + challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : undefined, + updateRefereeResponse, + ], }); useEffect(() => { @@ -79,11 +78,7 @@ const ChallengeCard = ({ } else { setRefereeAddress(challenge.referee); } - }, [updateRefereeAccepted, challenge.referee]); - - if (challenge && challenge.challengeId && challengeIdArg != challenge.challengeId.toString()) { - setChallengeIdArg(challenge.challengeId.toString()); - } + }, [updateRefereeAccepted]); return (
@@ -294,13 +289,23 @@ const ChallengeCard = ({ {updateRefereeRequest?.proposingTeam != address && ( -