Skip to content

Commit

Permalink
more improvements to force withdraw script
Browse files Browse the repository at this point in the history
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
  • Loading branch information
microwavedcola1 committed Aug 19, 2024
1 parent f1e9420 commit 83c7efa
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions ts/client/scripts/force-withdraw-token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { AnchorProvider, Wallet } from '@coral-xyz/anchor';
import { Cluster, Connection, Keypair, PublicKey } from '@solana/web3.js';
import fs from 'fs';
import uniqWith from 'lodash/uniqWith';
import { TokenIndex } from '../src/accounts/bank';
import { MangoAccount } from '../src/accounts/mangoAccount';
import { MangoClient } from '../src/client';
import { MANGO_V4_ID } from '../src/constants';

Expand Down Expand Up @@ -43,47 +45,58 @@ async function forceWithdrawTokens(): Promise<void> {
group.serum3MarketsMapByMarketIndex.values(),
).filter((m) => m.baseTokenIndex == TOKEN_INDEX)[0];

const mangoAccountsWithTp = (await client.getAllMangoAccounts(group)).filter(
(a) => a.getToken(forceWithdrawBank.tokenIndex)?.isActive() ?? false,
);
const mangoAccountsWithInUseCount = (
await client.getAllMangoAccounts(group)
).filter((a) => a.getTokenInUseCount(forceWithdrawBank) > 0);

const mangoAccounts: MangoAccount[] = uniqWith(
[...mangoAccountsWithTp, ...mangoAccountsWithInUseCount],
function (a, b) {
return a.publicKey.equals(b.publicKey);
},
);

console.log(
`Found ${mangoAccountsWithInUseCount.length} mango accounts with in use count > 0`,
`Found ${mangoAccounts.length} mango accounts with in use count > 0 or tp`,
);

for (const mangoAccount of mangoAccountsWithInUseCount) {
for (const mangoAccount of mangoAccounts) {
console.log(
`${mangoAccount.getTokenBalanceUi(forceWithdrawBank)} for ${
mangoAccount.publicKey
}`,
);

client
.serum3LiqForceCancelOrders(
try {
const sig = await client.serum3LiqForceCancelOrders(
group,
mangoAccount,
serum3Market.serumMarketExternal,
)
);
console.log(
` serum3LiqForceCancelOrders for ${mangoAccount.publicKey}, owner ${
mangoAccount.owner
}, sig https://explorer.solana.com/tx/${sig.signature}?cluster=${
CLUSTER == 'devnet' ? 'devnet' : ''
}`,
);
} catch (error) {
console.log(error);
}

await client
.tokenForceWithdraw(group, mangoAccount, TOKEN_INDEX)
.then((sig) => {
console.log(
` serum3LiqForceCancelOrders for ${mangoAccount.publicKey}, owner ${
` tokenForceWithdraw for ${mangoAccount.publicKey}, owner ${
mangoAccount.owner
}, sig https://explorer.solana.com/tx/${sig.signature}?cluster=${
CLUSTER == 'devnet' ? 'devnet' : ''
}`,
);

client
.tokenForceWithdraw(group, mangoAccount, TOKEN_INDEX)
.then((sig) => {
console.log(
` tokenForceWithdraw for ${mangoAccount.publicKey}, owner ${
mangoAccount.owner
}, sig https://explorer.solana.com/tx/${sig.signature}?cluster=${
CLUSTER == 'devnet' ? 'devnet' : ''
}`,
);
});
});
}

Expand Down

0 comments on commit 83c7efa

Please sign in to comment.