Skip to content

Commit

Permalink
Merge pull request #75 from lets-fucking-game/dev
Browse files Browse the repository at this point in the history
Feat/ Dapp claim buttons
  • Loading branch information
0xGeegZ committed Nov 28, 2022
2 parents 68800ba + f79e794 commit 7c0006f
Show file tree
Hide file tree
Showing 29 changed files with 3,487 additions and 6,681 deletions.
9,580 changes: 3,087 additions & 6,493 deletions packages/dapp/src/config/internal/internal.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/dapp/src/config/types/typechain/contracts/GameV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export declare namespace GameV1Interface {

export type GameDataStruct = {
id: PromiseOrValue<BigNumberish>;
versionId: PromiseOrValue<BigNumberish>;
roundId: PromiseOrValue<BigNumberish>;
name: PromiseOrValue<BytesLike>;
playerAddressesCount: PromiseOrValue<BigNumberish>;
Expand All @@ -70,6 +71,7 @@ export declare namespace GameV1Interface {
};

export type GameDataStructOutput = [
BigNumber,
BigNumber,
BigNumber,
string,
Expand All @@ -87,6 +89,7 @@ export declare namespace GameV1Interface {
string
] & {
id: BigNumber;
versionId: BigNumber;
roundId: BigNumber;
name: string;
playerAddressesCount: BigNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export declare namespace GameV1Interface {

export type GameDataStruct = {
id: PromiseOrValue<BigNumberish>;
versionId: PromiseOrValue<BigNumberish>;
roundId: PromiseOrValue<BigNumberish>;
name: PromiseOrValue<BytesLike>;
playerAddressesCount: PromiseOrValue<BigNumberish>;
Expand All @@ -70,6 +71,7 @@ export declare namespace GameV1Interface {
};

export type GameDataStructOutput = [
BigNumber,
BigNumber,
BigNumber,
string,
Expand All @@ -87,6 +89,7 @@ export declare namespace GameV1Interface {
string
] & {
id: BigNumber;
versionId: BigNumber;
roundId: BigNumber;
name: string;
playerAddressesCount: BigNumber;
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,11 @@ const _abi = [
name: "id",
type: "uint256",
},
{
internalType: "uint256",
name: "versionId",
type: "uint256",
},
{
internalType: "uint256",
name: "roundId",
Expand Down
45 changes: 43 additions & 2 deletions packages/dapp/src/state/games/fetchGameData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,35 @@ export const fetchPublicGamesData = async (
return chunk(gameMultiCallResult, chunkSize)
}

export const fetchGamesRemainingPlayersCount = async (
export const fetchGamesCreatorAmounts = async (
games: GameFactory.GameStructOutput[],
chainId = ChainId.BSC,
): Promise<any[]> => {
const gameCalls = games.map((game) => {
return {
address: game.deployedAddress,
name: 'getRemainingPlayersCount',
name: 'creatorAmount',
}
})
const chunkSize = gameCalls.length / games.length

const gameMultiCallResult = await multicallv2({
abi: internal[chainId || ChainId.BSC].GameV1.abi,
calls: gameCalls,
chainId,
})

return chunk(gameMultiCallResult, chunkSize)
}

export const fetchGamesTreasuryAmounts = async (
games: GameFactory.GameStructOutput[],
chainId = ChainId.BSC,
): Promise<any[]> => {
const gameCalls = games.map((game) => {
return {
address: game.deployedAddress,
name: 'treasuryAmount',
}
})
const chunkSize = gameCalls.length / games.length
Expand Down Expand Up @@ -96,3 +117,23 @@ export const fetchGamesPrizes = async (games: any[], chainId = ChainId.BSC): Pro

return chunk(gameMultiCallResult, chunkSize)
}

export const fetchGamesWinners = async (games: any[], chainId = ChainId.BSC): Promise<any[]> => {
const gameCalls = games.map((game) => {
const roundId = game.roundId ? game.roundId - 1 : game.roundId
return {
address: game.address,
name: 'getWinners',
params: [roundId],
}
})
const chunkSize = gameCalls.length / games.length

const gameMultiCallResult = await multicallv2({
abi: internal[chainId || ChainId.BSC].GameV1.abi,
calls: gameCalls,
chainId,
})

return chunk(gameMultiCallResult, chunkSize)
}
91 changes: 0 additions & 91 deletions packages/dapp/src/state/games/fetchGamePlayerData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,94 +80,3 @@ export const fetchGamePlayersData = async (game: any, chainId = ChainId.BSC): Pr

return chunk(gameMultiCallResult, chunkSize)
}

export const fetchGamePlayerAllowances = async (
account: string,
gamesToFetch: SerializedGame[],
chainId: number,
proxyAddress?: string,
) => {
const isBscNetwork = verifyBscNetwork(chainId)

const calls = gamesToFetch.map((game) => {
const contractAddress = game.address
return { address: contractAddress, name: 'allowance', params: [account, proxyAddress] }
})

const rawLpAllowances = await multicall<BigNumber[]>(erc20ABI, calls, chainId)
const parsedLpAllowances = rawLpAllowances.map((lpBalance) => {
return new BigNumber(lpBalance).toJSON()
})

return parsedLpAllowances
}

export const fetchGamePlayerTokenBalances = async (
account: string,
gamesToFetch: SerializedGame[],
chainId: number,
) => {
const calls = gamesToFetch.map((game) => {
const contractAddress = game.address
return {
address: contractAddress,
name: 'balanceOf',
params: [account],
}
})

const rawTokenBalances = await multicall(erc20ABI, calls, chainId)
const parsedTokenBalances = rawTokenBalances.map((tokenBalance) => {
return new BigNumber(tokenBalance).toJSON()
})
return parsedTokenBalances
}

export const fetchGamePlayerStakedBalances = async (
account: string,
gamesToFetch: SerializedGame[],
chainId: number,
) => {
const isBscNetwork = verifyBscNetwork(chainId)

const calls = gamesToFetch.map((game) => {
return {
address: account,
name: 'userInfo',
params: [game.id],
}
})

const rawStakedBalances = await multicallv2({
abi: isBscNetwork ? null : null,
calls,
chainId,
options: { requireSuccess: false },
})
const parsedStakedBalances = rawStakedBalances.map((stakedBalance) => {
return new BigNumber(stakedBalance[0]._hex).toJSON()
})
return parsedStakedBalances
}

export const fetchGamePlayerEarnings = async (account: string, gamesToFetch: SerializedGame[], chainId: number) => {
const isBscNetwork = verifyBscNetwork(chainId)
const multiCallChainId = isChainTestnet(chainId) ? ChainId.BSC_TESTNET : ChainId.BSC
const userAddress = account

const calls = gamesToFetch.map((game) => {
return {
address: account,
name: 'pendingCake',
params: [game.id, userAddress],
}
})

const rawEarnings = await multicallv2({ abi: null, calls, chainId: multiCallChainId })

const parsedEarnings = rawEarnings.map((earnings) => {
return new BigNumber(earnings).toJSON()
})

return parsedEarnings
}
41 changes: 38 additions & 3 deletions packages/dapp/src/state/games/fetchGames.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
import { getGameFactoryV1Contract } from 'utils/contractHelpers'
import { GameFactory } from 'config/types/typechain'

const fetchGames = async (chainId: number): Promise<GameFactory.GameStructOutput[]> => {
const gameFactoryContract: GameFactory = getGameFactoryV1Contract(chainId)
return gameFactoryContract.getDeployedGames()
import { gameBaseTransformer, gameExtendedTransformer } from './transformers'

import {
fetchPublicGamesData,
fetchGamesTreasuryAmounts,
fetchGamesCreatorAmounts,
fetchGamesPlayersAddresses,
fetchGamesPrizes,
fetchGamesWinners,
fetchGamesPlayersData,
} from './fetchGameData'
import { State, SerializedGame, DeserializedGame, DeserializedGameUserData } from '../types'

const fetchGames = async (chainId: number): Promise<SerializedGame[]> => {
try {
const gameFactoryContract: GameFactory = getGameFactoryV1Contract(chainId)
const gamesToFetch: GameFactory.GameStructOutput[] = await gameFactoryContract.getDeployedGames()

const [gameData, gamePlayers, gameCreatorAmounts, gameTreasuryAmounts] = await Promise.all([
fetchPublicGamesData(gamesToFetch, chainId),
fetchGamesPlayersAddresses(gamesToFetch, chainId),
fetchGamesCreatorAmounts(gamesToFetch, chainId),
fetchGamesTreasuryAmounts(gamesToFetch, chainId),
])
const transformedGames = gamesToFetch.map(
gameBaseTransformer(gameData, gamePlayers, gameCreatorAmounts, gameTreasuryAmounts),
)

const [gamePrizes, gameWinners] = await Promise.all([
fetchGamesPrizes(transformedGames, chainId),
fetchGamesWinners(transformedGames, chainId),
])
const completeGames = transformedGames.map(gameExtendedTransformer(gamePrizes, gameWinners))
return completeGames
} catch (e) {
console.log('🚀 ~ file: fetchGames.ts ~ line 21 ~ fetchGames ~ e', e)
return []
}
}

export default fetchGames
16 changes: 11 additions & 5 deletions packages/dapp/src/state/games/fetchGamesFull.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { getGameFactoryV1Contract } from 'utils/contractHelpers'
import { GameFactory } from 'config/types/typechain'

import { gameBaseTransformer, gameExtendedTransformer } from './transformers'
import { gameBaseTransformer, gameFullTransformer } from './transformers'

import {
fetchPublicGamesData,
fetchGamesRemainingPlayersCount,
fetchGamesTreasuryAmounts,
fetchGamesCreatorAmounts,
fetchGamesPlayersAddresses,
fetchGamesPrizes,
fetchGamesPlayersData,
Expand All @@ -16,17 +17,22 @@ const fetchGamesFull = async (chainId: number): Promise<SerializedGame[]> => {
const gameFactoryContract: GameFactory = getGameFactoryV1Contract(chainId)
const gamesToFetch: GameFactory.GameStructOutput[] = await gameFactoryContract.getDeployedGames()

const [gameData, gamePlayers] = await Promise.all([
const [gameData, gamePlayers, gameCreatorAmounts, gameTreasuryAmounts] = await Promise.all([
fetchPublicGamesData(gamesToFetch, chainId),
fetchGamesPlayersAddresses(gamesToFetch, chainId),
fetchGamesCreatorAmounts(gamesToFetch, chainId),
fetchGamesTreasuryAmounts(gamesToFetch, chainId),
])
const transformedGames = gamesToFetch.map(gameBaseTransformer(gameData, gamePlayers))
const transformedGames = gamesToFetch.map(
gameBaseTransformer(gameData, gamePlayers, gameCreatorAmounts, gameTreasuryAmounts),
)

// TODO GUIGUI HANDLE gamePlayersData
const [gamePrizes /* , gamePlayersData */] = await Promise.all([
fetchGamesPrizes(transformedGames, chainId),
// fetchGamesPlayersData(transformedGames, chainId),
])
const completeGames = transformedGames.map(gameExtendedTransformer(gamePrizes /* , gamePlayersData */))
const completeGames = transformedGames.map(gameFullTransformer(gamePrizes /* , gamePlayersData */))
return completeGames
}

Expand Down
11 changes: 5 additions & 6 deletions packages/dapp/src/state/games/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { chains } from 'utils/wagmi'

import { resetUserState } from '../global/actions'
import { SerializedGame, SerializedGamesState, SerializedGamePlayerData } from '../types'
import fetchGamesFull from './fetchGamesFull'
import fetchGames from './fetchGames'
import { fetchGamesPlayerData } from './fetchGamePlayerData'
import { gamePlayerDataTransformer } from './transformers'

const fetchGamePublicDataPkg = async ({ chainId }): Promise<SerializedGame[]> => fetchGamesFull(chainId)
const fetchGamePublicDataPkg = async ({ chainId }): Promise<SerializedGame[]> => fetchGames(chainId)

const initialState: SerializedGamesState = {
data: [],
Expand Down Expand Up @@ -43,12 +43,11 @@ export const fetchInitialGamesData = createAsyncThunk<
isPlaying: false,
isCreator: false,
isAdmin: false,
wonAmount: '0',
nextFromRange: '0',
nextToRange: '0',
isWonLastGames: false,
isCanVoteSplitPot: false,
isInTimeRange: false,
isLoosing: false,
},
playerData: {
playerAddress: '',
Expand Down Expand Up @@ -165,6 +164,7 @@ export const fetchGamePlayerDataAsync = createAsyncThunk<

const playerData = await fetchGamesPlayerData(games, account, chainId)

// TODO GUIGUI LOAD ROUND WINNERS HISTORY AND CHECK IF PLAYER HAS WON
return games.map(gamePlayerDataTransformer(playerData, account))
},
{
Expand Down Expand Up @@ -209,12 +209,11 @@ export const gamesSlice = createSlice({
isPlaying: false,
isCreator: false,
isAdmin: false,
wonAmount: '0',
nextFromRange: '0',
nextToRange: '0',
isWonLastGames: false,
isCanVoteSplitPot: false,
isInTimeRange: false,
isLoosing: false,
},
playerData: {
playerAddress: '',
Expand Down
Loading

1 comment on commit 7c0006f

@vercel
Copy link

@vercel vercel bot commented on 7c0006f Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.