fix account creation for USDC

This commit is contained in:
Adrian Brzeziński 2024-03-03 20:52:45 +01:00
parent 436e0de872
commit f56a8a7785
3 changed files with 91 additions and 78 deletions

View File

@ -21,7 +21,7 @@ import useMangoAccountAccounts from 'hooks/useMangoAccountAccounts'
import InlineNotification from './shared/InlineNotification' import InlineNotification from './shared/InlineNotification'
import Link from 'next/link' import Link from 'next/link'
import useMangoGroup from 'hooks/useMangoGroup' import useMangoGroup from 'hooks/useMangoGroup'
import { depositAndCreate } from 'utils/transactions' import { depositAndCreate, getNextAccountNumber } from 'utils/transactions'
// import { MangoAccount } from '@blockworks-foundation/mango-v4' // import { MangoAccount } from '@blockworks-foundation/mango-v4'
import { AnchorProvider } from '@project-serum/anchor' import { AnchorProvider } from '@project-serum/anchor'
import SheenLoader from './shared/SheenLoader' import SheenLoader from './shared/SheenLoader'
@ -125,6 +125,7 @@ function DespositForm({ token: selectedToken }: StakeFormProps) {
const actions = mangoStore.getState().actions const actions = mangoStore.getState().actions
const mangoAccounts = mangoStore.getState().mangoAccounts const mangoAccounts = mangoStore.getState().mangoAccounts
const mangoAccount = mangoStore.getState().mangoAccount.current const mangoAccount = mangoStore.getState().mangoAccount.current
const accNumber = getNextAccountNumber(mangoAccounts)
if (!group || !depositBank || !publicKey) return if (!group || !depositBank || !publicKey) return
@ -142,7 +143,7 @@ function DespositForm({ token: selectedToken }: StakeFormProps) {
mangoAccount, mangoAccount,
depositBank.mint, depositBank.mint,
parseFloat(inputAmount), parseFloat(inputAmount),
mangoAccounts?.length + 1 ?? 0, accNumber ?? 0,
) )
notify({ notify({
title: 'Transaction confirmed', title: 'Transaction confirmed',

View File

@ -31,7 +31,7 @@ import Link from 'next/link'
import LeverageSlider from './shared/LeverageSlider' import LeverageSlider from './shared/LeverageSlider'
import useMangoGroup from 'hooks/useMangoGroup' import useMangoGroup from 'hooks/useMangoGroup'
import FormatNumericValue from './shared/FormatNumericValue' import FormatNumericValue from './shared/FormatNumericValue'
import { stakeAndCreate } from 'utils/transactions' import { getNextAccountNumber, stakeAndCreate } from 'utils/transactions'
// import { MangoAccount } from '@blockworks-foundation/mango-v4' // import { MangoAccount } from '@blockworks-foundation/mango-v4'
import { AnchorProvider } from '@project-serum/anchor' import { AnchorProvider } from '@project-serum/anchor'
import useBankRates from 'hooks/useBankRates' import useBankRates from 'hooks/useBankRates'
@ -101,10 +101,10 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
const { usedTokens, totalTokens } = useMangoAccountAccounts() const { usedTokens, totalTokens } = useMangoAccountAccounts()
const { group } = useMangoGroup() const { group } = useMangoGroup()
const groupLoaded = mangoStore((s) => s.groupLoaded) const groupLoaded = mangoStore((s) => s.groupLoaded)
const { const { financialMetrics, borrowBankBorrowRate } = useBankRates(
financialMetrics, selectedToken,
borrowBankBorrowRate, leverage,
} = useBankRates(selectedToken, leverage) )
const leverageMax = useLeverageMax(selectedToken) * 0.9 // Multiplied by 0.975 becuase you cant actually get to the end of the inifinite geometric series? const leverageMax = useLeverageMax(selectedToken) * 0.9 // Multiplied by 0.975 becuase you cant actually get to the end of the inifinite geometric series?
const stakeBank = useMemo(() => { const stakeBank = useMemo(() => {
@ -120,9 +120,11 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
const borrowMaintLiabWeight = Number(borrowBank?.maintLiabWeight) const borrowMaintLiabWeight = Number(borrowBank?.maintLiabWeight)
const stakeMaintAssetWeight = Number(stakeBank?.maintAssetWeight) const stakeMaintAssetWeight = Number(stakeBank?.maintAssetWeight)
const loanOriginationFee = Number(borrowBank?.loanOriginationFeeRate) const loanOriginationFee = Number(borrowBank?.loanOriginationFeeRate)
const liqPrice = price * const liqPrice =
((borrowMaintLiabWeight * (1 + loanOriginationFee)) / stakeMaintAssetWeight) * price *
(1 - (1 / leverage)) ((borrowMaintLiabWeight * (1 + loanOriginationFee)) /
stakeMaintAssetWeight) *
(1 - 1 / leverage)
return liqPrice.toFixed(3) return liqPrice.toFixed(3)
}, [stakeBank, borrowBank, leverage]) }, [stakeBank, borrowBank, leverage])
@ -197,10 +199,7 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
const actions = mangoStore.getState().actions const actions = mangoStore.getState().actions
const mangoAccount = mangoStore.getState().mangoAccount.current const mangoAccount = mangoStore.getState().mangoAccount.current
const mangoAccounts = mangoStore.getState().mangoAccounts const mangoAccounts = mangoStore.getState().mangoAccounts
const nextAccNumber = const accNumber = getNextAccountNumber(mangoAccounts)
mangoAccounts.reduce((prev, current) => {
return prev.accountNum > current.accountNum ? prev : current
}, mangoAccounts[0])?.accountNum + 1
if (!group || !stakeBank || !publicKey) return if (!group || !stakeBank || !publicKey) return
console.log(mangoAccounts) console.log(mangoAccounts)
@ -220,7 +219,7 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
amountToBorrow, amountToBorrow,
stakeBank.mint, stakeBank.mint,
parseFloat(inputAmount), parseFloat(inputAmount),
mangoAccounts ? nextAccNumber : 0, accNumber ?? 0,
) )
notify({ notify({
title: 'Transaction confirmed', title: 'Transaction confirmed',
@ -430,7 +429,6 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
</div> </div>
</Disclosure.Button> </Disclosure.Button>
<Disclosure.Panel className="space-y-2 rounded-xl rounded-t-none border-2 border-t-0 border-th-bkg-3 px-4 pb-3"> <Disclosure.Panel className="space-y-2 rounded-xl rounded-t-none border-2 border-t-0 border-th-bkg-3 px-4 pb-3">
<div className="flex justify-between"> <div className="flex justify-between">
<p className="text-th-fgd-4">{`${stakeBank.name} Position`}</p> <p className="text-th-fgd-4">{`${stakeBank.name} Position`}</p>
<span <span
@ -487,7 +485,9 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
{formatTokenSymbol(selectedToken)} Returns APY {formatTokenSymbol(selectedToken)} Returns APY
</p> </p>
<span className="font-bold text-th-success"> <span className="font-bold text-th-success">
{financialMetrics.collectedReturnsAPY > 0.01 ? '+' : ''} {financialMetrics.collectedReturnsAPY > 0.01
? '+'
: ''}
<FormatNumericValue <FormatNumericValue
value={financialMetrics.collectedReturnsAPY} value={financialMetrics.collectedReturnsAPY}
decimals={2} decimals={2}

View File

@ -3,7 +3,6 @@ import {
Bank, Bank,
FlashLoanType, FlashLoanType,
Group, Group,
I80F48,
MangoAccount, MangoAccount,
MangoClient, MangoClient,
MangoSignatureStatus, MangoSignatureStatus,
@ -845,3 +844,16 @@ const tokenWithdrawNativeIx = async (
return [...preInstructions, ix, ...postInstructions] return [...preInstructions, ix, ...postInstructions]
} }
export const getNextAccountNumber = (accounts: MangoAccount[]): number => {
if (accounts.length > 1) {
return (
accounts
.map((a) => a.accountNum)
.reduce((a, b) => Math.max(a, b), -Infinity) + 1
)
} else if (accounts.length === 1) {
return accounts[0].accountNum + 1
}
return 0
}