Use emptyAndCloseMangoAccount

This commit is contained in:
Riordan Panayides 2021-11-09 19:19:07 +00:00 committed by Tyler Shipe
parent f4c5509f76
commit 62a6f58e0e
1 changed files with 6 additions and 56 deletions

View File

@ -5,7 +5,7 @@ import {
useMemo, useMemo,
useState, useState,
} from 'react' } from 'react'
import useMangoStore, { MNGO_INDEX, programId } from '../stores/useMangoStore' import useMangoStore, { MNGO_INDEX } from '../stores/useMangoStore'
import { XCircleIcon } from '@heroicons/react/outline' import { XCircleIcon } from '@heroicons/react/outline'
import Button from './Button' import Button from './Button'
import Modal from './Modal' import Modal from './Modal'
@ -15,17 +15,12 @@ import { notify } from '../utils/notifications'
import { CheckCircleIcon } from '@heroicons/react/solid' import { CheckCircleIcon } from '@heroicons/react/solid'
import { import {
getMultipleAccounts, getMultipleAccounts,
makeCloseAdvancedOrdersInstruction,
makeCloseMangoAccountInstruction,
makeCloseSpotOpenOrdersInstruction,
nativeToUi, nativeToUi,
zeroKey, zeroKey,
ZERO_BN, ZERO_BN,
ZERO_I80F48, ZERO_I80F48,
} from '@blockworks-foundation/mango-client' } from '@blockworks-foundation/mango-client'
import usePerpPositions from '../hooks/usePerpPositions' import usePerpPositions from '../hooks/usePerpPositions'
import { Transaction } from '@solana/web3.js'
import useMangoGroupConfig from '../hooks/useMangoGroupConfig'
import { useOpenOrders } from '../hooks/useOpenOrders' import { useOpenOrders } from '../hooks/useOpenOrders'
interface CloseAccountModalProps { interface CloseAccountModalProps {
@ -50,7 +45,6 @@ const CloseAccountModal: FunctionComponent<CloseAccountModalProps> = ({
const actions = useMangoStore((s) => s.actions) const actions = useMangoStore((s) => s.actions)
const connection = useMangoStore((s) => s.connection.current) const connection = useMangoStore((s) => s.connection.current)
const client = useMangoStore((s) => s.connection.client) const client = useMangoStore((s) => s.connection.client)
const groupConfig = useMangoGroupConfig()
const openOrders = useOpenOrders() const openOrders = useOpenOrders()
const setMangoStore = useMangoStore((s) => s.set) const setMangoStore = useMangoStore((s) => s.set)
const mangoAccounts = useMangoStore((s) => s.mangoAccounts) const mangoAccounts = useMangoStore((s) => s.mangoAccounts)
@ -98,66 +92,22 @@ const CloseAccountModal: FunctionComponent<CloseAccountModalProps> = ({
const closeAccount = async () => { const closeAccount = async () => {
const wallet = useMangoStore.getState().wallet.current const wallet = useMangoStore.getState().wallet.current
try { try {
console.log('withdrawAll') await client.emptyAndCloseMangoAccount(
await client.withdrawAll(mangoGroup, mangoAccount, wallet) mangoGroup,
console.log('resolveAllDust') mangoAccount,
await client.resolveAllDust(mangoGroup, mangoAccount, mangoCache, wallet) mangoCache,
const tx = new Transaction() wallet
for (let i = 0; i < mangoAccount.spotOpenOrders.length; i++) {
const openOrders = mangoAccount.spotOpenOrders[i]
const spotMarket = groupConfig.spotMarkets[i]
if (!openOrders.equals(zeroKey)) {
tx.add(
makeCloseSpotOpenOrdersInstruction(
programId,
mangoGroup.publicKey,
mangoAccount.publicKey,
wallet.publicKey,
mangoGroup.dexProgramId,
openOrders,
spotMarket.publicKey,
mangoGroup.signerKey
)
)
}
}
if (!mangoAccount.advancedOrdersKey.equals(zeroKey)) {
tx.add(
makeCloseAdvancedOrdersInstruction(
programId,
mangoGroup.publicKey,
mangoAccount.publicKey,
wallet.publicKey,
mangoAccount.advancedOrdersKey
)
)
}
tx.add(
makeCloseMangoAccountInstruction(
programId,
mangoGroup.publicKey,
mangoAccount.publicKey,
wallet.publicKey
)
) )
const txid = await client.sendTransaction(tx, wallet, [])
console.log('fetchAllMangoAccounts')
actions.fetchAllMangoAccounts() actions.fetchAllMangoAccounts()
console.log('setSelectedAccount')
setMangoStore((state) => { setMangoStore((state) => {
state.selectedMangoAccount.current = mangoAccounts[0] state.selectedMangoAccount.current = mangoAccounts[0]
}) })
console.log('reloadMangoAccount')
actions.reloadMangoAccount() actions.reloadMangoAccount()
onClose() onClose()
notify({ notify({
title: 'Account Deleted', title: 'Account Deleted',
txid,
}) })
} catch (err) { } catch (err) {
console.warn('Error deleting account:', err) console.warn('Error deleting account:', err)