mango-ui-v3/utils/mango.tsx

67 lines
1.9 KiB
TypeScript
Raw Normal View History

import { MangoAccount, TokenAccount } from '@blockworks-foundation/mango-client'
2021-07-01 07:12:42 -07:00
import { PublicKey } from '@solana/web3.js'
2021-06-23 08:32:33 -07:00
import useMangoStore, { mangoClient } from '../stores/useMangoStore'
export async function deposit({
amount,
fromTokenAcc,
mangoAccount,
}: {
amount: number
2021-06-18 08:37:42 -07:00
fromTokenAcc: TokenAccount
mangoAccount?: MangoAccount
}) {
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
const wallet = useMangoStore.getState().wallet.current
2021-06-18 08:37:42 -07:00
const tokenIndex = mangoGroup.getTokenIndex(fromTokenAcc.mint)
2021-08-13 08:31:00 -07:00
if (mangoAccount) {
return await mangoClient.deposit(
mangoGroup,
2021-08-13 08:31:00 -07:00
mangoAccount,
wallet,
mangoGroup.tokens[tokenIndex].rootBank,
mangoGroup.rootBankAccounts[tokenIndex].nodeBankAccounts[0].publicKey,
mangoGroup.rootBankAccounts[tokenIndex].nodeBankAccounts[0].vault,
fromTokenAcc.publicKey,
Number(amount)
)
2021-08-13 08:31:00 -07:00
} else {
return await mangoClient.initMangoAccountAndDeposit(
mangoGroup,
wallet,
2021-06-18 08:37:42 -07:00
mangoGroup.tokens[tokenIndex].rootBank,
mangoGroup.rootBankAccounts[tokenIndex].nodeBankAccounts[0].publicKey,
mangoGroup.rootBankAccounts[tokenIndex].nodeBankAccounts[0].vault,
fromTokenAcc.publicKey,
Number(amount)
2021-08-13 08:31:00 -07:00
)
}
}
2021-07-01 07:12:42 -07:00
export async function withdraw({
amount,
token,
allowBorrow,
}: {
amount: number
token: PublicKey
allowBorrow: boolean
}) {
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
const wallet = useMangoStore.getState().wallet.current
const tokenIndex = mangoGroup.getTokenIndex(token)
return await mangoClient.withdraw(
2021-07-01 07:12:42 -07:00
mangoGroup,
mangoAccount,
wallet,
mangoGroup.tokens[tokenIndex].rootBank,
mangoGroup.rootBankAccounts[tokenIndex].nodeBankAccounts[0].publicKey,
mangoGroup.rootBankAccounts[tokenIndex].nodeBankAccounts[0].vault,
Number(amount),
allowBorrow
)
}