From f462c62816cd62aec775a03b942cd877034c5903 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Thu, 10 Aug 2023 13:48:28 +0200 Subject: [PATCH] ts: reuse tcs creation function --- .../liqtest/liqtest-make-tcs-candidates.ts | 12 +- ts/client/src/client.ts | 109 +++++++----------- 2 files changed, 48 insertions(+), 73 deletions(-) diff --git a/ts/client/scripts/liqtest/liqtest-make-tcs-candidates.ts b/ts/client/scripts/liqtest/liqtest-make-tcs-candidates.ts index 5a81f6e7d..6685c6da3 100644 --- a/ts/client/scripts/liqtest/liqtest-make-tcs-candidates.ts +++ b/ts/client/scripts/liqtest/liqtest-make-tcs-candidates.ts @@ -180,8 +180,8 @@ async function main() { account, MINTS.get('SOL')!, MINTS.get('USDC')!, - 100000000, - 20000, // liqee only has 1k USDC-native, leverage does not go that far! + new BN(100000000), + new BN(20000), // liqee only has 1k USDC-native, leverage does not go that far! null, 0.0, 1000000.0, @@ -204,8 +204,8 @@ async function main() { account, MINTS.get('SOL')!, MINTS.get('USDC')!, - 1000, - 1000, + new BN(1000), + new BN(1000), null, 0.0, 1000000.0, @@ -228,8 +228,8 @@ async function main() { account, MINTS.get('SOL')!, MINTS.get('USDC')!, - 1000, - 1000, + new BN(1000), + new BN(1000), Date.now() / 1000 + 15, // expire in 15s 0.0, 1000000.0, diff --git a/ts/client/src/client.ts b/ts/client/src/client.ts index 74097d436..84cef99dd 100644 --- a/ts/client/src/client.ts +++ b/ts/client/src/client.ts @@ -3587,9 +3587,6 @@ export class MangoClient { upperLimit = thresholdPrice; } - const expiryTimestampBn = - expiryTimestamp !== null ? new BN(expiryTimestamp) : U64_MAX_BN; - if (!pricePremium) { const buyTokenPriceImpact = group.getPriceImpactByTokenIndex( buyBank.tokenIndex, @@ -3621,19 +3618,55 @@ export class MangoClient { break; } + return await this.tokenConditionalSwapCreateRaw( + group, + account, + buyBank.mint, + sellBank.mint, + maxBuy, + maxSell, + expiryTimestamp, + lowerLimit, + upperLimit, + pricePremiumRate, + allowCreatingDeposits, + allowCreatingBorrows, + thresholdPriceInSellPerBuyToken + ? TokenConditionalSwapDisplayPriceStyle.sellTokenPerBuyToken + : TokenConditionalSwapDisplayPriceStyle.buyTokenPerSellToken, + intention, + ); + } + + public async tokenConditionalSwapCreateRaw( + group: Group, + account: MangoAccount, + buyMintPk: PublicKey, + sellMintPk: PublicKey, + maxBuy: BN, + maxSell: BN, + expiryTimestamp: number | null, + priceLowerLimit: number, + priceUpperLimit: number, + pricePremiumRate: number, + allowCreatingDeposits: boolean, + allowCreatingBorrows: boolean, + priceDisplayStyle: TokenConditionalSwapDisplayPriceStyle, + intention: TokenConditionalSwapIntention, + ): Promise { + const buyBank: Bank = group.getFirstBankByMint(buyMintPk); + const sellBank: Bank = group.getFirstBankByMint(sellMintPk); const tcsIx = await this.program.methods .tokenConditionalSwapCreateV2( maxBuy, maxSell, - expiryTimestampBn, - lowerLimit, - upperLimit, + expiryTimestamp !== null ? new BN(expiryTimestamp) : U64_MAX_BN, + priceLowerLimit, + priceUpperLimit, pricePremiumRate, allowCreatingDeposits, allowCreatingBorrows, - thresholdPriceInSellPerBuyToken - ? TokenConditionalSwapDisplayPriceStyle.sellTokenPerBuyToken - : TokenConditionalSwapDisplayPriceStyle.buyTokenPerSellToken, + priceDisplayStyle, intention, ) .accounts({ @@ -3664,64 +3697,6 @@ export class MangoClient { return await this.sendAndConfirmTransactionForGroup(group, ixs); } - public async tokenConditionalSwapCreateRaw( - group: Group, - account: MangoAccount, - buyMintPk: PublicKey, - sellMintPk: PublicKey, - maxBuy: number, - maxSell: number, - expiryTimestamp: number | null, - priceLowerLimit: number, - priceUpperLimit: number, - pricePremiumRate: number, - allowCreatingDeposits: boolean, - allowCreatingBorrows: boolean, - priceDisplayStyle: TokenConditionalSwapDisplayPriceStyle, - intention: TokenConditionalSwapIntention, - ): Promise { - const buyBank: Bank = group.getFirstBankByMint(buyMintPk); - const sellBank: Bank = group.getFirstBankByMint(sellMintPk); - const ix = await this.program.methods - .tokenConditionalSwapCreateV2( - new BN(maxBuy), - new BN(maxSell), - expiryTimestamp !== null ? new BN(expiryTimestamp) : U64_MAX_BN, - priceLowerLimit, - priceUpperLimit, - pricePremiumRate, - allowCreatingDeposits, - allowCreatingBorrows, - priceDisplayStyle, - intention, - ) - .accounts({ - group: group.publicKey, - account: account.publicKey, - authority: (this.program.provider as AnchorProvider).wallet.publicKey, - buyBank: buyBank.publicKey, - sellBank: sellBank.publicKey, - }) - .instruction(); - - const ixs = [ix]; - if (account.tokenConditionalSwaps.length == 0) { - ixs.push( - await this.accountExpandV2Ix( - group, - account, - account.tokens.length, - account.serum3.length, - account.perps.length, - account.perpOpenOrders.length, - 8, - ), - ); - } - - return await this.sendAndConfirmTransactionForGroup(group, ixs); - } - public async tokenConditionalSwapCancel( group: Group, account: MangoAccount,