Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-04-03 08:47:02 +02:00
parent e6727cbfae
commit 2cc8bfbeeb
10 changed files with 311 additions and 253 deletions

View File

@ -20,7 +20,10 @@ import {
withdraw,
} from './instructions';
import { findOrCreate } from './utils';
import { Bank, Group, MangoAccount, Serum3Market } from './types';
import { Bank } from './types/bank';
import { MangoAccount } from './types/mangoAccount';
import { Group } from './types/group';
import { Serum3Market } from './types/serum3';
async function main() {
//

View File

@ -1,4 +1,11 @@
export * from './client';
export * from './instructions';
export * from './types';
export * from './I80F48';
export * from './types/types';
export * from './types/I80F48';
export { Bank } from './types/bank';
export { MangoAccount } from './types/mangoAccount';
export { Group } from './types/group';
export { TokenAccountDto } from './types/mangoAccount';
export { TokenAccount } from './types/mangoAccount';
export { Serum3Market } from './types/serum3';
export { StubOracle } from './types/oracle';

View File

@ -8,8 +8,12 @@ import {
} from '@solana/web3.js';
import * as bs58 from 'bs58';
import { MangoClient } from './client';
import { Bank, Group, MangoAccount, Serum3Market, StubOracle } from './types';
import { I80F48 } from './I80F48';
import { I80F48 } from './types/I80F48';
import { Bank } from './types/bank';
import { MangoAccount } from './types/mangoAccount';
import { Group } from './types/group';
import { Serum3Market } from './types/serum3';
import { StubOracle } from './types/oracle';
//
// group
@ -384,6 +388,40 @@ export async function withdrawIx(
// Serum3 instructions
//
// export async function serum3PlaceOrder(
// client: MangoClient,
// side: Serum3Side,
// limitPrice: number,
// maxBaseQty: number,
// maxNativeQuoteQtyIncludingFees: number,
// selfTradeBehavior: Serum3SelfTradeBehavior,
// orderType: Serum3OrderType,
// clientOrderId: number,
// limit: number,
// ): Promise<void> {
// return await client.program.methods
// .serum3PlaceOrder(
// side,
// limitPrice,
// maxBaseQty,
// maxNativeQuoteQtyIncludingFees,
// selfTradeBehavior,
// orderType,
// clientOrderId,
// limit,
// )
// .accounts({
// group: groupPk,
// admin: adminPk,
// serumProgram: serumProgramPk,
// serumMarketExternal: serumMarketExternalPk,
// quoteBank: quoteBankPk,
// baseBank: baseBankPk,
// payer: payer.publicKey,
// })
// .rpc();
// }
export async function serum3RegisterMarket(
client: MangoClient,
groupPk: PublicKey,

View File

@ -1,247 +0,0 @@
import { PublicKey } from '@solana/web3.js';
import BN from 'bn.js';
import { I80F48 } from './I80F48';
export class Group {
static from(publicKey: PublicKey, obj: { admin: PublicKey }): Group {
return new Group(publicKey, obj.admin);
}
constructor(public publicKey: PublicKey, public admin: PublicKey) {}
}
export class Bank {
public depositIndex: I80F48;
public borrowIndex: I80F48;
static from(
publicKey: PublicKey,
obj: {
group: PublicKey;
mint: PublicKey;
vault: PublicKey;
oracle: PublicKey;
depositIndex: I80F48Dto;
borrowIndex: I80F48Dto;
indexedTotalDeposits: I80F48Dto;
indexedTotalBorrows: I80F48Dto;
maintAssetWeight: I80F48Dto;
initAssetWeight: I80F48Dto;
maintLiabWeight: I80F48Dto;
initLiabWeight: I80F48Dto;
liquidationFee: I80F48Dto;
dust: Object;
tokenIndex: number;
},
) {
return new Bank(
publicKey,
obj.group,
obj.mint,
obj.vault,
obj.oracle,
obj.depositIndex,
obj.borrowIndex,
obj.indexedTotalDeposits,
obj.indexedTotalBorrows,
obj.maintAssetWeight,
obj.initAssetWeight,
obj.maintLiabWeight,
obj.initLiabWeight,
obj.liquidationFee,
obj.dust,
obj.tokenIndex,
);
}
constructor(
public publicKey: PublicKey,
group: PublicKey,
mint: PublicKey,
public vault: PublicKey,
oracle: PublicKey,
depositIndex: I80F48Dto,
borrowIndex: I80F48Dto,
indexedTotalDeposits: I80F48Dto,
indexedTotalBorrows: I80F48Dto,
maintAssetWeight: I80F48Dto,
initAssetWeight: I80F48Dto,
maintLiabWeight: I80F48Dto,
initLiabWeight: I80F48Dto,
liquidationFee: I80F48Dto,
dust: Object,
public tokenIndex: number,
) {
this.depositIndex = I80F48.from(depositIndex);
this.borrowIndex = I80F48.from(borrowIndex);
}
toString(): string {
return `Bank ${
this.tokenIndex
} deposit index - ${this.depositIndex.toNumber()}, borrow index - ${this.borrowIndex.toNumber()}`;
}
}
export class MangoAccount {
public tokens: TokenAccount[];
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[] },
obj.serum3,
obj.perps,
obj.beingLiquidated,
obj.isBankrupt,
obj.accountNum,
obj.bump,
obj.reserved,
);
}
constructor(
public publicKey: PublicKey,
group: PublicKey,
owner: PublicKey,
delegate: PublicKey,
tokens: { values: TokenAccountDto[] },
serum3: Object,
perps: unknown,
beingLiquidated: number,
isBankrupt: number,
accountNum: number,
bump: number,
reserved: number[],
) {
this.tokens = tokens.values.map((dto) => TokenAccount.from(dto));
}
find(tokenIndex: number): TokenAccount | undefined {
return this.tokens.find((ta) => ta.tokenIndex == tokenIndex);
}
getNativeDeposit(bank: Bank): I80F48 {
const ta = this.find(bank.tokenIndex);
return bank.depositIndex.mul(ta?.indexedValue!);
}
getNativeBorrow(bank: Bank): I80F48 {
const ta = this.find(bank.tokenIndex);
return bank.borrowIndex.mul(ta?.indexedValue!);
}
}
export class I80F48Dto {
constructor(public val: BN) {}
}
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[],
) {}
}
export class Serum3Market {
static from(
publicKey: PublicKey,
obj: {
group: PublicKey;
serumProgram: PublicKey;
serumMarketExternal: PublicKey;
marketIndex: number;
baseTokenIndex: number;
quoteTokenIndex: number;
bump: number;
reserved: unknown;
},
): Serum3Market {
return new Serum3Market(
publicKey,
obj.group,
obj.serumProgram,
obj.serumMarketExternal,
obj.marketIndex,
obj.baseTokenIndex,
obj.quoteTokenIndex,
);
}
constructor(
public publicKey: PublicKey,
public group: PublicKey,
public serumProgram: PublicKey,
public serumMarketExternal: PublicKey,
public marketIndex: number,
public baseTokenIndex: number,
public quoteTokenIndex: number,
) {}
}
export class StubOracle {
public price: I80F48;
public lastUpdated: number;
static from(
publicKey: PublicKey,
obj: {
group: PublicKey;
mint: PublicKey;
price: I80F48Dto;
lastUpdated: BN;
reserved: unknown;
},
): StubOracle {
return new StubOracle(
publicKey,
obj.group,
obj.mint,
obj.price,
obj.lastUpdated,
);
}
constructor(
public publicKey: PublicKey,
public group: PublicKey,
public mint: PublicKey,
price: I80F48Dto,
lastUpdated: BN,
) {
this.price = I80F48.from(price);
this.lastUpdated = lastUpdated.toNumber();
}
}

