From 90993fa1c9e5eb44b02bd4d6f37a9cab15da2b30 Mon Sep 17 00:00:00 2001 From: Jibz1 Date: Fri, 27 Jan 2023 05:19:01 -0800 Subject: [PATCH] Wallet fix unstaking bug (#7747) - Removed the form component from unstake - added Available SUI balance - Fixed unstaking bug requiring Amount field - set rewards to absolute value. - fixed typo Screenshot 2023-01-26 at 5 17 31 PM --- .../src/ui/app/staking/getStakingRewards.ts | 4 +- .../src/ui/app/staking/stake/StakeForm.tsx | 46 +++++++++++++++++-- .../src/ui/app/staking/stake/StakingCard.tsx | 20 ++------ .../src/ui/app/staking/stake/UnstakeForm.tsx | 19 ++------ 4 files changed, 52 insertions(+), 37 deletions(-) diff --git a/apps/wallet/src/ui/app/staking/getStakingRewards.ts b/apps/wallet/src/ui/app/staking/getStakingRewards.ts index 72809175930b8..ee1400d3c1110 100644 --- a/apps/wallet/src/ui/app/staking/getStakingRewards.ts +++ b/apps/wallet/src/ui/app/staking/getStakingRewards.ts @@ -41,6 +41,6 @@ export function getStakingRewards( .multipliedBy(suiBalance) .dividedBy(delegationTokenSupply); - const earnToken = currentSuiWorth.decimalPlaces(0, 1).minus(pricipalAmout); - return earnToken.toNumber(); + const earnToken = currentSuiWorth.minus(pricipalAmout); + return earnToken.decimalPlaces(0, BigNumber.ROUND_DOWN).toNumber(); } diff --git a/apps/wallet/src/ui/app/staking/stake/StakeForm.tsx b/apps/wallet/src/ui/app/staking/stake/StakeForm.tsx index 58332889c3a81..8a065aad78bfa 100644 --- a/apps/wallet/src/ui/app/staking/stake/StakeForm.tsx +++ b/apps/wallet/src/ui/app/staking/stake/StakeForm.tsx @@ -3,13 +3,14 @@ import { SUI_TYPE_ARG } from '@mysten/sui.js'; import { ErrorMessage, Field, Form, useFormikContext } from 'formik'; -import { useRef, memo, useCallback } from 'react'; +import { useRef, memo, useCallback, useMemo } from 'react'; import { Content } from '_app/shared/bottom-menu-layout'; import { Card } from '_app/shared/card'; import { Text } from '_app/shared/text'; import Alert from '_components/alert'; import NumberInput from '_components/number-input'; +import { parseAmount } from '_helpers'; import { useFormatCoin } from '_hooks'; import { DEFAULT_GAS_BUDGET_FOR_STAKE } from '_redux/slices/sui-objects/Coin'; @@ -21,18 +22,38 @@ export type StakeFromProps = { submitError: string | null; coinBalance: bigint; coinType: string; + coinDecimals: number; epoch: string; onClearSubmitError: () => void; }; +function AvailableBalance({ + amount, + coinType, +}: { + amount: bigint; + coinType: string; +}) { + const [formatted, symbol] = useFormatCoin(amount, coinType); + return ( + + Available - {+formatted > 0 ? formatted : 0} {symbol} + + ); +} + function StakeForm({ submitError, coinBalance, coinType, onClearSubmitError, + coinDecimals, epoch, }: StakeFromProps) { - const { setFieldValue } = useFormikContext(); + const { + setFieldValue, + values: { amount }, + } = useFormikContext(); const onClearRef = useRef(onClearSubmitError); onClearRef.current = onClearSubmitError; @@ -42,12 +63,12 @@ function StakeForm({ SUI_TYPE_ARG ); - const coinBalanceMinusGas = + const totalAvailableBalance = coinBalance - BigInt(coinType === SUI_TYPE_ARG ? DEFAULT_GAS_BUDGET_FOR_STAKE : 0); const [maxToken, symbol, queryResult] = useFormatCoin( - coinBalanceMinusGas, + totalAvailableBalance, coinType ); @@ -56,9 +77,24 @@ function StakeForm({ setFieldValue('amount', maxToken); }, [maxToken, setFieldValue]); + const calculateRemaining = useMemo(() => { + if (!coinBalance) return 0n; + const bigIntAmount = parseAmount(amount, coinDecimals); + return totalAvailableBalance - bigIntAmount; + }, [amount, coinBalance, coinDecimals, totalAvailableBalance]); + return (
+
+ + Enter the amount of SUI to stake + + +
- Epoch #{+epoch + 1} + Epoch #{+epoch + 2} diff --git a/apps/wallet/src/ui/app/staking/stake/StakingCard.tsx b/apps/wallet/src/ui/app/staking/stake/StakingCard.tsx index fda97c03210ef..ccade8843fdd0 100644 --- a/apps/wallet/src/ui/app/staking/stake/StakingCard.tsx +++ b/apps/wallet/src/ui/app/staking/stake/StakingCard.tsx @@ -87,7 +87,7 @@ function StakingCard() { ); const totalTokenBalance = useMemo(() => { - if (!allDelegation) return BigInt(0); + if (!allDelegation) return 0n; // return only the total amount of tokens staked for a specific stakeId if (stakeIdParams) { const balance = @@ -108,7 +108,7 @@ function StakingCard() { () => unstake ? totalTokenBalance - : (coinType && aggregateBalances[coinType]) || BigInt(0), + : (coinType && aggregateBalances[coinType]) || 0n, [unstake, totalTokenBalance, coinType, aggregateBalances] ); @@ -143,7 +143,7 @@ function StakingCard() { () => unstake ? coinBalance + BigInt(DEFAULT_GAS_BUDGET_FOR_STAKE) - : aggregateBalances[GAS_TYPE_ARG] || BigInt(0), + : aggregateBalances[GAS_TYPE_ARG] || 0n, [aggregateBalances, coinBalance, unstake] ); @@ -332,7 +332,7 @@ function StakingCard() { > @@ -346,17 +346,6 @@ function StakingCard() { stakedId={stakeIdParams} /> - {!unstake && ( -
- - Enter the amount of SUI to stake - -
- )} {unstake ? ( { onClearRef.current(); setFieldValue('amount', tokenBalance); - setTouched({ amount: true }); }, [setFieldValue, setTouched, tokenBalance]); return ( -