2022-07-04 03:09:33 -07:00
|
|
|
import { AnchorProvider, Wallet } from '@project-serum/anchor';
|
|
|
|
import { Connection, Keypair } from '@solana/web3.js';
|
|
|
|
import fs from 'fs';
|
|
|
|
import { Serum3Side } from '../accounts/serum3';
|
|
|
|
import { MangoClient } from '../client';
|
|
|
|
import { MANGO_V4_ID } from '../constants';
|
|
|
|
|
2022-07-04 06:52:32 -07:00
|
|
|
//
|
|
|
|
// script which shows how to close a mango account cleanly i.e. close all active positions, withdraw all tokens, etc.
|
|
|
|
//
|
|
|
|
|
2022-07-04 03:09:33 -07:00
|
|
|
// note: either use finalized or expect closing certain things to fail and having to runs scrript multiple times
|
|
|
|
async function main() {
|
|
|
|
const options = AnchorProvider.defaultOptions();
|
|
|
|
|
|
|
|
// note: see note above
|
|
|
|
// options.commitment = 'finalized';
|
|
|
|
|
|
|
|
const connection = new Connection(
|
|
|
|
'https://mango.devnet.rpcpool.com',
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
|
|
|
|
// user
|
|
|
|
const user = Keypair.fromSecretKey(
|
|
|
|
Buffer.from(
|
|
|
|
JSON.parse(fs.readFileSync(process.env.USER_KEYPAIR!, 'utf-8')),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
const userWallet = new Wallet(user);
|
|
|
|
const userProvider = new AnchorProvider(connection, userWallet, options);
|
|
|
|
const client = await MangoClient.connect(
|
|
|
|
userProvider,
|
|
|
|
'devnet',
|
|
|
|
MANGO_V4_ID['devnet'],
|
|
|
|
);
|
|
|
|
console.log(`User ${userWallet.publicKey.toBase58()}`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// fetch group
|
|
|
|
const admin = Keypair.fromSecretKey(
|
|
|
|
Buffer.from(
|
|
|
|
JSON.parse(fs.readFileSync(process.env.ADMIN_KEYPAIR!, 'utf-8')),
|
|
|
|
),
|
|
|
|
);
|
2022-08-04 00:53:38 -07:00
|
|
|
const group = await client.getGroupForCreator(admin.publicKey, 0);
|
2022-07-04 03:09:33 -07:00
|
|
|
console.log(`Found group ${group.publicKey.toBase58()}`);
|
|
|
|
|
|
|
|
// fetch account
|
|
|
|
const mangoAccount = (
|
2022-08-04 00:53:38 -07:00
|
|
|
await client.getMangoAccountsForOwner(group, user.publicKey)
|
2022-07-04 03:09:33 -07:00
|
|
|
)[0];
|
|
|
|
console.log(`...found mangoAccount ${mangoAccount.publicKey}`);
|
|
|
|
console.log(mangoAccount.toString());
|
|
|
|
|
|
|
|
// close mango account serum3 positions, closing might require cancelling orders and settling
|
|
|
|
for (const serum3Account of mangoAccount.serum3Active()) {
|
|
|
|
let orders = await client.getSerum3Orders(
|
|
|
|
group,
|
2022-08-03 09:05:16 -07:00
|
|
|
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
2022-07-04 03:09:33 -07:00
|
|
|
);
|
|
|
|
for (const order of orders) {
|
|
|
|
console.log(
|
|
|
|
` - Order orderId ${order.orderId}, ${order.side}, ${order.price}, ${order.size}`,
|
|
|
|
);
|
|
|
|
console.log(` - Cancelling order with ${order.orderId}`);
|
|
|
|
await client.serum3CancelOrder(
|
|
|
|
group,
|
|
|
|
mangoAccount,
|
|
|
|
|
|
|
|
'BTC/USDC',
|
|
|
|
order.side === 'buy' ? Serum3Side.bid : Serum3Side.ask,
|
|
|
|
order.orderId,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
await client.serum3SettleFunds(
|
|
|
|
group,
|
|
|
|
mangoAccount,
|
2022-08-03 09:05:16 -07:00
|
|
|
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
2022-07-04 03:09:33 -07:00
|
|
|
);
|
|
|
|
await client.serum3CloseOpenOrders(
|
|
|
|
group,
|
|
|
|
mangoAccount,
|
2022-08-03 09:05:16 -07:00
|
|
|
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
2022-07-04 03:09:33 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we closed a serum account, this changes the health accounts we are passing in for future ixs
|
2022-07-04 03:29:35 -07:00
|
|
|
await mangoAccount.reload(client, group);
|
2022-07-04 03:09:33 -07:00
|
|
|
|
|
|
|
// withdraw all tokens
|
|
|
|
for (const token of mangoAccount.tokensActive()) {
|
2022-08-03 09:05:16 -07:00
|
|
|
let native = token.native(group.findBank(token.tokenIndex)!);
|
2022-07-04 03:09:33 -07:00
|
|
|
|
|
|
|
// to avoid rounding issues
|
|
|
|
if (native.toNumber() < 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let nativeFlooredNumber = Math.floor(native.toNumber());
|
|
|
|
console.log(
|
|
|
|
`withdrawing token ${
|
2022-08-03 09:05:16 -07:00
|
|
|
group.findBank(token.tokenIndex)!.name
|
2022-07-04 03:09:33 -07:00
|
|
|
} native amount ${nativeFlooredNumber} `,
|
|
|
|
);
|
|
|
|
|
2022-08-03 09:05:16 -07:00
|
|
|
await client.tokenWithdrawNative(
|
2022-07-04 03:09:33 -07:00
|
|
|
group,
|
|
|
|
mangoAccount,
|
2022-08-03 09:05:16 -07:00
|
|
|
group.findBank(token.tokenIndex)!.name,
|
2022-07-04 03:09:33 -07:00
|
|
|
nativeFlooredNumber,
|
|
|
|
false,
|
2022-08-04 00:53:38 -07:00
|
|
|
user,
|
2022-07-04 03:09:33 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// reload and print current positions
|
2022-07-04 03:29:35 -07:00
|
|
|
await mangoAccount.reload(client, group);
|
2022-07-04 03:09:33 -07:00
|
|
|
console.log(`...mangoAccount ${mangoAccount.publicKey}`);
|
|
|
|
console.log(mangoAccount.toString());
|
|
|
|
|
|
|
|
// close account
|
|
|
|
console.log(`Close mango account...`);
|
|
|
|
const res = await client.closeMangoAccount(group, mangoAccount);
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|