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 usePrice from 'hooks/usePrice'
|
||||||
import useStore from 'store'
|
import useStore from 'store'
|
||||||
import { BNCoin } from 'types/classes/BNCoin'
|
import { BNCoin } from 'types/classes/BNCoin'
|
||||||
import { getAmount } from 'utils/accounts'
|
import { accumulateAmounts, getAmount } from 'utils/accounts'
|
||||||
import { findCoinByDenom } from 'utils/assets'
|
import { findCoinByDenom } from 'utils/assets'
|
||||||
import { BN } from 'utils/helpers'
|
import { BN } from 'utils/helpers'
|
||||||
|
|
||||||
@ -32,8 +32,15 @@ interface Props {
|
|||||||
export default function VaultDeposit(props: Props) {
|
export default function VaultDeposit(props: Props) {
|
||||||
const { deposits, primaryAsset, secondaryAsset, account, onChangeDeposits } = props
|
const { deposits, primaryAsset, secondaryAsset, account, onChangeDeposits } = props
|
||||||
const baseCurrency = useStore((s) => s.baseCurrency)
|
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 primaryPrice = usePrice(primaryAsset.denom)
|
||||||
const secondaryPrice = usePrice(secondaryAsset.denom)
|
const secondaryPrice = usePrice(secondaryAsset.denom)
|
||||||
|
|
||||||
|
@ -10,6 +10,8 @@ import { BN_ZERO } from 'constants/math'
|
|||||||
import useDepositVault from 'hooks/broadcast/useDepositVault'
|
import useDepositVault from 'hooks/broadcast/useDepositVault'
|
||||||
import useIsOpenArray from 'hooks/useIsOpenArray'
|
import useIsOpenArray from 'hooks/useIsOpenArray'
|
||||||
import { useUpdatedAccount } from 'hooks/useUpdatedAccount'
|
import { useUpdatedAccount } from 'hooks/useUpdatedAccount'
|
||||||
|
import { byDenom } from 'utils/array'
|
||||||
|
import { BNCoin } from 'types/classes/BNCoin'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
vault: Vault | DepositedVault
|
vault: Vault | DepositedVault
|
||||||
@ -20,18 +22,58 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function VaultModalContent(props: Props) {
|
export default function VaultModalContent(props: Props) {
|
||||||
const { addDebt, removeDeposits, addedDebt, removedDeposits, updatedAccount, addVaultValues } =
|
const {
|
||||||
useUpdatedAccount(props.account)
|
addDebt,
|
||||||
|
removeDeposits,
|
||||||
|
addedDebt,
|
||||||
|
removedDeposits,
|
||||||
|
removedLends,
|
||||||
|
setRemovedLends,
|
||||||
|
updatedAccount,
|
||||||
|
addVaultValues,
|
||||||
|
} = useUpdatedAccount(props.account)
|
||||||
|
|
||||||
const [isOpen, toggleOpen] = useIsOpenArray(2, false)
|
const [isOpen, toggleOpen] = useIsOpenArray(2, false)
|
||||||
const [isCustomRatio, setIsCustomRatio] = useState(false)
|
const [isCustomRatio, setIsCustomRatio] = useState(false)
|
||||||
|
const [selectedCoins, setSelectedCoins] = useState<BNCoin[]>([])
|
||||||
|
|
||||||
const { actions: depositActions, totalValue } = useDepositVault({
|
const { actions: depositActions, totalValue } = useDepositVault({
|
||||||
vault: props.vault,
|
vault: props.vault,
|
||||||
|
reclaims: removedLends,
|
||||||
deposits: removedDeposits,
|
deposits: removedDeposits,
|
||||||
borrowings: addedDebt,
|
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(() => {
|
useEffect(() => {
|
||||||
addVaultValues([
|
addVaultValues([
|
||||||
{
|
{
|
||||||
@ -84,8 +126,8 @@ export default function VaultModalContent(props: Props) {
|
|||||||
{
|
{
|
||||||
renderContent: () => (
|
renderContent: () => (
|
||||||
<VaultDeposit
|
<VaultDeposit
|
||||||
deposits={removedDeposits}
|
deposits={selectedCoins}
|
||||||
onChangeDeposits={removeDeposits}
|
onChangeDeposits={handleDepositSelect}
|
||||||
primaryAsset={props.primaryAsset}
|
primaryAsset={props.primaryAsset}
|
||||||
secondaryAsset={props.secondaryAsset}
|
secondaryAsset={props.secondaryAsset}
|
||||||
account={props.account}
|
account={props.account}
|
||||||
|
@ -17,6 +17,7 @@ import { BN_ZERO } from 'constants/math'
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
vault: Vault
|
vault: Vault
|
||||||
|
reclaims: BNCoin[]
|
||||||
deposits: BNCoin[]
|
deposits: BNCoin[]
|
||||||
borrowings: BNCoin[]
|
borrowings: BNCoin[]
|
||||||
}
|
}
|
||||||
@ -45,6 +46,12 @@ export default function useDepositVault(props: Props): {
|
|||||||
[deposits, borrowings, props.vault, prices],
|
[deposits, borrowings, props.vault, prices],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const reclaimActions: Action[] = useMemo(() => {
|
||||||
|
return props.reclaims.map((bnCoin) => ({
|
||||||
|
reclaim: bnCoin.toActionCoin(),
|
||||||
|
}))
|
||||||
|
}, [props.reclaims])
|
||||||
|
|
||||||
const borrowActions: Action[] = useMemo(() => {
|
const borrowActions: Action[] = useMemo(() => {
|
||||||
return borrowings.map((bnCoin) => ({
|
return borrowings.map((bnCoin) => ({
|
||||||
borrow: bnCoin.toCoin(),
|
borrow: bnCoin.toCoin(),
|
||||||
@ -84,8 +91,8 @@ export default function useDepositVault(props: Props): {
|
|||||||
}, [props.vault, primaryCoin, secondaryCoin, minLpToReceive])
|
}, [props.vault, primaryCoin, secondaryCoin, minLpToReceive])
|
||||||
|
|
||||||
const actions = useMemo(
|
const actions = useMemo(
|
||||||
() => [...borrowActions, ...swapActions, ...enterVaultActions],
|
() => [...reclaimActions, ...borrowActions, ...swapActions, ...enterVaultActions],
|
||||||
[borrowActions, swapActions, enterVaultActions],
|
[reclaimActions, borrowActions, swapActions, enterVaultActions],
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -77,7 +77,9 @@ export function useUpdatedAccount(account?: Account) {
|
|||||||
addedDebt,
|
addedDebt,
|
||||||
removedDeposits,
|
removedDeposits,
|
||||||
removedDebt,
|
removedDebt,
|
||||||
|
addedLends,
|
||||||
setAddedLends,
|
setAddedLends,
|
||||||
|
removedLends,
|
||||||
setRemovedLends,
|
setRemovedLends,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,6 +132,10 @@ export function getAmount(denom: string, coins: Coin[]): BigNumber {
|
|||||||
return BN(coins.find((asset) => asset.denom === denom)?.amount ?? 0)
|
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 {
|
export function convertAccountToPositions(account: Account): Positions {
|
||||||
return {
|
return {
|
||||||
account_id: account.id,
|
account_id: account.id,
|
||||||
|
Loading…
Reference in New Issue
Block a user