empty and close mango account
This commit is contained in:
parent
6e3e72436f
commit
80cf41ff5f
|
@ -829,13 +829,46 @@ export class MangoClient {
|
|||
);
|
||||
}
|
||||
|
||||
//withdraw all assets, close oo, deactivating perp positions, close account
|
||||
public async emptyAndCloseMangoAccount(
|
||||
group: Group,
|
||||
mangoAccount: MangoAccount,
|
||||
forceClose = false,
|
||||
): Promise<TransactionSignature> {
|
||||
const ix = await this.program.methods
|
||||
const instructions: TransactionInstruction[] = [];
|
||||
for (const token of mangoAccount.tokensActive()) {
|
||||
const bank = group.getFirstBankByTokenIndex(token.tokenIndex);
|
||||
const withdrawIx = await this.tokenWithdrawNativeIx(
|
||||
group,
|
||||
mangoAccount,
|
||||
bank.mint,
|
||||
new BN(token.balance(bank).toNumber()),
|
||||
false,
|
||||
);
|
||||
instructions.push(...withdrawIx);
|
||||
}
|
||||
|
||||
for (const serum3Account of mangoAccount.serum3Active()) {
|
||||
const serum3Market = group.serum3MarketsMapByMarketIndex.get(
|
||||
serum3Account.marketIndex,
|
||||
)!.serumMarketExternal!;
|
||||
const closeOOIx = await this.serum3CloseOpenOrdersIx(
|
||||
group,
|
||||
mangoAccount,
|
||||
serum3Market,
|
||||
);
|
||||
instructions.push(closeOOIx);
|
||||
}
|
||||
|
||||
for (const perp of mangoAccount.perpActive()) {
|
||||
const deactivatingPositionIx = await this.perpDeactivatePositionIx(
|
||||
group,
|
||||
mangoAccount,
|
||||
perp.marketIndex,
|
||||
);
|
||||
instructions.push(deactivatingPositionIx);
|
||||
}
|
||||
|
||||
const closeIx = await this.program.methods
|
||||
.accountClose(forceClose)
|
||||
.accounts({
|
||||
group: group.publicKey,
|
||||
|
@ -844,9 +877,10 @@ export class MangoClient {
|
|||
solDestination: mangoAccount.owner,
|
||||
})
|
||||
.instruction();
|
||||
instructions.push(closeIx);
|
||||
|
||||
return await this.sendAndConfirmTransaction(
|
||||
[ix],
|
||||
[...instructions],
|
||||
group.addressLookupTablesList,
|
||||
);
|
||||
}
|
||||
|
@ -1783,11 +1817,11 @@ export class MangoClient {
|
|||
);
|
||||
}
|
||||
|
||||
public async perpDeactivatePosition(
|
||||
public async perpDeactivatePositionIx(
|
||||
group: Group,
|
||||
mangoAccount: MangoAccount,
|
||||
perpMarketIndex: PerpMarketIndex,
|
||||
): Promise<TransactionSignature> {
|
||||
): Promise<TransactionInstruction> {
|
||||
const perpMarket = group.getPerpMarketByMarketIndex(perpMarketIndex);
|
||||
const healthRemainingAccounts: PublicKey[] =
|
||||
this.buildHealthRemainingAccounts(
|
||||
|
@ -1811,7 +1845,23 @@ export class MangoClient {
|
|||
({ pubkey: pk, isWritable: false, isSigner: false } as AccountMeta),
|
||||
),
|
||||
)
|
||||
.rpc();
|
||||
.instruction();
|
||||
}
|
||||
|
||||
public async perpDeactivatePosition(
|
||||
group: Group,
|
||||
mangoAccount: MangoAccount,
|
||||
perpMarketIndex: PerpMarketIndex,
|
||||
): Promise<TransactionSignature> {
|
||||
const ix = await this.perpDeactivatePositionIx(
|
||||
group,
|
||||
mangoAccount,
|
||||
perpMarketIndex,
|
||||
);
|
||||
return await this.sendAndConfirmTransaction(
|
||||
[ix],
|
||||
group.addressLookupTablesList,
|
||||
);
|
||||
}
|
||||
|
||||
public async perpPlaceOrder(
|
||||
|
|
|
@ -114,7 +114,7 @@ async function main() {
|
|||
} native amount ${nativeFlooredNumber} `,
|
||||
);
|
||||
|
||||
await client.tokenWithdrawNative(
|
||||
const withdrawIx = await client.tokenWithdrawNativeIx(
|
||||
group,
|
||||
mangoAccount,
|
||||
group.getFirstBankByTokenIndex(token.tokenIndex).mint,
|
||||
|
@ -123,6 +123,10 @@ async function main() {
|
|||
) /* see comment in token_withdraw in program */,
|
||||
false,
|
||||
);
|
||||
await this.sendAndConfirmTransaction(
|
||||
[...withdrawIx],
|
||||
group.addressLookupTablesList,
|
||||
);
|
||||
}
|
||||
|
||||
// reload and print current positions
|
||||
|
|
|
@ -99,7 +99,7 @@ async function closeUserAccount(userKeypairFile: string) {
|
|||
continue;
|
||||
}
|
||||
|
||||
await client.tokenWithdrawNative(
|
||||
const withdrawIx = await client.tokenWithdrawNativeIx(
|
||||
group,
|
||||
mangoAccount,
|
||||
group.getFirstBankByTokenIndex(token.tokenIndex)!.mint,
|
||||
|
@ -110,6 +110,10 @@ async function closeUserAccount(userKeypairFile: string) {
|
|||
),
|
||||
false,
|
||||
);
|
||||
await this.sendAndConfirmTransaction(
|
||||
[...withdrawIx],
|
||||
group.addressLookupTablesList,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
|
|
@ -192,13 +192,17 @@ async function main() {
|
|||
try {
|
||||
await setBankPrice(bank, PRICES[liabName] / 2);
|
||||
|
||||
await client.tokenWithdrawNative(
|
||||
const withdrawIx = await client.tokenWithdrawNativeIx(
|
||||
group,
|
||||
mangoAccount,
|
||||
liabMint,
|
||||
new BN(liabAmount),
|
||||
true,
|
||||
);
|
||||
await this.sendAndConfirmTransaction(
|
||||
[...withdrawIx],
|
||||
group.addressLookupTablesList,
|
||||
);
|
||||
} finally {
|
||||
// restore the oracle
|
||||
await setBankPrice(bank, PRICES[liabName]);
|
||||
|
@ -446,13 +450,17 @@ async function main() {
|
|||
await setBankPrice(collateralBank, PRICES['SOL'] * 10);
|
||||
|
||||
// Spot-borrow more than the collateral is worth
|
||||
await client.tokenWithdrawNative(
|
||||
const withdrawIx = await client.tokenWithdrawNativeIx(
|
||||
group,
|
||||
mangoAccount,
|
||||
liabMint,
|
||||
new BN(-5000),
|
||||
true,
|
||||
);
|
||||
await this.sendAndConfirmTransaction(
|
||||
[...withdrawIx],
|
||||
group.addressLookupTablesList,
|
||||
);
|
||||
await mangoAccount.reload(client);
|
||||
|
||||
// Execute two trades that leave the account with +$0.011 positive pnl
|
||||
|
|
Loading…
Reference in New Issue