From 4be4d089b4c292930f44a67002c455384a2d6f90 Mon Sep 17 00:00:00 2001 From: Matthew Russell Date: Fri, 22 Sep 2023 13:36:54 -0700 Subject: [PATCH] feat: show balance and allowance --- .../deposit/deposit-get-started.tsx | 7 +- apps/trading/client-pages/deposit/deposit.tsx | 231 ++++++++++++------ 2 files changed, 162 insertions(+), 76 deletions(-) diff --git a/apps/trading/client-pages/deposit/deposit-get-started.tsx b/apps/trading/client-pages/deposit/deposit-get-started.tsx index 3873f9673..3a807d0d4 100644 --- a/apps/trading/client-pages/deposit/deposit-get-started.tsx +++ b/apps/trading/client-pages/deposit/deposit-get-started.tsx @@ -20,10 +20,9 @@ export const DepositGetStarted = () => { ); // Dont show unless still onboarding - // TODO: re add - // if (onboardingDismissed) { - // return null; - // } + if (onboardingDismissed) { + return null; + } return (
diff --git a/apps/trading/client-pages/deposit/deposit.tsx b/apps/trading/client-pages/deposit/deposit.tsx index c84495d48..c6934a375 100644 --- a/apps/trading/client-pages/deposit/deposit.tsx +++ b/apps/trading/client-pages/deposit/deposit.tsx @@ -18,6 +18,8 @@ import { Loader, TradingButton, TradingInput, + VegaIcon, + VegaIconNames, } from '@vegaprotocol/ui-toolkit'; import { useWeb3React } from '@web3-react/core'; import { @@ -32,6 +34,7 @@ import { MaxUint256 } from '@ethersproject/constants'; import { useTopTradedMarkets } from '../../lib/hooks/use-top-traded-markets'; import type { MarketMaybeWithDataAndCandles } from '@vegaprotocol/markets'; import { getAsset } from '@vegaprotocol/markets'; +import { CompactNumber } from '@vegaprotocol/react-helpers'; export const Deposit = () => { return ( @@ -78,6 +81,7 @@ interface DepositState { asset: AssetFieldsFragment | undefined; amount: string; allowance: BigNumber | undefined; + balance: BigNumber | undefined; } const DepositFlow = ({ @@ -99,6 +103,7 @@ const DepositFlow = ({ asset, amount: '', allowance: undefined, + balance: undefined, }; }); @@ -127,6 +132,7 @@ const DepositFlow = ({ step: DepositSteps.Asset, asset: undefined, allowance: undefined, + balance: undefined, })); return; } @@ -137,14 +143,25 @@ const DepositFlow = ({ // ethereum wallet connect fetch current allowance and bypass approval // step if approval already given let allowance = new BigNumber(0); + let balance = new BigNumber(0); if (account) { const signer = provider.getSigner(); const tokenContract = new Token( asset.source.contractAddress, signer || provider ); - const res = await tokenContract.allowance(account, bridgeAddress); - allowance = new BigNumber(addDecimal(res.toString(), asset.decimals)); + const allowanceRes = await tokenContract.allowance( + account, + bridgeAddress + ); + allowance = new BigNumber( + addDecimal(allowanceRes.toString(), asset.decimals) + ); + + const balanceRes = await tokenContract.balanceOf(account); + balance = new BigNumber( + addDecimal(balanceRes.toString(), asset.decimals) + ); } const step = allowance.isGreaterThan(0) @@ -156,6 +173,7 @@ const DepositFlow = ({ step, asset, allowance, + balance, })); }; @@ -168,6 +186,7 @@ const DepositFlow = ({ assets={assets} onSelect={handleAssetChanged} markets={markets} + balance={state.balance} /> @@ -184,18 +203,20 @@ const DepositFlow = ({ step={state.step} asset={state.asset} allowance={state.allowance} + balance={state.balance} amount={state.amount} setAmount={(amount) => setState((curr) => ({ ...curr, amount }))} />
-
-        {JSON.stringify(state, null, 2)}
-      
); }; +//
+//  {JSON.stringify(state, null, 2)}
+// 
+ const StepWrapper = ({ children }: { children: ReactNode }) => { return (
@@ -209,11 +230,13 @@ const AssetSelector = ({ assets, onSelect, markets, + balance, }: { asset?: AssetFieldsFragment; assets: AssetFieldsFragment[]; onSelect: (asset?: AssetFieldsFragment) => void; markets: MarketMaybeWithDataAndCandles[]; + balance: BigNumber | undefined; }) => { const [search, setSearch] = useState(''); const getTopMarkets = (a: AssetFieldsFragment) => { @@ -228,24 +251,32 @@ const AssetSelector = ({ if (asset && isAssetTypeERC20(asset)) { return (
-
+
-
-

+

+

{asset.symbol}{' '} - ({truncateByChars(asset.source.contractAddress)}) + {truncateByChars(asset.source.contractAddress)}

- onSelect(undefined)} size="small"> - Change asset - +
+ {balance && ( +
+ {balance.toString()} + {asset.symbol} +
+ )} + onSelect(undefined)} size="small"> + Change asset + +
@@ -284,7 +315,7 @@ const AssetSelector = ({ if (!isAssetTypeERC20(asset)) return null; return (
-
+
-
+