-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #702 from madfish-solutions/QUIPU-5-connect-contra…
…ct-coinflip-dashboard QUIPU-5 connect contract stats to dashboard
- Loading branch information
Showing
12 changed files
with
232 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { BigMapAbstraction } from '@taquito/taquito'; | ||
import { BigNumber } from 'bignumber.js'; | ||
|
||
export interface CoinflipStorage { | ||
admin: string; | ||
asset_to_id: BigMapAbstraction; | ||
gamer_stats: BigMapAbstraction; | ||
games: BigMapAbstraction; | ||
id_to_asset: BigMapAbstraction; | ||
assets_counter: BigNumber; | ||
games_counter: BigNumber; | ||
network_bank: BigNumber; | ||
network_fee: BigNumber; | ||
server: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { TezosToolkit } from '@taquito/taquito'; | ||
import { BigNumber } from 'bignumber.js'; | ||
|
||
import { COINFLIP_CONTRACT_DECIMALS } from '@config/config'; | ||
import { DEFAULT_TOKEN } from '@config/tokens'; | ||
import { getStorageInfo } from '@shared/dapp'; | ||
import { isEqual } from '@shared/helpers'; | ||
|
||
import { DashboardGeneralStats } from '../interfaces/dashboard-general-stats.interface'; | ||
import { CoinflipStorage } from './coinflip-contract.interface'; | ||
|
||
enum TOKEN_ASSETS { | ||
QUIPU = 0, | ||
TEZOS = 1 | ||
} | ||
|
||
interface GeneralStatsInterface { | ||
bank: BigNumber; | ||
games_count: BigNumber; | ||
payout_quot_f: BigNumber; | ||
total_won_amt: BigNumber; | ||
} | ||
|
||
export const getCoinflipGeneralStats = async ( | ||
tezos: TezosToolkit, | ||
contractAddress: string, | ||
tokenAddress: string | ||
): Promise<Nullable<DashboardGeneralStats>> => { | ||
const tokenAsset = isEqual(tokenAddress, DEFAULT_TOKEN.contractAddress) ? TOKEN_ASSETS.QUIPU : TOKEN_ASSETS.TEZOS; | ||
const storage = await getStorageInfo<CoinflipStorage>(tezos, contractAddress); | ||
const generalStats = (await storage.id_to_asset.get<GeneralStatsInterface>(tokenAsset)) ?? null; | ||
|
||
if (!generalStats) { | ||
return null; | ||
} | ||
|
||
const { bank, games_count: gamesCount, payout_quot_f, total_won_amt: totalWins } = generalStats; | ||
const payoutCoefficient = payout_quot_f.div(COINFLIP_CONTRACT_DECIMALS); | ||
|
||
return { bank, gamesCount, payoutCoefficient, totalWins }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './flip.api'; | ||
export * from './get-coinflip-general-stats'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...les/coinflip/components/dashboard-general-stats-info/coinflip-dashboard-stats.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@import '@styles/breakpoints'; | ||
|
||
.dashboardCard > :nth-child(n) { | ||
font-size: 14px; | ||
} | ||
|
||
@include media('>Lphone') { | ||
.dashboardCard > :nth-child(1) { | ||
font-size: 16px; | ||
} | ||
|
||
.dashboardCard > :nth-child(2) { | ||
font-size: 18px; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/modules/coinflip/components/dashboard-general-stats-info/coinflip-dashboard-stats.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { FC } from 'react'; | ||
|
||
import { observer } from 'mobx-react-lite'; | ||
|
||
import { useCoinflipStore } from '@modules/coinflip/hooks'; | ||
import { DashboardCard, DashboardStatsInfo } from '@shared/components'; | ||
|
||
import styles from './coinflip-dashboard-stats.module.scss'; | ||
import { useCoinflipDashboardStatsViewModel } from './use-coinflip-dashboard-stats.vm'; | ||
|
||
export const CoinflipDashboardStatsInfo: FC = observer(() => { | ||
const coinflipStore = useCoinflipStore(); | ||
const { token } = coinflipStore; | ||
const { generalStats, isLoading, currency } = useCoinflipDashboardStatsViewModel(token); | ||
const { bank, gamesCount, payoutCoefficient, totalWins } = generalStats; | ||
|
||
return ( | ||
<DashboardStatsInfo | ||
header="Game Info" | ||
cards={[ | ||
<DashboardCard | ||
size="large" | ||
volume={bank} | ||
label="Bank" | ||
currency={currency} | ||
loading={isLoading} | ||
hideTooltip | ||
className={styles.dashboardCard} | ||
/>, | ||
<DashboardCard | ||
size="large" | ||
volume={payoutCoefficient} | ||
label="Payout coefficient" | ||
currency="X" | ||
loading={isLoading} | ||
hideTooltip | ||
className={styles.dashboardCard} | ||
/>, | ||
<DashboardCard | ||
size="large" | ||
volume={totalWins} | ||
label="Total wins" | ||
currency={currency} | ||
loading={isLoading} | ||
hideTooltip | ||
className={styles.dashboardCard} | ||
/>, | ||
<DashboardCard | ||
size="large" | ||
volume={gamesCount} | ||
label="Games count" | ||
loading={isLoading} | ||
hideTooltip | ||
className={styles.dashboardCard} | ||
/> | ||
]} | ||
countOfRightElements={1} | ||
/> | ||
); | ||
}); |
74 changes: 74 additions & 0 deletions
74
...dules/coinflip/components/dashboard-general-stats-info/use-coinflip-dashboard-stats.vm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { NetworkType } from '@airgap/beacon-sdk'; | ||
import { TezosToolkit } from '@taquito/taquito'; | ||
|
||
import { DEFAULT_COINFLIP_CONTRACT } from '@config/config'; | ||
import { RPC_URLS } from '@config/enviroment'; | ||
import { DashboardGeneralStats } from '@modules/coinflip/interfaces/dashboard-general-stats.interface'; | ||
import { TokenToPlay } from '@modules/coinflip/stores'; | ||
import { bigNumberToString, isTezosToken } from '@shared/helpers'; | ||
import { Token } from '@shared/types'; | ||
import { useToasts } from '@shared/utils'; | ||
|
||
import { getCoinflipGeneralStats } from '../../api'; | ||
|
||
interface DashboardGeneralStatsMapped { | ||
bank: Nullable<string>; | ||
gamesCount: Nullable<string>; | ||
payoutCoefficient: Nullable<string>; | ||
totalWins: Nullable<string>; | ||
} | ||
|
||
const DEFAULT_GENERAL_STATS: DashboardGeneralStatsMapped = { | ||
bank: null, | ||
gamesCount: null, | ||
payoutCoefficient: null, | ||
totalWins: null | ||
}; | ||
|
||
export const useCoinflipDashboardStatsViewModel = (token: Token) => { | ||
const [isLoading, setLoading] = useState(true); | ||
const [generalStats, setGeneralStats] = useState<Nullable<DashboardGeneralStats>>(null); | ||
const { showErrorToast } = useToasts(); | ||
|
||
const currency = isTezosToken(token) ? TokenToPlay.Tezos : TokenToPlay.Quipu; | ||
|
||
useEffect(() => { | ||
const asyncLoad = async () => { | ||
const tezos = new TezosToolkit(RPC_URLS[NetworkType.ITHACANET]); | ||
setLoading(true); | ||
try { | ||
const coinflipGeneralStats = await getCoinflipGeneralStats( | ||
tezos, | ||
DEFAULT_COINFLIP_CONTRACT, | ||
token.contractAddress | ||
); | ||
setGeneralStats(coinflipGeneralStats); | ||
} catch (error) { | ||
showErrorToast(error as Error); | ||
} | ||
|
||
setLoading(false); | ||
}; | ||
void asyncLoad(); | ||
}, [showErrorToast, token]); | ||
|
||
const generalStatsMapping = ({ | ||
bank, | ||
gamesCount, | ||
payoutCoefficient, | ||
totalWins | ||
}: DashboardGeneralStats): DashboardGeneralStatsMapped => { | ||
return { | ||
bank: bank ? bigNumberToString(bank) : null, | ||
gamesCount: gamesCount ? bigNumberToString(gamesCount) : null, | ||
payoutCoefficient: payoutCoefficient ? bigNumberToString(payoutCoefficient) : null, | ||
totalWins: totalWins ? bigNumberToString(totalWins) : null | ||
}; | ||
}; | ||
|
||
const generalStatsMap = generalStats ? generalStatsMapping(generalStats) : DEFAULT_GENERAL_STATS; | ||
|
||
return { generalStats: generalStatsMap, currency, isLoading }; | ||
}; |
8 changes: 8 additions & 0 deletions
8
src/modules/coinflip/interfaces/dashboard-general-stats.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { BigNumber } from 'bignumber.js'; | ||
|
||
export interface DashboardGeneralStats { | ||
bank: Nullable<BigNumber>; | ||
gamesCount: Nullable<BigNumber>; | ||
payoutCoefficient: Nullable<BigNumber>; | ||
totalWins: Nullable<BigNumber>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters