Skip to content

Commit

Permalink
Improve communicating balance required to play
Browse files Browse the repository at this point in the history
  • Loading branch information
lindlof committed Dec 8, 2020
1 parent bfb73de commit 312a469
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 40 deletions.
23 changes: 21 additions & 2 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import { Button } from '@material-ui/core';
import { Button, Typography } from '@material-ui/core';
import Box from '@material-ui/core/Box';
import * as SecretJS from 'secretjs';
import { useLocalStorage } from './utils';
import * as Msg from './msg';
Expand All @@ -8,6 +9,7 @@ import * as Game from './game';
import GamePlaying from './GamePlaying';
import { useSnackbar } from 'notistack';
import Wallet from './wallet/Wallet';
import useAccount from './wallet/useAccount';
import Banner from './Banner';
import Grid from '@material-ui/core/Grid';
import CircularProgress from '@material-ui/core/CircularProgress';
Expand All @@ -22,6 +24,8 @@ export const App: React.FC = () => {
undefined,
Game.defaults,
);
const account = useAccount(client, game);
const lowBalance = account && account.balance < 20;
const { enqueueSnackbar } = useSnackbar();
routeUrl(client, setGame, enqueueSnackbar);

Expand All @@ -34,10 +38,10 @@ export const App: React.FC = () => {
<Grid item xs={12} sm={4}>
<Grid container justify="flex-end">
<Wallet
account={account}
client={client}
setClient={setClient}
faucetUrl={config.faucetUrl}
refreshBalance={game}
/>
</Grid>
</Grid>
Expand All @@ -58,11 +62,25 @@ export const App: React.FC = () => {
{game === null && <CircularProgress />}
{game === undefined && client && (
<Grid container direction="column" justify="center" alignItems="center" spacing={1}>
<Grid item xs={12}>
<Box bgcolor="secondary.main" color="primary.contrastText" p={1}>
{lowBalance && (
<div>
<Typography>11 SCRT required to play</Typography>
<Typography variant="subtitle2" align="center">
(10 SCRT entry + fees)
</Typography>
</div>
)}
{!lowBalance && <Typography>Play for 10 SCRT</Typography>}
</Box>
</Grid>
<Grid item xs={12}>
<Button
variant="contained"
color="primary"
size="large"
disabled={lowBalance}
onClick={() => playGame(client, config.contract, true, setGame, enqueueSnackbar)}
>
Play with Friend
Expand All @@ -72,6 +90,7 @@ export const App: React.FC = () => {
<Button
variant="contained"
color="primary"
disabled={lowBalance}
onClick={() => playGame(client, config.contract, false, setGame, enqueueSnackbar)}
>
Play with Anyone
Expand Down
42 changes: 4 additions & 38 deletions web/src/wallet/Wallet.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React from 'react';
import * as SecretJS from 'secretjs';
import { makeStyles } from '@material-ui/core/styles';
import { Button } from '@material-ui/core';
Expand All @@ -8,7 +8,7 @@ import CardActions from '@material-ui/core/CardActions';
import Typography from '@material-ui/core/Typography';
import SelectWalletModal from './SelectWalletModal';
import CircularProgress from '@material-ui/core/CircularProgress';
import { WalletType, walletTypeName } from './model';
import { Account, WalletType, walletTypeName } from './model';
import { useLocalStorage } from '../utils';

const useStyles = makeStyles((theme) => ({
Expand All @@ -29,35 +29,20 @@ const useStyles = makeStyles((theme) => ({
}));

interface Props {
account: Account | undefined;
client: SecretJS.SigningCosmWasmClient | undefined;
setClient: (client: SecretJS.SigningCosmWasmClient | undefined) => void;
faucetUrl: string | undefined;
refreshBalance: Object;
}

interface Account {
balance: number;
loading: boolean;
}

const Wallet = (props: Props) => {
const classes = useStyles();
const { client, setClient, faucetUrl, refreshBalance } = props;
const { account, client, setClient, faucetUrl } = props;
const [walletType, setWalletType] = useLocalStorage<WalletType | undefined>(
'wallet_type',
undefined,
);

const [account, setAccount] = useState<Account | undefined>();
useEffect(() => {
if (!client) return;
getAccount(client, setAccount);
const interval = setInterval(() => {
getAccount(client, setAccount);
}, 10000);
return () => clearInterval(interval);
}, [client, refreshBalance]);

return (
<div className={classes.root}>
<SelectWalletModal
Expand Down Expand Up @@ -113,23 +98,4 @@ const Wallet = (props: Props) => {
);
};

const getAccount = async (client: SecretJS.SigningCosmWasmClient, setAccount: Function) => {
try {
const account = await client.getAccount(client.senderAddress);
setAccount({ balance: getScrtBalance(account), loading: true });
} catch {
setAccount((a: Account) => ({ ...a, loading: false }));
}
};

const getScrtBalance = (account: SecretJS.Account | undefined): number => {
if (account === undefined) return 0;
for (let balance of account.balance) {
if (balance.denom === 'uscrt') {
return parseFloat(balance.amount) / 1000000;
}
}
return 0;
};

export default Wallet;
6 changes: 6 additions & 0 deletions web/src/wallet/model.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
interface Account {
balance: number;
loading: boolean;
}

enum WalletType {
Keplr = 'KEPLR',
LocalWallet = 'LOCAL_WALLET',
Expand All @@ -8,4 +13,5 @@ const walletTypeName = new Map([
[WalletType.LocalWallet, 'Local'],
]);

export type { Account };
export { WalletType, walletTypeName };
40 changes: 40 additions & 0 deletions web/src/wallet/useAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useEffect, useState } from 'react';
import * as SecretJS from 'secretjs';
import { Account } from './model';

const useAccount = (
client: SecretJS.SigningCosmWasmClient | undefined,
refreshOn: any,
): Account | undefined => {
const [account, setAccount] = useState<Account | undefined>();
useEffect(() => {
if (!client) return;
getAccount(client, setAccount);
const interval = setInterval(() => {
getAccount(client, setAccount);
}, 10000);
return () => clearInterval(interval);
}, [client, refreshOn]);
return account;
};

const getAccount = async (client: SecretJS.SigningCosmWasmClient, setAccount: Function) => {
try {
const account = await client.getAccount(client.senderAddress);
setAccount({ balance: getScrtBalance(account), loading: true });
} catch {
setAccount((a: Account) => ({ ...a, loading: false }));
}
};

const getScrtBalance = (account: SecretJS.Account | undefined): number => {
if (account === undefined) return 0;
for (let balance of account.balance) {
if (balance.denom === 'uscrt') {
return parseFloat(balance.amount) / 1000000;
}
}
return 0;
};

export default useAccount;

0 comments on commit 312a469

Please sign in to comment.