From 58fe5f0f7e1e7bd68c9d0d2cb7da20af21e8e10e Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Thu, 25 Aug 2022 13:38:26 +0300 Subject: [PATCH 1/6] Fix displaying assets won --- src/modules/coinflip/coinflip-page.vm.ts | 21 ++++++--- .../coinflip-details/coinflip-details.vm.ts | 15 ++++++- .../coinflip-game/coinflip-game.vm.ts | 5 ++- src/modules/coinflip/hooks/index.ts | 1 + .../coinflip/hooks/use-user-last-game.ts | 20 +-------- .../coinflip/hooks/use-user-pending-game.ts | 45 +++++++++++++++++++ src/modules/coinflip/stores/coinflip.store.ts | 42 ++++++++++++++++- 7 files changed, 121 insertions(+), 28 deletions(-) create mode 100644 src/modules/coinflip/hooks/use-user-pending-game.ts diff --git a/src/modules/coinflip/coinflip-page.vm.ts b/src/modules/coinflip/coinflip-page.vm.ts index 2f18d02a8..e54d9f76e 100644 --- a/src/modules/coinflip/coinflip-page.vm.ts +++ b/src/modules/coinflip/coinflip-page.vm.ts @@ -15,11 +15,13 @@ const ZERO_BN = new BigNumber('0'); export const useCoinflipPageViewModel = () => { const [isInitialized, setIsInitialized] = useState(false); const coinflipStore = useCoinflipStore(); - const token = coinflipStore?.token; + const token = coinflipStore?.pendingGameToken; + const selectedToken = coinflipStore?.token; const rootStore = useRootStore(); const dAppReady = useReady(); const { accountPkh } = useAuthStore(); const prevAccountPkhRef = useRef>(accountPkh); + const prevSelectedTokenRef = useRef(token); const { getGamesUserInfo } = useGamesUserInfo(); const { getCoinflipGeneralStats } = useCoinflipGeneralStats(); const { getGamersStats } = useGamersStats(); @@ -35,8 +37,17 @@ export const useCoinflipPageViewModel = () => { await getGamersStats(); await getUserLastGame(); prevAccountPkhRef.current = accountPkh; + prevSelectedTokenRef.current = selectedToken; })(); - }, [getGamesUserInfo, getCoinflipGeneralStats, getGamersStats, getUserLastGame, dAppReady, accountPkh, token]); + }, [ + getGamesUserInfo, + getCoinflipGeneralStats, + getGamersStats, + getUserLastGame, + dAppReady, + accountPkh, + selectedToken + ]); useEffect(() => { (async () => { @@ -53,15 +64,15 @@ export const useCoinflipPageViewModel = () => { coinflipStore?.generalStatsStore.model.payoutCoefficient ?? ZERO_BN, COINFLIP_CONTRACT_DECIMALS ); - const realBidSize = toReal(coinflipStore?.userLastGameInfo.model.bidSize ?? ZERO_BN, token); + const realBidSize = toReal(coinflipStore?.pendingGameStore.model.bidSize ?? ZERO_BN, token); return realPayoutCoefficient.multipliedBy(realBidSize); - }, [coinflipStore?.generalStatsStore.model.payoutCoefficient, coinflipStore?.userLastGameInfo.model.bidSize, token]); + }, [coinflipStore?.generalStatsStore.model.payoutCoefficient, coinflipStore?.pendingGameStore.model.bidSize, token]); return { isInitialized, wonAmount, - result: coinflipStore?.userLastGameInfo.model.status, + result: coinflipStore?.pendingGameStore.model.status, currency: token?.metadata.symbol }; }; diff --git a/src/modules/coinflip/components/coinflip-details/coinflip-details.vm.ts b/src/modules/coinflip/components/coinflip-details/coinflip-details.vm.ts index fa833c843..977bc1eea 100644 --- a/src/modules/coinflip/components/coinflip-details/coinflip-details.vm.ts +++ b/src/modules/coinflip/components/coinflip-details/coinflip-details.vm.ts @@ -11,7 +11,7 @@ import { useAuthStore, useOnBlock } from '@shared/hooks'; import { Token } from '@shared/types'; import { getBetCoinSide, getGameResult, getTokenInstanceFromSymbol } from '../../helpers'; -import { useCoinflipGeneralStats, useCoinflipStore, useUserLastGame } from '../../hooks'; +import { useCoinflipGeneralStats, useCoinflipStore, useUserLastGame, useUserPendingGame } from '../../hooks'; import { DashboardGeneralStats } from '../../interfaces'; const mapping = ({ bank, gamesCount, payoutCoefficient, totalWins }: DashboardGeneralStats, token: Token) => ({ @@ -39,6 +39,7 @@ export const useCoinflipDetailsViewModel = () => { const { accountPkh } = useAuthStore(); const { getUserLastGame } = useUserLastGame(); const { getCoinflipGeneralStats } = useCoinflipGeneralStats(); + const { getUserPendingGame } = useUserPendingGame(); const updateGeneralStats = useCallback(async () => { if (isNull(tezos)) { @@ -56,13 +57,23 @@ export const useCoinflipDetailsViewModel = () => { await getUserLastGame(); }, [accountPkh, tezos, getUserLastGame]); + const updateUserPendingGame = useCallback(async () => { + if (isNull(tezos) || isNull(accountPkh)) { + return; + } + + await getUserPendingGame(); + }, [accountPkh, tezos, getUserPendingGame]); + useEffect(() => { updateUserLastGame(); + updateUserPendingGame(); updateGeneralStats(); - }, [updateUserLastGame, updateGeneralStats]); + }, [updateUserLastGame, updateGeneralStats, updateUserPendingGame]); useOnBlock(updateUserLastGame); useOnBlock(updateGeneralStats); + useOnBlock(updateUserPendingGame); const tokenInstance = getTokenInstanceFromSymbol(tokenToPlay); diff --git a/src/modules/coinflip/components/coinflip-game/coinflip-game.vm.ts b/src/modules/coinflip/components/coinflip-game/coinflip-game.vm.ts index 2776aeab3..ccb551fa4 100644 --- a/src/modules/coinflip/components/coinflip-game/coinflip-game.vm.ts +++ b/src/modules/coinflip/components/coinflip-game/coinflip-game.vm.ts @@ -4,7 +4,7 @@ import { useAmountInUsd, useAmplitudeService, useTokenBalance } from '@shared/ho import { useToasts } from '@shared/utils'; import { getGameResult, Statuses } from '../../helpers'; -import { useCoinFlip, useCoinflipStore, useGamersStats, useUserLastGame } from '../../hooks'; +import { useCoinFlip, useCoinflipStore, useGamersStats, useUserLastGame, useUserPendingGame } from '../../hooks'; import { CoinSide } from '../../stores'; export const useCoinflipGameViewModel = () => { @@ -15,6 +15,7 @@ export const useCoinflipGameViewModel = () => { const { getGamersStats } = useGamersStats(); const { getUserLastGame } = useUserLastGame(); + const { getUserPendingGame } = useUserPendingGame(); const { handleCoinFlip: handleCoinFlipPure } = useCoinFlip(); const { showErrorToast } = useToasts(); @@ -37,9 +38,11 @@ export const useCoinflipGameViewModel = () => { log('CLICK_FLIP_BUTTON_CLICK', logData); await handleCoinFlipPure(input, coinSide); + coinflipStore.setPendingGameTokenToPlay(tokenToPlay); await getGamersStats(); await getUserLastGame(); + await getUserPendingGame(); log('CLICK_FLIP_OPERATION_SUCCESS', logData); } catch (error) { diff --git a/src/modules/coinflip/hooks/index.ts b/src/modules/coinflip/hooks/index.ts index 800ad08a0..85d3721f2 100644 --- a/src/modules/coinflip/hooks/index.ts +++ b/src/modules/coinflip/hooks/index.ts @@ -4,3 +4,4 @@ export * from './use-coin-flip.ts'; export * from './use-gamers-stats'; export * from './use-games-user-info'; export * from './use-user-last-game'; +export * from './use-user-pending-game'; diff --git a/src/modules/coinflip/hooks/use-user-last-game.ts b/src/modules/coinflip/hooks/use-user-last-game.ts index 07f85ad37..555d554f3 100644 --- a/src/modules/coinflip/hooks/use-user-last-game.ts +++ b/src/modules/coinflip/hooks/use-user-last-game.ts @@ -1,33 +1,15 @@ -import { useCallback, useEffect, useRef } from 'react'; +import { useCallback } from 'react'; import { useReady } from '@providers/use-dapp'; -import { useGlobalModalsState } from '@providers/use-global-modals-state'; import { isNull } from '@shared/helpers'; import { useToasts } from '@shared/utils'; -import { getGameResult, Statuses } from '../helpers'; import { useCoinflipStore } from './stores'; -import { useLastGameResultAmplitude } from './use-last-game-result-amplitude'; export const useUserLastGame = () => { const { showErrorToast } = useToasts(); const coinflipStore = useCoinflipStore(); const isReady = useReady(); - const lastGameResultRef = useRef>(null); - const { openCoinflipModal } = useGlobalModalsState(); - const { lastGameLogEvent } = useLastGameResultAmplitude(); - - useEffect(() => { - const gameResult = getGameResult(coinflipStore?.userLastGameInfo?.model?.status); - - // TODO: avoid excess render - if (lastGameResultRef.current === Statuses.started && gameResult !== Statuses.started) { - openCoinflipModal(); - lastGameLogEvent(); - } - - lastGameResultRef.current = gameResult; - }, [coinflipStore?.userLastGameInfo?.model?.status, lastGameLogEvent, openCoinflipModal]); const getUserLastGame = useCallback(async () => { if (isReady && !isNull(coinflipStore)) { diff --git a/src/modules/coinflip/hooks/use-user-pending-game.ts b/src/modules/coinflip/hooks/use-user-pending-game.ts new file mode 100644 index 000000000..8815bfff6 --- /dev/null +++ b/src/modules/coinflip/hooks/use-user-pending-game.ts @@ -0,0 +1,45 @@ +import { useCallback, useEffect, useRef } from 'react'; + +import { useReady } from '@providers/use-dapp'; +import { useGlobalModalsState } from '@providers/use-global-modals-state'; +import { isNull } from '@shared/helpers'; +import { useToasts } from '@shared/utils'; + +import { getGameResult, Statuses } from '../helpers'; +import { useCoinflipStore } from './stores'; +import { useLastGameResultAmplitude } from './use-last-game-result-amplitude'; + +export const useUserPendingGame = () => { + const { showErrorToast } = useToasts(); + const coinflipStore = useCoinflipStore(); + const isReady = useReady(); + const lastGameResultRef = useRef>(null); + const { openCoinflipModal } = useGlobalModalsState(); + const { lastGameLogEvent } = useLastGameResultAmplitude(); + + useEffect(() => { + const gameResult = getGameResult(coinflipStore?.pendingGameStore?.model?.status); + + // TODO: avoid excess render + if (lastGameResultRef.current === Statuses.started && gameResult !== Statuses.started) { + openCoinflipModal(); + lastGameLogEvent(); + } + + lastGameResultRef.current = gameResult; + }, [coinflipStore?.pendingGameStore?.model?.status, lastGameLogEvent, openCoinflipModal]); + + const getUserPendingGame = useCallback(async () => { + if (isReady && !isNull(coinflipStore)) { + try { + await coinflipStore.pendingGameStore.load(); + } catch (error) { + showErrorToast(error as Error); + } + } + }, [isReady, showErrorToast, coinflipStore]); + + return { + getUserPendingGame + }; +}; diff --git a/src/modules/coinflip/stores/coinflip.store.ts b/src/modules/coinflip/stores/coinflip.store.ts index b33b0c6d2..7775447bf 100644 --- a/src/modules/coinflip/stores/coinflip.store.ts +++ b/src/modules/coinflip/stores/coinflip.store.ts @@ -72,6 +72,7 @@ export class CoinflipStore { isLoading = false; tokenToPlay: TokenToPlay = DEFAULT_TOKEN_TO_PLAY; game: CoinflipGame = { ...DEFAULT_COINFLIP_GAME }; + pendingGameTokenToPlay = DEFAULT_TOKEN_TO_PLAY; //#region games count store @Led({ @@ -167,12 +168,26 @@ export class CoinflipStore { } //#endregion user last game info store + //#region pending game store + @Led({ + default: DEFAULT_USER_LAST_GAME, + loader: async self => self.getPendingGameInfo(), + model: LastGameModel + }) + readonly pendingGameStore: LoadingErrorData; + + get pendingGame(): UserLastGame { + return this.pendingGameStore.model; + } + //#endregion pending game store + constructor(private rootStore: RootStore) { makeObservable(this, { tokenToPlay: observable, game: observable, gamersStatsInfo: observable, userLastGameInfo: observable, + pendingGameTokenToPlay: observable, payout: computed, token: computed, @@ -186,7 +201,8 @@ export class CoinflipStore { isUserLastGameLoading: computed, setToken: action, - setInput: action + setInput: action, + setPendingGameTokenToPlay: action }); } @@ -194,6 +210,10 @@ export class CoinflipStore { return this.tokenToPlay === TokenToPlay.Tezos ? TEZOS_TOKEN : QUIPU_TOKEN; } + get pendingGameToken() { + return this.pendingGameTokenToPlay === TokenToPlay.Tezos ? TEZOS_TOKEN : QUIPU_TOKEN; + } + setToken(token: TokenToPlay) { this.tokenToPlay = token; this.game = { ...DEFAULT_COINFLIP_GAME }; @@ -215,6 +235,10 @@ export class CoinflipStore { this.isLoading = false; } + setPendingGameTokenToPlay(token: TokenToPlay) { + this.pendingGameTokenToPlay = token; + } + async getGamesCount() { const allTokens = [TEZOS_TOKEN, QUIPU_TOKEN]; const gamesAmounts = await Promise.all( @@ -258,4 +282,20 @@ export class CoinflipStore { (await getUserLastGameInfo(this.rootStore.tezos, this.gamersStatsInfo.model.lastGameId)) ?? DEFAULT_USER_LAST_GAME ); } + + async getPendingGameInfo() { + const gamersStats = await getGamersStatsApi( + this.rootStore.tezos, + this.rootStore.authStore.accountPkh, + this.pendingGameToken + ); + + if (!gamersStats) { + return DEFAULT_USER_LAST_GAME; + } + + const lastGameInfo = await getUserLastGameInfo(this.rootStore.tezos, gamersStats.lastGameId); + + return lastGameInfo ?? DEFAULT_USER_LAST_GAME; + } } From 512ca2ce9dfd8867935cda0f562d0a383b46a9a4 Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Thu, 25 Aug 2022 15:01:07 +0300 Subject: [PATCH 2/6] Add new getters into makeObservable --- src/modules/coinflip/stores/coinflip.store.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/modules/coinflip/stores/coinflip.store.ts b/src/modules/coinflip/stores/coinflip.store.ts index 7775447bf..e6912e2c9 100644 --- a/src/modules/coinflip/stores/coinflip.store.ts +++ b/src/modules/coinflip/stores/coinflip.store.ts @@ -199,6 +199,8 @@ export class CoinflipStore { userLastGame: computed, isGamersStatsLoading: computed, isUserLastGameLoading: computed, + pendingGame: computed, + pendingGameToken: computed, setToken: action, setInput: action, From 0ece0909b868b376105e37769e816dc793fd6642 Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Mon, 29 Aug 2022 13:27:12 +0300 Subject: [PATCH 3/6] Update tokens whitelist --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 1eb9d719c..c4d3183f5 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "@craco/craco": "^6.4.3", "@percy/cli": "^1.7.2", "@quipuswap/sdk": "^3.0.0", - "@quipuswap/tokens-whitelist": "^1.0.7", + "@quipuswap/tokens-whitelist": "^1.0.8", "@sentry/react": "^7.11.1", "@sentry/tracing": "^7.11.1", "@taquito/beacon-wallet": "13.0.0", diff --git a/yarn.lock b/yarn.lock index 950babcb3..2d8a0045d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2541,10 +2541,10 @@ dependencies: bignumber.js "^9.0.2" -"@quipuswap/tokens-whitelist@^1.0.7": - version "1.0.7" - resolved "https://registry.yarnpkg.com/@quipuswap/tokens-whitelist/-/tokens-whitelist-1.0.7.tgz#2a61ca69a75c2fc979c18fa0ef1d49ccd6558447" - integrity sha512-DBnkJNDIg7gROiAyH7WIrjjK2OvBWJ87wNLpvg1dauxANFwz1TuyHkHVR5QMdkT1hpM+43eSgTCxZIg6B3ukVA== +"@quipuswap/tokens-whitelist@^1.0.8": + version "1.0.8" + resolved "https://registry.yarnpkg.com/@quipuswap/tokens-whitelist/-/tokens-whitelist-1.0.8.tgz#c1bb48dd979975275f4ee8e8839db0ae22d85886" + integrity sha512-S12GVtRj3ndXgPBjSTkbCFp1Ps6Nh5r2saG8/GM7nqPr3bWP0tTX0jIn5mGNUoYTohLy1vI7fgLuv9eL4R/Kzg== "@rollup/plugin-babel@^5.2.0": version "5.3.1" From 0d365aabf8c92e60788d0f4db786d7710149fa58 Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Mon, 29 Aug 2022 15:22:53 +0300 Subject: [PATCH 4/6] releases/v3.1.32 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c4d3183f5..0c039c8f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "quipuswap-webapp-2", - "version": "3.1.31", + "version": "3.1.32", "private": true, "scripts": { "pre-build": "node -v && npm -v && yarn -v && node ./scripts/build.js", From 4e2781fd10ed237111a50742fee27987e257df47 Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Mon, 29 Aug 2022 15:40:57 +0300 Subject: [PATCH 5/6] Revert "releases/v3.1.32" This reverts commit 0d365aabf8c92e60788d0f4db786d7710149fa58. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0c039c8f7..c4d3183f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "quipuswap-webapp-2", - "version": "3.1.32", + "version": "3.1.31", "private": true, "scripts": { "pre-build": "node -v && npm -v && yarn -v && node ./scripts/build.js", From 522731cb5eecc9a43ecbe6831ccb0d184ae4db84 Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Mon, 29 Aug 2022 15:41:45 +0300 Subject: [PATCH 6/6] v3.1.32 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c4d3183f5..0c039c8f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "quipuswap-webapp-2", - "version": "3.1.31", + "version": "3.1.32", "private": true, "scripts": { "pre-build": "node -v && npm -v && yarn -v && node ./scripts/build.js",