Skip to content

Commit

Permalink
reimburse: ensure ata, ensure not transferred in last 10 txs, transfer
Browse files Browse the repository at this point in the history
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
  • Loading branch information
microwavedcola1 committed Oct 13, 2022
1 parent eadbedc commit b4c9911
Showing 1 changed file with 212 additions and 0 deletions.
212 changes: 212 additions & 0 deletions src/scripts/reimburse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import {
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID,
Token,
} from '@solana/spl-token';
import {
Commitment,
Connection,
Keypair,
PublicKey,
sendAndConfirmTransaction,
SystemProgram,
Transaction,
TransactionInstruction,
} from '@solana/web3.js';
import BN from 'bn.js';
import { MangoClient } from '../client';
import { Cluster, Config } from '../config';
import fs from 'fs';

const PAYER_KEYPAIR = process.env.MB_PAYER_KEYPAIR;
const PAYER = Keypair.fromSecretKey(
Buffer.from(JSON.parse(fs.readFileSync(PAYER_KEYPAIR!, 'utf-8'))),
);
const SOURCE = PAYER.publicKey;

const config = Config.ids();
const cluster = 'mainnet' as Cluster;
const connection = new Connection(
config.cluster_urls[cluster],
'confirmed' as Commitment,
);

const groupName = 'mainnet.1';
const groupIds = config.getGroup(cluster, groupName);
if (!groupIds) {
throw new Error(`Group ${groupName} not found`);
}

const mangoProgramId = groupIds.mangoProgramId;
const mangoGroupKey = groupIds.publicKey;
const client = new MangoClient(connection, mangoProgramId);

export async function createAssociatedTokenAccountIdempotentInstruction(
payer: PublicKey,
owner: PublicKey,
mint: PublicKey,
ata: PublicKey,
): Promise<TransactionInstruction> {
return new TransactionInstruction({
keys: [
{ pubkey: payer, isSigner: true, isWritable: true },
{ pubkey: ata, isSigner: false, isWritable: true },
{ pubkey: owner, isSigner: false, isWritable: false },
{ pubkey: mint, isSigner: false, isWritable: false },
{
pubkey: SystemProgram.programId,
isSigner: false,
isWritable: false,
},
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
],
programId: ASSOCIATED_TOKEN_PROGRAM_ID,
data: Buffer.from([0x1]),
});
}

async function tokenTransfer(
mangoAccountOwnerPk: PublicKey,
mint: PublicKey,
nativeTokenAmountToReimburse: BN,
) {
const sourceAta = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
mint,
SOURCE,
);
const destinationAta = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
mint,
mangoAccountOwnerPk,
);

const tx = new Transaction();
tx.add(
await createAssociatedTokenAccountIdempotentInstruction(
PAYER.publicKey,
mangoAccountOwnerPk,
mint,
destinationAta,
),
);
tx.add(
await Token.createTransferInstruction(
TOKEN_PROGRAM_ID,
sourceAta,
await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
mint,
mangoAccountOwnerPk,
),
PAYER.publicKey,
[PAYER],
nativeTokenAmountToReimburse.toNumber(), // throws `Note: Blob.encode[amount] requires (length 8) Buffer as src` when BN is used
),
);

const sigs = await connection.getConfirmedSignaturesForAddress2(
destinationAta,
);
for (const sig of sigs.slice(0, 10)) {
const meta = await connection.getParsedTransaction(
sig.signature,
'confirmed',
);

if (
meta?.transaction.message.instructions.length === 2 &&
(meta?.transaction.message.instructions[1] as any).parsed &&
(meta?.transaction.message.instructions[1] as any).parsed.type &&
(meta?.transaction.message.instructions[1] as any).parsed.type ===
'transfer'
) {
const res = (meta?.transaction.message.instructions[1] as any).parsed
.info as {
amount: string;
destination: string;
source: string;
};
if (
res.amount === nativeTokenAmountToReimburse.toString() &&
sourceAta.toBase58() === res.source &&
destinationAta.toBase58() === res.destination
) {
console.log(` - already transferred`);
return;
}
}
}

const sig = await sendAndConfirmTransaction(connection, tx, [PAYER], {
skipPreflight: true,
});
console.log(` - transferrd, sig https://explorer.solana.com/tx/${sig}`);
}

async function reimburseUser(
mangoAccountOwnerPk: PublicKey,
nativeTokenAmountsToReimburse: BN[],
): Promise<void> {
const group = await client.getMangoGroup(mangoGroupKey);
const allTokens = 16;

if (nativeTokenAmountsToReimburse.length !== allTokens) {
throw new Error(
`Mango V3 has ${allTokens} tokens, expected ${allTokens} token amounts to reimburse!`,
);
}

group.tokens.map(async (token, tokenIndex) => {
const tokenConfig = groupIds?.tokens.find((tokenConfig) =>
token.mint.equals(tokenConfig.mintKey),
);

if (!tokenConfig) {
return;
}

if (token.oracleInactive) {
return;
}

const nativeTokenAmountToReimburse =
nativeTokenAmountsToReimburse[tokenIndex];

if (nativeTokenAmountToReimburse.eq(new BN(0))) {
return;
}

console.log(
`Transferring ${nativeTokenAmountToReimburse} native ${tokenConfig.symbol} (mint - ${tokenConfig.mintKey}) to ${mangoAccountOwnerPk}`,
);

return await tokenTransfer(
mangoAccountOwnerPk,
token.mint,
nativeTokenAmountToReimburse,
);
});
}

reimburseUser(new PublicKey('9Ut1gZJnd5D7EjPXm2DygYWZkZGpt5QSMEYAaVx2hur4'), [
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(0),
new BN(1), // USDC
]);

0 comments on commit b4c9911

Please sign in to comment.