From 6da404c879c92591f84ebb03366de469b5218efb Mon Sep 17 00:00:00 2001 From: kaladinlight <35275952+kaladinlight@users.noreply.github.com> Date: Mon, 25 Nov 2024 09:54:07 -0700 Subject: [PATCH] hotfix: savers withdraw --- .../Withdraw/components/Confirm.tsx | 40 ++++--------------- .../Withdraw/components/Status.tsx | 8 ++-- .../Withdraw/components/Withdraw.tsx | 4 +- src/lib/utils/thorchain/balance.ts | 8 +++- .../resolvers/thorchainsavers/index.ts | 4 +- 5 files changed, 23 insertions(+), 41 deletions(-) diff --git a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Confirm.tsx b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Confirm.tsx index 20a9157faef..129840913cd 100644 --- a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Confirm.tsx +++ b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Confirm.tsx @@ -40,7 +40,6 @@ import { fromBaseUnit, toBaseUnit } from 'lib/math' import { trackOpportunityEvent } from 'lib/mixpanel/helpers' import { getMixPanel } from 'lib/mixpanel/mixPanelSingleton' import { MixPanelEvent } from 'lib/mixpanel/types' -import { isToken } from 'lib/utils' import { fromThorBaseUnit, toThorBaseUnit } from 'lib/utils/thorchain' import { BASE_BPS_POINTS } from 'lib/utils/thorchain/constants' import { useSendThorTx } from 'lib/utils/thorchain/hooks/useSendThorTx' @@ -145,21 +144,11 @@ export const Confirm: React.FC = ({ accountId, onNext }) => { if (!asset) throw new Error(`Asset not found for AssetId ${opportunityData?.assetId}`) if (!feeAsset) throw new Error(`Fee asset not found for AssetId ${assetId}`) - const isTokenWithdraw = isToken(assetId) - // user info const { state: { wallet }, } = useWallet() - const assetBalanceFilter = useMemo( - () => ({ assetId: asset?.assetId, accountId: accountId ?? '' }), - [accountId, asset?.assetId], - ) - const assetBalanceBaseUnit = useAppSelector(s => - selectPortfolioCryptoBalanceBaseUnitByFilter(s, assetBalanceFilter), - ) - const feeAssetBalanceFilter = useMemo( () => ({ assetId: feeAsset?.assetId, accountId }), [accountId, feeAsset?.assetId], @@ -221,9 +210,7 @@ export const Confirm: React.FC = ({ accountId, onNext }) => { toBaseUnit(fromThorBaseUnit(protocolFeeCryptoThorBaseUnit), asset.precision), ) setDustAmountCryptoBaseUnit( - bnOrZero(toBaseUnit(fromThorBaseUnit(dust_amount), asset.precision)).toFixed( - asset.precision, - ), + bnOrZero(toBaseUnit(fromThorBaseUnit(dust_amount), feeAsset.precision)).toFixed(), ) const percentage = bnOrZero(slippage_bps).div(BASE_BPS_POINTS).times(100) // total downside (slippage going into position) - 0.007 ETH for 5 ETH deposit @@ -241,6 +228,7 @@ export const Confirm: React.FC = ({ accountId, onNext }) => { accountId, asset, dustAmountCryptoBaseUnit, + feeAsset, opportunity?.apy, opportunityData?.rewardsCryptoBaseUnit, opportunityData?.stakedAmountCryptoBaseUnit, @@ -425,26 +413,14 @@ export const Confirm: React.FC = ({ accountId, onNext }) => { }, [onNext]) const missingBalanceForGasCryptoPrecision = useMemo(() => { - // Token withdraws aren't dust sends, they're actual contract calls - // Hence, the balance required for them is denominated in the native fee asset - if (isTokenWithdraw) { - return fromBaseUnit( - bnOrZero(feeAssetBalanceCryptoBaseUnit) - .minus(bnOrZero(state?.withdraw.estimatedGasCryptoBaseUnit)) - .times(-1), - feeAsset.precision, - ) - } return fromBaseUnit( - bnOrZero(assetBalanceBaseUnit) + bnOrZero(feeAssetBalanceCryptoBaseUnit) .minus(bnOrZero(state?.withdraw.estimatedGasCryptoBaseUnit)) .minus(bnOrZero(dustAmountCryptoBaseUnit)) .times(-1), feeAsset.precision, ) }, [ - isTokenWithdraw, - assetBalanceBaseUnit, state?.withdraw.estimatedGasCryptoBaseUnit, dustAmountCryptoBaseUnit, feeAssetBalanceCryptoBaseUnit, @@ -598,7 +574,7 @@ export const Confirm: React.FC = ({ accountId, onNext }) => { - {!isTokenWithdraw && !isRunePool && ( + {!isRunePool && ( @@ -610,14 +586,14 @@ export const Confirm: React.FC = ({ accountId, onNext }) => { diff --git a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Status.tsx b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Status.tsx index 3b73cac421a..109a266abb5 100644 --- a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Status.tsx +++ b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Status.tsx @@ -255,16 +255,16 @@ export const Status: React.FC = ({ accountId }) => { diff --git a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Withdraw.tsx b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Withdraw.tsx index 0debc0c911e..ae691a6f007 100644 --- a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Withdraw.tsx +++ b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Withdraw.tsx @@ -335,10 +335,10 @@ export const Withdraw: React.FC = ({ accountId, fromAddress, onNe return toBaseUnit(outboundFeeInAssetCryptoPrecision, asset.precision) }, [outboundFeeCryptoBaseUnit, assetPriceInFeeAsset, asset, feeAsset]) + // https://gitlab.com/thorchain/thornode/-/blob/develop/x/thorchain/querier_quotes.go#L467 const safeOutboundFeeInAssetCryptoBaseUnit = useMemo(() => { if (!outboundFeeInAssetCryptoBaseUnit) return - // Add 5% as as a safety factor since the dust threshold fee is not necessarily going to cut it - return bnOrZero(outboundFeeInAssetCryptoBaseUnit).times(1.05).toFixed(0) + return bnOrZero(outboundFeeInAssetCryptoBaseUnit).times(4).toFixed(0) }, [outboundFeeInAssetCryptoBaseUnit]) const _validateCryptoAmount = useCallback( diff --git a/src/lib/utils/thorchain/balance.ts b/src/lib/utils/thorchain/balance.ts index 1b0a354101f..1aa7389788e 100644 --- a/src/lib/utils/thorchain/balance.ts +++ b/src/lib/utils/thorchain/balance.ts @@ -139,13 +139,19 @@ export const fetchHasEnoughBalanceForTxPlusFeesPlusSweep = async ({ throw new Error('Invalid type') } })() + const amountCryptoPrecision = type === 'deposit' ? _amountCryptoPrecision : fromThorBaseUnit( (quote as ThorchainSaversWithdrawQuoteResponseSuccess).dust_amount, ).toFixed() - const amountCryptoBaseUnit = toBaseUnit(amountCryptoPrecision, asset.precision) + + const amountCryptoBaseUnit = toBaseUnit( + amountCryptoPrecision, + type === 'deposit' ? asset.precision : feeAsset?.precision ?? 0, + ) + const estimatedFeesQueryArgs = { estimateFeesInput: { amountCryptoPrecision, diff --git a/src/state/slices/opportunitiesSlice/resolvers/thorchainsavers/index.ts b/src/state/slices/opportunitiesSlice/resolvers/thorchainsavers/index.ts index 65249b8115b..7a3daa11c87 100644 --- a/src/state/slices/opportunitiesSlice/resolvers/thorchainsavers/index.ts +++ b/src/state/slices/opportunitiesSlice/resolvers/thorchainsavers/index.ts @@ -356,7 +356,7 @@ export const thorchainSaversStakingOpportunitiesUserDataResolver = async ({ ) // to actual asset precision base unit const rewardsAmountsCryptoBaseUnit: [string] = [ - stakedAmountCryptoBaseUnitIncludeRewards.minus(stakedAmountCryptoBaseUnit).toFixed(), + stakedAmountCryptoBaseUnitIncludeRewards.minus(stakedAmountCryptoBaseUnit).toFixed(0), ] const dateUnlocked = await (async () => { @@ -388,7 +388,7 @@ export const thorchainSaversStakingOpportunitiesUserDataResolver = async ({ stakingOpportunitiesUserDataByUserStakingId[userStakingId] = { isLoaded: true, userStakingId, - stakedAmountCryptoBaseUnit: stakedAmountCryptoBaseUnit.toFixed(), + stakedAmountCryptoBaseUnit: stakedAmountCryptoBaseUnit.toFixed(0), rewardsCryptoBaseUnit: { amounts: rewardsAmountsCryptoBaseUnit, claimable: false }, dateUnlocked, }