2022-08-09 22:32:21 -07:00
|
|
|
import { AnchorProvider, Wallet } from '@project-serum/anchor';
|
2022-09-23 02:43:26 -07:00
|
|
|
import { Cluster, Connection, Keypair } from '@solana/web3.js';
|
2022-08-09 22:32:21 -07:00
|
|
|
import fs from 'fs';
|
2022-08-23 04:47:08 -07:00
|
|
|
import { Group } from '../accounts/group';
|
2022-09-29 06:51:09 -07:00
|
|
|
import { HealthCache } from '../accounts/healthCache';
|
2022-08-23 04:47:08 -07:00
|
|
|
import { HealthType, MangoAccount } from '../accounts/mangoAccount';
|
2022-09-23 02:43:26 -07:00
|
|
|
import { PerpMarket } from '../accounts/perp';
|
2022-09-23 00:34:08 -07:00
|
|
|
import { Serum3Market } from '../accounts/serum3';
|
2022-08-09 22:32:21 -07:00
|
|
|
import { MangoClient } from '../client';
|
|
|
|
import { MANGO_V4_ID } from '../constants';
|
2022-08-11 08:44:12 -07:00
|
|
|
import { toUiDecimalsForQuote } from '../utils';
|
2022-08-09 22:32:21 -07:00
|
|
|
|
2022-09-23 02:43:26 -07:00
|
|
|
const CLUSTER_URL =
|
|
|
|
process.env.CLUSTER_URL_OVERRIDE || process.env.MB_CLUSTER_URL;
|
|
|
|
const PAYER_KEYPAIR =
|
|
|
|
process.env.PAYER_KEYPAIR_OVERRIDE || process.env.MB_PAYER_KEYPAIR;
|
|
|
|
const USER_KEYPAIR =
|
|
|
|
process.env.USER_KEYPAIR_OVERRIDE || process.env.MB_PAYER_KEYPAIR;
|
|
|
|
const GROUP_NUM = Number(process.env.GROUP_NUM || 2);
|
|
|
|
const CLUSTER: Cluster =
|
|
|
|
(process.env.CLUSTER_OVERRIDE as Cluster) || 'mainnet-beta';
|
|
|
|
|
2022-08-23 04:47:08 -07:00
|
|
|
async function debugUser(
|
|
|
|
client: MangoClient,
|
|
|
|
group: Group,
|
|
|
|
mangoAccount: MangoAccount,
|
|
|
|
) {
|
2022-08-09 22:32:21 -07:00
|
|
|
console.log(mangoAccount.toString(group));
|
2022-08-26 01:08:45 -07:00
|
|
|
|
2022-09-29 06:51:09 -07:00
|
|
|
await mangoAccount.reload(client);
|
2022-08-11 08:44:12 -07:00
|
|
|
|
2022-08-09 22:32:21 -07:00
|
|
|
console.log(
|
|
|
|
'mangoAccount.getEquity() ' +
|
2022-09-29 06:51:09 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getEquity(group)!.toNumber()),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealth(HealthType.init) ' +
|
2022-09-29 06:51:09 -07:00
|
|
|
toUiDecimalsForQuote(
|
|
|
|
mangoAccount.getHealth(group, HealthType.init)!.toNumber(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'HealthCache.fromMangoAccount(group,mangoAccount).health(HealthType.init) ' +
|
|
|
|
toUiDecimalsForQuote(
|
|
|
|
HealthCache.fromMangoAccount(group, mangoAccount)
|
|
|
|
.health(HealthType.init)
|
|
|
|
.toNumber(),
|
|
|
|
),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatio(HealthType.init) ' +
|
2022-09-29 06:51:09 -07:00
|
|
|
mangoAccount.getHealthRatio(group, HealthType.init)!.toNumber(),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
2022-08-19 12:18:16 -07:00
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatioUi(HealthType.init) ' +
|
2022-09-29 06:51:09 -07:00
|
|
|
mangoAccount.getHealthRatioUi(group, HealthType.init),
|
2022-08-19 12:18:16 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatio(HealthType.maint) ' +
|
2022-09-29 06:51:09 -07:00
|
|
|
mangoAccount.getHealthRatio(group, HealthType.maint)!.toNumber(),
|
2022-08-19 12:18:16 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatioUi(HealthType.maint) ' +
|
2022-09-29 06:51:09 -07:00
|
|
|
mangoAccount.getHealthRatioUi(group, HealthType.maint),
|
2022-08-19 12:18:16 -07:00
|
|
|
);
|
2022-08-09 22:32:21 -07:00
|
|
|
console.log(
|
|
|
|
'mangoAccount.getCollateralValue() ' +
|
2022-09-29 06:51:09 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getCollateralValue(group)!.toNumber()),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
console.log(
|
2022-08-18 22:43:32 -07:00
|
|
|
'mangoAccount.getAssetsValue() ' +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(
|
2022-09-29 06:51:09 -07:00
|
|
|
mangoAccount.getAssetsValue(group, HealthType.init)!.toNumber(),
|
2022-08-11 08:44:12 -07:00
|
|
|
),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
console.log(
|
2022-08-18 22:43:32 -07:00
|
|
|
'mangoAccount.getLiabsValue() ' +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(
|
2022-09-29 06:51:09 -07:00
|
|
|
mangoAccount.getLiabsValue(group, HealthType.init)!.toNumber(),
|
2022-08-11 08:44:12 -07:00
|
|
|
),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
|
2022-08-23 04:47:08 -07:00
|
|
|
async function getMaxWithdrawWithBorrowForTokenUiWrapper(token) {
|
|
|
|
console.log(
|
|
|
|
`mangoAccount.getMaxWithdrawWithBorrowForTokenUi(group, ${token}) ` +
|
|
|
|
mangoAccount.getMaxWithdrawWithBorrowForTokenUi(
|
|
|
|
group,
|
2022-09-01 00:48:23 -07:00
|
|
|
group.banksMapByName.get(token)![0].mint,
|
2022-08-23 04:47:08 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for (const srcToken of Array.from(group.banksMapByName.keys())) {
|
|
|
|
await getMaxWithdrawWithBorrowForTokenUiWrapper(srcToken);
|
|
|
|
}
|
2022-08-09 22:32:21 -07:00
|
|
|
|
2022-08-23 04:47:08 -07:00
|
|
|
function simHealthRatioWithTokenPositionChangesWrapper(debug, change) {
|
|
|
|
console.log(
|
|
|
|
`mangoAccount.simHealthRatioWithTokenPositionChanges ${debug}` +
|
2022-08-23 07:23:20 -07:00
|
|
|
mangoAccount.simHealthRatioWithTokenPositionUiChanges(group, [change]),
|
2022-08-23 04:47:08 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
for (const srcToken of Array.from(group.banksMapByName.keys())) {
|
|
|
|
simHealthRatioWithTokenPositionChangesWrapper(`${srcToken} 1 `, {
|
2022-09-01 00:48:23 -07:00
|
|
|
mintPk: group.banksMapByName.get(srcToken)![0].mint,
|
2022-08-23 04:47:08 -07:00
|
|
|
uiTokenAmount: 1,
|
|
|
|
});
|
|
|
|
simHealthRatioWithTokenPositionChangesWrapper(`${srcToken} -1 `, {
|
2022-09-01 00:48:23 -07:00
|
|
|
mintPk: group.banksMapByName.get(srcToken)![0].mint,
|
2022-08-23 04:47:08 -07:00
|
|
|
uiTokenAmount: -1,
|
|
|
|
});
|
|
|
|
}
|
2022-08-11 08:44:12 -07:00
|
|
|
|
|
|
|
function getMaxSourceForTokenSwapWrapper(src, tgt) {
|
|
|
|
console.log(
|
|
|
|
`getMaxSourceForTokenSwap ${src.padEnd(4)} ${tgt.padEnd(4)} ` +
|
2022-09-23 02:43:26 -07:00
|
|
|
mangoAccount.getMaxSourceUiForTokenSwap(
|
|
|
|
group,
|
|
|
|
group.banksMapByName.get(src)![0].mint,
|
|
|
|
group.banksMapByName.get(tgt)![0].mint,
|
|
|
|
1,
|
|
|
|
),
|
2022-08-11 08:44:12 -07:00
|
|
|
);
|
|
|
|
}
|
2022-08-23 04:47:08 -07:00
|
|
|
for (const srcToken of Array.from(group.banksMapByName.keys())) {
|
|
|
|
for (const tgtToken of Array.from(group.banksMapByName.keys())) {
|
|
|
|
// if (srcToken === 'SOL')
|
|
|
|
// if (tgtToken === 'MSOL')
|
|
|
|
getMaxSourceForTokenSwapWrapper(srcToken, tgtToken);
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 00:34:08 -07:00
|
|
|
|
2022-09-23 02:43:26 -07:00
|
|
|
function getMaxForPerpWrapper(perpMarket: PerpMarket) {
|
|
|
|
console.log(
|
2022-09-30 04:33:21 -07:00
|
|
|
`getMaxQuoteForPerpBidUi ${perpMarket.perpMarketIndex} ` +
|
2022-10-09 23:07:27 -07:00
|
|
|
mangoAccount.getMaxQuoteForPerpBidUi(group, perpMarket.perpMarketIndex),
|
2022-09-23 02:43:26 -07:00
|
|
|
);
|
|
|
|
console.log(
|
2022-09-30 04:33:21 -07:00
|
|
|
`getMaxBaseForPerpAskUi ${perpMarket.perpMarketIndex} ` +
|
2022-10-09 23:07:27 -07:00
|
|
|
mangoAccount.getMaxBaseForPerpAskUi(group, perpMarket.perpMarketIndex),
|
2022-09-23 02:43:26 -07:00
|
|
|
);
|
|
|
|
}
|
2022-09-30 04:33:21 -07:00
|
|
|
for (const perpMarket of Array.from(
|
|
|
|
group.perpMarketsMapByMarketIndex.values(),
|
|
|
|
)) {
|
2022-09-23 02:43:26 -07:00
|
|
|
getMaxForPerpWrapper(perpMarket);
|
|
|
|
}
|
|
|
|
|
2022-09-23 00:34:08 -07:00
|
|
|
function getMaxForSerum3Wrapper(serum3Market: Serum3Market) {
|
|
|
|
// if (serum3Market.name !== 'SOL/USDC') return;
|
|
|
|
console.log(
|
|
|
|
`getMaxQuoteForSerum3BidUi ${serum3Market.name} ` +
|
|
|
|
mangoAccount.getMaxQuoteForSerum3BidUi(
|
|
|
|
group,
|
|
|
|
serum3Market.serumMarketExternal,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
`getMaxBaseForSerum3AskUi ${serum3Market.name} ` +
|
|
|
|
mangoAccount.getMaxBaseForSerum3AskUi(
|
|
|
|
group,
|
|
|
|
serum3Market.serumMarketExternal,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for (const serum3Market of Array.from(
|
|
|
|
group.serum3MarketsMapByExternal.values(),
|
|
|
|
)) {
|
|
|
|
getMaxForSerum3Wrapper(serum3Market);
|
|
|
|
}
|
2022-08-09 22:32:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const options = AnchorProvider.defaultOptions();
|
2022-09-23 02:43:26 -07:00
|
|
|
const connection = new Connection(CLUSTER_URL!, options);
|
2022-08-09 22:32:21 -07:00
|
|
|
|
|
|
|
const admin = Keypair.fromSecretKey(
|
2022-09-23 02:43:26 -07:00
|
|
|
Buffer.from(JSON.parse(fs.readFileSync(PAYER_KEYPAIR!, 'utf-8'))),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
console.log(`Admin ${admin.publicKey.toBase58()}`);
|
|
|
|
|
|
|
|
const adminWallet = new Wallet(admin);
|
|
|
|
const adminProvider = new AnchorProvider(connection, adminWallet, options);
|
|
|
|
const client = MangoClient.connect(
|
|
|
|
adminProvider,
|
2022-09-23 02:43:26 -07:00
|
|
|
CLUSTER,
|
|
|
|
MANGO_V4_ID[CLUSTER],
|
|
|
|
{},
|
|
|
|
'get-program-accounts',
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
|
2022-09-23 02:43:26 -07:00
|
|
|
const group = await client.getGroupForCreator(admin.publicKey, GROUP_NUM);
|
2022-08-09 22:32:21 -07:00
|
|
|
|
2022-09-23 02:43:26 -07:00
|
|
|
for (const keypair of [USER_KEYPAIR!]) {
|
2022-08-09 22:32:21 -07:00
|
|
|
console.log();
|
|
|
|
const user = Keypair.fromSecretKey(
|
|
|
|
Buffer.from(JSON.parse(fs.readFileSync(keypair, 'utf-8'))),
|
|
|
|
);
|
|
|
|
const userWallet = new Wallet(user);
|
|
|
|
console.log(`User ${userWallet.publicKey.toBase58()}`);
|
2022-08-26 01:09:36 -07:00
|
|
|
|
|
|
|
const mangoAccounts = await client.getMangoAccountsForOwner(
|
|
|
|
group,
|
|
|
|
user.publicKey,
|
|
|
|
);
|
|
|
|
|
|
|
|
// const mangoAccounts = await Promise.all([
|
|
|
|
// await client.getMangoAccount({
|
|
|
|
// publicKey: new PublicKey(
|
|
|
|
// '6mqHfpJqnXcu6RgDYZSVW9CQXQPFyRYhgvdzvWXN9mPW',
|
|
|
|
// ),
|
|
|
|
// } as any),
|
|
|
|
// ]);
|
|
|
|
|
2022-08-18 23:41:57 -07:00
|
|
|
for (const mangoAccount of mangoAccounts) {
|
2022-08-23 07:23:20 -07:00
|
|
|
console.log(`MangoAccount ${mangoAccount.publicKey}`);
|
2022-09-29 06:51:09 -07:00
|
|
|
if (mangoAccount.name === 'PnL Test') {
|
|
|
|
await debugUser(client, group, mangoAccount);
|
|
|
|
}
|
2022-08-18 23:41:57 -07:00
|
|
|
}
|
2022-08-09 22:32:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
main();
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|