Skip to content

Commit

Permalink
Fix price impact for USDC pairs (#990)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
riordanp authored Aug 9, 2024
1 parent 4a868c4 commit 96db0b1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
8 changes: 7 additions & 1 deletion ts/client/src/accounts/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
32 changes: 23 additions & 9 deletions ts/client/src/accounts/mangoAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!`,
);
}

Expand Down
30 changes: 22 additions & 8 deletions ts/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion ts/client/src/risk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export function computePriceImpactOnJup(
pis: PriceImpact[],
usdcAmount: number,
tokenName: string,
side: 'bid' | 'ask' | undefined = undefined,
): number {
try {
const closestTo = [
Expand All @@ -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;
Expand Down

0 comments on commit 96db0b1

Please sign in to comment.