From 00f632ea79e6887cba751cb1af40f6c942e1e205 Mon Sep 17 00:00:00 2001 From: Matthew Pereira Date: Wed, 29 Nov 2023 20:44:51 -0800 Subject: [PATCH] dump events for crazy verbose contract reads --- packages/hardhat/contracts/Market.sol | 26 +- packages/nextjs/components/Footer.tsx | 2 - .../vaults-of-fortune/Leaderboard.tsx | 368 ++++++++++++------ 3 files changed, 264 insertions(+), 132 deletions(-) diff --git a/packages/hardhat/contracts/Market.sol b/packages/hardhat/contracts/Market.sol index d457ded..294f910 100644 --- a/packages/hardhat/contracts/Market.sol +++ b/packages/hardhat/contracts/Market.sol @@ -357,19 +357,19 @@ contract Market is VRFConsumerBaseV2, AutomationCompatibleInterface, Ownable { } // emit event with each players updated total assets - for (uint i = 0; i < players.length; i++) { - address player = players[i]; - uint256 totalAssets = lowRiskVault.maxWithdraw(player) + - mediumRiskVault.maxWithdraw(player) + - highRiskVault.maxWithdraw(player) + - token.balanceOf(player); - emit PlayerTotalAssetUpdate( - currentContest.number, - currentRound.number, - player, - totalAssets - ); - } + // for (uint i = 0; i < players.length; i++) { + // address player = players[i]; + // uint256 totalAssets = lowRiskVault.maxWithdraw(player) + + // mediumRiskVault.maxWithdraw(player) + + // highRiskVault.maxWithdraw(player) + + // token.balanceOf(player); + // emit PlayerTotalAssetUpdate( + // currentContest.number, + // currentRound.number, + // player, + // totalAssets + // ); + // } } /** Uses random value to distribute/take tokens to/from the vaults diff --git a/packages/nextjs/components/Footer.tsx b/packages/nextjs/components/Footer.tsx index 9fff705..d789db3 100644 --- a/packages/nextjs/components/Footer.tsx +++ b/packages/nextjs/components/Footer.tsx @@ -22,8 +22,6 @@ export const Footer = () => { args: [account.address], }); - console.log("userGoldBalance", userGoldBalance); - return (
diff --git a/packages/nextjs/components/vaults-of-fortune/Leaderboard.tsx b/packages/nextjs/components/vaults-of-fortune/Leaderboard.tsx index 21ec35a..cac5fda 100644 --- a/packages/nextjs/components/vaults-of-fortune/Leaderboard.tsx +++ b/packages/nextjs/components/vaults-of-fortune/Leaderboard.tsx @@ -1,134 +1,268 @@ -// import { useEffect, useState } from "react"; import { useEffect, useState } from "react"; import { formatEther } from "viem"; +import { usePublicClient } from "wagmi"; import { Address } from "~~/components/scaffold-eth"; -import { useScaffoldContractRead, useScaffoldEventHistory, useScaffoldEventSubscriber } from "~~/hooks/scaffold-eth"; - -interface IPlayerScores { - contestNumber: number | undefined; - roundNumber: number | undefined; - player: string | undefined; - totalAssets: number; - blockNumber: bigint; +import { + useDeployedContractInfo, // useScaffoldContractRead, + // useScaffoldEventHistory, + // useScaffoldEventSubscriber, +} from "~~/hooks/scaffold-eth"; + +// interface IPlayerScores { +// contestNumber: number | undefined; +// roundNumber: number | undefined; +// player: string | undefined; +// totalAssets: number; +// blockNumber: bigint; +// } + +interface IPlayersAssets { + player: string; + totalAssets: bigint; } -export const Leaderboard = () => { - const [playersScores, setPlayersScores] = useState([]); - - const { data: currentContestNumber } = useScaffoldContractRead({ - contractName: "Market", - functionName: "getCurrentContestNumber", - }); - - const { data: currentRoundNumber } = useScaffoldContractRead({ - contractName: "Market", - functionName: "getCurrentRoundNumber", - }); - - const { data: events, isLoading: isLoadingEvents } = useScaffoldEventHistory({ - contractName: "Market", - eventName: "PlayerTotalAssetUpdate", - fromBlock: 0n, - // Apply filters to the event based on parameter names and values { [parameterName]: value }, - filters: { - contestNumber: currentContestNumber, - roundNumber: currentRoundNumber, - }, - }); - - useScaffoldEventSubscriber({ - contractName: "Market", - eventName: "ContestOpened", - - listener: logs => { - logs.forEach(log => { - console.log("ContestOpened", log); - setPlayersScores([]); - }); - }, - }); - - useScaffoldEventSubscriber({ - contractName: "Market", - eventName: "PlayerTotalAssetUpdate", - - listener: logs => { - logs.forEach(log => { - const { contestNumber, roundNumber, player, totalAssets } = log.args; - const { blockNumber } = log; - - setPlayersScores(prevScores => { - // Find index of existing entry for the same player - const existingIndex = prevScores.findIndex(score => score.player === player); - - // Prepare the new score entry - const newScore = { - contestNumber: Number(contestNumber), - roundNumber: Number(roundNumber), - player: player, - totalAssets: Number(formatEther(totalAssets || 0n)), - blockNumber: blockNumber, - }; - - // If the player already exists in the array - if (existingIndex !== -1) { - // Compare block numbers and update only if the new event is more recent - if (prevScores[existingIndex].blockNumber < blockNumber) { - // Replace the existing entry with the new one - const updatedScores = [...prevScores]; - updatedScores[existingIndex] = newScore; - updatedScores.sort((a, b) => b.totalAssets - a.totalAssets); - return updatedScores; - } else { - // Keep the array as it is if the existing event is more recent - return prevScores; - } - } else { - // Add the new score if the player is not already in the array - return [...prevScores, newScore].sort((a, b) => b.totalAssets - a.totalAssets); - } - }); - }); - }, - }); +const maxWithdrawABI = [ + { + inputs: [ + { + internalType: "address", + name: "owner", + type: "address", + }, + ], + name: "maxWithdraw", + outputs: [ + { + internalType: "uint256", + name: "", + type: "uint256", + }, + ], + stateMutability: "view", + type: "function", + }, +]; - console.log("playersScores", playersScores); +const balanceOfABI = [ + { + inputs: [ + { + internalType: "address", + name: "account", + type: "address", + }, + ], + name: "balanceOf", + outputs: [ + { + internalType: "uint256", + name: "", + type: "uint256", + }, + ], + stateMutability: "view", + type: "function", + }, +]; + +export const Leaderboard = () => { + const [playersAssets, setPlayersAssets] = useState([]); + const publicClient = usePublicClient(); + const { data: MarketContract } = useDeployedContractInfo("Market"); useEffect(() => { - if (!playersScores?.length && !!events?.length && !isLoadingEvents) { - const rawScores: IPlayerScores[] = events.map(event => { - const { args, log } = event; - console.log("event", event); - return { - contestNumber: Number(args.contestNumber), - roundNumber: Number(args.roundNumber), - player: args.player, - totalAssets: Number(formatEther(args.totalAssets || 0n)), - blockNumber: log.blockNumber, - }; + async function getPlayers() { + const players = await publicClient.readContract({ + address: MarketContract?.address || "0x537cc9F7dd8ec7B0423021d61668De0DDA9079ed", + abi: [ + { + inputs: [], + name: "getPlayers", + outputs: [ + { + internalType: "address[]", + name: "", + type: "address[]", + }, + ], + stateMutability: "view", + type: "function", + }, + ], + functionName: "getPlayers", }); - const uniquePlayerScores = rawScores.reduce>((acc, item) => { - // Check if this player already exists in the accumulator + const scores = players.map(async player => { + const lowRiskAssets = (await publicClient.readContract({ + address: "0x81f731219A738DEB8Eb3cF5496E99d5a5cbe53D5", + abi: maxWithdrawABI, + functionName: "maxWithdraw", + args: [player], + })) as bigint; - if (item.player) { - if (!acc[item.player] || acc[item.player].blockNumber < item.blockNumber) { - // If not present, or if the current item's blockNumber is greater, store it - acc[item.player] = item; - } - } + const mediumRiskAssets = (await publicClient.readContract({ + address: "0x6FEbe74C84219a6560683e4c89ed8AdeCe5F3301", + abi: maxWithdrawABI, + functionName: "maxWithdraw", + args: [player], + })) as bigint; + + const highRiskAssets = (await publicClient.readContract({ + address: "0x359d9fD1a6d8d3c4483eFf5323e49450CbcB5ff7", + abi: maxWithdrawABI, + functionName: "maxWithdraw", + args: [player], + })) as bigint; - return acc; - }, {}); + const goldBalance = (await publicClient.readContract({ + address: "0x1A9b869fdB9Ecc7Ae1998447c0EBf19E260C8b7d", + abi: balanceOfABI, + functionName: "balanceOf", + args: [player], + })) as bigint; - const playerScores = Object.values(uniquePlayerScores); + const totalAssets = lowRiskAssets + mediumRiskAssets + highRiskAssets + goldBalance; - const sortedScores = playerScores.sort((a, b) => { - return b.totalAssets - a.totalAssets; + return { player, totalAssets }; }); - setPlayersScores(sortedScores); + + const playerScores = await Promise.all(scores); + + playerScores.sort((a, b) => { + if (a.totalAssets < b.totalAssets) { + return 1; + } + if (a.totalAssets > b.totalAssets) { + return -1; + } + return 0; + }); + setPlayersAssets(playerScores); + console.log("playerScores", formatEther(playerScores[0].totalAssets)); } - }, [playersScores.length, events, isLoadingEvents]); + + getPlayers(); + }, [publicClient, MarketContract]); + + console.log("playersAssets", playersAssets); + + /////////////////////////////////////////////////////////////////////// + // const [playersScores, setPlayersScores] = useState([]); + + // const { data: currentContestNumber } = useScaffoldContractRead({ + // contractName: "Market", + // functionName: "getCurrentContestNumber", + // }); + + // const { data: currentRoundNumber } = useScaffoldContractRead({ + // contractName: "Market", + // functionName: "getCurrentRoundNumber", + // }); + + // const { data: events, isLoading: isLoadingEvents } = useScaffoldEventHistory({ + // contractName: "Market", + // eventName: "PlayerTotalAssetUpdate", + // fromBlock: 0n, + // // Apply filters to the event based on parameter names and values { [parameterName]: value }, + // filters: { + // contestNumber: currentContestNumber, + // roundNumber: currentRoundNumber, + // }, + // }); + + // useScaffoldEventSubscriber({ + // contractName: "Market", + // eventName: "ContestOpened", + + // listener: logs => { + // logs.forEach(log => { + // console.log("ContestOpened", log); + // setPlayersScores([]); + // }); + // }, + // }); + + // useScaffoldEventSubscriber({ + // contractName: "Market", + // eventName: "PlayerTotalAssetUpdate", + + // listener: logs => { + // logs.forEach(log => { + // const { contestNumber, roundNumber, player, totalAssets } = log.args; + // const { blockNumber } = log; + + // setPlayersScores(prevScores => { + // // Find index of existing entry for the same player + // const existingIndex = prevScores.findIndex(score => score.player === player); + + // // Prepare the new score entry + // const newScore = { + // contestNumber: Number(contestNumber), + // roundNumber: Number(roundNumber), + // player: player, + // totalAssets: Number(formatEther(totalAssets || 0n)), + // blockNumber: blockNumber, + // }; + + // // If the player already exists in the array + // if (existingIndex !== -1) { + // // Compare block numbers and update only if the new event is more recent + // if (prevScores[existingIndex].blockNumber < blockNumber) { + // // Replace the existing entry with the new one + // const updatedScores = [...prevScores]; + // updatedScores[existingIndex] = newScore; + // updatedScores.sort((a, b) => b.totalAssets - a.totalAssets); + // return updatedScores; + // } else { + // // Keep the array as it is if the existing event is more recent + // return prevScores; + // } + // } else { + // // Add the new score if the player is not already in the array + // return [...prevScores, newScore].sort((a, b) => b.totalAssets - a.totalAssets); + // } + // }); + // }); + // }, + // }); + + // // console.log("playersScores", playersScores); + + // useEffect(() => { + // if (!playersScores?.length && !!events?.length && !isLoadingEvents) { + // const rawScores: IPlayerScores[] = events.map(event => { + // const { args, log } = event; + // // console.log("event", event); + // return { + // contestNumber: Number(args.contestNumber), + // roundNumber: Number(args.roundNumber), + // player: args.player, + // totalAssets: Number(formatEther(args.totalAssets || 0n)), + // blockNumber: log.blockNumber, + // }; + // }); + + // const uniquePlayerScores = rawScores.reduce>((acc, item) => { + // // Check if this player already exists in the accumulator + + // if (item.player) { + // if (!acc[item.player] || acc[item.player].blockNumber < item.blockNumber) { + // // If not present, or if the current item's blockNumber is greater, store it + // acc[item.player] = item; + // } + // } + + // return acc; + // }, {}); + + // const playerScores = Object.values(uniquePlayerScores); + + // const sortedScores = playerScores.sort((a, b) => { + // return b.totalAssets - a.totalAssets; + // }); + // setPlayersScores(sortedScores); + // } + // }, [playersScores.length, events, isLoadingEvents]); return (
@@ -143,14 +277,14 @@ export const Leaderboard = () => { - {playersScores.map((score, idx) => { + {playersAssets.map((score, idx) => { return ( {idx + 1}
- {score.totalAssets?.toFixed(1)} + {Number(formatEther(score.totalAssets)).toFixed(0)} ); })}