2022-08-09 22:32:21 -07:00
|
|
|
import { AnchorProvider, Wallet } from '@project-serum/anchor';
|
|
|
|
import { Connection, Keypair } from '@solana/web3.js';
|
|
|
|
import fs from 'fs';
|
2022-08-11 08:44:12 -07:00
|
|
|
import { I80F48 } from '../accounts/I80F48';
|
2022-08-09 22:32:21 -07:00
|
|
|
import { HealthType } from '../accounts/mangoAccount';
|
|
|
|
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
|
|
|
|
|
|
|
async function debugUser(client, group, mangoAccount) {
|
|
|
|
console.log(mangoAccount.toString(group));
|
|
|
|
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
|
|
|
|
.buildFixedAccountRetrieverHealthAccounts(group, mangoAccount, [
|
2022-08-18 22:43:32 -07:00
|
|
|
group.banksMapByName.get('BTC')[0],
|
|
|
|
group.banksMapByName.get('USDC')[0],
|
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-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getEquity().toNumber()),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealth(HealthType.init) ' +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getHealth(HealthType.init).toNumber()),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatio(HealthType.init) ' +
|
|
|
|
mangoAccount.getHealthRatio(HealthType.init).toNumber(),
|
|
|
|
);
|
2022-08-19 12:18:16 -07:00
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatioUi(HealthType.init) ' +
|
|
|
|
mangoAccount.getHealthRatioUi(HealthType.init),
|
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatio(HealthType.maint) ' +
|
|
|
|
mangoAccount.getHealthRatio(HealthType.maint).toNumber(),
|
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatioUi(HealthType.maint) ' +
|
|
|
|
mangoAccount.getHealthRatioUi(HealthType.maint),
|
|
|
|
);
|
2022-08-09 22:32:21 -07:00
|
|
|
console.log(
|
|
|
|
'mangoAccount.getCollateralValue() ' +
|
2022-08-11 08:44:12 -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-08-18 22:43:32 -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-08-18 22:43:32 -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-18 22:43:32 -07:00
|
|
|
console.log(group.banksMapByName.get('SOL')[0].mint.toBase58());
|
|
|
|
|
2022-08-09 22:32:21 -07:00
|
|
|
console.log(
|
|
|
|
"mangoAccount.getMaxWithdrawWithBorrowForToken(group, 'SOL') " +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(
|
2022-08-09 22:32:21 -07:00
|
|
|
(
|
2022-08-18 22:43:32 -07:00
|
|
|
await mangoAccount.getMaxWithdrawWithBorrowForToken(
|
|
|
|
group,
|
|
|
|
group.banksMapByName.get('SOL')[0].mint,
|
|
|
|
)
|
2022-08-09 22:32:21 -07:00
|
|
|
).toNumber(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log(
|
2022-08-11 23:30:13 -07:00
|
|
|
'mangoAccount.simHealthRatioWithTokenPositionChanges ' +
|
|
|
|
(
|
|
|
|
await mangoAccount.simHealthRatioWithTokenPositionChanges(group, [
|
|
|
|
{
|
2022-08-18 22:43:32 -07:00
|
|
|
mintPk: group.banksMapByName.get('USDC')[0].mint,
|
2022-08-11 23:30:13 -07:00
|
|
|
tokenAmount:
|
2022-08-18 22:43:32 -07:00
|
|
|
-95_000 *
|
|
|
|
Math.pow(10, group.banksMapByName.get('USDC')[0]!.mintDecimals!),
|
2022-08-11 23:30:13 -07:00
|
|
|
},
|
|
|
|
{
|
2022-08-18 22:43:32 -07:00
|
|
|
mintPk: group.banksMapByName.get('BTC')[0].mint,
|
2022-08-11 23:30:13 -07:00
|
|
|
tokenAmount:
|
2022-08-18 22:43:32 -07:00
|
|
|
4 *
|
|
|
|
Math.pow(10, group.banksMapByName.get('BTC')[0]!.mintDecimals!),
|
2022-08-11 23:30:13 -07:00
|
|
|
},
|
|
|
|
])
|
|
|
|
).toNumber(),
|
2022-08-09 22:32:21 -07:00
|
|
|
);
|
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,
|
|
|
|
group.banksMapByName.get(src)[0].mint,
|
|
|
|
group.banksMapByName.get(tgt)[0].mint,
|
|
|
|
0.9,
|
|
|
|
)
|
2022-08-11 08:44:12 -07:00
|
|
|
.div(
|
|
|
|
I80F48.fromNumber(
|
2022-08-18 22:43:32 -07:00
|
|
|
Math.pow(10, group.banksMapByName.get(src)[0].mintDecimals),
|
2022-08-11 08:44:12 -07:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.toNumber(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
getMaxSourceForTokenSwapWrapper('SOL', 'BTC');
|
2022-08-15 11:16:46 -07:00
|
|
|
getMaxSourceForTokenSwapWrapper('USDC', 'USDC');
|
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
|
|
|
console.log(`${group.toString()}`);
|
|
|
|
|
|
|
|
for (const keypair of [
|
2022-08-12 01:21:06 -07:00
|
|
|
process.env.MB_PAYER_KEYPAIR,
|
2022-08-09 22:32:21 -07:00
|
|
|
process.env.MB_USER2_KEYPAIR,
|
|
|
|
]) {
|
|
|
|
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-18 23:41:57 -07:00
|
|
|
const mangoAccounts = await client.getMangoAccountsForOwner(
|
|
|
|
group,
|
|
|
|
user.publicKey,
|
|
|
|
);
|
|
|
|
for (const mangoAccount of mangoAccounts) {
|
|
|
|
console.log(`MangoAccount ${mangoAccount.publicKey}`);
|
|
|
|
await debugUser(client, group, mangoAccount);
|
|
|
|
}
|
2022-08-09 22:32:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
main();
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|