fix(swap leftover amounts): use account_balance as sell amount on swaps (#405)

This commit is contained in:
Yusuf Seyrek 2023-08-29 00:26:28 +03:00 committed by GitHub
parent 44d5d5e42c
commit 8ec4d61d31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 5 additions and 10 deletions

View File

@ -147,6 +147,7 @@ export default function SwapForm(props: Props) {
borrow: borrowCoin, borrow: borrowCoin,
denomOut: buyAsset.denom, denomOut: buyAsset.denom,
slippage, slippage,
isMax: sellAssetAmount.isEqualTo(maxSellAmount),
}) })
}, [ }, [
account?.id, account?.id,
@ -156,6 +157,7 @@ export default function SwapForm(props: Props) {
sellAssetAmount, sellAssetAmount,
slippage, slippage,
swap, swap,
maxSellAmount,
]) ])
const debouncedUpdateAccount = useMemo( const debouncedUpdateAccount = useMemo(

View File

@ -15,7 +15,6 @@ import {
HealthComputer, HealthComputer,
} from 'types/generated/mars-rover-health-computer/MarsRoverHealthComputer.types' } from 'types/generated/mars-rover-health-computer/MarsRoverHealthComputer.types'
import { convertAccountToPositions } from 'utils/accounts' import { convertAccountToPositions } from 'utils/accounts'
import { HEALTH_BUFFER } from 'utils/constants'
import { import {
BorrowTarget, BorrowTarget,
compute_health_js, compute_health_js,
@ -146,8 +145,6 @@ export default function useHealthComputer(account?: Account) {
if (!healthComputer) return BN_ZERO if (!healthComputer) return BN_ZERO
try { try {
return BN(max_borrow_estimate_js(healthComputer, denom, target)) return BN(max_borrow_estimate_js(healthComputer, denom, target))
.times(1 - HEALTH_BUFFER)
.integerValue()
} catch (err) { } catch (err) {
console.error(err) console.error(err)
return BN_ZERO return BN_ZERO
@ -161,8 +158,6 @@ export default function useHealthComputer(account?: Account) {
if (!healthComputer) return BN_ZERO if (!healthComputer) return BN_ZERO
try { try {
return BN(max_withdraw_estimate_js(healthComputer, denom)) return BN(max_withdraw_estimate_js(healthComputer, denom))
.times(1 - HEALTH_BUFFER)
.integerValue()
} catch (err) { } catch (err) {
console.error(err) console.error(err)
return BN_ZERO return BN_ZERO
@ -176,8 +171,6 @@ export default function useHealthComputer(account?: Account) {
if (!healthComputer) return BN_ZERO if (!healthComputer) return BN_ZERO
try { try {
return BN(max_swap_estimate_js(healthComputer, from, to, kind)) return BN(max_swap_estimate_js(healthComputer, from, to, kind))
.times(1 - HEALTH_BUFFER)
.integerValue()
} catch { } catch {
return BN_ZERO return BN_ZERO
} }

View File

@ -430,6 +430,7 @@ export default function createBroadcastSlice(
borrow?: BNCoin borrow?: BNCoin
denomOut: string denomOut: string
slippage: number slippage: number
isMax?: boolean
}) => { }) => {
const msg: CreditManagerExecuteMsg = { const msg: CreditManagerExecuteMsg = {
update_credit_account: { update_credit_account: {
@ -438,7 +439,7 @@ export default function createBroadcastSlice(
...(options.borrow ? [{ borrow: options.borrow.toCoin() }] : []), ...(options.borrow ? [{ borrow: options.borrow.toCoin() }] : []),
{ {
swap_exact_in: { swap_exact_in: {
coin_in: options.coinIn.toActionCoin(), coin_in: options.coinIn.toActionCoin(options.isMax),
denom_out: options.denomOut, denom_out: options.denomOut,
slippage: options.slippage.toString(), slippage: options.slippage.toString(),
}, },

View File

@ -32,6 +32,7 @@ interface BroadcastSlice {
borrow: BNCoin borrow: BNCoin
denomOut: string denomOut: string
slippage: number slippage: number
isMax?: boolean
}) => ExecutableTx }) => ExecutableTx
toast: { message: string; isError?: boolean; title?: string } | null toast: { message: string; isError?: boolean; title?: string } | null
unlock: (options: { unlock: (options: {

View File

@ -9,5 +9,3 @@ export const defaultFee: StdFee = {
} }
export const SECONDS_IN_A_YEAR = 31540000 export const SECONDS_IN_A_YEAR = 31540000
export const HEALTH_BUFFER = 0.01