2022-04-08 08:21:49 -07:00
|
|
|
import { PublicKey } from '@solana/web3.js';
|
2022-04-02 23:57:45 -07:00
|
|
|
import { Bank } from './bank';
|
|
|
|
import { I80F48, I80F48Dto } from './I80F48';
|
|
|
|
|
|
|
|
export class MangoAccount {
|
|
|
|
public tokens: TokenAccount[];
|
2022-04-03 07:02:14 -07:00
|
|
|
public serum3: Serum3Account[];
|
2022-04-02 23:57:45 -07:00
|
|
|
|
|
|
|
static from(
|
|
|
|
publicKey: PublicKey,
|
|
|
|
obj: {
|
|
|
|
group: PublicKey;
|
|
|
|
owner: PublicKey;
|
|
|
|
delegate: PublicKey;
|
|
|
|
tokens: unknown;
|
|
|
|
serum3: Object;
|
|
|
|
perps: unknown;
|
|
|
|
beingLiquidated: number;
|
|
|
|
isBankrupt: number;
|
|
|
|
accountNum: number;
|
|
|
|
bump: number;
|
|
|
|
reserved: number[];
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
return new MangoAccount(
|
|
|
|
publicKey,
|
|
|
|
obj.group,
|
|
|
|
obj.owner,
|
|
|
|
obj.delegate,
|
|
|
|
obj.tokens as { values: TokenAccountDto[] },
|
2022-04-03 07:02:14 -07:00
|
|
|
obj.serum3 as { values: Serum3AccountDto[] },
|
2022-04-02 23:57:45 -07:00
|
|
|
obj.perps,
|
|
|
|
obj.beingLiquidated,
|
|
|
|
obj.isBankrupt,
|
|
|
|
obj.accountNum,
|
|
|
|
obj.bump,
|
|
|
|
obj.reserved,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public publicKey: PublicKey,
|
2022-04-07 09:58:42 -07:00
|
|
|
public group: PublicKey,
|
|
|
|
public owner: PublicKey,
|
|
|
|
public delegate: PublicKey,
|
2022-04-02 23:57:45 -07:00
|
|
|
tokens: { values: TokenAccountDto[] },
|
2022-04-03 07:02:14 -07:00
|
|
|
serum3: { values: Serum3AccountDto[] },
|
2022-04-02 23:57:45 -07:00
|
|
|
perps: unknown,
|
|
|
|
beingLiquidated: number,
|
|
|
|
isBankrupt: number,
|
|
|
|
accountNum: number,
|
|
|
|
bump: number,
|
|
|
|
reserved: number[],
|
|
|
|
) {
|
|
|
|
this.tokens = tokens.values.map((dto) => TokenAccount.from(dto));
|
2022-04-03 07:02:14 -07:00
|
|
|
this.serum3 = serum3.values.map((dto) => Serum3Account.from(dto));
|
2022-04-02 23:57:45 -07:00
|
|
|
}
|
|
|
|
|
2022-04-03 07:02:14 -07:00
|
|
|
findToken(tokenIndex: number): TokenAccount | undefined {
|
2022-04-02 23:57:45 -07:00
|
|
|
return this.tokens.find((ta) => ta.tokenIndex == tokenIndex);
|
|
|
|
}
|
|
|
|
|
2022-04-08 03:30:21 -07:00
|
|
|
findSerum3Account(marketIndex: number): Serum3Account | undefined {
|
|
|
|
return this.serum3.find((sa) => sa.marketIndex == marketIndex);
|
|
|
|
}
|
|
|
|
|
2022-04-02 23:57:45 -07:00
|
|
|
getNativeDeposit(bank: Bank): I80F48 {
|
2022-04-03 07:02:14 -07:00
|
|
|
const ta = this.findToken(bank.tokenIndex);
|
2022-04-02 23:57:45 -07:00
|
|
|
return bank.depositIndex.mul(ta?.indexedValue!);
|
|
|
|
}
|
|
|
|
|
|
|
|
getNativeBorrow(bank: Bank): I80F48 {
|
2022-04-03 07:02:14 -07:00
|
|
|
const ta = this.findToken(bank.tokenIndex);
|
2022-04-02 23:57:45 -07:00
|
|
|
return bank.borrowIndex.mul(ta?.indexedValue!);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TokenAccount {
|
|
|
|
static from(dto: TokenAccountDto) {
|
|
|
|
return new TokenAccount(
|
|
|
|
I80F48.from(dto.indexedValue),
|
|
|
|
dto.tokenIndex,
|
|
|
|
dto.inUseCount,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public indexedValue: I80F48,
|
|
|
|
public tokenIndex: number,
|
|
|
|
public inUseCount: number,
|
|
|
|
) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TokenAccountDto {
|
|
|
|
constructor(
|
|
|
|
public indexedValue: I80F48Dto,
|
|
|
|
public tokenIndex: number,
|
|
|
|
public inUseCount: number,
|
|
|
|
public reserved: number[],
|
|
|
|
) {}
|
|
|
|
}
|
|
|
|
|
2022-04-03 07:02:14 -07:00
|
|
|
export class Serum3Account {
|
2022-04-03 11:08:56 -07:00
|
|
|
static Serum3MarketIndexUnset = 65535;
|
2022-04-03 07:02:14 -07:00
|
|
|
static from(dto: Serum3AccountDto) {
|
|
|
|
return new Serum3Account(
|
|
|
|
dto.openOrders,
|
|
|
|
dto.marketIndex,
|
|
|
|
dto.baseTokenIndex,
|
|
|
|
dto.quoteTokenIndex,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public openOrders: PublicKey,
|
|
|
|
public marketIndex: number,
|
|
|
|
public baseTokenIndex: number,
|
|
|
|
public quoteTokenIndex: number,
|
|
|
|
) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Serum3AccountDto {
|
|
|
|
constructor(
|
|
|
|
public openOrders: PublicKey,
|
|
|
|
public marketIndex: number,
|
|
|
|
public baseTokenIndex: number,
|
|
|
|
public quoteTokenIndex: number,
|
|
|
|
public reserved: number[],
|
|
|
|
) {}
|
|
|
|
}
|