Skip to content

Commit

Permalink
hotfix: savers withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
kaladinlight committed Nov 25, 2024
1 parent aaf6695 commit 6da404c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -145,21 +144,11 @@ export const Confirm: React.FC<ConfirmProps> = ({ 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],
Expand Down Expand Up @@ -221,9 +210,7 @@ export const Confirm: React.FC<ConfirmProps> = ({ 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
Expand All @@ -241,6 +228,7 @@ export const Confirm: React.FC<ConfirmProps> = ({ accountId, onNext }) => {
accountId,
asset,
dustAmountCryptoBaseUnit,
feeAsset,
opportunity?.apy,
opportunityData?.rewardsCryptoBaseUnit,
opportunityData?.stakedAmountCryptoBaseUnit,
Expand Down Expand Up @@ -425,26 +413,14 @@ export const Confirm: React.FC<ConfirmProps> = ({ 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,
Expand Down Expand Up @@ -598,7 +574,7 @@ export const Confirm: React.FC<ConfirmProps> = ({ accountId, onNext }) => {
</Box>
</Row.Value>
</Row>
{!isTokenWithdraw && !isRunePool && (
{!isRunePool && (
<Row variant='gutter'>
<Row.Label>
<HelperTooltip label={translate('defi.modals.saversVaults.dustAmountTooltip')}>
Expand All @@ -610,14 +586,14 @@ export const Confirm: React.FC<ConfirmProps> = ({ accountId, onNext }) => {
<Skeleton isLoaded={!quoteLoading}>
<Amount.Fiat
fontWeight='bold'
value={bn(fromBaseUnit(dustAmountCryptoBaseUnit, asset.precision))
.times(marketData.price)
value={bn(fromBaseUnit(dustAmountCryptoBaseUnit, feeAsset.precision))
.times(feeMarketData.price)
.toFixed(2)}
/>
<Amount.Crypto
color='text.subtle'
value={fromBaseUnit(dustAmountCryptoBaseUnit, asset.precision)}
symbol={asset.symbol}
value={fromBaseUnit(dustAmountCryptoBaseUnit, feeAsset.precision)}
symbol={feeAsset.symbol}
/>
</Skeleton>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,16 @@ export const Status: React.FC<StatusProps> = ({ accountId }) => {
<Amount.Fiat
fontWeight='bold'
value={bnOrZero(state.withdraw.dustAmountCryptoBaseUnit)
.div(bn(10).pow(asset.precision))
.times(marketData.price)
.div(bn(10).pow(feeAsset.precision))
.times(feeMarketData.price)
.toFixed()}
/>
<Amount.Crypto
color='text.subtle'
value={bnOrZero(state.withdraw.dustAmountCryptoBaseUnit)
.div(bn(10).pow(asset.precision))
.div(bn(10).pow(feeAsset.precision))
.toFixed()}
symbol={asset.symbol}
symbol={feeAsset.symbol}
/>
</Box>
</Row.Value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ export const Withdraw: React.FC<WithdrawProps> = ({ 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(
Expand Down
8 changes: 7 additions & 1 deletion src/lib/utils/thorchain/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 6da404c

Please sign in to comment.