added functions
This commit is contained in:
parent
ee877e69d0
commit
0ab3bbab24
|
@ -13,7 +13,7 @@ import {
|
||||||
import {
|
import {
|
||||||
encodeMangoInstruction,
|
encodeMangoInstruction,
|
||||||
MangoGroupLayout,
|
MangoGroupLayout,
|
||||||
MarginAccountLayout, NUM_TOKENS,
|
MarginAccountLayout, NUM_MARKETS, NUM_TOKENS,
|
||||||
WideBits,
|
WideBits,
|
||||||
} from './layout';
|
} from './layout';
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
|
@ -90,6 +90,8 @@ export class MangoGroup {
|
||||||
|
|
||||||
export class MarginAccount {
|
export class MarginAccount {
|
||||||
publicKey: PublicKey;
|
publicKey: PublicKey;
|
||||||
|
createTime: number; // used to determine when to update
|
||||||
|
// TODO maybe this is obviated by websocket feed onUpdate
|
||||||
|
|
||||||
accountFlags!: WideBits;
|
accountFlags!: WideBits;
|
||||||
mangoGroup!: PublicKey;
|
mangoGroup!: PublicKey;
|
||||||
|
@ -99,9 +101,12 @@ export class MarginAccount {
|
||||||
openOrders!: PublicKey[];
|
openOrders!: PublicKey[];
|
||||||
|
|
||||||
openOrdersAccounts: undefined | (OpenOrders | undefined)[] // undefined if an openOrdersAccount not yet initialized and has zeroKey
|
openOrdersAccounts: undefined | (OpenOrders | undefined)[] // undefined if an openOrdersAccount not yet initialized and has zeroKey
|
||||||
|
// TODO keep updated with websocket
|
||||||
|
|
||||||
constructor(publicKey: PublicKey, decoded: any) {
|
constructor(publicKey: PublicKey, decoded: any) {
|
||||||
this.publicKey = publicKey;
|
this.publicKey = publicKey
|
||||||
Object.assign(this, decoded);
|
this.createTime = getUnixTs()
|
||||||
|
Object.assign(this, decoded)
|
||||||
}
|
}
|
||||||
|
|
||||||
getNativeDeposit(mangoGroup: MangoGroup, tokenIndex: number): number { // insufficient precision
|
getNativeDeposit(mangoGroup: MangoGroup, tokenIndex: number): number { // insufficient precision
|
||||||
|
@ -174,6 +179,65 @@ export class MarginAccount {
|
||||||
|
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getAssetsVal(mangoGroup: MangoGroup, prices: number[]): number {
|
||||||
|
let assetsVal = 0
|
||||||
|
for (let i = 0; i < NUM_TOKENS; i++) {
|
||||||
|
assetsVal += this.getUiDeposit(mangoGroup, i) * prices[i]
|
||||||
|
}
|
||||||
|
if (this.openOrdersAccounts == undefined) {
|
||||||
|
return assetsVal
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < NUM_MARKETS; i++) {
|
||||||
|
const openOrdersAccount = this.openOrdersAccounts[i]
|
||||||
|
if (openOrdersAccount == undefined) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
assetsVal += nativeToUi(openOrdersAccount.baseTokenTotal.toNumber(), mangoGroup.mintDecimals[i]) * prices[i]
|
||||||
|
assetsVal += nativeToUi(openOrdersAccount.quoteTokenTotal.toNumber(), mangoGroup.mintDecimals[NUM_TOKENS-1])
|
||||||
|
}
|
||||||
|
|
||||||
|
return assetsVal
|
||||||
|
}
|
||||||
|
|
||||||
|
getLiabsVal(mangoGroup: MangoGroup, prices: number[]) {
|
||||||
|
let liabsVal = 0
|
||||||
|
for (let i = 0; i < NUM_TOKENS; i++) {
|
||||||
|
liabsVal += this.getUiBorrow(mangoGroup, i) * prices[i]
|
||||||
|
}
|
||||||
|
return liabsVal
|
||||||
|
}
|
||||||
|
|
||||||
|
getCollateralRatio(mangoGroup: MangoGroup, prices: number[]): number {
|
||||||
|
let assetsVal = 0
|
||||||
|
let liabsVal = 0
|
||||||
|
for (let i = 0; i < NUM_TOKENS; i++) {
|
||||||
|
assetsVal += this.getUiDeposit(mangoGroup, i) * prices[i]
|
||||||
|
liabsVal += this.getUiBorrow(mangoGroup, i) * prices[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (liabsVal === 0) {
|
||||||
|
return 100
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.openOrdersAccounts == undefined) {
|
||||||
|
return assetsVal / liabsVal
|
||||||
|
}
|
||||||
|
for (let i = 0; i < NUM_MARKETS; i++) {
|
||||||
|
const openOrdersAccount = this.openOrdersAccounts[i]
|
||||||
|
if (openOrdersAccount == undefined) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
assetsVal += nativeToUi(openOrdersAccount.baseTokenTotal.toNumber(), mangoGroup.mintDecimals[i]) * prices[i]
|
||||||
|
assetsVal += nativeToUi(openOrdersAccount.quoteTokenTotal.toNumber(), mangoGroup.mintDecimals[NUM_TOKENS-1])
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return assetsVal / liabsVal
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MangoClient {
|
export class MangoClient {
|
||||||
|
@ -736,3 +800,7 @@ async function getFilteredProgramAccounts(
|
||||||
async function promiseUndef(): Promise<undefined> {
|
async function promiseUndef(): Promise<undefined> {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const getUnixTs = () => {
|
||||||
|
return new Date().getTime() / 1000;
|
||||||
|
};
|
||||||
|
|
26
src/ids.json
26
src/ids.json
|
@ -2,14 +2,8 @@
|
||||||
"cluster_urls": {
|
"cluster_urls": {
|
||||||
"devnet": "https://devnet.solana.com",
|
"devnet": "https://devnet.solana.com",
|
||||||
"localnet": "http://127.0.0.1:8899",
|
"localnet": "http://127.0.0.1:8899",
|
||||||
"mainnet": "https://api.mainnet-beta.solana.com"
|
"mainnet": "https://api.mainnet-beta.solana.com",
|
||||||
},
|
"testnet": "https://testnet.solana.com"
|
||||||
"mainnet-beta": {
|
|
||||||
"dex_program_id": "EUqojwWA2rd19FZrzeBncJsm38Jm1hEhE3zsmX3bRc2o",
|
|
||||||
"spot_markets": {
|
|
||||||
"BTC/USDC": "CVfYa8RGXnuDBeGmniCcdkBwoLqVxh92xB1JqgRQx3F",
|
|
||||||
"ETH/USDC": "H5uzEytiByuXt964KampmuNCurNDwkVVypkym75J2DQW"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"devnet": {
|
"devnet": {
|
||||||
"dex_program_id": "9MVDeYQnJmN2Dt7H44Z8cob4bET2ysdNu2uFJcatDJno",
|
"dex_program_id": "9MVDeYQnJmN2Dt7H44Z8cob4bET2ysdNu2uFJcatDJno",
|
||||||
|
@ -66,5 +60,21 @@
|
||||||
"ETH": "",
|
"ETH": "",
|
||||||
"USDC": ""
|
"USDC": ""
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"testnet": {
|
||||||
|
"dex_program_id": "",
|
||||||
|
"mango_groups": {
|
||||||
|
"BTC_ETH_USDC": {}
|
||||||
|
},
|
||||||
|
"mango_program_id": "",
|
||||||
|
"spot_markets": {
|
||||||
|
"BTC_USDC": "",
|
||||||
|
"ETH_USDC": ""
|
||||||
|
},
|
||||||
|
"symbols": {
|
||||||
|
"BTC": "BahhjqQVqw3LELEVfyhHyEp7Db1TbFUNVrMiSsmTaBR2",
|
||||||
|
"ETH": "Dw91oaC5UGxzSDGaFontF1JS3XoViQV3EaTAgdtRPprM",
|
||||||
|
"USDC": "9q4p8UFxphSipGL3TGku8byTijgk4koTMwhBMV4QKvjw"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue