From 620f48c45aac582389a87adab8aad09ab92d54e2 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Wed, 28 Aug 2024 13:59:58 -0400 Subject: [PATCH 01/21] Switch order params to use token price --- client/ts/src/client.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 22a46081a..6a081d316 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -345,7 +345,7 @@ export class ManifestClient { params: { cancels: [], cancelAll: false, - orders: [toWrapperPlaceOrderParams(params)], + orders: [toWrapperPlaceOrderParams(this.market, params)], traderIndexHint: null, }, }, @@ -462,7 +462,7 @@ export class ManifestClient { cancels: cancelParams, cancelAll, orders: placeParams.map((params: WrapperPlaceOrderParamsExternal) => - toWrapperPlaceOrderParams(params), + toWrapperPlaceOrderParams(this.market, params), ), traderIndexHint: null, }, @@ -477,7 +477,7 @@ export class ManifestClient { export type WrapperPlaceOrderParamsExternal = { /** Number of base atoms in the order. */ baseAtoms: bignum; - /** Price as float in atoms of quote per atoms of base. */ + /** Price as float in quote tokens per base tokens. */ price: number; /** Boolean for whether this order is on the bid side. */ isBid: boolean; @@ -492,11 +492,17 @@ export type WrapperPlaceOrderParamsExternal = { }; function toWrapperPlaceOrderParams( + market: Market, wrapperPlaceOrderParamsExternal: WrapperPlaceOrderParamsExternal, ): WrapperPlaceOrderParams { + const quoteAtoms = 10 ** market.quoteDecimals(); + const baseAtoms = 10 ** market.baseDecimals(); + // Converts token price to atom price since not always equal + // Ex. BONK/USDC = 0.00001854 USDC tokens/BONK tokens -> 0.0001854 USDC Atoms/BONK Atoms + const priceQuoteAtomsPerBaseAtoms = wrapperPlaceOrderParamsExternal.price * (quoteAtoms / baseAtoms); // TODO: Make a helper and test it for this logic. const { priceMantissa, priceExponent } = toMantissaAndExponent( - wrapperPlaceOrderParamsExternal.price, + priceQuoteAtomsPerBaseAtoms ); return { From 5a254ab995e036ce3c3f8aac8a39cd8fbb63addf Mon Sep 17 00:00:00 2001 From: DonDuala Date: Wed, 28 Aug 2024 14:16:56 -0400 Subject: [PATCH 02/21] Use baseTokens as number in order params external --- client/ts/src/client.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 6a081d316..d147bb6a6 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -475,8 +475,8 @@ export class ManifestClient { * Same as the autogenerated WrapperPlaceOrderParams except price here is a number. */ export type WrapperPlaceOrderParamsExternal = { - /** Number of base atoms in the order. */ - baseAtoms: bignum; + /** Number of base tokens in the order. */ + numBaseTokens: number; /** Price as float in quote tokens per base tokens. */ price: number; /** Boolean for whether this order is on the bid side. */ @@ -504,9 +504,11 @@ function toWrapperPlaceOrderParams( const { priceMantissa, priceExponent } = toMantissaAndExponent( priceQuoteAtomsPerBaseAtoms ); + const numBaseAtoms : bignum = wrapperPlaceOrderParamsExternal.numBaseTokens * baseAtoms; return { ...wrapperPlaceOrderParamsExternal, + baseAtoms: numBaseAtoms, priceMantissa, priceExponent, minOutAtoms: wrapperPlaceOrderParamsExternal.minOutAtoms ?? 0, From fdb1304c83621de59c1447a4b44554bdc24b5f17 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Wed, 28 Aug 2024 14:22:18 -0400 Subject: [PATCH 03/21] Update test fcts --- client/ts/tests/batchUpdate.ts | 4 ++-- client/ts/tests/placeOrder.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client/ts/tests/batchUpdate.ts b/client/ts/tests/batchUpdate.ts index 898c80f07..839d3f8a9 100644 --- a/client/ts/tests/batchUpdate.ts +++ b/client/ts/tests/batchUpdate.ts @@ -57,7 +57,7 @@ async function batchUpdate( connection: Connection, payerKeypair: Keypair, marketAddress: PublicKey, - baseAtoms: number, + numBaseTokens: number, price: number, isBid: boolean, orderType: OrderType, @@ -74,7 +74,7 @@ async function batchUpdate( const placeOrderIx = client.batchUpdateIx( [ { - baseAtoms, + numBaseTokens, price, isBid, lastValidSlot: lastValidSlot, diff --git a/client/ts/tests/placeOrder.ts b/client/ts/tests/placeOrder.ts index 67f7dd9bf..7e3266904 100644 --- a/client/ts/tests/placeOrder.ts +++ b/client/ts/tests/placeOrder.ts @@ -67,7 +67,7 @@ export async function placeOrder( connection: Connection, payerKeypair: Keypair, marketAddress: PublicKey, - baseAtoms: number, + numBaseTokens: number, price: number, isBid: boolean, orderType: OrderType, @@ -82,7 +82,7 @@ export async function placeOrder( ); const placeOrderIx = client.placeOrderIx({ - baseAtoms, + numBaseTokens, price, isBid, lastValidSlot: lastValidSlot, From 7d6c7891ddc2d29260fb8d65f95dec0245ca270c Mon Sep 17 00:00:00 2001 From: DonDuala Date: Wed, 28 Aug 2024 14:32:20 -0400 Subject: [PATCH 04/21] lint --- client/ts/src/client.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index d147bb6a6..171610c2a 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -499,12 +499,14 @@ function toWrapperPlaceOrderParams( const baseAtoms = 10 ** market.baseDecimals(); // Converts token price to atom price since not always equal // Ex. BONK/USDC = 0.00001854 USDC tokens/BONK tokens -> 0.0001854 USDC Atoms/BONK Atoms - const priceQuoteAtomsPerBaseAtoms = wrapperPlaceOrderParamsExternal.price * (quoteAtoms / baseAtoms); + const priceQuoteAtomsPerBaseAtoms = + wrapperPlaceOrderParamsExternal.price * (quoteAtoms / baseAtoms); // TODO: Make a helper and test it for this logic. const { priceMantissa, priceExponent } = toMantissaAndExponent( - priceQuoteAtomsPerBaseAtoms + priceQuoteAtomsPerBaseAtoms, ); - const numBaseAtoms : bignum = wrapperPlaceOrderParamsExternal.numBaseTokens * baseAtoms; + const numBaseAtoms: bignum = + wrapperPlaceOrderParamsExternal.numBaseTokens * baseAtoms; return { ...wrapperPlaceOrderParamsExternal, From 62411f62661d6750ddf22a5e44a8f75985324fe3 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Wed, 28 Aug 2024 14:57:02 -0400 Subject: [PATCH 05/21] rename _AtomsPerToken --- client/ts/src/client.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 171610c2a..4eca9ae84 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -495,18 +495,18 @@ function toWrapperPlaceOrderParams( market: Market, wrapperPlaceOrderParamsExternal: WrapperPlaceOrderParamsExternal, ): WrapperPlaceOrderParams { - const quoteAtoms = 10 ** market.quoteDecimals(); - const baseAtoms = 10 ** market.baseDecimals(); + const quoteAtomsPerToken = 10 ** market.quoteDecimals(); + const baseAtomsPerToken = 10 ** market.baseDecimals(); // Converts token price to atom price since not always equal // Ex. BONK/USDC = 0.00001854 USDC tokens/BONK tokens -> 0.0001854 USDC Atoms/BONK Atoms const priceQuoteAtomsPerBaseAtoms = - wrapperPlaceOrderParamsExternal.price * (quoteAtoms / baseAtoms); + wrapperPlaceOrderParamsExternal.price * (quoteAtomsPerToken / baseAtomsPerToken); // TODO: Make a helper and test it for this logic. const { priceMantissa, priceExponent } = toMantissaAndExponent( priceQuoteAtomsPerBaseAtoms, ); const numBaseAtoms: bignum = - wrapperPlaceOrderParamsExternal.numBaseTokens * baseAtoms; + wrapperPlaceOrderParamsExternal.numBaseTokens * baseAtomsPerToken; return { ...wrapperPlaceOrderParamsExternal, From f5349a88543ff249dc800ca8f2fab28bb7cfbaa7 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Wed, 28 Aug 2024 16:08:53 -0400 Subject: [PATCH 06/21] lint2 --- client/ts/src/client.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 4eca9ae84..5cea80b3d 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -500,7 +500,8 @@ function toWrapperPlaceOrderParams( // Converts token price to atom price since not always equal // Ex. BONK/USDC = 0.00001854 USDC tokens/BONK tokens -> 0.0001854 USDC Atoms/BONK Atoms const priceQuoteAtomsPerBaseAtoms = - wrapperPlaceOrderParamsExternal.price * (quoteAtomsPerToken / baseAtomsPerToken); + wrapperPlaceOrderParamsExternal.price * + (quoteAtomsPerToken / baseAtomsPerToken); // TODO: Make a helper and test it for this logic. const { priceMantissa, priceExponent } = toMantissaAndExponent( priceQuoteAtomsPerBaseAtoms, From 6ff69e3ebfa8e62c45155019e0e83ad06205d5d1 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Wed, 28 Aug 2024 22:41:34 -0400 Subject: [PATCH 07/21] Use tokens in deposit and withdraw, fix tests --- client/ts/src/client.ts | 22 +++++++++++++++------- client/ts/src/market.ts | 26 ++++++++++++++++++++++++++ client/ts/tests/batchUpdate.ts | 4 ++-- client/ts/tests/deposit.ts | 18 +++++++++++------- client/ts/tests/fillFeed.ts | 12 +++--------- client/ts/tests/market.ts | 18 ++++++------------ client/ts/tests/placeOrder.ts | 16 +++++----------- client/ts/tests/withdraw.ts | 10 +++++----- client/ts/tests/wrapper.ts | 10 ++-------- 9 files changed, 75 insertions(+), 61 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 5cea80b3d..15b8773e1 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -238,14 +238,14 @@ export class ManifestClient { * * @param payer PublicKey of the trader * @param mint PublicKey for deposit mint. Must be either the base or quote - * @param amountAtoms Number of atoms to deposit. + * @param amountTokens Number of tokens to deposit. * * @returns TransactionInstruction */ public depositIx( payer: PublicKey, mint: PublicKey, - amountAtoms: number, + amountTokens: number, ): TransactionInstruction { const vault: PublicKey = getVaultAddress(this.market.address, mint); const traderTokenAccount: PublicKey = getAssociatedTokenAddressSync( @@ -255,6 +255,7 @@ export class ManifestClient { const is22: boolean = (mint == this.baseMint.address && this.isBase22) || (mint == this.baseMint.address && this.isBase22); + const amountAtoms = amountTokens * 10 ** this.market.baseDecimals(); return createDepositInstruction( { @@ -282,14 +283,14 @@ export class ManifestClient { * @param payer PublicKey of the trader * @param market PublicKey of the market * @param mint PublicKey for withdraw mint. Must be either the base or quote - * @param amountAtoms Number of atoms to withdraw. + * @param amountTokens Number of tokens to withdraw. * * @returns TransactionInstruction */ public withdrawIx( payer: PublicKey, mint: PublicKey, - amountAtoms: number, + amountTokens: number, ): TransactionInstruction { const vault: PublicKey = getVaultAddress(this.market.address, mint); const traderTokenAccount: PublicKey = getAssociatedTokenAddressSync( @@ -299,6 +300,7 @@ export class ManifestClient { const is22: boolean = (mint == this.baseMint.address && this.isBase22) || (mint == this.baseMint.address && this.isBase22); + const amountAtoms = amountTokens * 10 ** this.market.baseDecimals(); return createWithdrawInstruction( { @@ -486,9 +488,9 @@ export type WrapperPlaceOrderParamsExternal = { /** Type of order (Limit, PostOnly, ...). */ orderType: OrderType; /** Used in fill or kill orders. Set to zero otherwise. */ - minOutAtoms?: bignum; + minOutTokens?: number; /** Client order id used for cancelling orders. Does not need to be unique. */ - clientOrderId: bignum; + clientOrderId: number; }; function toWrapperPlaceOrderParams( @@ -509,12 +511,18 @@ function toWrapperPlaceOrderParams( const numBaseAtoms: bignum = wrapperPlaceOrderParamsExternal.numBaseTokens * baseAtomsPerToken; + const minOutTokens = wrapperPlaceOrderParamsExternal.minOutTokens ?? 0; + + const minOutAtoms = wrapperPlaceOrderParamsExternal.isBid + ? minOutTokens * baseAtomsPerToken + : minOutTokens * quoteAtomsPerToken; + return { ...wrapperPlaceOrderParamsExternal, baseAtoms: numBaseAtoms, priceMantissa, priceExponent, - minOutAtoms: wrapperPlaceOrderParamsExternal.minOutAtoms ?? 0, + minOutAtoms, }; } diff --git a/client/ts/src/market.ts b/client/ts/src/market.ts index 8e7af93fa..5e30740fd 100644 --- a/client/ts/src/market.ts +++ b/client/ts/src/market.ts @@ -180,6 +180,32 @@ export class Market { return toNum(isBase ? seat.baseBalance : seat.quoteBalance); } + /** + * Get the amount in tokens of balance that is deposited on the exchange, does + * not include tokens currently in open orders. + * + * @param trader PublicKey of the trader to check balance of + * @param isBase boolean for whether this is checking base or quote + * + * @returns number in tokens + */ + public getWithdrawableBalanceTokens( + trader: PublicKey, + isBase: boolean, + ): number { + const filteredSeats = this.data.claimedSeats.filter((claimedSeat) => { + return claimedSeat.publicKey.toBase58() == trader.toBase58(); + }); + // No seat claimed. + if (filteredSeats.length == 0) { + return 0; + } + const seat: ClaimedSeat = filteredSeats[0]; + return isBase + ? toNum(seat.baseBalance) * 10 ** this.baseDecimals() + : toNum(seat.quoteBalance) * 10 ** this.quoteDecimals(); + } + /** * Gets the base mint of the market * diff --git a/client/ts/tests/batchUpdate.ts b/client/ts/tests/batchUpdate.ts index 839d3f8a9..9490c9683 100644 --- a/client/ts/tests/batchUpdate.ts +++ b/client/ts/tests/batchUpdate.ts @@ -62,7 +62,7 @@ async function batchUpdate( isBid: boolean, orderType: OrderType, clientOrderId: number, - minOutAtoms: number = 0, + minOutTokens: number = 0, lastValidSlot: number = 0, ): Promise { const client: ManifestClient = await ManifestClient.getClientForMarket( @@ -79,7 +79,7 @@ async function batchUpdate( isBid, lastValidSlot: lastValidSlot, orderType: orderType, - minOutAtoms, + minOutTokens, clientOrderId, }, ], diff --git a/client/ts/tests/deposit.ts b/client/ts/tests/deposit.ts index 3185d70b7..4780cb676 100644 --- a/client/ts/tests/deposit.ts +++ b/client/ts/tests/deposit.ts @@ -28,11 +28,11 @@ async function testDeposit(): Promise { await market.reload(connection); assert( - market.getWithdrawableBalanceAtoms(payerKeypair.publicKey, true) == 10, + market.getWithdrawableBalanceTokens(payerKeypair.publicKey, true) == 10, 'deposit withdrawable balance check base', ); assert( - market.getWithdrawableBalanceAtoms(payerKeypair.publicKey, false) == 0, + market.getWithdrawableBalanceTokens(payerKeypair.publicKey, false) == 0, 'deposit withdrawable balance check quote', ); market.prettyPrint(); @@ -43,14 +43,18 @@ export async function deposit( payerKeypair: Keypair, marketAddress: PublicKey, mint: PublicKey, - amountAtoms: number, + amountTokens: number, ): Promise { const client: ManifestClient = await ManifestClient.getClientForMarket( connection, marketAddress, payerKeypair, ); - const depositIx = client.depositIx(payerKeypair.publicKey, mint, amountAtoms); + const depositIx = client.depositIx( + payerKeypair.publicKey, + mint, + amountTokens, + ); const traderTokenAccount = await createAssociatedTokenAccountIdempotent( connection, @@ -65,9 +69,9 @@ export async function deposit( mint, traderTokenAccount, payerKeypair.publicKey, - amountAtoms, + amountTokens, ); - console.log(`Minted ${amountAtoms} to ${traderTokenAccount} in ${mintSig}`); + console.log(`Minted ${amountTokens} to ${traderTokenAccount} in ${mintSig}`); const signature = await sendAndConfirmTransaction( connection, @@ -77,7 +81,7 @@ export async function deposit( commitment: 'confirmed', }, ); - console.log(`Deposited ${amountAtoms} atoms in ${signature}`); + console.log(`Deposited ${amountTokens} tokens in ${signature}`); } describe('Deposit test', () => { diff --git a/client/ts/tests/fillFeed.ts b/client/ts/tests/fillFeed.ts index 00b25bdca..850e71c45 100644 --- a/client/ts/tests/fillFeed.ts +++ b/client/ts/tests/fillFeed.ts @@ -19,25 +19,19 @@ async function testFillFeed(): Promise { }); // Deposit and place the first order. - await deposit( - connection, - payerKeypair, - marketAddress, - market.baseMint(), - 10_000_000_000, - ); + await deposit(connection, payerKeypair, marketAddress, market.baseMint(), 10); await deposit( connection, payerKeypair, marketAddress, market.quoteMint(), - 10_000_000_000, + 10, ); await placeOrder( connection, payerKeypair, marketAddress, - 5_000_000_000, + 5, 5, false, OrderType.Limit, diff --git a/client/ts/tests/market.ts b/client/ts/tests/market.ts index 63fc9a01d..9f6cea919 100644 --- a/client/ts/tests/market.ts +++ b/client/ts/tests/market.ts @@ -43,7 +43,7 @@ async function testMarket(): Promise { // Market withdrawable balance not init assert( - market.getWithdrawableBalanceAtoms(payerKeypair.publicKey, true) == 0, + market.getWithdrawableBalanceTokens(payerKeypair.publicKey, true) == 0, 'Get withdrawable balance with no seat', ); @@ -60,11 +60,11 @@ async function testMarket(): Promise { payerKeypair, marketAddress, market.quoteMint(), - 10_000_000_000, + 10, ); // Market withdrawable balance after deposit assert( - market.getWithdrawableBalanceAtoms(payerKeypair.publicKey, false) == 0, + market.getWithdrawableBalanceTokens(payerKeypair.publicKey, false) == 0, 'Get withdrawable balance after deposit', ); @@ -72,18 +72,12 @@ async function testMarket(): Promise { assert(market.quoteDecimals() == 6, 'quote decimals'); // Put orders on both sides to test pretty printing. - await deposit( - connection, - payerKeypair, - marketAddress, - market.baseMint(), - 10_000_000_000, - ); + await deposit(connection, payerKeypair, marketAddress, market.baseMint(), 10); await placeOrder( connection, payerKeypair, marketAddress, - 5_000, + 0.005, 5, false, OrderType.Limit, @@ -93,7 +87,7 @@ async function testMarket(): Promise { connection, payerKeypair, marketAddress, - 5_000, + 0.005, 5, true, OrderType.Limit, diff --git a/client/ts/tests/placeOrder.ts b/client/ts/tests/placeOrder.ts index 7e3266904..74081ac4c 100644 --- a/client/ts/tests/placeOrder.ts +++ b/client/ts/tests/placeOrder.ts @@ -22,18 +22,12 @@ async function testPlaceOrder(): Promise { address: marketAddress, }); - await deposit( - connection, - payerKeypair, - marketAddress, - market.baseMint(), - 10_000_000_000, - ); + await deposit(connection, payerKeypair, marketAddress, market.baseMint(), 10); await placeOrder( connection, payerKeypair, marketAddress, - 5_000_000_000, + 5, 5, false, OrderType.Limit, @@ -43,7 +37,7 @@ async function testPlaceOrder(): Promise { connection, payerKeypair, marketAddress, - 3_000_000_000, + 3, 3, false, OrderType.Limit, @@ -72,7 +66,7 @@ export async function placeOrder( isBid: boolean, orderType: OrderType, clientOrderId: number, - minOutAtoms: number = 0, + minOutTokens: number = 0, lastValidSlot: number = 0, ): Promise { const client: ManifestClient = await ManifestClient.getClientForMarket( @@ -87,7 +81,7 @@ export async function placeOrder( isBid, lastValidSlot: lastValidSlot, orderType: orderType, - minOutAtoms, + minOutTokens, clientOrderId, }); diff --git a/client/ts/tests/withdraw.ts b/client/ts/tests/withdraw.ts index 20f5be7e2..3c99f1de6 100644 --- a/client/ts/tests/withdraw.ts +++ b/client/ts/tests/withdraw.ts @@ -26,11 +26,11 @@ async function testWithdraw(): Promise { await market.reload(connection); assert( - market.getWithdrawableBalanceAtoms(payerKeypair.publicKey, true) == 5, + market.getWithdrawableBalanceTokens(payerKeypair.publicKey, true) == 5, 'withdraw withdrawable balance check base', ); assert( - market.getWithdrawableBalanceAtoms(payerKeypair.publicKey, false) == 0, + market.getWithdrawableBalanceTokens(payerKeypair.publicKey, false) == 0, 'withdraw withdrawable balance check quote', ); market.prettyPrint(); @@ -41,7 +41,7 @@ export async function withdraw( payerKeypair: Keypair, marketAddress: PublicKey, mint: PublicKey, - amountAtoms: number, + amountTokens: number, ): Promise { const client: ManifestClient = await ManifestClient.getClientForMarket( connection, @@ -51,7 +51,7 @@ export async function withdraw( const withdrawIx = client.withdrawIx( payerKeypair.publicKey, mint, - amountAtoms, + amountTokens, ); const signature = await sendAndConfirmTransaction( @@ -62,7 +62,7 @@ export async function withdraw( commitment: 'confirmed', }, ); - console.log(`Withdrew ${amountAtoms} atoms in ${signature}`); + console.log(`Withdrew ${amountTokens} tokens in ${signature}`); } describe('Withdraw test', () => { diff --git a/client/ts/tests/wrapper.ts b/client/ts/tests/wrapper.ts index ffe97e2b8..7aa72c755 100644 --- a/client/ts/tests/wrapper.ts +++ b/client/ts/tests/wrapper.ts @@ -80,18 +80,12 @@ async function testWrapper(): Promise { ); // Place an order to get more coverage on the pretty print. - await deposit( - connection, - payerKeypair, - marketAddress, - market.baseMint(), - 10_000_000_000, - ); + await deposit(connection, payerKeypair, marketAddress, market.baseMint(), 10); await placeOrder( connection, payerKeypair, marketAddress, - 5_000_000_000, + 5, 5, false, OrderType.Limit, From abdeec2274bbf3c7e3012078c7c96a87eb1c786c Mon Sep 17 00:00:00 2001 From: DonDuala Date: Thu, 29 Aug 2024 10:48:17 -0400 Subject: [PATCH 08/21] update test sizes --- client/ts/tests/batchUpdate.ts | 4 ++-- client/ts/tests/market.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client/ts/tests/batchUpdate.ts b/client/ts/tests/batchUpdate.ts index 9490c9683..896e7873a 100644 --- a/client/ts/tests/batchUpdate.ts +++ b/client/ts/tests/batchUpdate.ts @@ -27,13 +27,13 @@ async function testBatchUpdate(): Promise { payerKeypair, marketAddress, market.baseMint(), - 10_000_000_000, + 10, ); await batchUpdate( connection, payerKeypair, marketAddress, - 5_000_000_000, + 5, 5, false, OrderType.Limit, diff --git a/client/ts/tests/market.ts b/client/ts/tests/market.ts index 9f6cea919..e3bf5d2f8 100644 --- a/client/ts/tests/market.ts +++ b/client/ts/tests/market.ts @@ -77,7 +77,7 @@ async function testMarket(): Promise { connection, payerKeypair, marketAddress, - 0.005, + 1, 5, false, OrderType.Limit, @@ -87,7 +87,7 @@ async function testMarket(): Promise { connection, payerKeypair, marketAddress, - 0.005, + 1, 5, true, OrderType.Limit, From a670b6eabee8dfea45ee652ed8c813183a7d6f4e Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 10:16:08 -0400 Subject: [PATCH 09/21] lint batch --- client/ts/tests/batchUpdate.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/client/ts/tests/batchUpdate.ts b/client/ts/tests/batchUpdate.ts index 896e7873a..605b0d91e 100644 --- a/client/ts/tests/batchUpdate.ts +++ b/client/ts/tests/batchUpdate.ts @@ -22,13 +22,7 @@ async function testBatchUpdate(): Promise { address: marketAddress, }); - await deposit( - connection, - payerKeypair, - marketAddress, - market.baseMint(), - 10, - ); + await deposit(connection, payerKeypair, marketAddress, market.baseMint(), 10); await batchUpdate( connection, payerKeypair, From fb2fea6cc099b059322c4ff91af097a5668ecae5 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 10:22:03 -0400 Subject: [PATCH 10/21] Debug lines --- client/ts/src/client.ts | 13 ++++++++++++- client/ts/src/market.ts | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 15b8773e1..ca79e4ab3 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -510,7 +510,18 @@ function toWrapperPlaceOrderParams( ); const numBaseAtoms: bignum = wrapperPlaceOrderParamsExternal.numBaseTokens * baseAtomsPerToken; - + console.log( + 'DEBUG', + market.quoteDecimals(), + quoteAtomsPerToken, + market.baseDecimals(), + baseAtomsPerToken, + wrapperPlaceOrderParamsExternal.price, + priceQuoteAtomsPerBaseAtoms, + priceMantissa, + priceExponent, + numBaseAtoms, + ); const minOutTokens = wrapperPlaceOrderParamsExternal.minOutTokens ?? 0; const minOutAtoms = wrapperPlaceOrderParamsExternal.isBid diff --git a/client/ts/src/market.ts b/client/ts/src/market.ts index 5e30740fd..1f181119b 100644 --- a/client/ts/src/market.ts +++ b/client/ts/src/market.ts @@ -286,7 +286,7 @@ export class Market { /** * Print all information loaded about the market in a human readable format. */ - public prettyPrint() { + public prettyPrint(): void { console.log(''); console.log(`Market: ${this.address}`); console.log(`========================`); From ff97c4160d98e4a6cbe40ae5558a2264bb3a2943 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 10:51:49 -0400 Subject: [PATCH 11/21] toMint in atoms --- client/ts/src/client.ts | 13 +------------ client/ts/tests/deposit.ts | 9 ++++++--- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index ca79e4ab3..15b8773e1 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -510,18 +510,7 @@ function toWrapperPlaceOrderParams( ); const numBaseAtoms: bignum = wrapperPlaceOrderParamsExternal.numBaseTokens * baseAtomsPerToken; - console.log( - 'DEBUG', - market.quoteDecimals(), - quoteAtomsPerToken, - market.baseDecimals(), - baseAtomsPerToken, - wrapperPlaceOrderParamsExternal.price, - priceQuoteAtomsPerBaseAtoms, - priceMantissa, - priceExponent, - numBaseAtoms, - ); + const minOutTokens = wrapperPlaceOrderParamsExternal.minOutTokens ?? 0; const minOutAtoms = wrapperPlaceOrderParamsExternal.isBid diff --git a/client/ts/tests/deposit.ts b/client/ts/tests/deposit.ts index 4780cb676..c80dac420 100644 --- a/client/ts/tests/deposit.ts +++ b/client/ts/tests/deposit.ts @@ -62,16 +62,19 @@ export async function deposit( mint, payerKeypair.publicKey, ); - + // Use a sufficiently large exponent for atoms + const amountAtoms = amountTokens * 10 ** 9; const mintSig = await mintTo( connection, payerKeypair, mint, traderTokenAccount, payerKeypair.publicKey, - amountTokens, + amountAtoms, + ); + console.log( + `Minted ${amountTokens} atoms to ${traderTokenAccount} in ${mintSig}`, ); - console.log(`Minted ${amountTokens} to ${traderTokenAccount} in ${mintSig}`); const signature = await sendAndConfirmTransaction( connection, From 1522dca2762a2e558db116d4c5d18d83b8f19e08 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 11:21:21 -0400 Subject: [PATCH 12/21] Use mint decimals in deposit test --- client/ts/tests/deposit.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/client/ts/tests/deposit.ts b/client/ts/tests/deposit.ts index c80dac420..2e5477a4d 100644 --- a/client/ts/tests/deposit.ts +++ b/client/ts/tests/deposit.ts @@ -9,6 +9,7 @@ import { ManifestClient } from '../src/client'; import { mintTo, createAssociatedTokenAccountIdempotent, + getMint, } from '@solana/spl-token'; import { createMarket } from './createMarket'; import { Market } from '../src/market'; @@ -62,8 +63,9 @@ export async function deposit( mint, payerKeypair.publicKey, ); - // Use a sufficiently large exponent for atoms - const amountAtoms = amountTokens * 10 ** 9; + + const mintDecimals = (await getMint(connection, mint)).decimals; + const amountAtoms = amountTokens * 10 ** mintDecimals; const mintSig = await mintTo( connection, payerKeypair, @@ -73,7 +75,7 @@ export async function deposit( amountAtoms, ); console.log( - `Minted ${amountTokens} atoms to ${traderTokenAccount} in ${mintSig}`, + `Minted ${amountTokens} tokens to ${traderTokenAccount} in ${mintSig}`, ); const signature = await sendAndConfirmTransaction( From 6a93fac2380a01b851d10c13791b8dda53e80e6b Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 12:36:51 -0400 Subject: [PATCH 13/21] Floor atoms and divide get getWithdrawableBalanceTokens --- client/ts/src/client.ts | 17 +++++++++++------ client/ts/src/market.ts | 4 ++-- client/ts/tests/deposit.ts | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 15b8773e1..6f42fe4a4 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -255,7 +255,9 @@ export class ManifestClient { const is22: boolean = (mint == this.baseMint.address && this.isBase22) || (mint == this.baseMint.address && this.isBase22); - const amountAtoms = amountTokens * 10 ** this.market.baseDecimals(); + const amountAtoms = Math.floor( + amountTokens * 10 ** this.market.baseDecimals(), + ); return createDepositInstruction( { @@ -300,7 +302,9 @@ export class ManifestClient { const is22: boolean = (mint == this.baseMint.address && this.isBase22) || (mint == this.baseMint.address && this.isBase22); - const amountAtoms = amountTokens * 10 ** this.market.baseDecimals(); + const amountAtoms = Math.floor( + amountTokens * 10 ** this.market.baseDecimals(), + ); return createWithdrawInstruction( { @@ -508,14 +512,15 @@ function toWrapperPlaceOrderParams( const { priceMantissa, priceExponent } = toMantissaAndExponent( priceQuoteAtomsPerBaseAtoms, ); - const numBaseAtoms: bignum = - wrapperPlaceOrderParamsExternal.numBaseTokens * baseAtomsPerToken; + const numBaseAtoms: bignum = Math.floor( + wrapperPlaceOrderParamsExternal.numBaseTokens * baseAtomsPerToken, + ); const minOutTokens = wrapperPlaceOrderParamsExternal.minOutTokens ?? 0; const minOutAtoms = wrapperPlaceOrderParamsExternal.isBid - ? minOutTokens * baseAtomsPerToken - : minOutTokens * quoteAtomsPerToken; + ? Math.floor(minOutTokens * baseAtomsPerToken) + : Math.floor(minOutTokens * quoteAtomsPerToken); return { ...wrapperPlaceOrderParamsExternal, diff --git a/client/ts/src/market.ts b/client/ts/src/market.ts index 1f181119b..152d081cb 100644 --- a/client/ts/src/market.ts +++ b/client/ts/src/market.ts @@ -202,8 +202,8 @@ export class Market { } const seat: ClaimedSeat = filteredSeats[0]; return isBase - ? toNum(seat.baseBalance) * 10 ** this.baseDecimals() - : toNum(seat.quoteBalance) * 10 ** this.quoteDecimals(); + ? toNum(seat.baseBalance) / 10 ** this.baseDecimals() + : toNum(seat.quoteBalance) / 10 ** this.quoteDecimals(); } /** diff --git a/client/ts/tests/deposit.ts b/client/ts/tests/deposit.ts index 2e5477a4d..18589a2f1 100644 --- a/client/ts/tests/deposit.ts +++ b/client/ts/tests/deposit.ts @@ -65,7 +65,7 @@ export async function deposit( ); const mintDecimals = (await getMint(connection, mint)).decimals; - const amountAtoms = amountTokens * 10 ** mintDecimals; + const amountAtoms = Math.floor(amountTokens * 10 ** mintDecimals); const mintSig = await mintTo( connection, payerKeypair, From bba2dcd417b5d306acc3b0df672b605cf76b9eb8 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 13:16:55 -0400 Subject: [PATCH 14/21] use ceil on mint and deposit, log price --- client/ts/src/client.ts | 2 +- client/ts/tests/batchUpdate.ts | 5 ++++- client/ts/tests/deposit.ts | 2 +- client/ts/tests/placeOrder.ts | 5 ++++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 6f42fe4a4..3bd8224d0 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -255,7 +255,7 @@ export class ManifestClient { const is22: boolean = (mint == this.baseMint.address && this.isBase22) || (mint == this.baseMint.address && this.isBase22); - const amountAtoms = Math.floor( + const amountAtoms = Math.ceil( amountTokens * 10 ** this.market.baseDecimals(), ); diff --git a/client/ts/tests/batchUpdate.ts b/client/ts/tests/batchUpdate.ts index 605b0d91e..f62ef1b8a 100644 --- a/client/ts/tests/batchUpdate.ts +++ b/client/ts/tests/batchUpdate.ts @@ -43,7 +43,10 @@ async function testBatchUpdate(): Promise { Number(market.asks()[0].numBaseAtoms) == 5_000_000_000, 'ask top of book wrong size', ); - assert(market.asks()[0].price == 5, 'ask top of book wrong price'); + assert( + market.asks()[0].price == 5, + `ask top of book wrong price ${market.asks()[0].price}`, + ); assert(market.bids().length == 0, 'place bids did not work'); } diff --git a/client/ts/tests/deposit.ts b/client/ts/tests/deposit.ts index 18589a2f1..cd538a2f8 100644 --- a/client/ts/tests/deposit.ts +++ b/client/ts/tests/deposit.ts @@ -65,7 +65,7 @@ export async function deposit( ); const mintDecimals = (await getMint(connection, mint)).decimals; - const amountAtoms = Math.floor(amountTokens * 10 ** mintDecimals); + const amountAtoms = Math.ceil(amountTokens * 10 ** mintDecimals); const mintSig = await mintTo( connection, payerKeypair, diff --git a/client/ts/tests/placeOrder.ts b/client/ts/tests/placeOrder.ts index 74081ac4c..0b43ac74b 100644 --- a/client/ts/tests/placeOrder.ts +++ b/client/ts/tests/placeOrder.ts @@ -53,7 +53,10 @@ async function testPlaceOrder(): Promise { Number(market.asks()[0].numBaseAtoms) == 5_000_000_000, 'ask top of book wrong size', ); - assert(market.asks()[0].price == 5, 'ask top of book wrong price'); + assert( + market.asks()[0].price == 5, + `ask top of book wrong price ${market.asks()[0].price}`, + ); assert(market.bids().length == 0, 'place bids did not work'); } From e53eecb4c32b760d8c2a564b118a77ce27ae6cb5 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 14:31:16 -0400 Subject: [PATCH 15/21] Fix fill size and ask price in atoms --- client/ts/tests/batchUpdate.ts | 3 ++- client/ts/tests/fillFeed.ts | 2 +- client/ts/tests/placeOrder.ts | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/client/ts/tests/batchUpdate.ts b/client/ts/tests/batchUpdate.ts index f62ef1b8a..98c218489 100644 --- a/client/ts/tests/batchUpdate.ts +++ b/client/ts/tests/batchUpdate.ts @@ -43,8 +43,9 @@ async function testBatchUpdate(): Promise { Number(market.asks()[0].numBaseAtoms) == 5_000_000_000, 'ask top of book wrong size', ); + // TODO: Update this back to 5 after changing RestingOrders to tokens assert( - market.asks()[0].price == 5, + market.asks()[0].price == 0.0005, `ask top of book wrong price ${market.asks()[0].price}`, ); assert(market.bids().length == 0, 'place bids did not work'); diff --git a/client/ts/tests/fillFeed.ts b/client/ts/tests/fillFeed.ts index 850e71c45..d7ddcfdd9 100644 --- a/client/ts/tests/fillFeed.ts +++ b/client/ts/tests/fillFeed.ts @@ -72,7 +72,7 @@ async function checkForFillMessage( connection, payerKeypair, marketAddress, - 5_000, + 5, 5, true, OrderType.Limit, diff --git a/client/ts/tests/placeOrder.ts b/client/ts/tests/placeOrder.ts index 0b43ac74b..370324dce 100644 --- a/client/ts/tests/placeOrder.ts +++ b/client/ts/tests/placeOrder.ts @@ -53,8 +53,9 @@ async function testPlaceOrder(): Promise { Number(market.asks()[0].numBaseAtoms) == 5_000_000_000, 'ask top of book wrong size', ); + // TODO: Update this back to 5 after changing RestingOrders to tokens assert( - market.asks()[0].price == 5, + market.asks()[0].price == 0.0005, `ask top of book wrong price ${market.asks()[0].price}`, ); assert(market.bids().length == 0, 'place bids did not work'); From 70169485fe2977c6065d153a7aa84bd0a23b74d5 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 15:16:29 -0400 Subject: [PATCH 16/21] change RestingOrder to tokens --- client/ts/src/market.ts | 22 ++++++++++++---------- client/ts/tests/batchUpdate.ts | 7 +++---- client/ts/tests/fillFeed.ts | 4 ++-- client/ts/tests/placeOrder.ts | 7 +++---- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/client/ts/src/market.ts b/client/ts/src/market.ts index 152d081cb..894671d05 100644 --- a/client/ts/src/market.ts +++ b/client/ts/src/market.ts @@ -27,14 +27,14 @@ export type RestingOrderInternal = { export type RestingOrder = { /** Trader public key. */ trader: PublicKey; - /** Number of base atoms remaining in the order. */ - numBaseAtoms: bignum; + /** Number of base tokens remaining in the order. */ + numBaseTokens: bignum; /** Last slot before this order is invalid and will be removed. */ lastValidSlot: bignum; /** Exchange defined sequenceNumber for this order, guaranteed to be unique. */ sequenceNumber: bignum; - /** Price as float in atoms of quote per atoms of base. */ - price: number; + /** Price as float in tokens of quote per tokens of base. */ + tokenPrice: number; }; /** @@ -298,13 +298,13 @@ export class Market { console.log('Bids:'); this.data.bids.forEach((bid) => { console.log( - `trader: ${bid.trader} numBaseAtoms: ${bid.numBaseAtoms} price: ${bid.price} lastValidSlot: ${bid.lastValidSlot} sequenceNumber: ${bid.sequenceNumber}`, + `trader: ${bid.trader} numBaseTokens: ${bid.numBaseTokens} token price: ${bid.tokenPrice} lastValidSlot: ${bid.lastValidSlot} sequenceNumber: ${bid.sequenceNumber}`, ); }); console.log('Asks:'); this.data.asks.forEach((ask) => { console.log( - `trader: ${ask.trader} numBaseAtoms: ${ask.numBaseAtoms} price: ${ask.price} lastValidSlot: ${ask.lastValidSlot} sequenceNumber: ${ask.sequenceNumber}`, + `trader: ${ask.trader} numBaseTokens: ${ask.numBaseTokens} token price: ${ask.tokenPrice} lastValidSlot: ${ask.lastValidSlot} sequenceNumber: ${ask.sequenceNumber}`, ); }); console.log('ClaimedSeats:'); @@ -385,7 +385,6 @@ export class Market { restingOrderBeet, ).map((restingOrderInternal: RestingOrderInternal) => { return { - ...restingOrderInternal, trader: publicKeyBeet.deserialize( data.subarray( Number(restingOrderInternal.traderIndex) + @@ -396,7 +395,9 @@ export class Market { FIXED_MANIFEST_HEADER_SIZE, ), )[0].publicKey, - price: convertU128(restingOrderInternal.price), + numBaseTokens: toNum(restingOrderInternal.numBaseAtoms) / 10 ** baseMintDecimals, + tokenPrice: convertU128(restingOrderInternal.price) * 10 ** (baseMintDecimals - quoteMintDecimals), + ...restingOrderInternal, }; }) : []; @@ -409,7 +410,6 @@ export class Market { restingOrderBeet, ).map((restingOrderInternal: RestingOrderInternal) => { return { - ...restingOrderInternal, trader: publicKeyBeet.deserialize( data.subarray( Number(restingOrderInternal.traderIndex) + @@ -420,7 +420,9 @@ export class Market { FIXED_MANIFEST_HEADER_SIZE, ), )[0].publicKey, - price: convertU128(restingOrderInternal.price), + numBaseTokens: toNum(restingOrderInternal.numBaseAtoms) / 10 ** baseMintDecimals, + tokenPrice: convertU128(restingOrderInternal.price) * 10 ** (baseMintDecimals - quoteMintDecimals), + ...restingOrderInternal, }; }) : []; diff --git a/client/ts/tests/batchUpdate.ts b/client/ts/tests/batchUpdate.ts index 98c218489..50b3c461a 100644 --- a/client/ts/tests/batchUpdate.ts +++ b/client/ts/tests/batchUpdate.ts @@ -40,13 +40,12 @@ async function testBatchUpdate(): Promise { // Asks are sorted worst to best. assert(market.asks().length == 1, 'batch update did not work'); assert( - Number(market.asks()[0].numBaseAtoms) == 5_000_000_000, + Number(market.asks()[0].numBaseTokens) == 5, 'ask top of book wrong size', ); - // TODO: Update this back to 5 after changing RestingOrders to tokens assert( - market.asks()[0].price == 0.0005, - `ask top of book wrong price ${market.asks()[0].price}`, + market.asks()[0].tokenPrice == 5, + `ask top of book wrong price ${market.asks()[0].tokenPrice}`, ); assert(market.bids().length == 0, 'place bids did not work'); } diff --git a/client/ts/tests/fillFeed.ts b/client/ts/tests/fillFeed.ts index d7ddcfdd9..9438c36e6 100644 --- a/client/ts/tests/fillFeed.ts +++ b/client/ts/tests/fillFeed.ts @@ -25,7 +25,7 @@ async function testFillFeed(): Promise { payerKeypair, marketAddress, market.quoteMint(), - 10, + 25, ); await placeOrder( connection, @@ -52,7 +52,7 @@ async function checkForFillMessage( connection: Connection, payerKeypair: Keypair, marketAddress: PublicKey, -) { +): Promise { const ws = new WebSocket('ws://localhost:1234'); ws.on('open', () => { diff --git a/client/ts/tests/placeOrder.ts b/client/ts/tests/placeOrder.ts index 370324dce..f7dd6346c 100644 --- a/client/ts/tests/placeOrder.ts +++ b/client/ts/tests/placeOrder.ts @@ -50,13 +50,12 @@ async function testPlaceOrder(): Promise { // Asks are sorted worst to best. assert(market.asks().length == 2, 'place asks did not work'); assert( - Number(market.asks()[0].numBaseAtoms) == 5_000_000_000, + Number(market.asks()[0].numBaseTokens) == 5_000_000_000, 'ask top of book wrong size', ); - // TODO: Update this back to 5 after changing RestingOrders to tokens assert( - market.asks()[0].price == 0.0005, - `ask top of book wrong price ${market.asks()[0].price}`, + market.asks()[0].tokenPrice == 5, + `ask top of book wrong price ${market.asks()[0].tokenPrice}`, ); assert(market.bids().length == 0, 'place bids did not work'); } From 508afbfa505165e7a7bfbb53982223398f74b5e0 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 16:10:13 -0400 Subject: [PATCH 17/21] fix deposit and withdraw mint decimals, ask size --- client/ts/src/client.ts | 16 ++++++++++------ client/ts/src/market.ts | 16 ++++++++++++---- client/ts/tests/placeOrder.ts | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 3bd8224d0..329cdc909 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -255,9 +255,11 @@ export class ManifestClient { const is22: boolean = (mint == this.baseMint.address && this.isBase22) || (mint == this.baseMint.address && this.isBase22); - const amountAtoms = Math.ceil( - amountTokens * 10 ** this.market.baseDecimals(), - ); + const mintDecimals = + this.market.quoteMint() === mint + ? this.market.quoteDecimals() + : this.market.baseDecimals(); + const amountAtoms = Math.ceil(amountTokens * 10 ** mintDecimals); return createDepositInstruction( { @@ -302,9 +304,11 @@ export class ManifestClient { const is22: boolean = (mint == this.baseMint.address && this.isBase22) || (mint == this.baseMint.address && this.isBase22); - const amountAtoms = Math.floor( - amountTokens * 10 ** this.market.baseDecimals(), - ); + const mintDecimals = + this.market.quoteMint() === mint + ? this.market.quoteDecimals() + : this.market.baseDecimals(); + const amountAtoms = Math.floor(amountTokens * 10 ** mintDecimals); return createWithdrawInstruction( { diff --git a/client/ts/src/market.ts b/client/ts/src/market.ts index 894671d05..240ab84de 100644 --- a/client/ts/src/market.ts +++ b/client/ts/src/market.ts @@ -395,8 +395,12 @@ export class Market { FIXED_MANIFEST_HEADER_SIZE, ), )[0].publicKey, - numBaseTokens: toNum(restingOrderInternal.numBaseAtoms) / 10 ** baseMintDecimals, - tokenPrice: convertU128(restingOrderInternal.price) * 10 ** (baseMintDecimals - quoteMintDecimals), + numBaseTokens: + toNum(restingOrderInternal.numBaseAtoms) / + 10 ** baseMintDecimals, + tokenPrice: + convertU128(restingOrderInternal.price) * + 10 ** (baseMintDecimals - quoteMintDecimals), ...restingOrderInternal, }; }) @@ -420,8 +424,12 @@ export class Market { FIXED_MANIFEST_HEADER_SIZE, ), )[0].publicKey, - numBaseTokens: toNum(restingOrderInternal.numBaseAtoms) / 10 ** baseMintDecimals, - tokenPrice: convertU128(restingOrderInternal.price) * 10 ** (baseMintDecimals - quoteMintDecimals), + numBaseTokens: + toNum(restingOrderInternal.numBaseAtoms) / + 10 ** baseMintDecimals, + tokenPrice: + convertU128(restingOrderInternal.price) * + 10 ** (baseMintDecimals - quoteMintDecimals), ...restingOrderInternal, }; }) diff --git a/client/ts/tests/placeOrder.ts b/client/ts/tests/placeOrder.ts index f7dd6346c..8963d7649 100644 --- a/client/ts/tests/placeOrder.ts +++ b/client/ts/tests/placeOrder.ts @@ -50,7 +50,7 @@ async function testPlaceOrder(): Promise { // Asks are sorted worst to best. assert(market.asks().length == 2, 'place asks did not work'); assert( - Number(market.asks()[0].numBaseTokens) == 5_000_000_000, + Number(market.asks()[0].numBaseTokens) == 5, 'ask top of book wrong size', ); assert( From 02cfe0a2e5551647af6e925031845c1aeeb7457e Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 16:40:43 -0400 Subject: [PATCH 18/21] compare strings --- client/ts/src/client.ts | 11 +++++++++-- client/ts/tests/deposit.ts | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 329cdc909..8abc8138e 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -256,10 +256,17 @@ export class ManifestClient { (mint == this.baseMint.address && this.isBase22) || (mint == this.baseMint.address && this.isBase22); const mintDecimals = - this.market.quoteMint() === mint + this.market.quoteMint().toBase58() === mint.toBase58() ? this.market.quoteDecimals() : this.market.baseDecimals(); const amountAtoms = Math.ceil(amountTokens * 10 ** mintDecimals); + console.log( + 'DEPOSIT IX', + mintDecimals, + amountAtoms, + this.market.quoteMint().toBase58(), + mint.toBase58(), + ); return createDepositInstruction( { @@ -305,7 +312,7 @@ export class ManifestClient { (mint == this.baseMint.address && this.isBase22) || (mint == this.baseMint.address && this.isBase22); const mintDecimals = - this.market.quoteMint() === mint + this.market.quoteMint().toBase58() === mint.toBase58() ? this.market.quoteDecimals() : this.market.baseDecimals(); const amountAtoms = Math.floor(amountTokens * 10 ** mintDecimals); diff --git a/client/ts/tests/deposit.ts b/client/ts/tests/deposit.ts index cd538a2f8..255b708be 100644 --- a/client/ts/tests/deposit.ts +++ b/client/ts/tests/deposit.ts @@ -75,7 +75,7 @@ export async function deposit( amountAtoms, ); console.log( - `Minted ${amountTokens} tokens to ${traderTokenAccount} in ${mintSig}`, + `Minted ${amountTokens} tokens to ${traderTokenAccount} in ${mintSig}, Decimals ${mintDecimals}, Atoms ${amountAtoms}`, ); const signature = await sendAndConfirmTransaction( From 4153aebc087092baecc6e8dcf97ea3e9052a124c Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 17:06:04 -0400 Subject: [PATCH 19/21] remove excess logging --- client/ts/src/client.ts | 7 ------- client/ts/tests/deposit.ts | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index 8abc8138e..c9cc9d331 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -260,13 +260,6 @@ export class ManifestClient { ? this.market.quoteDecimals() : this.market.baseDecimals(); const amountAtoms = Math.ceil(amountTokens * 10 ** mintDecimals); - console.log( - 'DEPOSIT IX', - mintDecimals, - amountAtoms, - this.market.quoteMint().toBase58(), - mint.toBase58(), - ); return createDepositInstruction( { diff --git a/client/ts/tests/deposit.ts b/client/ts/tests/deposit.ts index 255b708be..cd538a2f8 100644 --- a/client/ts/tests/deposit.ts +++ b/client/ts/tests/deposit.ts @@ -75,7 +75,7 @@ export async function deposit( amountAtoms, ); console.log( - `Minted ${amountTokens} tokens to ${traderTokenAccount} in ${mintSig}, Decimals ${mintDecimals}, Atoms ${amountAtoms}`, + `Minted ${amountTokens} tokens to ${traderTokenAccount} in ${mintSig}`, ); const signature = await sendAndConfirmTransaction( From 1de9ebd3dbec3cf67c8982ec9e8b75dcb660b925 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 22:32:08 -0400 Subject: [PATCH 20/21] clarify price is tokenPrice --- client/ts/src/client.ts | 5 ++--- client/ts/tests/batchUpdate.ts | 4 ++-- client/ts/tests/placeOrder.ts | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/client/ts/src/client.ts b/client/ts/src/client.ts index c9cc9d331..48067a903 100644 --- a/client/ts/src/client.ts +++ b/client/ts/src/client.ts @@ -285,7 +285,6 @@ export class ManifestClient { * Withdraw instruction * * @param payer PublicKey of the trader - * @param market PublicKey of the market * @param mint PublicKey for withdraw mint. Must be either the base or quote * @param amountTokens Number of tokens to withdraw. * @@ -488,7 +487,7 @@ export type WrapperPlaceOrderParamsExternal = { /** Number of base tokens in the order. */ numBaseTokens: number; /** Price as float in quote tokens per base tokens. */ - price: number; + tokenPrice: number; /** Boolean for whether this order is on the bid side. */ isBid: boolean; /** Last slot before this order is invalid and will be removed. */ @@ -510,7 +509,7 @@ function toWrapperPlaceOrderParams( // Converts token price to atom price since not always equal // Ex. BONK/USDC = 0.00001854 USDC tokens/BONK tokens -> 0.0001854 USDC Atoms/BONK Atoms const priceQuoteAtomsPerBaseAtoms = - wrapperPlaceOrderParamsExternal.price * + wrapperPlaceOrderParamsExternal.tokenPrice * (quoteAtomsPerToken / baseAtomsPerToken); // TODO: Make a helper and test it for this logic. const { priceMantissa, priceExponent } = toMantissaAndExponent( diff --git a/client/ts/tests/batchUpdate.ts b/client/ts/tests/batchUpdate.ts index 50b3c461a..e25a9a0f1 100644 --- a/client/ts/tests/batchUpdate.ts +++ b/client/ts/tests/batchUpdate.ts @@ -55,7 +55,7 @@ async function batchUpdate( payerKeypair: Keypair, marketAddress: PublicKey, numBaseTokens: number, - price: number, + tokenPrice: number, isBid: boolean, orderType: OrderType, clientOrderId: number, @@ -72,7 +72,7 @@ async function batchUpdate( [ { numBaseTokens, - price, + tokenPrice, isBid, lastValidSlot: lastValidSlot, orderType: orderType, diff --git a/client/ts/tests/placeOrder.ts b/client/ts/tests/placeOrder.ts index 8963d7649..9ef51bce7 100644 --- a/client/ts/tests/placeOrder.ts +++ b/client/ts/tests/placeOrder.ts @@ -65,7 +65,7 @@ export async function placeOrder( payerKeypair: Keypair, marketAddress: PublicKey, numBaseTokens: number, - price: number, + tokenPrice: number, isBid: boolean, orderType: OrderType, clientOrderId: number, @@ -80,7 +80,7 @@ export async function placeOrder( const placeOrderIx = client.placeOrderIx({ numBaseTokens, - price, + tokenPrice, isBid, lastValidSlot: lastValidSlot, orderType: orderType, From f36b2402dede2fff3d8dd70a5296273358b49a2f Mon Sep 17 00:00:00 2001 From: DonDuala Date: Fri, 30 Aug 2024 23:19:36 -0400 Subject: [PATCH 21/21] clarify balance is just this market --- client/ts/src/market.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/ts/src/market.ts b/client/ts/src/market.ts index 240ab84de..76c9cca45 100644 --- a/client/ts/src/market.ts +++ b/client/ts/src/market.ts @@ -157,7 +157,7 @@ export class Market { } /** - * Get the amount in atoms of balance that is deposited on the exchange, does + * Get the amount in atoms of balance that is deposited on this market, does * not include tokens currently in open orders. * * @param trader PublicKey of the trader to check balance of @@ -181,7 +181,7 @@ export class Market { } /** - * Get the amount in tokens of balance that is deposited on the exchange, does + * Get the amount in tokens of balance that is deposited on this market, does * not include tokens currently in open orders. * * @param trader PublicKey of the trader to check balance of