View File

@ -1,6 +1,9 @@
import BN from 'bn.js';
import Big from 'big.js';
import { I80F48Dto } from './types';
export class I80F48Dto {
constructor(public val: BN) {}
}
// TODO - this whole class is inefficient; consider optimizing
export class I80F48 {

75
ts/types/bank.ts Normal file
View File

@ -0,0 +1,75 @@
import { I80F48, I80F48Dto } from './I80F48';
import { PublicKey } from '@solana/web3.js';
export class Bank {
public depositIndex: I80F48;
public borrowIndex: I80F48;
static from(
publicKey: PublicKey,
obj: {
group: PublicKey;
mint: PublicKey;
vault: PublicKey;
oracle: PublicKey;
depositIndex: I80F48Dto;
borrowIndex: I80F48Dto;
indexedTotalDeposits: I80F48Dto;
indexedTotalBorrows: I80F48Dto;
maintAssetWeight: I80F48Dto;
initAssetWeight: I80F48Dto;
maintLiabWeight: I80F48Dto;
initLiabWeight: I80F48Dto;
liquidationFee: I80F48Dto;
dust: Object;
tokenIndex: number;
},
) {
return new Bank(
publicKey,
obj.group,
obj.mint,
obj.vault,
obj.oracle,
obj.depositIndex,
obj.borrowIndex,
obj.indexedTotalDeposits,
obj.indexedTotalBorrows,
obj.maintAssetWeight,
obj.initAssetWeight,
obj.maintLiabWeight,
obj.initLiabWeight,
obj.liquidationFee,
obj.dust,
obj.tokenIndex,
);
}
constructor(
public publicKey: PublicKey,
group: PublicKey,
mint: PublicKey,
public vault: PublicKey,
oracle: PublicKey,
depositIndex: I80F48Dto,
borrowIndex: I80F48Dto,
indexedTotalDeposits: I80F48Dto,
indexedTotalBorrows: I80F48Dto,
maintAssetWeight: I80F48Dto,
initAssetWeight: I80F48Dto,
maintLiabWeight: I80F48Dto,
initLiabWeight: I80F48Dto,
liquidationFee: I80F48Dto,
dust: Object,
public tokenIndex: number,
) {
this.depositIndex = I80F48.from(depositIndex);
this.borrowIndex = I80F48.from(borrowIndex);
}
toString(): string {
return `Bank ${
this.tokenIndex
} deposit index - ${this.depositIndex.toNumber()}, borrow index - ${this.borrowIndex.toNumber()}`;
}
}

9
ts/types/group.ts Normal file
View File

@ -0,0 +1,9 @@
import { PublicKey } from '@solana/web3.js';
export class Group {
static from(publicKey: PublicKey, obj: { admin: PublicKey }): Group {
return new Group(publicKey, obj.admin);
}
constructor(public publicKey: PublicKey, public admin: PublicKey) {}
}

95
ts/types/mangoAccount.ts Normal file
View File

@ -0,0 +1,95 @@
import { PublicKey } from '@solana/web3.js';
import { Bank } from './bank';
import { I80F48, I80F48Dto } from './I80F48';
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[],
) {}
}
export class MangoAccount {
public tokens: TokenAccount[];
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[] },
obj.serum3,
obj.perps,
obj.beingLiquidated,
obj.isBankrupt,
obj.accountNum,
obj.bump,
obj.reserved,
);
}
constructor(
public publicKey: PublicKey,
group: PublicKey,
owner: PublicKey,
delegate: PublicKey,
tokens: { values: TokenAccountDto[] },
serum3: Object,
perps: unknown,
beingLiquidated: number,
isBankrupt: number,
accountNum: number,
bump: number,
reserved: number[],
) {
this.tokens = tokens.values.map((dto) => TokenAccount.from(dto));
}
find(tokenIndex: number): TokenAccount | undefined {
return this.tokens.find((ta) => ta.tokenIndex == tokenIndex);
}
getNativeDeposit(bank: Bank): I80F48 {
const ta = this.find(bank.tokenIndex);
return bank.depositIndex.mul(ta?.indexedValue!);
}
getNativeBorrow(bank: Bank): I80F48 {
const ta = this.find(bank.tokenIndex);
return bank.borrowIndex.mul(ta?.indexedValue!);
}
}

