2022-06-23 06:22:59 -07:00
|
|
|
import { AnchorProvider, Wallet } from '@project-serum/anchor';
|
|
|
|
import { Connection, Keypair } from '@solana/web3.js';
|
|
|
|
import fs from 'fs';
|
2022-07-12 03:05:19 -07:00
|
|
|
import { HealthType } from '../accounts/mangoAccount';
|
2022-08-10 00:31:58 -07:00
|
|
|
import { MangoClient, MANGO_V4_ID } from '../index';
|
2022-06-23 06:22:59 -07:00
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const options = AnchorProvider.defaultOptions();
|
2022-08-09 04:50:37 -07:00
|
|
|
const connection = new Connection(process.env.MB_CLUSTER_URL!, options);
|
2022-06-23 06:22:59 -07:00
|
|
|
|
|
|
|
const user = Keypair.fromSecretKey(
|
|
|
|
Buffer.from(
|
2022-08-09 04:50:37 -07:00
|
|
|
JSON.parse(fs.readFileSync(process.env.MB_PAYER_KEYPAIR!, 'utf-8')),
|
2022-06-23 06:22:59 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
const userWallet = new Wallet(user);
|
|
|
|
const userProvider = new AnchorProvider(connection, userWallet, options);
|
2022-08-10 00:31:58 -07:00
|
|
|
const client = await MangoClient.connect(
|
2022-06-23 06:22:59 -07:00
|
|
|
userProvider,
|
2022-08-10 00:31:58 -07:00
|
|
|
'mainnet-beta',
|
|
|
|
MANGO_V4_ID['mainnet-beta'],
|
2022-06-23 06:22:59 -07:00
|
|
|
);
|
|
|
|
console.log(`User ${userWallet.publicKey.toBase58()}`);
|
|
|
|
|
|
|
|
const admin = Keypair.fromSecretKey(
|
|
|
|
Buffer.from(
|
2022-08-09 04:50:37 -07:00
|
|
|
JSON.parse(fs.readFileSync(process.env.MB_PAYER_KEYPAIR!, 'utf-8')),
|
2022-06-23 06:22:59 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
console.log(`Admin ${admin.publicKey.toBase58()}`);
|
|
|
|
|
2022-08-04 00:28:18 -07:00
|
|
|
const group = await client.getGroupForCreator(admin.publicKey, 0);
|
2022-07-04 06:34:16 -07:00
|
|
|
console.log(`${group.toString()}`);
|
|
|
|
|
|
|
|
// create + fetch account
|
|
|
|
console.log(`Creating mangoaccount...`);
|
|
|
|
const mangoAccount = await client.getOrCreateMangoAccount(
|
|
|
|
group,
|
2022-08-04 11:13:46 -07:00
|
|
|
user.publicKey,
|
2022-07-04 06:34:16 -07:00
|
|
|
);
|
|
|
|
console.log(`...created/found mangoAccount ${mangoAccount.publicKey}`);
|
|
|
|
console.log(mangoAccount.toString(group));
|
|
|
|
|
2022-08-10 00:31:58 -07:00
|
|
|
if (true) {
|
2022-07-14 00:47:42 -07:00
|
|
|
console.log(`...depositing 10 USDC`);
|
2022-08-04 08:35:05 -07:00
|
|
|
await client.tokenDeposit(group, mangoAccount, 'USDC', 10);
|
2022-07-14 00:47:42 -07:00
|
|
|
await mangoAccount.reload(client, group);
|
|
|
|
|
|
|
|
console.log(`...depositing 1 SOL`);
|
2022-08-04 08:35:05 -07:00
|
|
|
await client.tokenDeposit(group, mangoAccount, 'SOL', 1);
|
2022-07-14 00:47:42 -07:00
|
|
|
await mangoAccount.reload(client, group);
|
|
|
|
}
|
|
|
|
|
2022-07-14 05:29:44 -07:00
|
|
|
await mangoAccount.reload(client, group);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getEquity() ' +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getEquity().toNumber()),
|
2022-07-14 05:29:44 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealth(HealthType.init) ' +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getHealth(HealthType.init).toNumber()),
|
2022-07-14 05:29:44 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getHealthRatio(HealthType.init) ' +
|
|
|
|
mangoAccount.getHealthRatio(HealthType.init).toNumber(),
|
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getCollateralValue() ' +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getCollateralValue().toNumber()),
|
2022-07-14 05:29:44 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getAssetsVal() ' +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getAssetsVal().toNumber()),
|
2022-07-14 05:29:44 -07:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.getLiabsVal() ' +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(mangoAccount.getLiabsVal().toNumber()),
|
2022-07-14 05:29:44 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
"mangoAccount.getMaxWithdrawWithBorrowForToken(group, 'SOL') " +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(
|
2022-07-14 05:29:44 -07:00
|
|
|
(
|
|
|
|
await mangoAccount.getMaxWithdrawWithBorrowForToken(group, 'SOL')
|
|
|
|
).toNumber(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
"mangoAccount.getMaxSourceForTokenSwap(group, 'USDC', 'BTC') " +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(
|
2022-07-14 05:29:44 -07:00
|
|
|
(
|
|
|
|
await mangoAccount.getMaxSourceForTokenSwap(
|
|
|
|
group,
|
|
|
|
'USDC',
|
|
|
|
'BTC',
|
|
|
|
0.94,
|
|
|
|
)
|
|
|
|
).toNumber(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
'mangoAccount.simHealthWithTokenPositionChanges ' +
|
2022-08-11 08:44:12 -07:00
|
|
|
toUiDecimalsForQuote(
|
2022-07-14 05:29:44 -07:00
|
|
|
(
|
|
|
|
await mangoAccount.simHealthWithTokenPositionChanges(group, [
|
|
|
|
{
|
|
|
|
tokenName: 'USDC',
|
|
|
|
tokenAmount:
|
|
|
|
-20_000 *
|
|
|
|
Math.pow(10, group.banksMap.get('BTC')?.mintDecimals!),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tokenName: 'BTC',
|
|
|
|
tokenAmount:
|
|
|
|
1 * Math.pow(10, group.banksMap.get('BTC')?.mintDecimals!),
|
|
|
|
},
|
|
|
|
])
|
|
|
|
).toNumber(),
|
|
|
|
),
|
|
|
|
);
|
2022-07-04 06:34:16 -07:00
|
|
|
|
2022-06-23 06:22:59 -07:00
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|