Skip to content

Commit 29ec917

Browse files
authored
feat(checkout): Return price impact as part of the quote (#2646)
1 parent 18fe545 commit 29ec917

File tree

22 files changed

+377
-168
lines changed

22 files changed

+377
-168
lines changed

packages/checkout/sdk/src/sdk.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ describe('Connect', () => {
10791079
},
10801080
value: BigInt('1050000000000000000'), // Example value with 5% max slippage
10811081
},
1082+
priceImpact: 0,
10821083
},
10831084
swapReceipt: {
10841085
to: '0xRecipientAddress',

packages/checkout/sdk/src/smartCheckout/routing/bridgeAndSwap/constructBridgeRequirements.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('constructBridgeRequirements', () => {
4747
basisPoints: 0,
4848
},
4949
],
50+
priceImpact: 0,
5051
},
5152
};
5253

packages/checkout/sdk/src/smartCheckout/routing/swap/quoteFetcher.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ describe('quoteFetcher', () => {
4646
basisPoints: 0,
4747
},
4848
],
49+
priceImpact: 0,
4950
},
5051
approval: null,
5152
};
@@ -98,6 +99,7 @@ describe('quoteFetcher', () => {
9899
basisPoints: 0,
99100
},
100101
],
102+
priceImpact: 0,
101103
},
102104
};
103105

packages/checkout/sdk/src/smartCheckout/routing/swap/swapRoute.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ describe('swapRoute', () => {
7171
basisPoints: 0,
7272
},
7373
],
74+
priceImpact: 0,
7475
},
7576
approval: {
7677
value: BigInt(1),
@@ -125,6 +126,7 @@ describe('swapRoute', () => {
125126
basisPoints: 0,
126127
},
127128
],
129+
priceImpact: 0,
128130
},
129131
approval: {
130132
value: BigInt(1),

packages/checkout/widgets-lib/src/widgets/swap/functions/swapConversionRate.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ describe('formatQuoteConversionRate', () => {
224224
amountWithMaxSlippage: {} as Amount,
225225
slippage: 0,
226226
fees: [],
227+
priceImpact: 0,
227228
} as Quote,
228229
swap: {
229230
gasFeeEstimate: {

packages/checkout/widgets-lib/src/widgets/swap/functions/swapFees.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('formatSwapFees', () => {
2424
amountWithMaxSlippage: {} as Amount,
2525
slippage: 0,
2626
fees: [],
27+
priceImpact: 0,
2728
} as Quote,
2829
swap: {
2930
gasFeeEstimate: {

packages/internal/dex/sdk/jest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const config: Config = {
2121
},
2222
coveragePathIgnorePatterns:['node_modules', 'src/contracts/', 'src/test/'],
2323
transformIgnorePatterns: [],
24+
setupFiles: ['<rootDir>/jest.setup.js'],
2425
};
2526

2627
export default config;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TextEncoder } from 'util';
2+
3+
global.TextEncoder = TextEncoder;
4+
5+
/**
6+
* Required for ethers v6
7+
* @see https://github.com/ethers-io/ethers.js/issues/4365
8+
*/
9+
Object.defineProperty(Uint8Array, Symbol.hasInstance, {
10+
value(potentialInstance) {
11+
return this === Uint8Array
12+
? Object.prototype.toString.call(potentialInstance) ===
13+
'[object Uint8Array]'
14+
: Uint8Array[Symbol.hasInstance].call(this, potentialInstance);
15+
},
16+
});

packages/internal/dex/sdk/src/exchange.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ const toPublicQuote = (
3636
amount: CoinAmount<Coin>,
3737
amountWithMaxSlippage: CoinAmount<Coin>,
3838
slippage: number,
39+
priceImpact: number,
3940
fees: Fees,
4041
): Quote => ({
4142
amount: toPublicAmount(amount),
4243
amountWithMaxSlippage: toPublicAmount(amountWithMaxSlippage),
4344
slippage,
45+
priceImpact,
4446
fees: fees.withAmounts().map((fee) => ({
4547
...fee,
4648
amount: toPublicAmount(fee.amount),
@@ -223,6 +225,7 @@ export class Exchange {
223225
amount: toPublicAmount(otherTokenCoinAmount),
224226
amountWithMaxSlippage: toPublicAmount(otherTokenCoinAmount),
225227
slippage: 0,
228+
priceImpact: 0,
226229
fees: [],
227230
};
228231

@@ -340,7 +343,13 @@ export class Exchange {
340343
? await getApproval(this.provider, fromAddress, preparedApproval, gasPrice)
341344
: null;
342345

343-
const quote = toPublicQuote(quotedAmount, quotedAmountWithMaxSlippage, slippagePercent, fees);
346+
const quote = toPublicQuote(
347+
quotedAmount,
348+
quotedAmountWithMaxSlippage,
349+
slippagePercent,
350+
Number(adjustedQuote.priceImpact.toSignificant(10)),
351+
fees,
352+
);
344353

345354
return { quote, approval, swap };
346355
}

0 commit comments

Comments
 (0)