feat(auto-unlend): implemented to the withdraws (#406)
This commit is contained in:
parent
8ec4d61d31
commit
f64d2a78ff
@ -29,8 +29,7 @@ export default function WithdrawFromAccount(props: Props) {
|
|||||||
const [isConfirming, setIsConfirming] = useToggle()
|
const [isConfirming, setIsConfirming] = useToggle()
|
||||||
const [currentAsset, setCurrentAsset] = useState(defaultAsset)
|
const [currentAsset, setCurrentAsset] = useState(defaultAsset)
|
||||||
const [amount, setAmount] = useState(BN_ZERO)
|
const [amount, setAmount] = useState(BN_ZERO)
|
||||||
const { updatedAccount, removeDepositAndLendsByDenom, removeDeposits, addDebts } =
|
const { updatedAccount, removeDeposits, removeLends, addDebts } = useUpdatedAccount(account)
|
||||||
useUpdatedAccount(account)
|
|
||||||
const { computeMaxWithdrawAmount } = useHealthComputer(account)
|
const { computeMaxWithdrawAmount } = useHealthComputer(account)
|
||||||
const { computeMaxBorrowAmount } = useHealthComputer(updatedAccount)
|
const { computeMaxBorrowAmount } = useHealthComputer(updatedAccount)
|
||||||
const maxWithdrawAmount = computeMaxWithdrawAmount(currentAsset.denom)
|
const maxWithdrawAmount = computeMaxWithdrawAmount(currentAsset.denom)
|
||||||
@ -42,10 +41,17 @@ export default function WithdrawFromAccount(props: Props) {
|
|||||||
const debtAmount = isWithinBalance ? BN_ZERO : amount.minus(maxWithdrawAmount)
|
const debtAmount = isWithinBalance ? BN_ZERO : amount.minus(maxWithdrawAmount)
|
||||||
const max = withdrawWithBorrowing ? maxWithdrawWithBorrowAmount : maxWithdrawAmount
|
const max = withdrawWithBorrowing ? maxWithdrawWithBorrowAmount : maxWithdrawAmount
|
||||||
|
|
||||||
|
const accountDeposit = account.deposits.find(byDenom(currentAsset.denom))?.amount ?? BN_ZERO
|
||||||
|
const shouldReclaim =
|
||||||
|
amount.isGreaterThan(accountDeposit) && !withdrawWithBorrowing && currentAsset.isAutoLendEnabled
|
||||||
|
const reclaimAmount = shouldReclaim ? amount.minus(accountDeposit) : BN_ZERO
|
||||||
|
const isReclaimingMaxAmount = maxWithdrawAmount.isEqualTo(amount)
|
||||||
|
|
||||||
function onChangeAmount(val: BigNumber) {
|
function onChangeAmount(val: BigNumber) {
|
||||||
setAmount(val)
|
setAmount(val)
|
||||||
removeDeposits([BNCoin.fromDenomAndBigNumber(currentAsset.denom, withdrawAmount)])
|
removeDeposits([BNCoin.fromDenomAndBigNumber(currentAsset.denom, withdrawAmount)])
|
||||||
addDebts([BNCoin.fromDenomAndBigNumber(currentAsset.denom, debtAmount)])
|
addDebts([BNCoin.fromDenomAndBigNumber(currentAsset.denom, debtAmount)])
|
||||||
|
removeLends([BNCoin.fromDenomAndBigNumber(currentAsset.denom, reclaimAmount)])
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetState() {
|
function resetState() {
|
||||||
@ -55,12 +61,24 @@ export default function WithdrawFromAccount(props: Props) {
|
|||||||
|
|
||||||
async function onConfirm() {
|
async function onConfirm() {
|
||||||
setIsConfirming(true)
|
setIsConfirming(true)
|
||||||
|
|
||||||
|
const coins = [BNCoin.fromDenomAndBigNumber(currentAsset.denom, amount)]
|
||||||
|
const borrow = !debtAmount.isZero()
|
||||||
|
? [BNCoin.fromDenomAndBigNumber(currentAsset.denom, debtAmount)]
|
||||||
|
: []
|
||||||
|
const reclaims = !reclaimAmount.isZero()
|
||||||
|
? [
|
||||||
|
BNCoin.fromDenomAndBigNumber(currentAsset.denom, reclaimAmount).toActionCoin(
|
||||||
|
isReclaimingMaxAmount,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
: []
|
||||||
|
|
||||||
const result = await withdraw({
|
const result = await withdraw({
|
||||||
accountId: account.id,
|
accountId: account.id,
|
||||||
coins: [BNCoin.fromDenomAndBigNumber(currentAsset.denom, amount)],
|
coins,
|
||||||
borrow: debtAmount.isZero()
|
borrow,
|
||||||
? []
|
reclaims,
|
||||||
: [BNCoin.fromDenomAndBigNumber(currentAsset.denom, debtAmount)],
|
|
||||||
})
|
})
|
||||||
|
|
||||||
setIsConfirming(false)
|
setIsConfirming(false)
|
||||||
|
@ -24,6 +24,7 @@ import {
|
|||||||
SwapKind,
|
SwapKind,
|
||||||
} from 'utils/health_computer'
|
} from 'utils/health_computer'
|
||||||
import { BN } from 'utils/helpers'
|
import { BN } from 'utils/helpers'
|
||||||
|
import { LTV_BUFFER } from 'utils/constants'
|
||||||
|
|
||||||
export default function useHealthComputer(account?: Account) {
|
export default function useHealthComputer(account?: Account) {
|
||||||
const { data: prices } = usePrices()
|
const { data: prices } = usePrices()
|
||||||
@ -145,6 +146,8 @@ 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))
|
||||||
|
.multipliedBy(1 - LTV_BUFFER)
|
||||||
|
.integerValue()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
return BN_ZERO
|
return BN_ZERO
|
||||||
|
@ -8,6 +8,7 @@ import { BNCoin } from 'types/classes/BNCoin'
|
|||||||
import { ExecuteMsg as AccountNftExecuteMsg } from 'types/generated/mars-account-nft/MarsAccountNft.types'
|
import { ExecuteMsg as AccountNftExecuteMsg } from 'types/generated/mars-account-nft/MarsAccountNft.types'
|
||||||
import {
|
import {
|
||||||
Action,
|
Action,
|
||||||
|
ActionCoin,
|
||||||
Action as CreditManagerAction,
|
Action as CreditManagerAction,
|
||||||
ExecuteMsg as CreditManagerExecuteMsg,
|
ExecuteMsg as CreditManagerExecuteMsg,
|
||||||
} from 'types/generated/mars-credit-manager/MarsCreditManager.types'
|
} from 'types/generated/mars-credit-manager/MarsCreditManager.types'
|
||||||
@ -318,7 +319,15 @@ export default function createBroadcastSlice(
|
|||||||
handleResponseMessages(response, `Deposited into vault`)
|
handleResponseMessages(response, `Deposited into vault`)
|
||||||
return !!response.result
|
return !!response.result
|
||||||
},
|
},
|
||||||
withdraw: async (options: { accountId: string; coins: BNCoin[]; borrow: BNCoin[] }) => {
|
withdraw: async (options: {
|
||||||
|
accountId: string
|
||||||
|
coins: BNCoin[]
|
||||||
|
borrow: BNCoin[]
|
||||||
|
reclaims: ActionCoin[]
|
||||||
|
}) => {
|
||||||
|
const reclaimActions = options.reclaims.map((coin) => ({
|
||||||
|
reclaim: coin,
|
||||||
|
}))
|
||||||
const withdrawActions = options.coins.map((coin) => ({
|
const withdrawActions = options.coins.map((coin) => ({
|
||||||
withdraw: coin.toCoin(),
|
withdraw: coin.toCoin(),
|
||||||
}))
|
}))
|
||||||
@ -329,7 +338,7 @@ export default function createBroadcastSlice(
|
|||||||
const msg: CreditManagerExecuteMsg = {
|
const msg: CreditManagerExecuteMsg = {
|
||||||
update_credit_account: {
|
update_credit_account: {
|
||||||
account_id: options.accountId,
|
account_id: options.accountId,
|
||||||
actions: [...borrowActions, ...withdrawActions],
|
actions: [...reclaimActions, ...borrowActions, ...withdrawActions],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
8
src/types/interfaces/store/broadcast.d.ts
vendored
8
src/types/interfaces/store/broadcast.d.ts
vendored
@ -1,4 +1,5 @@
|
|||||||
const BNCoin = import('types/classes/BNCoin').BNCoin
|
const BNCoin = import('types/classes/BNCoin').BNCoin
|
||||||
|
const ActionCoin = import('types/generated').ActionCoin
|
||||||
|
|
||||||
interface BroadcastResult {
|
interface BroadcastResult {
|
||||||
result?: import('@delphi-labs/shuttle-react').TxBroadcastResult
|
result?: import('@delphi-labs/shuttle-react').TxBroadcastResult
|
||||||
@ -41,5 +42,10 @@ interface BroadcastSlice {
|
|||||||
amount: string
|
amount: string
|
||||||
}) => Promise<boolean>
|
}) => Promise<boolean>
|
||||||
withdrawFromVaults: (options: { accountId: string; vaults: DepositedVault[] }) => Promise<boolean>
|
withdrawFromVaults: (options: { accountId: string; vaults: DepositedVault[] }) => Promise<boolean>
|
||||||
withdraw: (options: { accountId: string; coins: BNCoin[]; borrow: BNCoin[] }) => Promise<boolean>
|
withdraw: (options: {
|
||||||
|
accountId: string
|
||||||
|
coins: BNCoin[]
|
||||||
|
borrow: BNCoin[]
|
||||||
|
reclaims: ActionCoin[]
|
||||||
|
}) => Promise<boolean>
|
||||||
}
|
}
|
||||||
|
@ -9,3 +9,5 @@ export const defaultFee: StdFee = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const SECONDS_IN_A_YEAR = 31540000
|
export const SECONDS_IN_A_YEAR = 31540000
|
||||||
|
|
||||||
|
export const LTV_BUFFER = 0.01
|
||||||
|
Loading…
Reference in New Issue
Block a user