@imtbl/sdk 0.27.8 → 0.27.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14735,18 +14735,45 @@ const newAmount = (amount, token) => ({
14735
14735
  token,
14736
14736
  });
14737
14737
  const isERC20Amount = (amount) => amount.token.type === 'erc20';
14738
+ const isNativeAmount = (amount) => amount.token.type === 'native';
14738
14739
  const addERC20Amount = (a, b) => {
14739
14740
  // Make sure the ERC20s have the same address
14740
14741
  if (a.token.address !== b.token.address)
14741
14742
  throw new Error('Token mismatch: token addresses must be the same');
14742
14743
  return { value: a.value.add(b.value), token: a.token };
14743
14744
  };
14745
+ const addNativeAmount = (a, b) => ({
14746
+ value: a.value.add(b.value),
14747
+ token: a.token,
14748
+ });
14749
+ const addAmount = (a, b) => {
14750
+ if (isERC20Amount(a) && isERC20Amount(b)) {
14751
+ return addERC20Amount(a, b);
14752
+ }
14753
+ if (isNativeAmount(a) && isNativeAmount(b)) {
14754
+ return addNativeAmount(a, b);
14755
+ }
14756
+ throw new Error('Token mismatch: token types must be the same');
14757
+ };
14744
14758
  const subtractERC20Amount = (a, b) => {
14745
14759
  // Make sure the ERC20s have the same address
14746
14760
  if (a.token.address !== b.token.address)
14747
14761
  throw new Error('Token mismatch: token addresses must be the same');
14748
14762
  return { value: a.value.sub(b.value), token: a.token };
14749
14763
  };
14764
+ const subtractNativeAmount = (a, b) => ({
14765
+ value: a.value.sub(b.value),
14766
+ token: a.token,
14767
+ });
14768
+ const subtractAmount = (a, b) => {
14769
+ if (isERC20Amount(a) && isERC20Amount(b)) {
14770
+ return subtractERC20Amount(a, b);
14771
+ }
14772
+ if (isNativeAmount(a) && isNativeAmount(b)) {
14773
+ return subtractNativeAmount(a, b);
14774
+ }
14775
+ throw new Error('Token mismatch: token types must be the same');
14776
+ };
14750
14777
 
14751
14778
  const amountIndex = 0;
14752
14779
  const gasEstimateIndex = 3;
@@ -15120,11 +15147,11 @@ const getUnsignedERC20ApproveTransaction = (ownerAddress, tokenAmount, spenderAd
15120
15147
  from: ownerAddress,
15121
15148
  };
15122
15149
  };
