2022-09-02 11:36:57 -07:00
|
|
|
import { Bank, Group, MangoAccount } from '@blockworks-foundation/mango-v4'
|
|
|
|
import Decimal from 'decimal.js'
|
2022-08-26 10:17:31 -07:00
|
|
|
import { useMemo } from 'react'
|
2022-09-12 08:53:57 -07:00
|
|
|
import mangoStore from '@store/mangoStore'
|
2022-09-02 16:52:07 -07:00
|
|
|
import { floorToDecimal } from '../../utils/numbers'
|
2022-11-18 09:09:39 -08:00
|
|
|
import useMangoAccount from '../../hooks/useMangoAccount'
|
2022-11-20 12:32:38 -08:00
|
|
|
import useMangoGroup from 'hooks/useMangoGroup'
|
2022-12-12 21:38:12 -08:00
|
|
|
import { PublicKey } from '@solana/web3.js'
|
2022-08-26 10:17:31 -07:00
|
|
|
|
2022-09-02 11:36:57 -07:00
|
|
|
export const getMaxWithdrawForBank = (
|
|
|
|
group: Group,
|
|
|
|
bank: Bank,
|
|
|
|
mangoAccount: MangoAccount,
|
2022-11-19 11:20:36 -08:00
|
|
|
allowBorrow = false
|
2022-09-02 11:36:57 -07:00
|
|
|
): Decimal => {
|
2022-11-19 11:20:36 -08:00
|
|
|
const accountBalance = mangoAccount.getTokenBalanceUi(bank)
|
2022-09-02 11:36:57 -07:00
|
|
|
const vaultBalance = group.getTokenVaultBalanceByMintUi(bank.mint)
|
2022-11-19 11:20:36 -08:00
|
|
|
const maxBorrow = mangoAccount.getMaxWithdrawWithBorrowForTokenUi(
|
2022-10-28 12:24:05 -07:00
|
|
|
group,
|
|
|
|
bank.mint
|
2022-11-19 11:20:36 -08:00
|
|
|
)
|
2022-10-07 15:27:36 -07:00
|
|
|
const maxWithdraw = allowBorrow
|
2022-11-19 11:20:36 -08:00
|
|
|
? Decimal.min(vaultBalance, maxBorrow)
|
|
|
|
: Decimal.min(accountBalance, vaultBalance, maxBorrow)
|
2022-10-07 15:27:36 -07:00
|
|
|
return Decimal.max(0, maxWithdraw)
|
2022-09-02 11:36:57 -07:00
|
|
|
}
|
|
|
|
|
2022-09-14 17:02:16 -07:00
|
|
|
export const getTokenInMax = (
|
2022-09-22 14:35:07 -07:00
|
|
|
mangoAccount: MangoAccount,
|
2022-12-12 21:38:12 -08:00
|
|
|
inputMint: PublicKey,
|
|
|
|
outputMint: PublicKey,
|
2022-09-14 17:02:16 -07:00
|
|
|
group: Group,
|
|
|
|
useMargin: boolean
|
|
|
|
) => {
|
2022-12-12 21:38:12 -08:00
|
|
|
const inputBank = group.getFirstBankByMint(inputMint)
|
|
|
|
const outputBank = group.getFirstBankByMint(outputMint)
|
2022-09-14 17:02:16 -07:00
|
|
|
|
|
|
|
if (!group || !inputBank || !mangoAccount || !outputBank) {
|
|
|
|
return {
|
|
|
|
amount: new Decimal(0.0),
|
|
|
|
decimals: 6,
|
|
|
|
amountWithBorrow: new Decimal(0.0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const inputTokenBalance = floorToDecimal(
|
|
|
|
mangoAccount.getTokenBalanceUi(inputBank),
|
|
|
|
inputBank.mintDecimals
|
|
|
|
)
|
|
|
|
const maxAmountWithoutMargin = inputTokenBalance.gt(0)
|
|
|
|
? inputTokenBalance
|
|
|
|
: new Decimal(0)
|
2022-11-21 20:40:55 -08:00
|
|
|
|
2022-09-14 17:02:16 -07:00
|
|
|
const maxUiAmountWithBorrow = floorToDecimal(
|
2022-11-19 11:20:36 -08:00
|
|
|
mangoAccount.getMaxSourceUiForTokenSwap(
|
2022-09-14 17:02:16 -07:00
|
|
|
group,
|
|
|
|
inputBank.mint,
|
|
|
|
outputBank.mint,
|
2022-12-14 08:20:36 -08:00
|
|
|
inputBank.uiPrice / outputBank.uiPrice
|
2022-11-19 11:20:36 -08:00
|
|
|
),
|
2022-09-14 17:02:16 -07:00
|
|
|
inputBank.mintDecimals
|
|
|
|
)
|
2022-11-21 20:40:55 -08:00
|
|
|
|
2022-12-14 12:54:25 -08:00
|
|
|
console.log(
|
|
|
|
'getMaxSourceUiForTokenSwap',
|
|
|
|
mangoAccount.getMaxSourceUiForTokenSwap(
|
|
|
|
group,
|
|
|
|
inputBank.mint,
|
|
|
|
outputBank.mint,
|
|
|
|
inputBank.uiPrice / outputBank.uiPrice
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-11-21 20:40:55 -08:00
|
|
|
const inputBankVaultBalance = floorToDecimal(
|
|
|
|
group.getTokenVaultBalanceByMintUi(inputBank.mint),
|
|
|
|
inputBank.mintDecimals
|
2022-09-14 17:02:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const maxAmount = useMargin
|
|
|
|
? Decimal.min(
|
|
|
|
maxAmountWithoutMargin,
|
|
|
|
inputBankVaultBalance,
|
2022-11-20 12:32:38 -08:00
|
|
|
maxUiAmountWithBorrow
|
2022-09-14 17:02:16 -07:00
|
|
|
)
|
|
|
|
: Decimal.min(maxAmountWithoutMargin, inputBankVaultBalance)
|
|
|
|
|
|
|
|
const maxAmountWithBorrow = Decimal.min(
|
2022-11-20 12:32:38 -08:00
|
|
|
maxUiAmountWithBorrow,
|
2022-09-14 17:02:16 -07:00
|
|
|
inputBankVaultBalance
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
amount: maxAmount,
|
|
|
|
amountWithBorrow: maxAmountWithBorrow,
|
|
|
|
decimals: inputBank.mintDecimals,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 10:17:31 -07:00
|
|
|
export const useTokenMax = (useMargin = true) => {
|
2022-09-02 16:52:07 -07:00
|
|
|
const { mangoAccount } = useMangoAccount()
|
2022-11-20 12:32:38 -08:00
|
|
|
const { group } = useMangoGroup()
|
2022-08-26 10:17:31 -07:00
|
|
|
const inputBank = mangoStore((s) => s.swap.inputBank)
|
2022-12-12 21:38:12 -08:00
|
|
|
const outputBank = mangoStore((s) => s.swap.outputBank)
|
2022-08-26 10:17:31 -07:00
|
|
|
|
|
|
|
const tokenInMax = useMemo(() => {
|
2022-12-12 21:38:12 -08:00
|
|
|
if (mangoAccount && group && inputBank && outputBank) {
|
2022-09-22 14:35:07 -07:00
|
|
|
return getTokenInMax(
|
|
|
|
mangoAccount,
|
2022-12-12 21:38:12 -08:00
|
|
|
inputBank.mint,
|
|
|
|
outputBank.mint,
|
2022-09-02 11:36:57 -07:00
|
|
|
group,
|
2022-09-22 14:35:07 -07:00
|
|
|
useMargin
|
|
|
|
)
|
|
|
|
}
|
2022-08-26 10:17:31 -07:00
|
|
|
|
|
|
|
return {
|
2022-09-22 14:35:07 -07:00
|
|
|
amount: new Decimal(0),
|
|
|
|
amountWithBorrow: new Decimal(0),
|
|
|
|
decimals: 6,
|
2022-08-26 10:17:31 -07:00
|
|
|
}
|
2022-12-12 21:38:12 -08:00
|
|
|
}, [mangoAccount, group, useMargin, inputBank, outputBank])
|
2022-08-26 10:17:31 -07:00
|
|
|
|
|
|
|
return tokenInMax
|
|
|
|
}
|