feat(vaults): included auto-lent amounts to the account balances (#394)
This commit is contained in:
parent
eeb49ba2ab
commit
63aba423b2
@ -14,7 +14,7 @@ import { BN_ZERO } from 'constants/math'
|
||||
import usePrice from 'hooks/usePrice'
|
||||
import useStore from 'store'
|
||||
import { BNCoin } from 'types/classes/BNCoin'
|
||||
import { getAmount } from 'utils/accounts'
|
||||
import { accumulateAmounts, getAmount } from 'utils/accounts'
|
||||
import { findCoinByDenom } from 'utils/assets'
|
||||
import { BN } from 'utils/helpers'
|
||||
|
||||
@ -32,8 +32,15 @@ interface Props {
|
||||
export default function VaultDeposit(props: Props) {
|
||||
const { deposits, primaryAsset, secondaryAsset, account, onChangeDeposits } = props
|
||||
const baseCurrency = useStore((s) => s.baseCurrency)
|
||||
const availablePrimaryAmount = getAmount(primaryAsset.denom, account.deposits)
|
||||
const availableSecondaryAmount = getAmount(secondaryAsset.denom, account.deposits)
|
||||
|
||||
const [availablePrimaryAmount, availableSecondaryAmount] = useMemo(
|
||||
() => [
|
||||
accumulateAmounts(primaryAsset.denom, [...account.deposits, ...account.lends]),
|
||||
accumulateAmounts(secondaryAsset.denom, [...account.deposits, ...account.lends]),
|
||||
],
|
||||
[account.deposits, account.lends, primaryAsset.denom, secondaryAsset.denom],
|
||||
)
|
||||
|
||||
const primaryPrice = usePrice(primaryAsset.denom)
|
||||
const secondaryPrice = usePrice(secondaryAsset.denom)
|
||||
|
||||
|
@ -10,6 +10,8 @@ import { BN_ZERO } from 'constants/math'
|
||||
import useDepositVault from 'hooks/broadcast/useDepositVault'
|
||||
import useIsOpenArray from 'hooks/useIsOpenArray'
|
||||
import { useUpdatedAccount } from 'hooks/useUpdatedAccount'
|
||||
import { byDenom } from 'utils/array'
|
||||
import { BNCoin } from 'types/classes/BNCoin'
|
||||
|
||||
interface Props {
|
||||
vault: Vault | DepositedVault
|
||||
@ -20,18 +22,58 @@ interface Props {
|
||||
}
|
||||
|
||||
export default function VaultModalContent(props: Props) {
|
||||
const { addDebt, removeDeposits, addedDebt, removedDeposits, updatedAccount, addVaultValues } =
|
||||
useUpdatedAccount(props.account)
|
||||
const {
|
||||
addDebt,
|
||||
removeDeposits,
|
||||
addedDebt,
|
||||
removedDeposits,
|
||||
removedLends,
|
||||
setRemovedLends,
|
||||
updatedAccount,
|
||||
addVaultValues,
|
||||
} = useUpdatedAccount(props.account)
|
||||
|
||||
const [isOpen, toggleOpen] = useIsOpenArray(2, false)
|
||||
const [isCustomRatio, setIsCustomRatio] = useState(false)
|
||||
const [selectedCoins, setSelectedCoins] = useState<BNCoin[]>([])
|
||||
|
||||
const { actions: depositActions, totalValue } = useDepositVault({
|
||||
vault: props.vault,
|
||||
reclaims: removedLends,
|
||||
deposits: removedDeposits,
|
||||
borrowings: addedDebt,
|
||||
})
|
||||
|
||||
const handleDepositSelect = useCallback(
|
||||
(selectedCoins: BNCoin[]) => {
|
||||
const reclaims: BNCoin[] = []
|
||||
const deposits: BNCoin[] = []
|
||||
|
||||
selectedCoins.forEach((selectedCoin) => {
|
||||
const { denom, amount: selectedAmount } = selectedCoin
|
||||
const accountDepositForSelectedCoin: BigNumber =
|
||||
props.account.deposits.find(byDenom(denom))?.amount ?? BN_ZERO
|
||||
|
||||
if (selectedAmount.gt(accountDepositForSelectedCoin)) {
|
||||
reclaims.push(
|
||||
BNCoin.fromDenomAndBigNumber(
|
||||
denom,
|
||||
selectedAmount.minus(accountDepositForSelectedCoin),
|
||||
),
|
||||
)
|
||||
deposits.push(BNCoin.fromDenomAndBigNumber(denom, accountDepositForSelectedCoin))
|
||||
} else {
|
||||
deposits.push(selectedCoin)
|
||||
}
|
||||
})
|
||||
|
||||
setRemovedLends(reclaims)
|
||||
removeDeposits(deposits)
|
||||
setSelectedCoins(selectedCoins)
|
||||
},
|
||||
[props.account.deposits, removeDeposits, setRemovedLends],
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
addVaultValues([
|
||||
{
|
||||
@ -84,8 +126,8 @@ export default function VaultModalContent(props: Props) {
|
||||
{
|
||||
renderContent: () => (
|
||||
<VaultDeposit
|
||||
deposits={removedDeposits}
|
||||
onChangeDeposits={removeDeposits}
|
||||
deposits={selectedCoins}
|
||||
onChangeDeposits={handleDepositSelect}
|
||||
primaryAsset={props.primaryAsset}
|
||||
secondaryAsset={props.secondaryAsset}
|
||||
account={props.account}
|
||||
|
@ -17,6 +17,7 @@ import { BN_ZERO } from 'constants/math'
|
||||
|
||||
interface Props {
|
||||
vault: Vault
|
||||
reclaims: BNCoin[]
|
||||
deposits: BNCoin[]
|
||||
borrowings: BNCoin[]
|
||||
}
|
||||
@ -45,6 +46,12 @@ export default function useDepositVault(props: Props): {
|
||||
[deposits, borrowings, props.vault, prices],
|
||||
)
|
||||
|
||||
const reclaimActions: Action[] = useMemo(() => {
|
||||
return props.reclaims.map((bnCoin) => ({
|
||||
reclaim: bnCoin.toActionCoin(),
|
||||
}))
|
||||
}, [props.reclaims])
|
||||
|
||||
const borrowActions: Action[] = useMemo(() => {
|
||||
return borrowings.map((bnCoin) => ({
|
||||
borrow: bnCoin.toCoin(),
|
||||
@ -84,8 +91,8 @@ export default function useDepositVault(props: Props): {
|
||||
}, [props.vault, primaryCoin, secondaryCoin, minLpToReceive])
|
||||
|
||||
const actions = useMemo(
|
||||
() => [...borrowActions, ...swapActions, ...enterVaultActions],
|
||||
[borrowActions, swapActions, enterVaultActions],
|
||||
() => [...reclaimActions, ...borrowActions, ...swapActions, ...enterVaultActions],
|
||||
[reclaimActions, borrowActions, swapActions, enterVaultActions],
|
||||
)
|
||||
|
||||
return {
|
||||
|
@ -77,7 +77,9 @@ export function useUpdatedAccount(account?: Account) {
|
||||
addedDebt,
|
||||
removedDeposits,
|
||||
removedDebt,
|
||||
addedLends,
|
||||
setAddedLends,
|
||||
removedLends,
|
||||
setRemovedLends,
|
||||
}
|
||||
}
|
||||
|
@ -132,6 +132,10 @@ export function getAmount(denom: string, coins: Coin[]): BigNumber {
|
||||
return BN(coins.find((asset) => asset.denom === denom)?.amount ?? 0)
|
||||
}
|
||||
|
||||
export function accumulateAmounts(denom: string, coins: BNCoin[]): BigNumber {
|
||||
return coins.reduce((acc, coin) => acc.plus(getAmount(denom, [coin.toCoin()])), BN_ZERO)
|
||||
}
|
||||
|
||||
export function convertAccountToPositions(account: Account): Positions {
|
||||
return {
|
||||
account_id: account.id,
|
||||
|
Loading…
Reference in New Issue
Block a user