From ea0267000345d11eb8038942965a397f70e14f76 Mon Sep 17 00:00:00 2001 From: microwavedcola1 Date: Fri, 25 Aug 2023 15:20:00 +0200 Subject: [PATCH] ts: drop the premium calculators chunk size for tcs swap, we were overestimating slippage before Signed-off-by: microwavedcola1 --- ts/client/src/client.ts | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/ts/client/src/client.ts b/ts/client/src/client.ts index 8a3b02b6f..bc833284a 100644 --- a/ts/client/src/client.ts +++ b/ts/client/src/client.ts @@ -3587,26 +3587,44 @@ export class MangoClient { allowCreatingBorrows: boolean, expiryTimestamp: number | null, ): Promise { - const maxBuy = - maxBuyUi == Number.MAX_SAFE_INTEGER - ? U64_MAX_BN - : toNative(maxBuyUi, buyBank.mintDecimals); - const maxSell = - maxSellUi == Number.MAX_SAFE_INTEGER - ? U64_MAX_BN - : toNative(maxSellUi, sellBank.mintDecimals); + let maxBuy, maxSell, buyAmountInUsd, sellAmountInUsd; + if (maxBuyUi == Number.MAX_SAFE_INTEGER) { + maxBuy = U64_MAX_BN; + } else { + buyAmountInUsd = maxBuyUi * buyBank.uiPrice; + maxBuy = toNative(maxBuyUi, buyBank.mintDecimals); + } + if (maxSellUi == Number.MAX_SAFE_INTEGER) { + maxSell = U64_MAX_BN; + } else { + sellAmountInUsd = maxSellUi * sellBank.uiPrice; + maxSell = toNative(maxSellUi, sellBank.mintDecimals); + } + + // Used for computing optimal premium + let liqorTcsChunkSizeInUsd = Math.min(buyAmountInUsd, sellAmountInUsd); + if (liqorTcsChunkSizeInUsd > 5000) { + liqorTcsChunkSizeInUsd = 5000; + } + // For small TCS swaps, reduce chunk size to 1000 USD + else { + liqorTcsChunkSizeInUsd = 1000; + } const expiryTimestampBn = expiryTimestamp !== null ? new BN(expiryTimestamp) : U64_MAX_BN; if (!pricePremium) { + if (maxBuy.eq(U64_MAX_BN)) { + maxSell.toNumber() * sellBank.uiPrice; + } const buyTokenPriceImpact = group.getPriceImpactByTokenIndex( buyBank.tokenIndex, - 5000, + liqorTcsChunkSizeInUsd, ); const sellTokenPriceImpact = group.getPriceImpactByTokenIndex( sellBank.tokenIndex, - 5000, + liqorTcsChunkSizeInUsd, ); pricePremium = ((1 + buyTokenPriceImpact / 100) * (1 + sellTokenPriceImpact / 100) -