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';
|
|
|
|
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-08-09 22:32:21 -07:00
|
|
|
await mangoAccount.reload(client, group);
|
2022-08-11 08:44:12 -07:00
|
|
|
|
2022-08-10 08:17:16 -07:00
|
|
|
console.log(
|
|
|
|
'buildFixedAccountRetrieverHealthAccounts ' +
|
|
|
|
client
|
2022-08-31 02:36:44 -07:00
|
|
|
.buildFixedAccountRetrieverHealthAccounts(
|
|
|
|
group,
|
|
|
|
mangoAccount,
|
|
|
|
[
|
2022-09-01 00:48:23 -07:00
|
|
|
group.banksMapByName.get('BTC')![0],
|
|
|
|
group.banksMapByName.get('USDC')![0],
|
2022-08-31 02:36:44 -07:00
|
|
|
],
|
|
|
|
[],
|
|
|
|
)
|
2022-08-10 08:17:16 -07:00
|
|
|
.map((pk) => pk.toBase58())
|
|
|
|
.join(', '),
|
|
|
|
);
|
2022-08-09 22:32:21 -07:00
|
|
|
console.log(
|
|
|
|
'mangoAccount.getEquity() ' +
|
2022-09-01 00:48:23 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getEquity()!.toNumber()),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealth(HealthType.init) ' +
|
2022-09-01 00:48:23 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getHealth(HealthType.init)!.toNumber()),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatio(HealthType.init) ' +
|
2022-09-01 00:48:23 -07:00
|
|
|
mangoAccount.getHealthRatio(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) ' +
|
|
|
|
mangoAccount.getHealthRatioUi(HealthType.init),
|
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatio(HealthType.maint) ' +
|
2022-09-01 00:48:23 -07:00
|
|
|
mangoAccount.getHealthRatio(HealthType.maint)!.toNumber(),
|
2022-08-19 12:18:16 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatioUi(HealthType.maint) ' +
|
|
|
|
mangoAccount.getHealthRatioUi(HealthType.maint),
|
|
|
|
);
|
2022-08-09 22:32:21 -07:00
|
|
|
console.log(
|
|
|
|
'mangoAccount.getCollateralValue() ' +
|
2022-09-01 00:48:23 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getCollateralValue()!.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-01 00:48:23 -07:00
|
|
|
mangoAccount.getAssetsValue(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-01 00:48:23 -07:00
|
|
|
mangoAccount.getLiabsValue(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(
|
|
|
|
`getMaxQuoteForPerpBidUi ${perpMarket.name} ` +
|
|
|
|
mangoAccount.getMaxQuoteForPerpBidUi(
|
|
|
|
group,
|
|
|
|
perpMarket.name,
|
|
|
|
perpMarket.price,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
`getMaxBaseForPerpAskUi ${perpMarket.name} ` +
|
|
|
|
mangoAccount.getMaxBaseForPerpAskUi(
|
|
|
|
group,
|
|
|
|
perpMarket.name,
|
|
|
|
perpMarket.price,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for (const perpMarket of Array.from(group.perpMarketsMap.values())) {
|
|
|
|
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-23 00:34:08 -07:00
|
|
|
// if (mangoAccount.name === 'PnL Test') {
|
2022-09-01 02:27:22 -07:00
|
|
|
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);
|
|
|
|
}
|