[Health Computer]: update wasm + support borrow to wallet (#318)
* [Health Computer]: update wasm + support borrow to wallet * [test]: fixed tests for account details
This commit is contained in:
parent
0aa3bb0c5f
commit
fef9227a0d
@ -5,6 +5,11 @@ import useCurrentAccount from 'hooks/useCurrentAccount'
|
|||||||
import useStore from 'store'
|
import useStore from 'store'
|
||||||
|
|
||||||
jest.mock('hooks/useCurrentAccount', () => jest.fn(() => null))
|
jest.mock('hooks/useCurrentAccount', () => jest.fn(() => null))
|
||||||
|
jest.mock('hooks/useHealthComputer', () =>
|
||||||
|
jest.fn(() => ({
|
||||||
|
health: 0,
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
|
||||||
const mockedUseCurrentAccount = useCurrentAccount as jest.Mock
|
const mockedUseCurrentAccount = useCurrentAccount as jest.Mock
|
||||||
|
|
||||||
|
@ -2,7 +2,9 @@ import { Gauge } from 'components/Gauge'
|
|||||||
import { Heart } from 'components/Icons'
|
import { Heart } from 'components/Icons'
|
||||||
import Text from 'components/Text'
|
import Text from 'components/Text'
|
||||||
import useCurrentAccount from 'hooks/useCurrentAccount'
|
import useCurrentAccount from 'hooks/useCurrentAccount'
|
||||||
|
import useHealthComputer from 'hooks/useHealthComputer'
|
||||||
import useStore from 'store'
|
import useStore from 'store'
|
||||||
|
import { formatHealth } from 'utils/formatters'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
account: Account
|
account: Account
|
||||||
@ -18,6 +20,7 @@ export default function AccountDetailsController() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function AccountDetails(props: Props) {
|
function AccountDetails(props: Props) {
|
||||||
|
const { health } = useHealthComputer(props.account)
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
data-testid='account-details'
|
data-testid='account-details'
|
||||||
@ -29,7 +32,7 @@ function AccountDetails(props: Props) {
|
|||||||
Health
|
Health
|
||||||
</Text>
|
</Text>
|
||||||
<Text size='xs' className='w-full text-center'>
|
<Text size='xs' className='w-full text-center'>
|
||||||
89%
|
{formatHealth(health)}
|
||||||
</Text>
|
</Text>
|
||||||
</div>
|
</div>
|
||||||
<div className='w-full border border-x-0 border-white/20 py-4'>
|
<div className='w-full border border-x-0 border-white/20 py-4'>
|
||||||
|
@ -20,6 +20,7 @@ import { hardcodedFee } from 'utils/constants'
|
|||||||
import { formatPercent, formatValue } from 'utils/formatters'
|
import { formatPercent, formatValue } from 'utils/formatters'
|
||||||
import { BN } from 'utils/helpers'
|
import { BN } from 'utils/helpers'
|
||||||
import useHealthComputer from 'hooks/useHealthComputer'
|
import useHealthComputer from 'hooks/useHealthComputer'
|
||||||
|
import { BorrowTarget } from 'types/enums/borrowTarget'
|
||||||
|
|
||||||
function getDebtAmount(modal: BorrowModal | null) {
|
function getDebtAmount(modal: BorrowModal | null) {
|
||||||
return BN((modal?.marketData as BorrowMarketTableData)?.debt ?? 0).toString()
|
return BN((modal?.marketData as BorrowMarketTableData)?.debt ?? 0).toString()
|
||||||
@ -110,10 +111,13 @@ function BorrowModal(props: Props) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
computeMaxBorrowAmount(asset.denom).then((maxBorrowAmount) => {
|
computeMaxBorrowAmount(
|
||||||
|
asset.denom,
|
||||||
|
borrowToWallet ? BorrowTarget.Wallet : BorrowTarget.Deposit,
|
||||||
|
).then((maxBorrowAmount) => {
|
||||||
setMax(BN(Math.min(maxBorrowAmount, modal?.marketData?.liquidity?.amount.toNumber() || 0)))
|
setMax(BN(Math.min(maxBorrowAmount, modal?.marketData?.liquidity?.amount.toNumber() || 0)))
|
||||||
})
|
})
|
||||||
}, [isRepay, modal, asset.denom, computeMaxBorrowAmount])
|
}, [isRepay, modal, asset.denom, computeMaxBorrowAmount, borrowToWallet])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!modal?.asset) return
|
if (!modal?.asset) return
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useMemo } from 'react'
|
import { useCallback, useEffect, useMemo, useState } from 'react'
|
||||||
|
|
||||||
import usePrices from 'hooks/usePrices'
|
import usePrices from 'hooks/usePrices'
|
||||||
import useAssetParams from 'hooks/useAssetParams'
|
import useAssetParams from 'hooks/useAssetParams'
|
||||||
@ -16,6 +16,7 @@ import {
|
|||||||
import { convertAccountToPositions } from 'utils/accounts'
|
import { convertAccountToPositions } from 'utils/accounts'
|
||||||
import { VaultPositionValue } from 'types/generated/mars-credit-manager/MarsCreditManager.types'
|
import { VaultPositionValue } from 'types/generated/mars-credit-manager/MarsCreditManager.types'
|
||||||
import useStore from 'store'
|
import useStore from 'store'
|
||||||
|
import { BorrowTarget } from 'types/enums/borrowTarget'
|
||||||
|
|
||||||
export default function useHealthComputer(account: Account) {
|
export default function useHealthComputer(account: Account) {
|
||||||
const { data: prices } = usePrices()
|
const { data: prices } = usePrices()
|
||||||
@ -23,6 +24,7 @@ export default function useHealthComputer(account: Account) {
|
|||||||
const { data: vaultConfigs } = useVaultConfigs()
|
const { data: vaultConfigs } = useVaultConfigs()
|
||||||
const baseCurrency = useStore((s) => s.baseCurrency)
|
const baseCurrency = useStore((s) => s.baseCurrency)
|
||||||
|
|
||||||
|
const [health, setHealth] = useState(0)
|
||||||
const positions = useMemo(() => convertAccountToPositions(account), [account])
|
const positions = useMemo(() => convertAccountToPositions(account), [account])
|
||||||
const baseCurrencyPrice = useMemo(
|
const baseCurrencyPrice = useMemo(
|
||||||
() => prices.find((price) => price.denom === baseCurrency.denom)?.amount || 0,
|
() => prices.find((price) => price.denom === baseCurrency.denom)?.amount || 0,
|
||||||
@ -120,20 +122,19 @@ export default function useHealthComputer(account: Account) {
|
|||||||
}
|
}
|
||||||
}, [priceData, denomsData, vaultConfigsData, vaultPositionValues, positions])
|
}, [priceData, denomsData, vaultConfigsData, vaultPositionValues, positions])
|
||||||
|
|
||||||
const computeHealth = useCallback(() => {
|
useEffect(() => {
|
||||||
async function callComputeHealthWasmFn(): Promise<number> {
|
async function computeHealthWasm() {
|
||||||
if (!healthComputer) return 0
|
if (!healthComputer) return
|
||||||
return Number((await compute_health_js(healthComputer)).max_ltv_health_factor) || 0
|
setHealth(Number((await compute_health_js(healthComputer)).max_ltv_health_factor) || 0)
|
||||||
}
|
}
|
||||||
|
computeHealthWasm()
|
||||||
return callComputeHealthWasmFn()
|
|
||||||
}, [healthComputer])
|
}, [healthComputer])
|
||||||
|
|
||||||
const computeMaxBorrowAmount = useCallback(
|
const computeMaxBorrowAmount = useCallback(
|
||||||
(denom: string) => {
|
(denom: string, target: BorrowTarget) => {
|
||||||
async function callMaxBorrowWasmFn(denom: string): Promise<number> {
|
async function callMaxBorrowWasmFn(denom: string): Promise<number> {
|
||||||
if (!healthComputer) return 0
|
if (!healthComputer) return 0
|
||||||
return await max_borrow_estimate_js(healthComputer, denom)
|
return await max_borrow_estimate_js(healthComputer, denom, target)
|
||||||
}
|
}
|
||||||
|
|
||||||
return callMaxBorrowWasmFn(denom)
|
return callMaxBorrowWasmFn(denom)
|
||||||
@ -153,5 +154,5 @@ export default function useHealthComputer(account: Account) {
|
|||||||
[healthComputer],
|
[healthComputer],
|
||||||
)
|
)
|
||||||
|
|
||||||
return { computeHealth, computeMaxBorrowAmount, computeMaxWithdrawAmount }
|
return { health, computeMaxBorrowAmount, computeMaxWithdrawAmount }
|
||||||
}
|
}
|
||||||
|
4
src/types/enums/borrowTarget.ts
Normal file
4
src/types/enums/borrowTarget.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export enum BorrowTarget {
|
||||||
|
Deposit = 'deposit',
|
||||||
|
Wallet = 'wallet',
|
||||||
|
}
|
@ -123,6 +123,12 @@ export const formatValue = (amount: number | string, options?: FormatOptions): s
|
|||||||
|
|
||||||
return returnValue
|
return returnValue
|
||||||
}
|
}
|
||||||
|
export function formatHealth(health: number) {
|
||||||
|
return formatValue(health, {
|
||||||
|
minDecimals: 0,
|
||||||
|
maxDecimals: 2,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export function formatLeverage(leverage: number) {
|
export function formatLeverage(leverage: number) {
|
||||||
return formatValue(leverage, {
|
return formatValue(leverage, {
|
||||||
|
7
src/utils/health_computer/index.d.ts
vendored
7
src/utils/health_computer/index.d.ts
vendored
@ -8,15 +8,16 @@ export function compute_health_js(health_computer: any): any
|
|||||||
/**
|
/**
|
||||||
* @param {any} health_computer
|
* @param {any} health_computer
|
||||||
* @param {any} withdraw_denom
|
* @param {any} withdraw_denom
|
||||||
* @returns {HealthResponse}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
export function max_withdraw_estimate_js(health_computer: any, withdraw_denom: any): any
|
export function max_withdraw_estimate_js(health_computer: any, withdraw_denom: any): any
|
||||||
/**
|
/**
|
||||||
* @param {any} health_computer
|
* @param {any} health_computer
|
||||||
* @param {any} borrow_denom
|
* @param {any} borrow_denom
|
||||||
|
* @param {any} target
|
||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
export function max_borrow_estimate_js(health_computer: any, borrow_denom: any): any
|
export function max_borrow_estimate_js(health_computer: any, borrow_denom: any, target: any): any
|
||||||
|
|
||||||
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module
|
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module
|
||||||
|
|
||||||
@ -24,7 +25,7 @@ export interface InitOutput {
|
|||||||
readonly memory: WebAssembly.Memory
|
readonly memory: WebAssembly.Memory
|
||||||
readonly compute_health_js: (a: number) => number
|
readonly compute_health_js: (a: number) => number
|
||||||
readonly max_withdraw_estimate_js: (a: number, b: number) => number
|
readonly max_withdraw_estimate_js: (a: number, b: number) => number
|
||||||
readonly max_borrow_estimate_js: (a: number, b: number) => number
|
readonly max_borrow_estimate_js: (a: number, b: number, c: number) => number
|
||||||
readonly allocate: (a: number) => number
|
readonly allocate: (a: number) => number
|
||||||
readonly deallocate: (a: number) => void
|
readonly deallocate: (a: number) => void
|
||||||
readonly requires_stargate: () => void
|
readonly requires_stargate: () => void
|
||||||
|
@ -242,12 +242,14 @@ export function max_withdraw_estimate_js(health_computer, withdraw_denom) {
|
|||||||
/**
|
/**
|
||||||
* @param {any} health_computer
|
* @param {any} health_computer
|
||||||
* @param {any} borrow_denom
|
* @param {any} borrow_denom
|
||||||
|
* @param {any} target
|
||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
export function max_borrow_estimate_js(health_computer, borrow_denom) {
|
export function max_borrow_estimate_js(health_computer, borrow_denom, target) {
|
||||||
const ret = wasm.max_borrow_estimate_js(
|
const ret = wasm.max_borrow_estimate_js(
|
||||||
addHeapObject(health_computer),
|
addHeapObject(health_computer),
|
||||||
addHeapObject(borrow_denom),
|
addHeapObject(borrow_denom),
|
||||||
|
addHeapObject(target),
|
||||||
)
|
)
|
||||||
return takeObject(ret)
|
return takeObject(ret)
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
2
src/utils/health_computer/index_bg.wasm.d.ts
vendored
2
src/utils/health_computer/index_bg.wasm.d.ts
vendored
@ -3,7 +3,7 @@
|
|||||||
export const memory: WebAssembly.Memory
|
export const memory: WebAssembly.Memory
|
||||||
export function compute_health_js(a: number): number
|
export function compute_health_js(a: number): number
|
||||||
export function max_withdraw_estimate_js(a: number, b: number): number
|
export function max_withdraw_estimate_js(a: number, b: number): number
|
||||||
export function max_borrow_estimate_js(a: number, b: number): number
|
export function max_borrow_estimate_js(a: number, b: number, c: number): number
|
||||||
export function allocate(a: number): number
|
export function allocate(a: number): number
|
||||||
export function deallocate(a: number): void
|
export function deallocate(a: number): void
|
||||||
export function requires_stargate(): void
|
export function requires_stargate(): void
|
||||||
|
Loading…
Reference in New Issue
Block a user