@hyperbridge/sdk 1.3.3 → 1.3.5

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.
@@ -941,6 +941,7 @@ interface GetRequestWithStatus extends GenericRequestWithStatuses {
941
941
  height: bigint;
942
942
  keys: HexString[];
943
943
  context: HexString;
944
+ commitment: HexString;
944
945
  }
945
946
  interface GetResponseByRequestIdResponse {
946
947
  getResponses: {
@@ -1202,7 +1203,7 @@ interface FillerConfig {
1202
1203
  * Policy for determining confirmation requirements
1203
1204
  */
1204
1205
  confirmationPolicy: {
1205
- getConfirmationBlocks: (chainId: number, amount: bigint) => number;
1206
+ getConfirmationBlocks: (chainId: number, amountUsd: number) => number;
1206
1207
  };
1207
1208
  /**
1208
1209
  * Maximum number of orders to process concurrently
@@ -1852,7 +1853,7 @@ declare function estimateGasForPost(params: {
1852
1853
  * to match the format expected by the IntentGateway contract.
1853
1854
  */
1854
1855
  declare function constructRedeemEscrowRequestBody(order: Order, beneficiary: HexString): HexString;
1855
- declare function fetchPrice(identifier: string, chainId?: number): Promise<number>;
1856
+ declare function fetchPrice(identifier: string, chainId?: number, apiKey?: string): Promise<number>;
1856
1857
  /**
1857
1858
  * ERC20 method signatures used for storage slot detection
1858
1859
  */
@@ -2262,14 +2263,14 @@ declare class IndexerClient {
2262
2263
  * @param commitment_hash - Can be commitment
2263
2264
  * @returns Latest status and block metadata of the request
2264
2265
  */
2265
- queryPostRequest(commitment_hash: string): Promise<PostRequestWithStatus | undefined>;
2266
+ queryPostRequest(commitment_hash: HexString): Promise<PostRequestWithStatus | undefined>;
2266
2267
  /**
2267
2268
  * Queries a request by any of its associated hashes and returns it alongside its statuses
2268
2269
  * Statuses will be one of SOURCE, HYPERBRIDGE_DELIVERED and DESTINATION
2269
2270
  * @param hash - Can be commitment, hyperbridge tx hash, source tx hash, destination tx hash, or timeout tx hash
2270
2271
  * @returns Latest status and block metadata of the request
2271
2272
  */
2272
- queryGetRequest(hash: string): Promise<GetRequestWithStatus | undefined>;
2273
+ queryGetRequest(hash: HexString): Promise<GetRequestWithStatus | undefined>;
2273
2274
  /**
2274
2275
  * Queries the response associated with a specific request ID and returns its commitment
2275
2276
  * @param requestId - The ID of the request to find the associated response for
@@ -2316,7 +2317,31 @@ declare class IndexerClient {
2316
2317
  * @returns Full request data with all inferred status events, including SOURCE_FINALIZED and HYPERBRIDGE_FINALIZED
2317
2318
  * @remarks Unlike queryRequest(), this method adds derived finalization status events by querying state machine updates
2318
2319
  */
2319
- queryRequestWithStatus(hash: string): Promise<PostRequestWithStatus | undefined>;
2320
+ queryRequestWithStatus(hash: HexString): Promise<PostRequestWithStatus | undefined>;
2321
+ /**
2322
+ * Queries a GET request and returns it alongside its statuses,
2323
+ * including any finalization events.
2324
+ * @param hash - Can be commitment, hyperbridge tx hash, source tx hash, destination tx hash, or timeout tx hash
2325
+ * @returns Full GET request data with all inferred status events, including SOURCE_FINALIZED and HYPERBRIDGE_FINALIZED
2326
+ * @remarks Unlike queryGetRequest(), this method adds derived finalization status events by querying state machine updates
2327
+ */
2328
+ queryGetRequestWithStatus(hash: HexString): Promise<GetRequestWithStatus | undefined>;
2329
+ /**
2330
+ * Enhances a GET request with finality events by querying state machine updates.
2331
+ *
2332
+ * This method augments a GET request object with additional inferred status events
2333
+ * that represent chain finality confirmations. It adds:
2334
+ * - SOURCE_FINALIZED: When the source chain has finalized the request
2335
+ * - HYPERBRIDGE_FINALIZED: When Hyperbridge has finalized the delivery confirmation and response is ready
2336
+ *
2337
+ * The method also generates appropriate calldata for submitting cross-chain proofs
2338
+ * when applicable.
2339
+ *
2340
+ * @param request - The GET request to enhance with finality events
2341
+ * @returns The request with finality events added
2342
+ * @private
2343
+ */
2344
+ private addGetRequestFinalityEvents;
2320
2345
  /**
2321
2346
  * Create a Stream of status updates for a post request.
2322
2347
  * Stream ends when either the request reaches the destination or times out.
@@ -2596,7 +2621,7 @@ declare class IntentGateway {
2596
2621
  * @param amountOut - The desired output amount
2597
2622
  * @returns Object containing the best protocol, required input amount, and fee tier (for V3/V4)
2598
2623
  */
2599
- findBestProtocolWithAmountOut(chain: string, tokenIn: HexString, tokenOut: HexString, amountOut: bigint): Promise<{
2624
+ findBestProtocolWithAmountOut(getQuoteIn: "source" | "dest", tokenIn: HexString, tokenOut: HexString, amountOut: bigint): Promise<{
2600
2625
  protocol: "v2" | "v3" | "v4" | null;
2601
2626
  amountIn: bigint;
2602
2627
  fee?: number;
@@ -2611,7 +2636,7 @@ declare class IntentGateway {
2611
2636
  * @param amountIn - The input amount to swap
2612
2637
  * @returns Object containing the best protocol, expected output amount, and fee tier (for V3/V4)
2613
2638
  */
2614
- findBestProtocolWithAmountIn(chain: string, tokenIn: HexString, tokenOut: HexString, amountIn: bigint): Promise<{
2639
+ findBestProtocolWithAmountIn(getQuoteIn: "source" | "dest", tokenIn: HexString, tokenOut: HexString, amountIn: bigint, selectedProtocol?: "v2" | "v3" | "v4"): Promise<{
2615
2640
  protocol: "v2" | "v3" | "v4" | null;
2616
2641
  amountOut: bigint;
2617
2642
  fee?: number;
@@ -3410,18 +3410,18 @@ var addresses = {
3410
3410
  ["EVM-10200" /* GNOSIS_CHIADO */]: "0x0000000000000000000000000000000000000000",
3411
3411
  ["EVM-11155111" /* SEPOLIA */]: "0x0000000000000000000000000000000000000000",
3412
3412
  ["EVM-1" /* MAINNET */]: "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
3413
- ["EVM-56" /* BSC_MAINNET */]: "0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24",
3414
- ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x0000000000000000000000000000000000000000",
3415
- ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000"
3413
+ ["EVM-56" /* BSC_MAINNET */]: "0x10ED43C718714eb63d5aA57B78B54704E256024E",
3414
+ ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24",
3415
+ ["EVM-8453" /* BASE_MAINNET */]: "0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24"
3416
3416
  },
3417
3417
  UniswapV2Factory: {
3418
3418
  ["EVM-97" /* BSC_CHAPEL */]: "0x12e036669DA18F4A2777853d6e2136b32AceEC86",
3419
3419
  ["EVM-10200" /* GNOSIS_CHIADO */]: "0x0000000000000000000000000000000000000000",
3420
3420
  ["EVM-11155111" /* SEPOLIA */]: "0x0000000000000000000000000000000000000000",
3421
3421
  ["EVM-1" /* MAINNET */]: "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
3422
- ["EVM-56" /* BSC_MAINNET */]: "0x8909Dc15e40173Ff4699343b6eB8132c65e18eC6",
3423
- ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x0000000000000000000000000000000000000000",
3424
- ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000"
3422
+ ["EVM-56" /* BSC_MAINNET */]: "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73",
3423
+ ["EVM-42161" /* ARBITRUM_MAINNET */]: "0xf1D7CC64Fb4452F05c498126312eBE29f30Fbcf9",
3424
+ ["EVM-8453" /* BASE_MAINNET */]: "0x8909Dc15e40173Ff4699343b6eB8132c65e18eC6"
3425
3425
  },
3426
3426
  BatchExecutor: {
3427
3427
  ["EVM-97" /* BSC_CHAPEL */]: "0x4CC58B5D8FBf838d062E4b21F75C327835B5F0ef",
@@ -4420,10 +4420,10 @@ function mapTestnetToMainnet(identifier) {
4420
4420
  return identifier;
4421
4421
  }
4422
4422
  }
4423
- async function fetchPrice(identifier, chainId = 1) {
4423
+ async function fetchPrice(identifier, chainId = 1, apiKey) {
4424
4424
  const mappedIdentifier = mapTestnetToMainnet(identifier);
4425
4425
  const network = new ChainConfigService().getCoingeckoId(`EVM-${chainId}`) || "ethereum";
4426
- const apiKey = typeof process !== "undefined" ? process?.env?.COINGECKO : void 0;
4426
+ apiKey = apiKey || (typeof process !== "undefined" ? process?.env?.COINGECKO : void 0);
4427
4427
  const baseUrl = apiKey ? "https://pro-api.coingecko.com/api/v3" : "https://api.coingecko.com/api/v3";
4428
4428
  const url = mappedIdentifier.startsWith("0x") ? `${baseUrl}/simple/token_price/${network}?contract_addresses=${mappedIdentifier}&vs_currencies=usd` : `${baseUrl}/simple/price?ids=${mappedIdentifier}&vs_currencies=usd`;
4429
4429
  const headers = apiKey ? { "x-cg-pro-api-key": apiKey } : void 0;
@@ -5692,6 +5692,7 @@ async function _queryGetRequestInternal(params) {
5692
5692
  const { statusMetadata, ...rest } = response.getRequests.nodes[0];
5693
5693
  return {
5694
5694
  ...rest,
5695
+ commitment: commitmentHash,
5695
5696
  timeoutTimestamp: BigInt(rest.timeoutTimestamp),
5696
5697
  nonce: BigInt(rest.nonce),
5697
5698
  height: BigInt(rest.height),
@@ -6109,6 +6110,120 @@ var IndexerClient = class {
6109
6110
  );
6110
6111
  return request;
6111
6112
  }
6113
+ /**
6114
+ * Queries a GET request and returns it alongside its statuses,
6115
+ * including any finalization events.
6116
+ * @param hash - Can be commitment, hyperbridge tx hash, source tx hash, destination tx hash, or timeout tx hash
6117
+ * @returns Full GET request data with all inferred status events, including SOURCE_FINALIZED and HYPERBRIDGE_FINALIZED
6118
+ * @remarks Unlike queryGetRequest(), this method adds derived finalization status events by querying state machine updates
6119
+ */
6120
+ async queryGetRequestWithStatus(hash) {
6121
+ let request = await this.queryGetRequest(hash);
6122
+ if (!request) return;
6123
+ request = await this.addGetRequestFinalityEvents(request);
6124
+ request.statuses = request.statuses.sort(
6125
+ (a, b) => COMBINED_STATUS_WEIGHTS[a.status] - COMBINED_STATUS_WEIGHTS[b.status]
6126
+ );
6127
+ return request;
6128
+ }
6129
+ /**
6130
+ * Enhances a GET request with finality events by querying state machine updates.
6131
+ *
6132
+ * This method augments a GET request object with additional inferred status events
6133
+ * that represent chain finality confirmations. It adds:
6134
+ * - SOURCE_FINALIZED: When the source chain has finalized the request
6135
+ * - HYPERBRIDGE_FINALIZED: When Hyperbridge has finalized the delivery confirmation and response is ready
6136
+ *
6137
+ * The method also generates appropriate calldata for submitting cross-chain proofs
6138
+ * when applicable.
6139
+ *
6140
+ * @param request - The GET request to enhance with finality events
6141
+ * @returns The request with finality events added
6142
+ * @private
6143
+ */
6144
+ async addGetRequestFinalityEvents(request) {
6145
+ const events = [];
6146
+ const addFinalityEvents = (request2) => {
6147
+ this.logger.trace(`Added ${events.length} \`GetRequest\` finality events`, events);
6148
+ request2.statuses = [...request2.statuses, ...events];
6149
+ return request2;
6150
+ };
6151
+ let hyperbridgeDelivered;
6152
+ if (request.source === this.config.hyperbridge.stateMachineId) {
6153
+ hyperbridgeDelivered = request.statuses[0];
6154
+ return addFinalityEvents(request);
6155
+ } else {
6156
+ const sourceFinality = await this.queryStateMachineUpdateByHeight({
6157
+ statemachineId: request.source,
6158
+ height: request.statuses[0].metadata.blockNumber,
6159
+ chain: this.config.hyperbridge.stateMachineId
6160
+ });
6161
+ if (!sourceFinality) return addFinalityEvents(request);
6162
+ events.push({
6163
+ status: RequestStatus.SOURCE_FINALIZED,
6164
+ metadata: {
6165
+ blockHash: sourceFinality.blockHash,
6166
+ blockNumber: sourceFinality.height,
6167
+ transactionHash: sourceFinality.transactionHash,
6168
+ timestamp: sourceFinality.timestamp
6169
+ }
6170
+ });
6171
+ hyperbridgeDelivered = request.statuses.find((item) => item.status === RequestStatus.HYPERBRIDGE_DELIVERED);
6172
+ if (!hyperbridgeDelivered) return addFinalityEvents(request);
6173
+ }
6174
+ const hyperbridgeFinality = await this.queryStateMachineUpdateByHeight({
6175
+ statemachineId: this.config.hyperbridge.stateMachineId,
6176
+ height: hyperbridgeDelivered.metadata.blockNumber,
6177
+ chain: request.source
6178
+ });
6179
+ if (!hyperbridgeFinality) return addFinalityEvents(request);
6180
+ const sourceChain = await getChain(this.config.source);
6181
+ const hyperbridge = await getChain({
6182
+ ...this.config.hyperbridge,
6183
+ hasher: "Keccak"
6184
+ });
6185
+ try {
6186
+ const response = await this.queryResponseByRequestId(request.commitment);
6187
+ if (!response) return addFinalityEvents(request);
6188
+ const proof = await hyperbridge.queryProof(
6189
+ { Responses: [response.commitment] },
6190
+ request.source,
6191
+ BigInt(hyperbridgeFinality.height)
6192
+ );
6193
+ const calldata = sourceChain.encode({
6194
+ kind: "GetResponse",
6195
+ proof: {
6196
+ stateMachine: this.config.hyperbridge.stateMachineId,
6197
+ consensusStateId: this.config.hyperbridge.consensusStateId,
6198
+ proof,
6199
+ height: BigInt(hyperbridgeFinality.height)
6200
+ },
6201
+ responses: [
6202
+ {
6203
+ get: request,
6204
+ values: request.keys.map((key, index) => ({
6205
+ key,
6206
+ value: response.values[index] || "0x"
6207
+ }))
6208
+ }
6209
+ ],
6210
+ signer: pad("0x")
6211
+ });
6212
+ events.push({
6213
+ status: RequestStatus.HYPERBRIDGE_FINALIZED,
6214
+ metadata: {
6215
+ blockHash: hyperbridgeFinality.blockHash,
6216
+ blockNumber: hyperbridgeFinality.height,
6217
+ transactionHash: hyperbridgeFinality.transactionHash,
6218
+ timestamp: hyperbridgeFinality.timestamp,
6219
+ calldata
6220
+ }
6221
+ });
6222
+ } catch (error) {
6223
+ this.logger.trace("Could not generate HYPERBRIDGE_FINALIZED event for GET request:", error);
6224
+ }
6225
+ return addFinalityEvents(request);
6226
+ }
6112
6227
  /**
6113
6228
  * Create a Stream of status updates for a post request.
6114
6229
  * Stream ends when either the request reaches the destination or times out.
@@ -11402,16 +11517,11 @@ var IntentGateway = class {
11402
11517
  from: this.source.config.getIntentGatewayAddress(order.destChain),
11403
11518
  to: this.source.config.getIntentGatewayAddress(order.sourceChain)
11404
11519
  };
11405
- const { decimals: sourceChainFeeTokenDecimals, address: sourceChainFeeTokenAddress } = await this.source.getFeeTokenWithDecimals();
11520
+ const { decimals: sourceChainFeeTokenDecimals } = await this.source.getFeeTokenWithDecimals();
11406
11521
  const { address: destChainFeeTokenAddress, decimals: destChainFeeTokenDecimals } = await this.dest.getFeeTokenWithDecimals();
11407
11522
  const { gas: postGasEstimate, postRequestCalldata } = await this.source.estimateGas(postRequest);
11408
- const postGasEstimateInSourceFeeToken = await this.convertGasToFeeToken(
11409
- postGasEstimate,
11410
- this.source.client,
11411
- sourceChainFeeTokenDecimals
11412
- );
11413
- const RELAYER_FEE_BPS = 200n;
11414
- const relayerFeeInSourceFeeToken = postGasEstimateInSourceFeeToken + postGasEstimateInSourceFeeToken * RELAYER_FEE_BPS / 10000n;
11523
+ const postGasEstimateInSourceFeeToken = await this.convertGasToFeeToken(postGasEstimate, "source");
11524
+ const relayerFeeInSourceFeeToken = postGasEstimateInSourceFeeToken + 25n * 10n ** BigInt(sourceChainFeeTokenDecimals - 2);
11415
11525
  const relayerFeeInDestFeeToken = adjustFeeDecimals(
11416
11526
  relayerFeeInSourceFeeToken,
11417
11527
  sourceChainFeeTokenDecimals,
@@ -11466,7 +11576,8 @@ var IntentGateway = class {
11466
11576
  ];
11467
11577
  let destChainFillGas = 0n;
11468
11578
  try {
11469
- const protocolFeeInNativeToken = await this.quoteNative(postRequest, relayerFeeInDestFeeToken);
11579
+ let protocolFeeInNativeToken = await this.quoteNative(postRequest, relayerFeeInDestFeeToken);
11580
+ protocolFeeInNativeToken = protocolFeeInNativeToken + protocolFeeInNativeToken * 50n / 10000n;
11470
11581
  destChainFillGas = await this.dest.client.estimateContractGas({
11471
11582
  abi: IntentGateway_default.ABI,
11472
11583
  address: intentGatewayAddress,
@@ -11510,9 +11621,10 @@ var IntentGateway = class {
11510
11621
  stateOverride: stateOverrides
11511
11622
  });
11512
11623
  }
11513
- const fillGasInSourceFeeToken = await this.convertGasToFeeToken(
11514
- destChainFillGas,
11515
- this.dest.client,
11624
+ const fillGasInDestFeeToken = await this.convertGasToFeeToken(destChainFillGas, "dest");
11625
+ const fillGasInSourceFeeToken = adjustFeeDecimals(
11626
+ fillGasInDestFeeToken,
11627
+ destChainFeeTokenDecimals,
11516
11628
  sourceChainFeeTokenDecimals
11517
11629
  );
11518
11630
  const protocolFeeInSourceFeeToken = adjustFeeDecimals(
@@ -11522,14 +11634,10 @@ var IntentGateway = class {
11522
11634
  sourceChainFeeTokenDecimals
11523
11635
  );
11524
11636
  const totalEstimate = fillGasInSourceFeeToken + protocolFeeInSourceFeeToken + relayerFeeInSourceFeeToken;
11525
- const SWAP_OPERATIONS_BPS = 2500n;
11637
+ const SWAP_OPERATIONS_BPS = 3500n;
11526
11638
  const swapOperationsInFeeToken = totalEstimate * SWAP_OPERATIONS_BPS / 10000n;
11527
11639
  const totalFeeTokenAmount = totalEstimate + swapOperationsInFeeToken;
11528
- const totalNativeTokenAmount = await this.convertFeeTokenToNative(
11529
- totalFeeTokenAmount,
11530
- this.source.client,
11531
- sourceChainFeeTokenDecimals
11532
- );
11640
+ const totalNativeTokenAmount = await this.convertFeeTokenToNative(totalFeeTokenAmount, "source");
11533
11641
  return {
11534
11642
  feeTokenAmount: totalFeeTokenAmount,
11535
11643
  nativeTokenAmount: totalNativeTokenAmount,
@@ -11546,16 +11654,19 @@ var IntentGateway = class {
11546
11654
  * @returns The fee token amount converted to native token amount
11547
11655
  * @private
11548
11656
  */
11549
- async convertFeeTokenToNative(feeTokenAmount, publicClient, feeTokenDecimals) {
11550
- const nativeToken = publicClient.chain?.nativeCurrency;
11551
- const chainId = publicClient.chain?.id;
11552
- if (!nativeToken?.symbol || !nativeToken?.decimals) {
11553
- throw new Error("Chain native currency information not available");
11554
- }
11555
- const feeTokenAmountNumber = Number(feeTokenAmount) / Math.pow(10, feeTokenDecimals);
11556
- const nativeTokenPriceUsd = await fetchPrice(nativeToken.symbol, chainId);
11557
- const totalCostInNativeToken = feeTokenAmountNumber / nativeTokenPriceUsd;
11558
- return BigInt(Math.floor(totalCostInNativeToken * Math.pow(10, nativeToken.decimals)));
11657
+ async convertFeeTokenToNative(feeTokenAmount, getQuoteIn) {
11658
+ const client = this[getQuoteIn].client;
11659
+ const evmChainID = `EVM-${client.chain?.id}`;
11660
+ const wethAsset = this[getQuoteIn].config.getWrappedNativeAssetWithDecimals(evmChainID).asset;
11661
+ const feeToken = await this[getQuoteIn].getFeeTokenWithDecimals();
11662
+ const { amountOut } = await this.findBestProtocolWithAmountIn(
11663
+ getQuoteIn,
11664
+ feeToken.address,
11665
+ wethAsset,
11666
+ feeTokenAmount,
11667
+ "v2"
11668
+ );
11669
+ return amountOut;
11559
11670
  }
11560
11671
  /**
11561
11672
  * Converts gas costs to the equivalent amount in the fee token (DAI).
@@ -11567,20 +11678,21 @@ var IntentGateway = class {
11567
11678
  * @returns The gas cost converted to fee token amount
11568
11679
  * @private
11569
11680
  */
11570
- async convertGasToFeeToken(gasEstimate, publicClient, targetDecimals) {
11571
- const gasPrice = await publicClient.getGasPrice();
11681
+ async convertGasToFeeToken(gasEstimate, gasEstimateIn) {
11682
+ const client = this[gasEstimateIn].client;
11683
+ const gasPrice = await client.getGasPrice();
11572
11684
  const gasCostInWei = gasEstimate * gasPrice;
11573
- const nativeToken = publicClient.chain?.nativeCurrency;
11574
- const chainId = publicClient.chain?.id;
11575
- if (!nativeToken?.symbol || !nativeToken?.decimals) {
11576
- throw new Error("Chain native currency information not available");
11577
- }
11578
- const gasCostInToken = Number(gasCostInWei) / Math.pow(10, nativeToken.decimals);
11579
- const tokenPriceUsd = await fetchPrice(nativeToken.symbol, chainId);
11580
- const gasCostUsd = gasCostInToken * tokenPriceUsd;
11581
- const feeTokenPriceUsd = await fetchPrice("DAI");
11582
- const gasCostInFeeToken = gasCostUsd / feeTokenPriceUsd;
11583
- return BigInt(Math.floor(gasCostInFeeToken * Math.pow(10, targetDecimals)));
11685
+ const evmChainID = `EVM-${client.chain?.id}`;
11686
+ const wethAddr = this[gasEstimateIn].config.getWrappedNativeAssetWithDecimals(evmChainID).asset;
11687
+ const feeToken = await this[gasEstimateIn].getFeeTokenWithDecimals();
11688
+ const { amountOut } = await this.findBestProtocolWithAmountIn(
11689
+ gasEstimateIn,
11690
+ wethAddr,
11691
+ feeToken.address,
11692
+ gasCostInWei,
11693
+ "v2"
11694
+ );
11695
+ return amountOut;
11584
11696
  }
11585
11697
  async quoteNative(postRequest, fee) {
11586
11698
  const dispatchPost = {
@@ -11609,31 +11721,32 @@ var IntentGateway = class {
11609
11721
  * @param amountOut - The desired output amount
11610
11722
  * @returns Object containing the best protocol, required input amount, and fee tier (for V3/V4)
11611
11723
  */
11612
- async findBestProtocolWithAmountOut(chain, tokenIn, tokenOut, amountOut) {
11613
- const destClient = this.dest.client;
11724
+ async findBestProtocolWithAmountOut(getQuoteIn, tokenIn, tokenOut, amountOut) {
11725
+ const client = this[getQuoteIn].client;
11726
+ const evmChainID = `EVM-${client.chain?.id}`;
11614
11727
  let amountInV2 = maxUint256;
11615
11728
  let amountInV3 = maxUint256;
11616
11729
  let amountInV4 = maxUint256;
11617
11730
  let bestV3Fee = 0;
11618
11731
  let bestV4Fee = 0;
11619
11732
  const commonFees = [100, 500, 3e3, 1e4];
11620
- const v2Router = this.source.config.getUniswapRouterV2Address(chain);
11621
- const v2Factory = this.source.config.getUniswapV2FactoryAddress(chain);
11622
- const v3Factory = this.source.config.getUniswapV3FactoryAddress(chain);
11623
- const v3Quoter = this.source.config.getUniswapV3QuoterAddress(chain);
11624
- const v4Quoter = this.source.config.getUniswapV4QuoterAddress(chain);
11625
- const wethAsset = this.source.config.getWrappedNativeAssetWithDecimals(chain).asset;
11733
+ const v2Router = this.source.config.getUniswapRouterV2Address(evmChainID);
11734
+ const v2Factory = this.source.config.getUniswapV2FactoryAddress(evmChainID);
11735
+ const v3Factory = this.source.config.getUniswapV3FactoryAddress(evmChainID);
11736
+ const v3Quoter = this.source.config.getUniswapV3QuoterAddress(evmChainID);
11737
+ const v4Quoter = this.source.config.getUniswapV4QuoterAddress(evmChainID);
11738
+ const wethAsset = this.source.config.getWrappedNativeAssetWithDecimals(evmChainID).asset;
11626
11739
  const tokenInForQuote = tokenIn === ADDRESS_ZERO ? wethAsset : tokenIn;
11627
11740
  const tokenOutForQuote = tokenOut === ADDRESS_ZERO ? wethAsset : tokenOut;
11628
11741
  try {
11629
- const v2PairExists = await destClient.readContract({
11742
+ const v2PairExists = await client.readContract({
11630
11743
  address: v2Factory,
11631
11744
  abi: uniswapV2Factory_default.ABI,
11632
11745
  functionName: "getPair",
11633
11746
  args: [tokenInForQuote, tokenOutForQuote]
11634
11747
  });
11635
11748
  if (v2PairExists !== ADDRESS_ZERO) {
11636
- const v2AmountIn = await destClient.readContract({
11749
+ const v2AmountIn = await client.readContract({
11637
11750
  address: v2Router,
11638
11751
  abi: uniswapRouterV2_default.ABI,
11639
11752
  functionName: "getAmountsIn",
@@ -11647,20 +11760,20 @@ var IntentGateway = class {
11647
11760
  let bestV3AmountIn = maxUint256;
11648
11761
  for (const fee of commonFees) {
11649
11762
  try {
11650
- const pool = await destClient.readContract({
11763
+ const pool = await client.readContract({
11651
11764
  address: v3Factory,
11652
11765
  abi: uniswapV3Factory_default.ABI,
11653
11766
  functionName: "getPool",
11654
11767
  args: [tokenInForQuote, tokenOutForQuote, fee]
11655
11768
  });
11656
11769
  if (pool !== ADDRESS_ZERO) {
11657
- const liquidity = await destClient.readContract({
11770
+ const liquidity = await client.readContract({
11658
11771
  address: pool,
11659
11772
  abi: uniswapV3Pool_default.ABI,
11660
11773
  functionName: "liquidity"
11661
11774
  });
11662
11775
  if (liquidity > BigInt(0)) {
11663
- const quoteResult = (await destClient.simulateContract({
11776
+ const quoteResult = (await client.simulateContract({
11664
11777
  address: v3Quoter,
11665
11778
  abi: uniswapV3Quoter_default.ABI,
11666
11779
  functionName: "quoteExactOutputSingle",
@@ -11700,7 +11813,7 @@ var IntentGateway = class {
11700
11813
  hooks: ADDRESS_ZERO
11701
11814
  // No hooks
11702
11815
  };
11703
- const quoteResult = (await destClient.simulateContract({
11816
+ const quoteResult = (await client.simulateContract({
11704
11817
  address: v4Quoter,
11705
11818
  abi: UNISWAP_V4_QUOTER_ABI,
11706
11819
  functionName: "quoteExactOutputSingle",
@@ -11773,37 +11886,41 @@ var IntentGateway = class {
11773
11886
  * @param amountIn - The input amount to swap
11774
11887
  * @returns Object containing the best protocol, expected output amount, and fee tier (for V3/V4)
11775
11888
  */
11776
- async findBestProtocolWithAmountIn(chain, tokenIn, tokenOut, amountIn) {
11777
- const destClient = this.dest.client;
11889
+ async findBestProtocolWithAmountIn(getQuoteIn, tokenIn, tokenOut, amountIn, selectedProtocol) {
11890
+ const client = this[getQuoteIn].client;
11891
+ const evmChainID = `EVM-${client.chain?.id}`;
11778
11892
  let amountOutV2 = BigInt(0);
11779
11893
  let amountOutV3 = BigInt(0);
11780
11894
  let amountOutV4 = BigInt(0);
11781
11895
  let bestV3Fee = 0;
11782
11896
  let bestV4Fee = 0;
11783
11897
  const commonFees = [100, 500, 3e3, 1e4];
11784
- const v2Router = this.source.config.getUniswapRouterV2Address(chain);
11785
- const v2Factory = this.source.config.getUniswapV2FactoryAddress(chain);
11786
- const v3Factory = this.source.config.getUniswapV3FactoryAddress(chain);
11787
- const v3Quoter = this.source.config.getUniswapV3QuoterAddress(chain);
11788
- const v4Quoter = this.source.config.getUniswapV4QuoterAddress(chain);
11789
- const wethAsset = this.source.config.getWrappedNativeAssetWithDecimals(chain).asset;
11898
+ const v2Router = this.source.config.getUniswapRouterV2Address(evmChainID);
11899
+ const v2Factory = this.source.config.getUniswapV2FactoryAddress(evmChainID);
11900
+ const v3Factory = this.source.config.getUniswapV3FactoryAddress(evmChainID);
11901
+ const v3Quoter = this.source.config.getUniswapV3QuoterAddress(evmChainID);
11902
+ const v4Quoter = this.source.config.getUniswapV4QuoterAddress(evmChainID);
11903
+ const wethAsset = this.source.config.getWrappedNativeAssetWithDecimals(evmChainID).asset;
11790
11904
  const tokenInForQuote = tokenIn === ADDRESS_ZERO ? wethAsset : tokenIn;
11791
11905
  const tokenOutForQuote = tokenOut === ADDRESS_ZERO ? wethAsset : tokenOut;
11792
11906
  try {
11793
- const v2PairExists = await destClient.readContract({
11907
+ const v2PairExists = await client.readContract({
11794
11908
  address: v2Factory,
11795
11909
  abi: uniswapV2Factory_default.ABI,
11796
11910
  functionName: "getPair",
11797
11911
  args: [tokenInForQuote, tokenOutForQuote]
11798
11912
  });
11799
11913
  if (v2PairExists !== ADDRESS_ZERO) {
11800
- const v2AmountOut = await destClient.readContract({
11914
+ const v2AmountOut = await client.readContract({
11801
11915
  address: v2Router,
11802
11916
  abi: uniswapRouterV2_default.ABI,
11803
11917
  functionName: "getAmountsOut",
11804
11918
  args: [amountIn, [tokenInForQuote, tokenOutForQuote]]
11805
11919
  });
11806
11920
  amountOutV2 = v2AmountOut[1];
11921
+ if (selectedProtocol === "v2") {
11922
+ return { protocol: "v2", amountOut: amountOutV2 };
11923
+ }
11807
11924
  }
11808
11925
  } catch (error) {
11809
11926
  console.warn("V2 quote failed:", error);
@@ -11811,20 +11928,20 @@ var IntentGateway = class {
11811
11928
  let bestV3AmountOut = BigInt(0);
11812
11929
  for (const fee of commonFees) {
11813
11930
  try {
11814
- const pool = await destClient.readContract({
11931
+ const pool = await client.readContract({
11815
11932
  address: v3Factory,
11816
11933
  abi: uniswapV3Factory_default.ABI,
11817
11934
  functionName: "getPool",
11818
11935
  args: [tokenInForQuote, tokenOutForQuote, fee]
11819
11936
  });
11820
11937
  if (pool !== ADDRESS_ZERO) {
11821
- const liquidity = await destClient.readContract({
11938
+ const liquidity = await client.readContract({
11822
11939
  address: pool,
11823
11940
  abi: uniswapV3Pool_default.ABI,
11824
11941
  functionName: "liquidity"
11825
11942
  });
11826
11943
  if (liquidity > BigInt(0)) {
11827
- const quoteResult = (await destClient.simulateContract({
11944
+ const quoteResult = (await client.simulateContract({
11828
11945
  address: v3Quoter,
11829
11946
  abi: uniswapV3Quoter_default.ABI,
11830
11947
  functionName: "quoteExactInputSingle",
@@ -11850,6 +11967,9 @@ var IntentGateway = class {
11850
11967
  }
11851
11968
  }
11852
11969
  amountOutV3 = bestV3AmountOut;
11970
+ if (selectedProtocol === "v3") {
11971
+ return { protocol: "v3", amountOut: amountOutV3, fee: bestV3Fee };
11972
+ }
11853
11973
  let bestV4AmountOut = BigInt(0);
11854
11974
  for (const fee of commonFees) {
11855
11975
  try {
@@ -11864,7 +11984,7 @@ var IntentGateway = class {
11864
11984
  hooks: ADDRESS_ZERO
11865
11985
  // No hooks
11866
11986
  };
11867
- const quoteResult = (await destClient.simulateContract({
11987
+ const quoteResult = (await client.simulateContract({
11868
11988
  address: v4Quoter,
11869
11989
  abi: UNISWAP_V4_QUOTER_ABI,
11870
11990
  functionName: "quoteExactInputSingle",
@@ -11888,6 +12008,9 @@ var IntentGateway = class {
11888
12008
  }
11889
12009
  }
11890
12010
  amountOutV4 = bestV4AmountOut;
12011
+ if (selectedProtocol === "v4") {
12012
+ return { protocol: "v4", amountOut: amountOutV4, fee: bestV4Fee };
12013
+ }
11891
12014
  if (amountOutV2 === BigInt(0) && amountOutV3 === BigInt(0) && amountOutV4 === BigInt(0)) {
11892
12015
  return {
11893
12016
  protocol: null,