38
ts/types/oracle.ts Normal file
View File

@ -0,0 +1,38 @@
import { I80F48, I80F48Dto } from './I80F48';
import { PublicKey } from '@solana/web3.js';
import BN from 'bn.js';
export class StubOracle {
public price: I80F48;
public lastUpdated: number;
static from(
publicKey: PublicKey,
obj: {
group: PublicKey;
mint: PublicKey;
price: I80F48Dto;
lastUpdated: BN;
reserved: unknown;
},
): StubOracle {
return new StubOracle(
publicKey,
obj.group,
obj.mint,
obj.price,
obj.lastUpdated,
);
}
constructor(
public publicKey: PublicKey,
public group: PublicKey,
public mint: PublicKey,
price: I80F48Dto,
lastUpdated: BN,
) {
this.price = I80F48.from(price);
this.lastUpdated = lastUpdated.toNumber();
}
}

37
ts/types/serum3.ts Normal file
View File

@ -0,0 +1,37 @@
import { PublicKey } from '@solana/web3.js';
export class Serum3Market {
static from(
publicKey: PublicKey,
obj: {
group: PublicKey;
serumProgram: PublicKey;
serumMarketExternal: PublicKey;
marketIndex: number;
baseTokenIndex: number;
quoteTokenIndex: number;
bump: number;
reserved: unknown;
},
): Serum3Market {
return new Serum3Market(
publicKey,
obj.group,
obj.serumProgram,
obj.serumMarketExternal,
obj.marketIndex,
obj.baseTokenIndex,
obj.quoteTokenIndex,
);
}
constructor(
public publicKey: PublicKey,
public group: PublicKey,
public serumProgram: PublicKey,
public serumMarketExternal: PublicKey,
public marketIndex: number,
public baseTokenIndex: number,
public quoteTokenIndex: number,
) {}
}