empty and close mango account

This commit is contained in:
Adrian Brzeziński 2023-01-13 13:19:04 +01:00
parent 6e3e72436f
commit 80cf41ff5f
4 changed files with 76 additions and 10 deletions

View File

@ -829,13 +829,46 @@ export class MangoClient {
); );
} }
//withdraw all assets, close oo, deactivating perp positions, close account
public async emptyAndCloseMangoAccount( public async emptyAndCloseMangoAccount(
group: Group, group: Group,
mangoAccount: MangoAccount, mangoAccount: MangoAccount,
forceClose = false, forceClose = false,
): Promise<TransactionSignature> { ): 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) .accountClose(forceClose)
.accounts({ .accounts({
group: group.publicKey, group: group.publicKey,
@ -844,9 +877,10 @@ export class MangoClient {
solDestination: mangoAccount.owner, solDestination: mangoAccount.owner,
}) })
.instruction(); .instruction();
instructions.push(closeIx);
return await this.sendAndConfirmTransaction( return await this.sendAndConfirmTransaction(
[ix], [...instructions],
group.addressLookupTablesList, group.addressLookupTablesList,
); );
} }
@ -1783,11 +1817,11 @@ export class MangoClient {
); );
} }
public async perpDeactivatePosition( public async perpDeactivatePositionIx(
group: Group, group: Group,
mangoAccount: MangoAccount, mangoAccount: MangoAccount,
perpMarketIndex: PerpMarketIndex, perpMarketIndex: PerpMarketIndex,
): Promise<TransactionSignature> { ): Promise<TransactionInstruction> {
const perpMarket = group.getPerpMarketByMarketIndex(perpMarketIndex); const perpMarket = group.getPerpMarketByMarketIndex(perpMarketIndex);
const healthRemainingAccounts: PublicKey[] = const healthRemainingAccounts: PublicKey[] =
this.buildHealthRemainingAccounts( this.buildHealthRemainingAccounts(
@ -1811,7 +1845,23 @@ export class MangoClient {
({ pubkey: pk, isWritable: false, isSigner: false } as AccountMeta), ({ 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( public async perpPlaceOrder(

View File

@ -114,7 +114,7 @@ async function main() {
} native amount ${nativeFlooredNumber} `, } native amount ${nativeFlooredNumber} `,
); );
await client.tokenWithdrawNative( const withdrawIx = await client.tokenWithdrawNativeIx(
group, group,
mangoAccount, mangoAccount,
group.getFirstBankByTokenIndex(token.tokenIndex).mint, group.getFirstBankByTokenIndex(token.tokenIndex).mint,
@ -123,6 +123,10 @@ async function main() {
) /* see comment in token_withdraw in program */, ) /* see comment in token_withdraw in program */,
false, false,
); );
await this.sendAndConfirmTransaction(
[...withdrawIx],
group.addressLookupTablesList,
);
} }
// reload and print current positions // reload and print current positions

View File

@ -99,7 +99,7 @@ async function closeUserAccount(userKeypairFile: string) {
continue; continue;
} }
await client.tokenWithdrawNative( const withdrawIx = await client.tokenWithdrawNativeIx(
group, group,
mangoAccount, mangoAccount,
group.getFirstBankByTokenIndex(token.tokenIndex)!.mint, group.getFirstBankByTokenIndex(token.tokenIndex)!.mint,
@ -110,6 +110,10 @@ async function closeUserAccount(userKeypairFile: string) {
), ),
false, false,
); );
await this.sendAndConfirmTransaction(
[...withdrawIx],
group.addressLookupTablesList,
);
} }
} catch (error) { } catch (error) {
console.log(error); console.log(error);

View File

@ -192,13 +192,17 @@ async function main() {
try { try {
await setBankPrice(bank, PRICES[liabName] / 2); await setBankPrice(bank, PRICES[liabName] / 2);
await client.tokenWithdrawNative( const withdrawIx = await client.tokenWithdrawNativeIx(
group, group,
mangoAccount, mangoAccount,
liabMint, liabMint,
new BN(liabAmount), new BN(liabAmount),
true, true,
); );
await this.sendAndConfirmTransaction(
[...withdrawIx],
group.addressLookupTablesList,
);
} finally { } finally {
// restore the oracle // restore the oracle
await setBankPrice(bank, PRICES[liabName]); await setBankPrice(bank, PRICES[liabName]);
@ -446,13 +450,17 @@ async function main() {
await setBankPrice(collateralBank, PRICES['SOL'] * 10); await setBankPrice(collateralBank, PRICES['SOL'] * 10);
// Spot-borrow more than the collateral is worth // Spot-borrow more than the collateral is worth
await client.tokenWithdrawNative( const withdrawIx = await client.tokenWithdrawNativeIx(
group, group,
mangoAccount, mangoAccount,
liabMint, liabMint,
new BN(-5000), new BN(-5000),
true, true,
); );
await this.sendAndConfirmTransaction(
[...withdrawIx],
group.addressLookupTablesList,
);
await mangoAccount.reload(client); await mangoAccount.reload(client);
// Execute two trades that leave the account with +$0.011 positive pnl // Execute two trades that leave the account with +$0.011 positive pnl