From 8ec4d61d313a6c48b5da4d1e434d150e8e9e708a Mon Sep 17 00:00:00 2001 From: Yusuf Seyrek Date: Tue, 29 Aug 2023 00:26:28 +0300 Subject: [PATCH] fix(swap leftover amounts): use account_balance as sell amount on swaps (#405) --- src/components/Trade/TradeModule/SwapForm/index.tsx | 2 ++ src/hooks/useHealthComputer.tsx | 7 ------- src/store/slices/broadcast.ts | 3 ++- src/types/interfaces/store/broadcast.d.ts | 1 + src/utils/constants.ts | 2 -- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/components/Trade/TradeModule/SwapForm/index.tsx b/src/components/Trade/TradeModule/SwapForm/index.tsx index 451d5129..e97afeb7 100644 --- a/src/components/Trade/TradeModule/SwapForm/index.tsx +++ b/src/components/Trade/TradeModule/SwapForm/index.tsx @@ -147,6 +147,7 @@ export default function SwapForm(props: Props) { borrow: borrowCoin, denomOut: buyAsset.denom, slippage, + isMax: sellAssetAmount.isEqualTo(maxSellAmount), }) }, [ account?.id, @@ -156,6 +157,7 @@ export default function SwapForm(props: Props) { sellAssetAmount, slippage, swap, + maxSellAmount, ]) const debouncedUpdateAccount = useMemo( diff --git a/src/hooks/useHealthComputer.tsx b/src/hooks/useHealthComputer.tsx index 2cfb3040..13d0c514 100644 --- a/src/hooks/useHealthComputer.tsx +++ b/src/hooks/useHealthComputer.tsx @@ -15,7 +15,6 @@ import { HealthComputer, } from 'types/generated/mars-rover-health-computer/MarsRoverHealthComputer.types' import { convertAccountToPositions } from 'utils/accounts' -import { HEALTH_BUFFER } from 'utils/constants' import { BorrowTarget, compute_health_js, @@ -146,8 +145,6 @@ export default function useHealthComputer(account?: Account) { if (!healthComputer) return BN_ZERO try { return BN(max_borrow_estimate_js(healthComputer, denom, target)) - .times(1 - HEALTH_BUFFER) - .integerValue() } catch (err) { console.error(err) return BN_ZERO @@ -161,8 +158,6 @@ export default function useHealthComputer(account?: Account) { if (!healthComputer) return BN_ZERO try { return BN(max_withdraw_estimate_js(healthComputer, denom)) - .times(1 - HEALTH_BUFFER) - .integerValue() } catch (err) { console.error(err) return BN_ZERO @@ -176,8 +171,6 @@ export default function useHealthComputer(account?: Account) { if (!healthComputer) return BN_ZERO try { return BN(max_swap_estimate_js(healthComputer, from, to, kind)) - .times(1 - HEALTH_BUFFER) - .integerValue() } catch { return BN_ZERO } diff --git a/src/store/slices/broadcast.ts b/src/store/slices/broadcast.ts index fa2bf7a8..5a78da29 100644 --- a/src/store/slices/broadcast.ts +++ b/src/store/slices/broadcast.ts @@ -430,6 +430,7 @@ export default function createBroadcastSlice( borrow?: BNCoin denomOut: string slippage: number + isMax?: boolean }) => { const msg: CreditManagerExecuteMsg = { update_credit_account: { @@ -438,7 +439,7 @@ export default function createBroadcastSlice( ...(options.borrow ? [{ borrow: options.borrow.toCoin() }] : []), { swap_exact_in: { - coin_in: options.coinIn.toActionCoin(), + coin_in: options.coinIn.toActionCoin(options.isMax), denom_out: options.denomOut, slippage: options.slippage.toString(), }, diff --git a/src/types/interfaces/store/broadcast.d.ts b/src/types/interfaces/store/broadcast.d.ts index c448377c..01a13c09 100644 --- a/src/types/interfaces/store/broadcast.d.ts +++ b/src/types/interfaces/store/broadcast.d.ts @@ -32,6 +32,7 @@ interface BroadcastSlice { borrow: BNCoin denomOut: string slippage: number + isMax?: boolean }) => ExecutableTx toast: { message: string; isError?: boolean; title?: string } | null unlock: (options: { diff --git a/src/utils/constants.ts b/src/utils/constants.ts index e88482d5..fb59eac3 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -9,5 +9,3 @@ export const defaultFee: StdFee = { } export const SECONDS_IN_A_YEAR = 31540000 - -export const HEALTH_BUFFER = 0.01