From f670846d4d6f6b7d14868f37e59a5de873fe1013 Mon Sep 17 00:00:00 2001 From: dd Date: Thu, 18 Feb 2021 15:15:54 -0500 Subject: [PATCH] got rid of complete margin account idea; fixed openOrdersAccounts to never be undefined --- src/client.ts | 73 ++++++++++++-------------------------------------- src/markets.ts | 0 2 files changed, 17 insertions(+), 56 deletions(-) delete mode 100644 src/markets.ts diff --git a/src/client.ts b/src/client.ts index 40afcc0..ec43eef 100644 --- a/src/client.ts +++ b/src/client.ts @@ -110,12 +110,13 @@ export class MarginAccount { borrows!: number[]; openOrders!: PublicKey[]; 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 constructor(publicKey: PublicKey, decoded: any) { this.publicKey = publicKey this.createTime = getUnixTs() + this.openOrdersAccounts = new Array(NUM_MARKETS).fill(undefined) Object.assign(this, decoded) } @@ -182,10 +183,6 @@ export class MarginAccount { 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++) { const oos = this.openOrdersAccounts[i] if (oos != undefined) { @@ -198,10 +195,6 @@ export class MarginAccount { } getAssets(mangoGroup: MangoGroup): number[] { - if (this.openOrdersAccounts == undefined) { - throw new Error("openOrdersAccounts not yet loaded") - } - const assets = new Array(NUM_TOKENS) for (let i = 0; i < NUM_TOKENS; i++) { @@ -235,9 +228,6 @@ export class MarginAccount { 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] @@ -277,13 +267,10 @@ export class MarginAccount { asks: Orderbook, owner: Account ): Promise { - if (this.openOrdersAccounts == undefined) { - throw new Error("Must load open orders accounts first") - } const marketIndex = mangoGroup.getMarketIndex(market) const openOrdersAccount = this.openOrdersAccounts[marketIndex] - if (!openOrdersAccount) { // no open orders for this market + if (openOrdersAccount == undefined) { // no open orders for this market return [] } @@ -296,16 +283,6 @@ export class MarginAccount { } - async cancelAllOrders( - connection: Connection - - ): Promise { - - // fetch all orders using order id - - return true - } - } export class MangoClient { @@ -522,10 +499,6 @@ export class MangoClient { owner: Account ): Promise { - if (!marginAccount.openOrdersAccounts) { - throw new Error("openOrderesAccounts must be initialized") - } - const transaction = new Transaction() for (let i = 0; i < NUM_MARKETS; i++) { if (marginAccount.openOrdersAccounts[i] == undefined) { @@ -828,7 +801,7 @@ export class MangoClient { const transaction = new Transaction() transaction.add(instruction) - // Specify signers in addition to the wallet + // Specify signers in addition to the owner account const additionalSigners = [] // sign, send and confirm transaction @@ -885,19 +858,12 @@ export class MangoClient { } async getMarginAccount( - connection: Connection, - marginAccountPk: PublicKey - ): Promise { - const acc = await connection.getAccountInfo(marginAccountPk, 'singleGossip') - return new MarginAccount(marginAccountPk, MarginAccountLayout.decode(acc == null ? undefined : acc.data)) - } - - async getCompleteMarginAccount( connection: Connection, marginAccountPk: PublicKey, dexProgramId: PublicKey ): Promise { - 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) return ma } @@ -905,13 +871,13 @@ export class MangoClient { async getAllMarginAccounts( connection: Connection, programId: PublicKey, - mangoGroupPk: PublicKey + mangoGroup: MangoGroup ): Promise{ const filters = [ { memcmp: { 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); - return accounts.map( + const marginAccounts = accounts.map( ({ publicKey, accountInfo }) => 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( @@ -954,23 +924,14 @@ export class MangoClient { ]; const accounts = await getFilteredProgramAccounts(connection, programId, filters); - return accounts.map( + const marginAccounts = accounts.map( ({ publicKey, accountInfo }) => new MarginAccount(publicKey, MarginAccountLayout.decode(accountInfo == null ? undefined : accountInfo.data)) - ); - } - - async getCompleteMarginAccountsForOwner( - connection: Connection, - programId: PublicKey, - mangoGroup: MangoGroup, - owner: Account | Wallet - ): Promise { - - const marginAccounts = await this.getMarginAccountsForOwner(connection, programId, mangoGroup, owner) + ) await Promise.all(marginAccounts.map((ma) => ma.loadOpenOrders(connection, mangoGroup.dexProgramId))) return marginAccounts } + } async function getMultipleAccounts( diff --git a/src/markets.ts b/src/markets.ts deleted file mode 100644 index e69de29..0000000