add exampole
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
dff3f7cd8c
commit
301b00a452
|
@ -3,7 +3,7 @@ import { utf8 } from '@project-serum/anchor/dist/cjs/utils/bytes';
|
|||
import { PublicKey } from '@solana/web3.js';
|
||||
import { MangoClient } from '../client';
|
||||
import { nativeI80F48ToUi } from '../utils';
|
||||
import { Bank } from './bank';
|
||||
import { Bank, QUOTE_DECIMALS } from './bank';
|
||||
import { Group } from './group';
|
||||
import { I80F48, I80F48Dto, ONE_I80F48, ZERO_I80F48 } from './I80F48';
|
||||
export class MangoAccount {
|
||||
|
@ -91,6 +91,16 @@ export class MangoAccount {
|
|||
return ta ? ta.native(bank) : ZERO_I80F48;
|
||||
}
|
||||
|
||||
getInNativeUsdcUnits(bank: Bank): I80F48 {
|
||||
const ta = this.findToken(bank.tokenIndex);
|
||||
return ta
|
||||
? ta
|
||||
.native(bank)
|
||||
.mul(I80F48.fromNumber(Math.pow(10, QUOTE_DECIMALS)))
|
||||
.div(I80F48.fromNumber(Math.pow(10, bank.mintDecimals)))
|
||||
: ZERO_I80F48;
|
||||
}
|
||||
|
||||
getNativeDeposits(bank: Bank): I80F48 {
|
||||
const native = this.getNative(bank);
|
||||
return native.gte(ZERO_I80F48) ? native : ZERO_I80F48;
|
||||
|
@ -168,13 +178,11 @@ export class MangoAccount {
|
|||
tokenName: string,
|
||||
): Promise<I80F48> {
|
||||
const bank = group.banksMap.get(tokenName);
|
||||
|
||||
const initHealth = (this.accountData as MangoAccountData).initHealth;
|
||||
|
||||
const newInitHealth = initHealth.sub(
|
||||
this.getNativeDeposits(bank).mul(bank.price).mul(bank.initAssetWeight),
|
||||
);
|
||||
|
||||
const inUsdcUnits = this.getInNativeUsdcUnits(bank)
|
||||
.mul(bank.price)
|
||||
.mul(bank.initAssetWeight);
|
||||
const newInitHealth = initHealth.sub(inUsdcUnits);
|
||||
return newInitHealth.div(bank.price.mul(bank.initLiabWeight));
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ import { AnchorProvider, Wallet } from '@project-serum/anchor';
|
|||
import { Connection, Keypair } from '@solana/web3.js';
|
||||
import fs from 'fs';
|
||||
import { MangoClient } from '../client';
|
||||
import { toUiDecimals } from '../utils';
|
||||
|
||||
async function main() {
|
||||
const options = AnchorProvider.defaultOptions();
|
||||
|
@ -9,7 +10,9 @@ async function main() {
|
|||
|
||||
const user = Keypair.fromSecretKey(
|
||||
Buffer.from(
|
||||
JSON.parse(fs.readFileSync(process.env.USER_KEYPAIR!, 'utf-8')),
|
||||
JSON.parse(
|
||||
fs.readFileSync(process.env.MANGO_MAINNET_PAYER_KEYPAIR!, 'utf-8'),
|
||||
),
|
||||
),
|
||||
);
|
||||
const userWallet = new Wallet(user);
|
||||
|
@ -30,7 +33,47 @@ async function main() {
|
|||
console.log(`Admin ${admin.publicKey.toBase58()}`);
|
||||
|
||||
const group = await client.getGroupForAdmin(admin.publicKey, 0);
|
||||
console.log(`Found group ${group.publicKey.toBase58()}`);
|
||||
console.log(`${group.toString()}`);
|
||||
|
||||
// create + fetch account
|
||||
console.log(`Creating mangoaccount...`);
|
||||
const mangoAccount = await client.getOrCreateMangoAccount(
|
||||
group,
|
||||
user.publicKey,
|
||||
0,
|
||||
'my_mango_account',
|
||||
);
|
||||
console.log(`...created/found mangoAccount ${mangoAccount.publicKey}`);
|
||||
console.log(mangoAccount.toString(group));
|
||||
|
||||
if (true) {
|
||||
await mangoAccount.reload(client, group);
|
||||
console.log(
|
||||
'mangoAccount.getEquity() ' +
|
||||
toUiDecimals(mangoAccount.getEquity().toNumber()),
|
||||
);
|
||||
console.log(
|
||||
'mangoAccount.getCollateralValue() ' +
|
||||
toUiDecimals(mangoAccount.getCollateralValue().toNumber()),
|
||||
);
|
||||
console.log(
|
||||
'mangoAccount.getAssetsVal() ' +
|
||||
toUiDecimals(mangoAccount.getAssetsVal().toNumber()),
|
||||
);
|
||||
console.log(
|
||||
'mangoAccount.getLiabsVal() ' +
|
||||
toUiDecimals(mangoAccount.getLiabsVal().toNumber()),
|
||||
);
|
||||
console.log(
|
||||
"mangoAccount.getMaxWithdrawWithBorrowForToken(group, 'SOL') " +
|
||||
toUiDecimals(
|
||||
(
|
||||
await mangoAccount.getMaxWithdrawWithBorrowForToken(group, 'SOL')
|
||||
).toNumber(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
process.exit();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue