ts: match client class definitions 1-1 to program state struct definitions (#268)

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-10-11 09:34:02 +02:00 committed by GitHub
parent 286563b0ef
commit a66177a77e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 35 deletions

View File

@ -84,7 +84,7 @@ pub struct MangoAccount {
pub net_settled: i64, pub net_settled: i64,
/// Init health as calculated during HealthReginBegin, rounded up. /// Init health as calculated during HealthReginBegin, rounded up.
pub health_region_pre_init_health: i64, pub health_region_begin_init_health: i64,
pub reserved: [u8; 240], pub reserved: [u8; 240],
@ -120,7 +120,7 @@ impl MangoAccount {
padding: Default::default(), padding: Default::default(),
net_deposits: 0, net_deposits: 0,
net_settled: 0, net_settled: 0,
health_region_pre_init_health: 0, health_region_begin_init_health: 0,
reserved: [0; 240], reserved: [0; 240],
header_version: DEFAULT_MANGO_ACCOUNT_VERSION, header_version: DEFAULT_MANGO_ACCOUNT_VERSION,
padding3: Default::default(), padding3: Default::default(),
@ -1065,7 +1065,7 @@ mod tests {
account.bump = 4; account.bump = 4;
account.net_deposits = 5; account.net_deposits = 5;
account.net_settled = 6; account.net_settled = 6;
account.health_region_pre_init_health = 7; account.health_region_begin_init_health = 7;
account.tokens.resize(8, TokenPosition::default()); account.tokens.resize(8, TokenPosition::default());
account.tokens[0].token_index = 8; account.tokens[0].token_index = 8;
account.serum3.resize(8, Serum3Orders::default()); account.serum3.resize(8, Serum3Orders::default());
@ -1091,7 +1091,7 @@ mod tests {
assert_eq!(account.net_deposits, account2.fixed.net_deposits); assert_eq!(account.net_deposits, account2.fixed.net_deposits);
assert_eq!(account.net_settled, account2.fixed.net_settled); assert_eq!(account.net_settled, account2.fixed.net_settled);
assert_eq!( assert_eq!(
account.health_region_pre_init_health, account.health_region_begin_init_health,
account2.fixed.health_region_begin_init_health account2.fixed.health_region_begin_init_health
); );
assert_eq!( assert_eq!(

View File

@ -12,12 +12,11 @@ import { HealthCache } from './healthCache';
import { PerpMarket, PerpMarketIndex, PerpOrder, PerpOrderSide } from './perp'; import { PerpMarket, PerpMarketIndex, PerpOrder, PerpOrderSide } from './perp';
import { MarketIndex, Serum3Side } from './serum3'; import { MarketIndex, Serum3Side } from './serum3';
export class MangoAccount { export class MangoAccount {
public name: string;
public tokens: TokenPosition[]; public tokens: TokenPosition[];
public serum3: Serum3Orders[]; public serum3: Serum3Orders[];
public perps: PerpPosition[]; public perps: PerpPosition[];
public perpOpenOrders: PerpOo[]; public perpOpenOrders: PerpOo[];
public name: string;
public netDeposits: BN;
static from( static from(
publicKey: PublicKey, publicKey: PublicKey,
@ -26,29 +25,31 @@ export class MangoAccount {
owner: PublicKey; owner: PublicKey;
name: number[]; name: number[];
delegate: PublicKey; delegate: PublicKey;
beingLiquidated: number;
accountNum: number; accountNum: number;
bump: number; beingLiquidated: number;
inHealthRegion: number;
netDeposits: BN; netDeposits: BN;
netSettled: BN; netSettled: BN;
healthRegionBeginInitHealth: BN;
headerVersion: number; headerVersion: number;
tokens: unknown; tokens: unknown;
serum3: unknown; serum3: unknown;
perps: unknown; perps: unknown;
perpOpenOrders: unknown; perpOpenOrders: unknown;
}, },
) { ): MangoAccount {
return new MangoAccount( return new MangoAccount(
publicKey, publicKey,
obj.group, obj.group,
obj.owner, obj.owner,
obj.name, obj.name,
obj.delegate, obj.delegate,
obj.beingLiquidated,
obj.accountNum, obj.accountNum,
obj.bump, obj.beingLiquidated == 1,
obj.inHealthRegion == 1,
obj.netDeposits, obj.netDeposits,
obj.netSettled, obj.netSettled,
obj.healthRegionBeginInitHealth,
obj.headerVersion, obj.headerVersion,
obj.tokens as TokenPositionDto[], obj.tokens as TokenPositionDto[],
obj.serum3 as Serum3PositionDto[], obj.serum3 as Serum3PositionDto[],
@ -64,12 +65,13 @@ export class MangoAccount {
public owner: PublicKey, public owner: PublicKey,
name: number[], name: number[],
public delegate: PublicKey, public delegate: PublicKey,
beingLiquidated: number,
public accountNum: number, public accountNum: number,
bump: number, public beingLiquidated: boolean,
netDeposits: BN, public inHealthRegion: boolean,
netSettled: BN, public netDeposits: BN,
headerVersion: number, public netSettled: BN,
public healthRegionBeginInitHealth: BN,
public headerVersion: number,
tokens: TokenPositionDto[], tokens: TokenPositionDto[],
serum3: Serum3PositionDto[], serum3: Serum3PositionDto[],
perps: PerpPositionDto[], perps: PerpPositionDto[],
@ -81,7 +83,6 @@ export class MangoAccount {
this.serum3 = serum3.map((dto) => Serum3Orders.from(dto)); this.serum3 = serum3.map((dto) => Serum3Orders.from(dto));
this.perps = perps.map((dto) => PerpPosition.from(dto)); this.perps = perps.map((dto) => PerpPosition.from(dto));
this.perpOpenOrders = perpOpenOrders.map((dto) => PerpOo.from(dto)); this.perpOpenOrders = perpOpenOrders.map((dto) => PerpOo.from(dto));
this.netDeposits = netDeposits;
} }
async reload(client: MangoClient): Promise<MangoAccount> { async reload(client: MangoClient): Promise<MangoAccount> {

View File

@ -5,7 +5,7 @@ import Big from 'big.js';
import { MangoClient } from '../client'; import { MangoClient } from '../client';
import { I80F48, I80F48Dto } from '../numbers/I80F48'; import { I80F48, I80F48Dto } from '../numbers/I80F48';
import { As, toNative, U64_MAX_BN } from '../utils'; import { As, toNative, U64_MAX_BN } from '../utils';
import { OracleConfig, QUOTE_DECIMALS } from './bank'; import { OracleConfig, QUOTE_DECIMALS, TokenIndex } from './bank';
export type PerpMarketIndex = number & As<'perp-market-index'>; export type PerpMarketIndex = number & As<'perp-market-index'>;
@ -22,21 +22,24 @@ export class PerpMarket {
public maxFunding: I80F48; public maxFunding: I80F48;
public longFunding: I80F48; public longFunding: I80F48;
public shortFunding: I80F48; public shortFunding: I80F48;
public openInterest: BN;
public seqNum: BN;
public feesAccrued: I80F48; public feesAccrued: I80F48;
priceLotsToUiConverter: number; public feesSettled: I80F48;
baseLotsToUiConverter: number;
quoteLotsToUiConverter: number;
public _price: I80F48; public _price: I80F48;
public _uiPrice: number; public _uiPrice: number;
private priceLotsToUiConverter: number;
private baseLotsToUiConverter: number;
private quoteLotsToUiConverter: number;
static from( static from(
publicKey: PublicKey, publicKey: PublicKey,
obj: { obj: {
group: PublicKey; group: PublicKey;
settleTokenIndex: number;
perpMarketIndex: number; perpMarketIndex: number;
trustedMarket: number; trustedMarket: number;
groupInsuranceFund: number;
name: number[]; name: number[];
oracle: PublicKey; oracle: PublicKey;
oracleConfig: OracleConfig; oracleConfig: OracleConfig;
@ -61,16 +64,22 @@ export class PerpMarket {
openInterest: BN; openInterest: BN;
seqNum: BN; seqNum: BN;
feesAccrued: I80F48Dto; feesAccrued: I80F48Dto;
bump: number;
baseDecimals: number; baseDecimals: number;
registrationTime: BN; registrationTime: BN;
feesSettled: I80F48Dto;
feePenalty: number;
settleFeeFlat: number;
settleFeeAmountThreshold: number;
settleFeeFractionLowHealth: number;
}, },
): PerpMarket { ): PerpMarket {
return new PerpMarket( return new PerpMarket(
publicKey, publicKey,
obj.group, obj.group,
obj.settleTokenIndex as TokenIndex,
obj.perpMarketIndex as PerpMarketIndex, obj.perpMarketIndex as PerpMarketIndex,
obj.trustedMarket == 1, obj.trustedMarket == 1,
obj.groupInsuranceFund == 1,
obj.name, obj.name,
obj.oracle, obj.oracle,
obj.oracleConfig, obj.oracleConfig,
@ -95,17 +104,23 @@ export class PerpMarket {
obj.openInterest, obj.openInterest,
obj.seqNum, obj.seqNum,
obj.feesAccrued, obj.feesAccrued,
obj.bump,
obj.baseDecimals, obj.baseDecimals,
obj.registrationTime, obj.registrationTime,
obj.feesSettled,
obj.feePenalty,
obj.settleFeeFlat,
obj.settleFeeAmountThreshold,
obj.settleFeeFractionLowHealth,
); );
} }
constructor( constructor(
public publicKey: PublicKey, public publicKey: PublicKey,
public group: PublicKey, public group: PublicKey,
public settleTokenIndex: TokenIndex,
public perpMarketIndex: PerpMarketIndex, // TODO rename to marketIndex? public perpMarketIndex: PerpMarketIndex, // TODO rename to marketIndex?
public trustedMarket: boolean, public trustedMarket: boolean,
public groupInsuranceFund: boolean,
name: number[], name: number[],
public oracle: PublicKey, public oracle: PublicKey,
oracleConfig: OracleConfig, oracleConfig: OracleConfig,
@ -126,13 +141,17 @@ export class PerpMarket {
public impactQuantity: BN, public impactQuantity: BN,
longFunding: I80F48Dto, longFunding: I80F48Dto,
shortFunding: I80F48Dto, shortFunding: I80F48Dto,
fundingLastUpdated: BN, public fundingLastUpdated: BN,
openInterest: BN, public openInterest: BN,
seqNum: BN, public seqNum: BN,
feesAccrued: I80F48Dto, feesAccrued: I80F48Dto,
bump: number,
public baseDecimals: number, public baseDecimals: number,
public registrationTime: BN, public registrationTime: BN,
feesSettled: I80F48Dto,
public feePenalty: number,
public settleFeeFlat: number,
public settleFeeAmountThreshold: number,
public settleFeeFractionLowHealth: number,
) { ) {
this.name = utf8.decode(new Uint8Array(name)).split('\x00')[0]; this.name = utf8.decode(new Uint8Array(name)).split('\x00')[0];
this.maintAssetWeight = I80F48.from(maintAssetWeight); this.maintAssetWeight = I80F48.from(maintAssetWeight);
@ -146,9 +165,8 @@ export class PerpMarket {
this.maxFunding = I80F48.from(maxFunding); this.maxFunding = I80F48.from(maxFunding);
this.longFunding = I80F48.from(longFunding); this.longFunding = I80F48.from(longFunding);
this.shortFunding = I80F48.from(shortFunding); this.shortFunding = I80F48.from(shortFunding);
this.openInterest = openInterest;
this.seqNum = seqNum;
this.feesAccrued = I80F48.from(feesAccrued); this.feesAccrued = I80F48.from(feesAccrued);
this.feesSettled = I80F48.from(feesSettled);
this.priceLotsToUiConverter = new Big(10) this.priceLotsToUiConverter = new Big(10)
.pow(baseDecimals - QUOTE_DECIMALS) .pow(baseDecimals - QUOTE_DECIMALS)

View File

@ -4,10 +4,10 @@ import { Cluster, PublicKey } from '@solana/web3.js';
import BN from 'bn.js'; import BN from 'bn.js';
import { MangoClient } from '../client'; import { MangoClient } from '../client';
import { SERUM3_PROGRAM_ID } from '../constants'; import { SERUM3_PROGRAM_ID } from '../constants';
import { MAX_I80F48, ONE_I80F48, ZERO_I80F48 } from '../numbers/I80F48';
import { As } from '../utils'; import { As } from '../utils';
import { TokenIndex } from './bank'; import { TokenIndex } from './bank';
import { Group } from './group'; import { Group } from './group';
import { MAX_I80F48, ONE_I80F48, ZERO_I80F48 } from '../numbers/I80F48';
export type MarketIndex = number & As<'market-index'>; export type MarketIndex = number & As<'market-index'>;
@ -23,7 +23,6 @@ export class Serum3Market {
serumProgram: PublicKey; serumProgram: PublicKey;
serumMarketExternal: PublicKey; serumMarketExternal: PublicKey;
marketIndex: number; marketIndex: number;
bump: number;
registrationTime: BN; registrationTime: BN;
}, },
): Serum3Market { ): Serum3Market {

View File

@ -3630,7 +3630,7 @@ export type MangoV4 = {
"type": "i64" "type": "i64"
}, },
{ {
"name": "healthRegionPreInitHealth", "name": "healthRegionBeginInitHealth",
"docs": [ "docs": [
"Init health as calculated during HealthReginBegin, rounded up." "Init health as calculated during HealthReginBegin, rounded up."
], ],
@ -9906,7 +9906,7 @@ export const IDL: MangoV4 = {
"type": "i64" "type": "i64"
}, },
{ {
"name": "healthRegionPreInitHealth", "name": "healthRegionBeginInitHealth",
"docs": [ "docs": [
"Init health as calculated during HealthReginBegin, rounded up." "Init health as calculated during HealthReginBegin, rounded up."
], ],