2022-08-09 22:32:21 -07:00
|
|
|
import { AnchorProvider, Wallet } from '@project-serum/anchor';
|
2022-08-26 01:09:36 -07:00
|
|
|
import { 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-08-26 01:10:33 -07:00
|
|
|
import { I80F48 } from '../accounts/I80F48';
|
2022-08-23 04:47:08 -07:00
|
|
|
import { HealthType, MangoAccount } from '../accounts/mangoAccount';
|
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-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)} ` +
|
|
|
|
mangoAccount
|
2022-08-18 22:43:32 -07:00
|
|
|
.getMaxSourceForTokenSwap(
|
|
|
|
group,
|
2022-09-01 00:48:23 -07:00
|
|
|
group.banksMapByName.get(src)![0].mint,
|
|
|
|
group.banksMapByName.get(tgt)![0].mint,
|
2022-08-23 04:47:08 -07:00
|
|
|
1,
|
2022-09-01 00:48:23 -07:00
|
|
|
)!
|
2022-08-11 08:44:12 -07:00
|
|
|
.div(
|
|
|
|
I80F48.fromNumber(
|
2022-09-01 00:48:23 -07:00
|
|
|
Math.pow(10, group.banksMapByName.get(src)![0].mintDecimals),
|
2022-08-11 08:44:12 -07:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.toNumber(),
|
|
|
|
);
|
|
|
|
}
|
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-08-09 22:32:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const options = AnchorProvider.defaultOptions();
|
|
|
|
const connection = new Connection(process.env.MB_CLUSTER_URL!, options);
|
|
|
|
|
|
|
|
const admin = Keypair.fromSecretKey(
|
|
|
|
Buffer.from(
|
|
|
|
JSON.parse(fs.readFileSync(process.env.MB_PAYER_KEYPAIR!, 'utf-8')),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
console.log(`Admin ${admin.publicKey.toBase58()}`);
|
|
|
|
|
|
|
|
const adminWallet = new Wallet(admin);
|
|
|
|
const adminProvider = new AnchorProvider(connection, adminWallet, options);
|
|
|
|
const client = MangoClient.connect(
|
|
|
|
adminProvider,
|
|
|
|
'mainnet-beta',
|
|
|
|
MANGO_V4_ID['mainnet-beta'],
|
|
|
|
);
|
|
|
|
|
2022-08-14 04:30:08 -07:00
|
|
|
const group = await client.getGroupForCreator(admin.publicKey, 2);
|
2022-08-09 22:32:21 -07:00
|
|
|
|
|
|
|
for (const keypair of [
|
2022-09-01 00:48:23 -07:00
|
|
|
process.env.MB_PAYER_KEYPAIR!,
|
|
|
|
process.env.MB_USER2_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-08-26 01:45:50 -07:00
|
|
|
if (mangoAccount.name === '2nd Account') {
|
2022-08-23 04:47:08 -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);
|
|
|
|
}
|