Skip to content

Commit

Permalink
Merge pull request #1008 from madfish-solutions/releases/v3.1.32
Browse files Browse the repository at this point in the history
Releases/v3.1.32
  • Loading branch information
keshan3262 authored Aug 29, 2022
2 parents 305afd6 + 522731c commit bc93bc1
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 34 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
21 changes: 16 additions & 5 deletions src/modules/coinflip/coinflip-page.vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Nullable<string>>(accountPkh);
const prevSelectedTokenRef = useRef(token);
const { getGamesUserInfo } = useGamesUserInfo();
const { getCoinflipGeneralStats } = useCoinflipGeneralStats();
const { getGamersStats } = useGamersStats();
Expand All @@ -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 () => {
Expand All @@ -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
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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) => ({
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -15,6 +15,7 @@ export const useCoinflipGameViewModel = () => {

const { getGamersStats } = useGamersStats();
const { getUserLastGame } = useUserLastGame();
const { getUserPendingGame } = useUserPendingGame();
const { handleCoinFlip: handleCoinFlipPure } = useCoinFlip();

const { showErrorToast } = useToasts();
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/modules/coinflip/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
20 changes: 1 addition & 19 deletions src/modules/coinflip/hooks/use-user-last-game.ts
Original file line number Diff line number Diff line change
@@ -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<Nullable<Statuses>>(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)) {
Expand Down
45 changes: 45 additions & 0 deletions src/modules/coinflip/hooks/use-user-pending-game.ts
Original file line number Diff line number Diff line change
@@ -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<Nullable<Statuses>>(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
};
};
44 changes: 43 additions & 1 deletion src/modules/coinflip/stores/coinflip.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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<LastGameModel, typeof DEFAULT_USER_LAST_GAME>;

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,
Expand All @@ -184,16 +199,23 @@ export class CoinflipStore {
userLastGame: computed,
isGamersStatsLoading: computed,
isUserLastGameLoading: computed,
pendingGame: computed,
pendingGameToken: computed,

setToken: action,
setInput: action
setInput: action,
setPendingGameTokenToPlay: action
});
}

get token(): Token {
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 };
Expand All @@ -215,6 +237,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(
Expand Down Expand Up @@ -258,4 +284,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;
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit bc93bc1

Please sign in to comment.