From 433b6dfeac33be0bb02ac1713f7eda7938084373 Mon Sep 17 00:00:00 2001 From: Linkie Link Date: Mon, 2 Oct 2023 08:58:51 +0200 Subject: [PATCH] v1.7.0 hotfix --- .../fields/getClosePositionActions.ts | 56 ++++++++++++------- .../fields/getRepayActionsAndFunds.ts | 23 +++++--- src/hooks/queries/useClosePosition.tsx | 5 +- src/hooks/queries/useEditPosition.tsx | 40 ++++++++++--- src/hooks/queries/useRepayPosition.tsx | 16 ++++-- .../MarsCreditManager.types.ts | 22 ++++++-- 6 files changed, 117 insertions(+), 45 deletions(-) diff --git a/src/functions/fields/getClosePositionActions.ts b/src/functions/fields/getClosePositionActions.ts index a8b7b4d..567ff21 100644 --- a/src/functions/fields/getClosePositionActions.ts +++ b/src/functions/fields/getClosePositionActions.ts @@ -7,6 +7,7 @@ export const getClosePositionActions = ( primaryToSecondaryRate: number, slippage: number, whitelistedAssets: Asset[], + isV2: boolean, ): Action[] => { const swapMessage: Action[] = [] @@ -56,6 +57,40 @@ export const getClosePositionActions = ( }) } + const withdrawLiquidity = isV2 + ? { + withdraw_liquidity: { + slippage: slippage.toString(), + lp_token: { + amount: 'account_balance' as ActionAmount, + denom: vault.denoms.lpToken, + }, + }, + } + : { + withdraw_liquidity: { + lp_token: { + amount: 'account_balance' as ActionAmount, + denom: vault.denoms.lpToken, + }, + }, + } + + const repay = isV2 + ? { + repay: { + coin: { + denom: vault.position.borrowDenom || vault.denoms.secondary, + amount: 'account_balance' as ActionAmount, + }, + }, + } + : { + repay: { + denom: vault.position.borrowDenom || vault.denoms.secondary, + amount: 'account_balance' as ActionAmount, + }, + } return [ { exit_vault_unlocked: { @@ -65,27 +100,10 @@ export const getClosePositionActions = ( }, }, }, - { - withdraw_liquidity: { - slippage: slippage.toString(), - lp_token: { - amount: 'account_balance', - denom: vault.denoms.lpToken, - }, - }, - }, + withdrawLiquidity, ...swapMessage, ...(Math.max(vault.position.amounts.borrowedPrimary, vault.position.amounts.borrowedSecondary) - ? [ - { - repay: { - coin: { - denom: vault.position.borrowDenom || vault.denoms.secondary, - amount: 'account_balance' as ActionAmount, - }, - }, - }, - ] + ? [repay] : []), { refund_all_coin_balances: {} }, ] diff --git a/src/functions/fields/getRepayActionsAndFunds.ts b/src/functions/fields/getRepayActionsAndFunds.ts index c5c3acc..da6256d 100644 --- a/src/functions/fields/getRepayActionsAndFunds.ts +++ b/src/functions/fields/getRepayActionsAndFunds.ts @@ -2,20 +2,29 @@ import { Action, Coin } from 'types/generated/mars-credit-manager/MarsCreditMana import { orderCoinsByDenom } from './orderCoinsByDenom' -export const getRepayActionsAndFunds = (coin: Coin): [Action[], Coin[]] => { - return [ - [ - { - deposit: coin, - }, - { +export const getRepayActionsAndFunds = (coin: Coin, isV2: boolean): [Action[], Coin[]] => { + const repay = isV2 + ? { repay: { coin: { denom: coin.denom, amount: { exact: coin.amount }, }, }, + } + : { + repay: { + denom: coin.denom, + amount: { exact: coin.amount }, + }, + } + + return [ + [ + { + deposit: coin, }, + repay, ], orderCoinsByDenom([coin]), ] diff --git a/src/hooks/queries/useClosePosition.tsx b/src/hooks/queries/useClosePosition.tsx index 8cc5b95..a49e8f5 100644 --- a/src/hooks/queries/useClosePosition.tsx +++ b/src/hooks/queries/useClosePosition.tsx @@ -13,6 +13,8 @@ export const useClosePosition = (props: Props) => { const getExchangeRate = useStore((s) => s.getExchangeRate) const slippage = useStore((s) => s.slippage) const whitelistedAssets = useStore((s) => s.whitelistedAssets) + const networkConfig = useStore((s) => s.networkConfig) + const isV2 = !!networkConfig.contracts?.params const actions = useMemo(() => { if (!props.activeVault) return [] @@ -25,8 +27,9 @@ export const useClosePosition = (props: Props) => { primaryToSecondaryRate, slippage, whitelistedAssets, + isV2, ) - }, [props.activeVault, getExchangeRate, slippage, whitelistedAssets]) + }, [isV2, props.activeVault, getExchangeRate, slippage, whitelistedAssets]) const { data: fee } = useEstimateFarmFee({ accountId: props.activeVault?.position.accountId, diff --git a/src/hooks/queries/useEditPosition.tsx b/src/hooks/queries/useEditPosition.tsx index d3017a1..ccea271 100644 --- a/src/hooks/queries/useEditPosition.tsx +++ b/src/hooks/queries/useEditPosition.tsx @@ -6,6 +6,7 @@ import useStore from 'store' import { Action, Coin } from 'types/generated/mars-credit-manager/MarsCreditManager.types' import { useEstimateFarmFee } from './useEstimateFarmFee' +import { useProvideLiquidity } from './useProvideLiquidity' interface Props { accountId?: null | string @@ -17,6 +18,8 @@ interface Props { } export const useEditPosition = (props: Props) => { + const networkConfig = useStore((s) => s.networkConfig) + const isV2 = !!networkConfig.contracts?.params const convertToBaseCurrency = useStore((s) => s.convertToBaseCurrency) const convertValueToAmount = useStore((s) => s.convertValueToAmount) const slippage = useStore((s) => s.slippage) @@ -115,8 +118,13 @@ export const useEditPosition = (props: Props) => { props.position.amounts.borrowedSecondary, ]) + const { data: minLpToReceive } = useProvideLiquidity({ + coins: coinsAfterSwap, + vault: props.vault, + }) + const { actions, funds } = useMemo<{ actions: Action[]; funds: Coin[] }>(() => { - if (props.isReducingPosition) return { actions: [], funds: [] } + if ((!isV2 && !minLpToReceive) || props.isReducingPosition) return { actions: [], funds: [] } const coins: { supply: Coin[]; borrow?: Coin } = { supply: [], borrow: undefined } @@ -188,6 +196,26 @@ export const useEditPosition = (props: Props) => { } BigNumber.config({ EXPONENTIAL_AT: [-7, 30] }) + const minimumReceive = new BigNumber(isV2 || !minLpToReceive ? 0 : minLpToReceive) + .times(1 - slippage) + .integerValue(BigNumber.ROUND_CEIL) + .toString() + + const provideLiquidity = isV2 + ? { + provide_liquidity: { + coins_in: coinsToActionCoins(coinsAfterSwap), + lp_token_out: props.vault?.denoms.lpToken || '', + slippage: slippage.toString(), + }, + } + : { + provide_liquidity: { + coins_in: coinsToActionCoins(coinsAfterSwap), + lp_token_out: props.vault?.denoms.lpToken || '', + minimum_receive: minimumReceive, + }, + } const actions: Action[] = [ ...(coins.supply[0] @@ -200,13 +228,7 @@ export const useEditPosition = (props: Props) => { ...(coins.supply[1] ? [{ deposit: coins.supply[1] }] : []), ...(coins.borrow ? [{ borrow: coins.borrow }] : []), ...swapMessage, - { - provide_liquidity: { - coins_in: coinsToActionCoins(coinsAfterSwap), - lp_token_out: props.vault?.denoms.lpToken || '', - slippage: slippage.toString(), - }, - }, + provideLiquidity, { enter_vault: { coin: { @@ -230,6 +252,8 @@ export const useEditPosition = (props: Props) => { coinsAfterSwap, convertToBaseCurrency, convertValueToAmount, + minLpToReceive, + isV2, slippage, props.isReducingPosition, ]) diff --git a/src/hooks/queries/useRepayPosition.tsx b/src/hooks/queries/useRepayPosition.tsx index 72f4567..89e269f 100644 --- a/src/hooks/queries/useRepayPosition.tsx +++ b/src/hooks/queries/useRepayPosition.tsx @@ -1,5 +1,6 @@ import { getRepayActionsAndFunds, orderCoinsByDenom } from 'functions/fields' import { useEffect, useMemo, useState } from 'react' +import useStore from 'store' import { useEstimateFarmFee } from './useEstimateFarmFee' @@ -12,6 +13,8 @@ interface Props { export const useRepayPosition = (props: Props) => { const [amount, setAmount] = useState(0) + const networkConfig = useStore((s) => s.networkConfig) + const isV2 = !!networkConfig.contracts?.params useEffect(() => { const borrowKey = @@ -32,11 +35,14 @@ export const useRepayPosition = (props: Props) => { const [actions, funds] = useMemo(() => { if (!amount) return [[], []] - return getRepayActionsAndFunds({ - denom: props.activeVault.position.borrowDenom || props.activeVault.denoms.secondary, - amount: amount.toString(), - }) - }, [amount, props.activeVault.denoms.secondary, props.activeVault.position.borrowDenom]) + return getRepayActionsAndFunds( + { + denom: props.activeVault.position.borrowDenom || props.activeVault.denoms.secondary, + amount: amount.toString(), + }, + isV2, + ) + }, [isV2, amount, props.activeVault.denoms.secondary, props.activeVault.position.borrowDenom]) const { data: fee } = useEstimateFarmFee({ accountId: props.prevPosition.accountId, diff --git a/src/types/generated/mars-credit-manager/MarsCreditManager.types.ts b/src/types/generated/mars-credit-manager/MarsCreditManager.types.ts index d05eed1..f9d981a 100644 --- a/src/types/generated/mars-credit-manager/MarsCreditManager.types.ts +++ b/src/types/generated/mars-credit-manager/MarsCreditManager.types.ts @@ -81,7 +81,9 @@ export type Action = } | { repay: { - coin: ActionCoin + coin?: ActionCoin + amount?: ActionAmount + denom?: string recipient_account_id?: string | null } } @@ -127,13 +129,14 @@ export type Action = provide_liquidity: { coins_in: ActionCoin[] lp_token_out: string - slippage: Decimal + slippage?: Decimal + minimum_receive?: Uint128 } } | { withdraw_liquidity: { lp_token: ActionCoin - slippage: Decimal + slippage?: Decimal } } | { @@ -191,7 +194,9 @@ export type CallbackMsg = | { repay: { account_id: string - coin: ActionCoin + coin?: ActionCoin + denom?: string + amount?: ActionAmount } } | { @@ -299,7 +304,8 @@ export type CallbackMsg = account_id: string coins_in: ActionCoin[] lp_token_out: string - slippage: Decimal + slippage?: Decimal + minimum_receive?: Uint128 } } | { @@ -309,6 +315,12 @@ export type CallbackMsg = slippage: Decimal } } + | { + withdraw_liquidity: { + account_id: string + lp_token: ActionCoin + } + } | { refund_all_coin_balances: { account_id: string