Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
abrzezinski94 committed Oct 23, 2024
1 parent 0526662 commit 24af94c
Showing 1 changed file with 97 additions and 82 deletions.
179 changes: 97 additions & 82 deletions ts/client/scripts/reimbursement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Connection, Keypair, PublicKey } from '@solana/web3.js';
import fs from 'fs';
import * as path from 'path';
import { parse } from 'csv-parse';
import { AnchorProvider, Wallet } from 'switchboard-anchor';
import { MANGO_V4_ID, MangoClient, USDC_MINT } from '../src';
import { AnchorProvider, Wallet } from '@coral-xyz/anchor';
import { MANGO_V4_ID, MangoClient, toNative, USDC_MINT } from '../src';
import { WRAPPED_SOL_MINT } from '@project-serum/serum/lib/token-instructions';

const MANGO_MAINNET_GROUP = new PublicKey(
Expand All @@ -19,6 +19,79 @@ type Reimbursement = {
Notional: string;
};

const setupWallet = () => {
const user = Keypair.fromSecretKey(
Buffer.from(
JSON.parse(
fs.readFileSync('keypair.json', {
encoding: 'utf-8',
}),
),
),
);

return user;
};

const ownerOfMangoAccount = async (mangoAccount: string) => {
try {
const respWrapped = await fetch(
`https://api.mngo.cloud/data/v4/user-data/profile-search?search-string=${mangoAccount}&search-method=mango-account`,
);
const resp = await respWrapped.json();
const accountOwner = resp?.length > 0 ? resp[0].wallet_pk : null;
if (accountOwner === null) {
throw 'not found';
}
return accountOwner as string;
} catch (e) {
console.log(e);
console.log('cant find mangoAccount:', mangoAccount);
}
};

const readCsv = async () => {
const csvFilePath = path.resolve(__dirname, 'reimbursement.csv');

const headers = [
'mango_account',
'mangoSOL',
'MOTHER',
'SOL',
'USDC',
'Notional',
];

return new Promise<Reimbursement[]>((resolve, reject) => {
const fileContent = fs.readFileSync(csvFilePath, { encoding: 'utf-8' });

parse(
fileContent,
{
delimiter: ',',
columns: headers,
},
(error, result: Reimbursement[]) => {
if (error) {
reject(error);
} else {
const resp = result.slice(1, result.length);
resolve(resp);
}
},
);
});
};

const tryGetPubKey = (pubkey: string | string[]) => {
try {
return new PublicKey(pubkey);
} catch (e) {
console.log(e);
return null;
}
};

const mints = {
mangoSOL: new PublicKey('MangmsBgFqJhW4cLUR9LxfVgMboY1xAoP8UUBiWwwuY'),
MOTHER: new PublicKey('3S8qX1MsMqRbiwKg2cQyx7nis1oHMgaCuc9c4VfvVdPN'),
Expand All @@ -28,11 +101,17 @@ const mints = {

const main = async () => {
const user = await setupWallet();
const mainConnection = new Connection('');
const backupConnections = [new Connection(''), new Connection('')];
const mainConnection = new Connection('http://fcs-da1._peer.internal:18899/');
const backupConnections = [
new Connection(
'https://staked.helius-rpc.com?api-key=c367d0d9-acf2-437d-9ef3-72304f1a4117',
),
new Connection('https://rpc.mngo.cloud/adr9quiemae0'),
];
const options = AnchorProvider.defaultOptions();
const userWallet = new Wallet(user);
const userProvider = new AnchorProvider(mainConnection, userWallet, options);

const client = await MangoClient.connect(
userProvider,
'mainnet-beta',
Expand All @@ -43,17 +122,18 @@ const main = async () => {
prioritizationFee: 200000,
},
);

console.log(userWallet.publicKey.toBase58(), '@@@@@');
const group = await client.getGroup(MANGO_MAINNET_GROUP);

const csvData = await readCsv();

const TO_PROCESS = csvData.slice(1, 5);
const TOKEN = 'SOL';
const TO_PROCESS = csvData;
const TOKEN = 'MOTHER';

const notReimbursedMangoAccounts: string[] = [];
for (const row of TO_PROCESS) {
const mangoAccountPk = tryGetPubKey(row.mango_account);

if (mangoAccountPk) {
try {
const mint = mints[TOKEN as keyof typeof mints];
Expand All @@ -67,19 +147,25 @@ const main = async () => {
}`,
);
try {
const signature = await client.tokenDeposit(
const decimals = group.getMintDecimals(mint);
const nativeAmount = toNative(amount, decimals);

const signature = await client.tokenDepositNative(
group,
mangoAccount,
mint,
amount,
nativeAmount,
false,
true,
);

console.log(
'Reimburse end ',
signature.signature,
signature.confirmationStatus,
signature.err,
);
if (signature.confirmationStatus === 'confirmed') {
if (!signature.err) {
console.log('OK');
} else {
notReimbursedMangoAccounts.push(row.mango_account);
Expand All @@ -90,7 +176,7 @@ const main = async () => {
}
}
} catch (e) {
console.log('Mango account not exists');
console.log('Mango account not exists', e);
const wallet = await ownerOfMangoAccount(row.mango_account);
if (!wallet) {
notReimbursedMangoAccounts.push(row.mango_account);
Expand All @@ -107,75 +193,4 @@ const main = async () => {
console.log(notReimbursedMangoAccounts);
};

const setupWallet = () => {
const user = Keypair.fromSecretKey(
Buffer.from(
JSON.parse(
fs.readFileSync('keypair.json', {
encoding: 'utf-8',
}),
),
),
);

return user;
};

const ownerOfMangoAccount = async (mangoAccount: string) => {
try {
const respWrapped = await fetch(
`https://api.mngo.cloud/data/v4/user-data/profile-search?search-string=${mangoAccount}&search-method=mango-account`,
);
const resp = await respWrapped.json();
const accountOwner = resp?.length > 0 ? resp[0].wallet_pk : null;
if (accountOwner === null) {
throw 'not found';
}
return accountOwner as string;
} catch (e) {
console.log('cant find mangoAccount:', mangoAccount);
}
};

const readCsv = async () => {
const csvFilePath = path.resolve(__dirname, 'reimbursement.csv');

const headers = [
'mango_account',
'mangoSOL',
'MOTHER',
'SOL',
'USDC',
'Notional',
];

return new Promise<Reimbursement[]>((resolve, reject) => {
const fileContent = fs.readFileSync(csvFilePath, { encoding: 'utf-8' });

parse(
fileContent,
{
delimiter: ',',
columns: headers,
},
(error, result: Reimbursement[]) => {
if (error) {
reject(error);
} else {
const resp = result.slice(1, result.length);
resolve(resp);
}
},
);
});
};

main();

export const tryGetPubKey = (pubkey: string | string[]) => {
try {
return new PublicKey(pubkey);
} catch (e) {
return null;
}
};

0 comments on commit 24af94c

Please sign in to comment.