got rid of complete margin account idea; fixed openOrdersAccounts to never be undefined

This commit is contained in:
dd 2021-02-18 15:15:54 -05:00
parent 0783c0cc7e
commit f670846d4d
2 changed files with 17 additions and 56 deletions

View File

@ -110,12 +110,13 @@ export class MarginAccount {
borrows!: number[]; borrows!: number[];
openOrders!: PublicKey[]; openOrders!: PublicKey[];
srmBalance!: number; srmBalance!: number;
openOrdersAccounts: undefined | (OpenOrders | undefined)[] // undefined if an openOrdersAccount not yet initialized and has zeroKey openOrdersAccounts: (OpenOrders | undefined)[] // undefined if an openOrdersAccount not yet initialized and has zeroKey
// TODO keep updated with websocket // TODO keep updated with websocket
constructor(publicKey: PublicKey, decoded: any) { constructor(publicKey: PublicKey, decoded: any) {
this.publicKey = publicKey this.publicKey = publicKey
this.createTime = getUnixTs() this.createTime = getUnixTs()
this.openOrdersAccounts = new Array(NUM_MARKETS).fill(undefined)
Object.assign(this, decoded) Object.assign(this, decoded)
} }
@ -182,10 +183,6 @@ export class MarginAccount {
value += (this.getUiDeposit(mangoGroup, i) - this.getUiBorrow(mangoGroup, i)) * prices[i] value += (this.getUiDeposit(mangoGroup, i) - this.getUiBorrow(mangoGroup, i)) * prices[i]
} }
if (this.openOrdersAccounts == undefined) {
return value
}
for (let i = 0; i < this.openOrdersAccounts.length; i++) { for (let i = 0; i < this.openOrdersAccounts.length; i++) {
const oos = this.openOrdersAccounts[i] const oos = this.openOrdersAccounts[i]
if (oos != undefined) { if (oos != undefined) {
@ -198,10 +195,6 @@ export class MarginAccount {
} }
getAssets(mangoGroup: MangoGroup): number[] { getAssets(mangoGroup: MangoGroup): number[] {
if (this.openOrdersAccounts == undefined) {
throw new Error("openOrdersAccounts not yet loaded")
}
const assets = new Array<number>(NUM_TOKENS) const assets = new Array<number>(NUM_TOKENS)
for (let i = 0; i < NUM_TOKENS; i++) { for (let i = 0; i < NUM_TOKENS; i++) {
@ -235,9 +228,6 @@ export class MarginAccount {
for (let i = 0; i < NUM_TOKENS; i++) { for (let i = 0; i < NUM_TOKENS; i++) {
assetsVal += this.getUiDeposit(mangoGroup, i) * prices[i] assetsVal += this.getUiDeposit(mangoGroup, i) * prices[i]
} }
if (this.openOrdersAccounts == undefined) {
return assetsVal
}
for (let i = 0; i < NUM_MARKETS; i++) { for (let i = 0; i < NUM_MARKETS; i++) {
const openOrdersAccount = this.openOrdersAccounts[i] const openOrdersAccount = this.openOrdersAccounts[i]
@ -277,13 +267,10 @@ export class MarginAccount {
asks: Orderbook, asks: Orderbook,
owner: Account owner: Account
): Promise<TransactionSignature[]> { ): Promise<TransactionSignature[]> {
if (this.openOrdersAccounts == undefined) {
throw new Error("Must load open orders accounts first")
}
const marketIndex = mangoGroup.getMarketIndex(market) const marketIndex = mangoGroup.getMarketIndex(market)
const openOrdersAccount = this.openOrdersAccounts[marketIndex] const openOrdersAccount = this.openOrdersAccounts[marketIndex]
if (!openOrdersAccount) { // no open orders for this market if (openOrdersAccount == undefined) { // no open orders for this market
return [] return []
} }
@ -296,16 +283,6 @@ export class MarginAccount {
} }
async cancelAllOrders(
connection: Connection
): Promise<boolean> {
// fetch all orders using order id
return true
}
} }
export class MangoClient { export class MangoClient {
@ -522,10 +499,6 @@ export class MangoClient {
owner: Account owner: Account
): Promise<TransactionSignature> { ): Promise<TransactionSignature> {
if (!marginAccount.openOrdersAccounts) {
throw new Error("openOrderesAccounts must be initialized")
}
const transaction = new Transaction() const transaction = new Transaction()
for (let i = 0; i < NUM_MARKETS; i++) { for (let i = 0; i < NUM_MARKETS; i++) {
if (marginAccount.openOrdersAccounts[i] == undefined) { if (marginAccount.openOrdersAccounts[i] == undefined) {
@ -828,7 +801,7 @@ export class MangoClient {
const transaction = new Transaction() const transaction = new Transaction()
transaction.add(instruction) transaction.add(instruction)
// Specify signers in addition to the wallet // Specify signers in addition to the owner account
const additionalSigners = [] const additionalSigners = []
// sign, send and confirm transaction // sign, send and confirm transaction
@ -885,19 +858,12 @@ export class MangoClient {
} }
async getMarginAccount( async getMarginAccount(
connection: Connection,
marginAccountPk: PublicKey
): Promise<MarginAccount> {
const acc = await connection.getAccountInfo(marginAccountPk, 'singleGossip')
return new MarginAccount(marginAccountPk, MarginAccountLayout.decode(acc == null ? undefined : acc.data))
}
async getCompleteMarginAccount(
connection: Connection, connection: Connection,
marginAccountPk: PublicKey, marginAccountPk: PublicKey,
dexProgramId: PublicKey dexProgramId: PublicKey
): Promise<MarginAccount> { ): Promise<MarginAccount> {
const ma = await this.getMarginAccount(connection, marginAccountPk) const acc = await connection.getAccountInfo(marginAccountPk, 'singleGossip')
const ma = new MarginAccount(marginAccountPk, MarginAccountLayout.decode(acc == null ? undefined : acc.data))
await ma.loadOpenOrders(connection, dexProgramId) await ma.loadOpenOrders(connection, dexProgramId)
return ma return ma
} }
@ -905,13 +871,13 @@ export class MangoClient {
async getAllMarginAccounts( async getAllMarginAccounts(
connection: Connection, connection: Connection,
programId: PublicKey, programId: PublicKey,
mangoGroupPk: PublicKey mangoGroup: MangoGroup
): Promise<MarginAccount[]>{ ): Promise<MarginAccount[]>{
const filters = [ const filters = [
{ {
memcmp: { memcmp: {
offset: MarginAccountLayout.offsetOf('mangoGroup'), offset: MarginAccountLayout.offsetOf('mangoGroup'),
bytes: mangoGroupPk.toBase58(), bytes: mangoGroup.publicKey.toBase58(),
} }
}, },
@ -921,10 +887,14 @@ export class MangoClient {
]; ];
const accounts = await getFilteredProgramAccounts(connection, programId, filters); const accounts = await getFilteredProgramAccounts(connection, programId, filters);
return accounts.map( const marginAccounts = accounts.map(
({ publicKey, accountInfo }) => ({ publicKey, accountInfo }) =>
new MarginAccount(publicKey, MarginAccountLayout.decode(accountInfo == null ? undefined : accountInfo.data)) new MarginAccount(publicKey, MarginAccountLayout.decode(accountInfo == null ? undefined : accountInfo.data))
); )
await Promise.all(marginAccounts.map((ma) => ma.loadOpenOrders(connection, mangoGroup.dexProgramId)))
return marginAccounts
} }
async getMarginAccountsForOwner( async getMarginAccountsForOwner(
@ -954,23 +924,14 @@ export class MangoClient {
]; ];
const accounts = await getFilteredProgramAccounts(connection, programId, filters); const accounts = await getFilteredProgramAccounts(connection, programId, filters);
return accounts.map( const marginAccounts = accounts.map(
({ publicKey, accountInfo }) => ({ publicKey, accountInfo }) =>
new MarginAccount(publicKey, MarginAccountLayout.decode(accountInfo == null ? undefined : accountInfo.data)) new MarginAccount(publicKey, MarginAccountLayout.decode(accountInfo == null ? undefined : accountInfo.data))
); )
}
async getCompleteMarginAccountsForOwner(
connection: Connection,
programId: PublicKey,
mangoGroup: MangoGroup,
owner: Account | Wallet
): Promise<MarginAccount[]> {
const marginAccounts = await this.getMarginAccountsForOwner(connection, programId, mangoGroup, owner)
await Promise.all(marginAccounts.map((ma) => ma.loadOpenOrders(connection, mangoGroup.dexProgramId))) await Promise.all(marginAccounts.map((ma) => ma.loadOpenOrders(connection, mangoGroup.dexProgramId)))
return marginAccounts return marginAccounts
} }
} }
async function getMultipleAccounts( async function getMultipleAccounts(

View File