15123
- const prepareApproval = (tradeType, amountSpecified, amountWithSlippage, routingContracts, secondaryFees) => {
15150
+ const prepareApproval = (tradeType, amountSpecified, amountWithSlippage, contracts, secondaryFees) => {
15124
15151
  const amountOfTokenIn = tradeType === TradeType.EXACT_INPUT ? amountSpecified : amountWithSlippage;
15125
15152
  const spender = secondaryFees.length === 0
15126
- ? routingContracts.peripheryRouterAddress
15127
- : routingContracts.secondaryFeeAddress;
15153
+ ? contracts.routerAddress
15154
+ : contracts.secondaryFeeAddress;
15128
15155
  return { spender, amount: amountOfTokenIn };
15129
15156
  };
15130
15157
  /**
@@ -15203,7 +15230,7 @@ function applySlippage(tradeType, amount, slippage) {
15203
15230
  const amountWithSlippage = maybeInverted.multiply(amount.toString()).quotient;
15204
15231
  return ethers.BigNumber.from(amountWithSlippage.toString());
15205
15232
  }
15206
- function prepareUserQuote(otherToken, tradeInfo, slippage, fees) {
15233
+ function prepareUserQuote(otherToken, tradeInfo, slippage, fees, nativeTokenService) {
15207
15234
  const quote = getQuoteAmountFromTradeType(tradeInfo);
15208
15235
  const amountWithSlippage = applySlippage(tradeInfo.tradeType, quote.value, slippage);
15209
15236
  return {
@@ -15213,15 +15240,14 @@ function prepareUserQuote(otherToken, tradeInfo, slippage, fees) {
15213
15240
  value: amountWithSlippage,
15214
15241
  },
15215
15242
  slippage,
15216
- fees: fees.withAmounts(),
15243
+ fees: fees.withAmounts().map((fee) => ({
15244
+ ...fee,
15245
+ amount: nativeTokenService.maybeWrapAmount(fee.amount),
15246
+ })),
15217
15247
  };
15218
15248
  }
15219
15249
  function getOurQuoteReqAmount(amountSpecified, // the amount specified by the user, either exactIn or exactOut
15220
15250
  fees, tradeType, nativeTokenService) {
15221
- // TODO: TP-1649: Remove this when we support Native
15222
- if (!isERC20Amount(amountSpecified)) {
15223
- throw new Error('Not implemented yet!');
15224
- }
15225
15251
  if (tradeType === TradeType.EXACT_OUTPUT) {
15226
15252
  // For an exact output swap, we do not need to subtract fees from the given amount
15227
15253
  return nativeTokenService.maybeWrapAmount(amountSpecified);
@@ -15252,14 +15278,17 @@ class Fees {
15252
15278
  this.secondaryFees = secondaryFees;
15253
15279
  this.amount = newAmount(BigNumber$1.from(0), token);
15254
15280
  }
15281
+ get token() {
15282
+ return this.amount.token;
15283
+ }
15255
15284
  addAmount(amount) {
15256
- this.amount = addERC20Amount(this.amount, amount);
15285
+ this.amount = addAmount(this.amount, amount);
15257
15286
  }
15258
15287
  amountWithFeesApplied() {
15259
- return addERC20Amount(this.amount, this.total());
15288
+ return addAmount(this.amount, this.total());
15260
15289
  }
15261
15290
  amountLessFees() {
15262
- return subtractERC20Amount(this.amount, this.total());
15291
+ return subtractAmount(this.amount, this.total());
15263
15292
  }
15264
15293
  withAmounts() {
15265
15294
  return this.secondaryFees.map((fee) => {
@@ -15278,7 +15307,7 @@ class Fees {
15278
15307
  const feeAmount = this.amount.value
15279
15308
  .mul(fee.basisPoints)
15280
15309
  .div(BASIS_POINT_PRECISION);
15281
- totalFees = addERC20Amount(totalFees, newAmount(feeAmount, this.amount.token));
15310
+ totalFees = addAmount(totalFees, newAmount(feeAmount, this.amount.token));
15282
15311
  }
15283
15312
  return totalFees;
15284
15313
  }
@@ -15454,21 +15483,33 @@ function getSwap(adjustedQuote, fromAddress, slippage, deadline, peripheryRouter
15454
15483
  gasFeeEstimate: gasFeeEstimate ? nativeTokenService.maybeWrapAmount(gasFeeEstimate) : null,
15455
15484
  };
15456
15485
  }
15457
- function prepareSwap(ourQuote, amountSpecified, fees) {
15486
+ const adjustAmountIn = (ourQuote, amountSpecified, fees, nativeTokenService) => {
15458
15487
  if (ourQuote.tradeType === TradeType.EXACT_OUTPUT) {
15459
- fees.addAmount(ourQuote.amountIn);
15460
- return {
15461
- gasEstimate: ourQuote.gasEstimate,
15462
- route: ourQuote.route,
15463
- amountIn: fees.amountWithFeesApplied(),
15464
- amountOut: amountSpecified,
15465
- tradeType: ourQuote.tradeType,
15466
- };
15467
- }
15488
+ // when doing exact output, calculate the fees based on the amountIn
15489
+ const amountToAdd = nativeTokenService.isNativeToken(fees.token)
15490
+ ? nativeTokenService.unwrapAmount(ourQuote.amountIn)
15491
+ : ourQuote.amountIn;
15492
+ fees.addAmount(amountToAdd);
15493
+ return nativeTokenService.maybeWrapAmount(fees.amountWithFeesApplied());
15494
+ }
15495
+ return nativeTokenService.maybeWrapAmount(amountSpecified);
15496
+ };
15497
+ /**
15498
+ * adjustQuoteWithFees adjusts the amountIn of the quote to account for fees
15499
+ * EXACT_OUTPUT swaps will have the fees added to the amountIn if there are fees specified
15500
+ * EXACT_INPUT swaps will have amountIn set to the user-specified amount
15501
+ * @param ourQuote The quote from calling the Quoter contract
15502
+ * @param amountSpecified The user-specified amount for the swap (EXACT...)
15503
+ * @param fees The fees applied to the swap
15504
+ * @param tokenWrapper Helper class for the native token and associated ERC20
15505
+ * @returns {QuoteResult} The adjusted quote
15506
+ */
15507
+ function adjustQuoteWithFees(ourQuote, amountSpecified, fees, nativeTokenService) {
15508
+ const adjustedAmountIn = adjustAmountIn(ourQuote, amountSpecified, fees, nativeTokenService);
15468
15509
  return {
15469
15510
  gasEstimate: ourQuote.gasEstimate,
15470
15511
  route: ourQuote.route,
15471
- amountIn: amountSpecified,
15512
+ amountIn: adjustedAmountIn,
15472
15513
  amountOut: ourQuote.amountOut,
15473
15514
  tradeType: ourQuote.tradeType,
15474
15515
  };
@@ -15569,6 +15610,8 @@ class Exchange {
15569
15610
  wrappedNativeToken;
15570
15611
  secondaryFees;
15571
15612
  nativeTokenService;
15613
+ secondaryFeeContractAddress;
15614
+ routerContractAddress;
15572
15615
  constructor(configuration) {
15573
15616
  const config = new ExchangeConfiguration(configuration);
15574
15617
  this.chainId = config.chain.chainId;
@@ -15576,13 +15619,13 @@ class Exchange {
15576
15619
  this.wrappedNativeToken = config.chain.wrappedNativeToken;
15577
15620
  this.nativeTokenService = new NativeTokenService(this.nativeToken, this.wrappedNativeToken);
15578
15621
  this.secondaryFees = config.secondaryFees;
15622
+ this.routerContractAddress = config.chain.contracts.peripheryRouter;
15623
+ this.secondaryFeeContractAddress = config.chain.contracts.secondaryFee;
15579
15624
  this.provider = new ethers.providers.JsonRpcProvider(config.chain.rpcUrl);
15580
15625
  this.router = new Router(this.provider, config.chain.commonRoutingTokens, {
15581
15626
  multicallAddress: config.chain.contracts.multicall,
15582
15627
  factoryAddress: config.chain.contracts.coreFactory,
15583
15628
  quoterAddress: config.chain.contracts.quoterV2,
15584
- peripheryRouterAddress: config.chain.contracts.peripheryRouter,
15585
- secondaryFeeAddress: config.chain.contracts.secondaryFee,
15586
15629
  });
15587
15630
  }
15588
15631
  static validate(tokenInAddress, tokenOutAddress, maxHops, slippagePercent, fromAddress) {
@@ -15599,7 +15642,7 @@ class Exchange {
15599
15642
  if (this.secondaryFees.length === 0) {
15600
15643
  return [];
15601
15644
  }
15602
- const secondaryFeeContract = SecondaryFee__factory.connect(this.router.routingContracts.secondaryFeeAddress, this.provider);
15645
+ const secondaryFeeContract = SecondaryFee__factory.connect(this.secondaryFeeContractAddress, this.provider);
15603
15646
  if (await secondaryFeeContract.paused()) {
15604
15647
  // Do not use secondary fees if the contract is paused
15605
15648
  return [];
@@ -15627,8 +15670,7 @@ class Exchange {
15627
15670
  decimals: tokenOutDecimals,
15628
15671
  };
15629
15672
  // determine which amount was specified for the swap from the TradeType
15630
- const [tokenSpecified, otherToken] = tradeType === TradeType.EXACT_INPUT
15631
- ? [tokenIn, tokenOut] : [tokenOut, tokenIn];
15673
+ const [tokenSpecified, otherToken] = tradeType === TradeType.EXACT_INPUT ? [tokenIn, tokenOut] : [tokenOut, tokenIn];
15632
15674
  const amountSpecified = newAmount(amount, tokenSpecified);
15633
15675
  const fees = new Fees(secondaryFees, tokenIn);
15634
15676
  const ourQuoteReqAmount = getOurQuoteReqAmount(amountSpecified, fees, tradeType, this.nativeTokenService);
@@ -15637,10 +15679,13 @@ class Exchange {
15637
15679
  this.router.findOptimalRoute(ourQuoteReqAmount, otherToken, tradeType, maxHops),
15638
15680
  fetchGasPrice(this.provider, this.nativeToken),
15639
15681
  ]);
15640
- const adjustedQuote = prepareSwap(ourQuote, amountSpecified, fees);
15641
- const swap = getSwap(adjustedQuote, fromAddress, slippagePercent, deadline, this.router.routingContracts.peripheryRouterAddress, this.router.routingContracts.secondaryFeeAddress, gasPrice, secondaryFees, this.nativeTokenService);
15642
- const userQuote = prepareUserQuote(otherToken, adjustedQuote, slippagePercent, fees);
15643
- const preparedApproval = prepareApproval(tradeType, amountSpecified, userQuote.amountWithMaxSlippage, this.router.routingContracts, secondaryFees);
15682
+ const adjustedQuote = adjustQuoteWithFees(ourQuote, amountSpecified, fees, this.nativeTokenService);
15683
+ const swap = getSwap(adjustedQuote, fromAddress, slippagePercent, deadline, this.routerContractAddress, this.secondaryFeeContractAddress, gasPrice, secondaryFees, this.nativeTokenService);
15684
+ const userQuote = prepareUserQuote(otherToken, adjustedQuote, slippagePercent, fees, this.nativeTokenService);
15685
+ const preparedApproval = prepareApproval(tradeType, amountSpecified, userQuote.amountWithMaxSlippage, {
15686
+ routerAddress: this.routerContractAddress,
15687
+ secondaryFeeAddress: this.secondaryFeeContractAddress,
15688
+ }, secondaryFees);
15644
15689
  // preparedApproval always uses the tokenIn address because we are always selling the tokenIn
15645
15690
  const approval = await getApproval(this.provider, fromAddress, preparedApproval, gasPrice, this.nativeTokenService);
15646
15691
  return {
@@ -20503,7 +20548,7 @@ class APIError extends Error {
20503
20548
 
20504
20549
  /* eslint-disable implicit-arrow-linebreak */
20505
20550
  const defaultHeaders = {
20506
- sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-0.27.8',
20551
+ sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-0.27.9',
20507
20552
  };
20508
20553
  /**
20509
20554
  * createAPIConfiguration to create a custom Configuration
@@ -25,7 +25,7 @@ var $045213b18e9f1f94$export$75be0f6af9650f2c;
25
25
  })($045213b18e9f1f94$export$75be0f6af9650f2c || ($045213b18e9f1f94$export$75be0f6af9650f2c = {}));
26
26
 
27
27
 
28
- const $d663162fd5a0853f$export$38a083d9fb5551ca = "0.27.8";
28
+ const $d663162fd5a0853f$export$38a083d9fb5551ca = "0.27.9";
29
29
  const $d663162fd5a0853f$export$83bf90b30cdfd29f = ()=>false;
30
30
  const $d663162fd5a0853f$export$1169f5475725171b = ()=>$d663162fd5a0853f$export$38a083d9fb5551ca;
31
31
 
@@ -16,7 +16,7 @@ const createImmutableXConfiguration = ({ basePath, chainID, coreContractAddress,
16
16
  chainID,
17
17
  coreContractAddress,
18
18
  registrationContractAddress,
19
- sdkVersion: 'ts-immutable-sdk-0.27.8',
19
+ sdkVersion: 'ts-immutable-sdk-0.27.9',
20
20
  });
21
21
  /**
22
22
  * Sets `sdkVersion` at the time of build