From 96db0b1619bf2ef8c076c69372edaafee3a5cffc Mon Sep 17 00:00:00 2001 From: riordanp Date: Fri, 9 Aug 2024 12:27:42 +0100 Subject: [PATCH] Fix price impact for USDC pairs (#990) * workaround for different WIF symbol in API response * handle price impact for TCS swaps with USDC in pair * handle price impact in other tcs function --- ts/client/src/accounts/group.ts | 8 ++++++- ts/client/src/accounts/mangoAccount.ts | 32 ++++++++++++++++++-------- ts/client/src/client.ts | 30 +++++++++++++++++------- ts/client/src/risk.ts | 9 +++++++- 4 files changed, 60 insertions(+), 19 deletions(-) diff --git a/ts/client/src/accounts/group.ts b/ts/client/src/accounts/group.ts index 7e9447118..5f1405bbd 100644 --- a/ts/client/src/accounts/group.ts +++ b/ts/client/src/accounts/group.ts @@ -604,9 +604,15 @@ export class Group { public getPriceImpactByTokenIndex( tokenIndex: TokenIndex, usdcAmountUi: number, + side: 'bid' | 'ask' | undefined = undefined, ): number { const bank = this.getFirstBankByTokenIndex(tokenIndex); - const pisBps = computePriceImpactOnJup(this.pis, usdcAmountUi, bank.name); + const pisBps = computePriceImpactOnJup( + this.pis, + usdcAmountUi, + bank.name, + side, + ); return (pisBps * 100) / 10000; } diff --git a/ts/client/src/accounts/mangoAccount.ts b/ts/client/src/accounts/mangoAccount.ts index 5f88887c7..8565f7322 100644 --- a/ts/client/src/accounts/mangoAccount.ts +++ b/ts/client/src/accounts/mangoAccount.ts @@ -2250,18 +2250,32 @@ export class TokenConditionalSwap { liqorTcsChunkSizeInUsd = 1000; } - const buyTokenPriceImpact = group.getPriceImpactByTokenIndex( - buyBank.tokenIndex, - liqorTcsChunkSizeInUsd, - ); - const sellTokenPriceImpact = group.getPriceImpactByTokenIndex( - sellBank.tokenIndex, - liqorTcsChunkSizeInUsd, - ); + const buyTokenPriceImpact = + buyBank.tokenIndex == 0 + ? group.getPriceImpactByTokenIndex( + sellBank.tokenIndex, + liqorTcsChunkSizeInUsd, + 'ask', + ) + : group.getPriceImpactByTokenIndex( + buyBank.tokenIndex, + liqorTcsChunkSizeInUsd, + ); + const sellTokenPriceImpact = + sellBank.tokenIndex == 0 + ? group.getPriceImpactByTokenIndex( + buyBank.tokenIndex, + liqorTcsChunkSizeInUsd, + 'bid', + ) + : group.getPriceImpactByTokenIndex( + buyBank.tokenIndex, + liqorTcsChunkSizeInUsd, + ); if (buyTokenPriceImpact <= 0 || sellTokenPriceImpact <= 0) { throw new Error( - `Error compitong slippage/premium for token conditional swap!`, + `Error computing slippage/premium for token conditional swap!`, ); } diff --git a/ts/client/src/client.ts b/ts/client/src/client.ts index c7f9f1517..9c71d9889 100644 --- a/ts/client/src/client.ts +++ b/ts/client/src/client.ts @@ -4624,14 +4624,28 @@ export class MangoClient { // TODO: The max premium should likely be computed differently if (!maxPricePremiumPercent) { - const buyTokenPriceImpact = group.getPriceImpactByTokenIndex( - buyBank.tokenIndex, - liqorTcsChunkSizeInUsd, - ); - const sellTokenPriceImpact = group.getPriceImpactByTokenIndex( - sellBank.tokenIndex, - liqorTcsChunkSizeInUsd, - ); + const buyTokenPriceImpact = + buyBank.tokenIndex == 0 + ? group.getPriceImpactByTokenIndex( + sellBank.tokenIndex, + liqorTcsChunkSizeInUsd, + 'ask', + ) + : group.getPriceImpactByTokenIndex( + buyBank.tokenIndex, + liqorTcsChunkSizeInUsd, + ); + const sellTokenPriceImpact = + sellBank.tokenIndex == 0 + ? group.getPriceImpactByTokenIndex( + buyBank.tokenIndex, + liqorTcsChunkSizeInUsd, + 'bid', + ) + : group.getPriceImpactByTokenIndex( + buyBank.tokenIndex, + liqorTcsChunkSizeInUsd, + ); if (buyTokenPriceImpact <= 0 || sellTokenPriceImpact <= 0) { throw new Error( diff --git a/ts/client/src/risk.ts b/ts/client/src/risk.ts index 00c6b93e2..34e832c59 100644 --- a/ts/client/src/risk.ts +++ b/ts/client/src/risk.ts @@ -70,6 +70,7 @@ export function computePriceImpactOnJup( pis: PriceImpact[], usdcAmount: number, tokenName: string, + side: 'bid' | 'ask' | undefined = undefined, ): number { try { const closestTo = [ @@ -81,8 +82,14 @@ export function computePriceImpactOnJup( if (tokenName == 'ETH (Portal)') { tokenName = 'ETH'; } + if (tokenName == 'WIF') { + tokenName = '$WIF'; + } const filteredPis: PriceImpact[] = pis.filter( - (pi) => pi.symbol == tokenName && pi.target_amount == closestTo, + (pi) => + pi.symbol == tokenName && + pi.target_amount == closestTo && + (side !== undefined ? pi.side == side : true), ); if (filteredPis.length > 0) { return (filteredPis[0].p90 * 10000) / 100;