@hyperbridge/sdk 1.3.10 → 1.3.12

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.
@@ -5,12 +5,11 @@ import { createConsola, LogLevels } from 'consola';
5
5
  import { flatten, zip, capitalize, maxBy, isNil } from 'lodash-es';
6
6
  import { toHex, hexToBytes, encodePacked, keccak256, encodeAbiParameters, bytesToHex, concatHex, createPublicClient, http, encodeFunctionData, erc20Abi, bytesToBigInt, pad, toBytes, maxUint256, formatUnits, parseUnits, hexToString as hexToString$1 } from 'viem';
7
7
  import mergeRace from '@async-generator/merge-race';
8
- import { gnosisChiado, gnosis, bscTestnet, bsc, soneium, baseSepolia, base, optimismSepolia, optimism, arbitrumSepolia, arbitrum, mainnet, sepolia } from 'viem/chains';
8
+ import { gnosisChiado, gnosis, bscTestnet, bsc, soneium, baseSepolia, base, optimismSepolia, optimism, arbitrumSepolia, arbitrum, mainnet, unichain, polygon, sepolia } from 'viem/chains';
9
9
  import { hasWindow, isNode, env } from 'std-env';
10
10
  import { Vector, u8, Struct, Tuple, Enum, _void, u64, u32, Option, bool, u128, Bytes } from 'scale-ts';
11
11
  import { match } from 'ts-pattern';
12
12
  import { WsProvider, ApiPromise } from '@polkadot/api';
13
- import { RpcWebSocketClient } from 'rpc-websocket-client';
14
13
  import { keccakAsU8a, decodeAddress, keccakAsHex, xxhashAsU8a } from '@polkadot/util-crypto';
15
14
  import { GraphQLClient } from 'graphql-request';
16
15
  import { Decimal } from 'decimal.js';
@@ -3313,6 +3312,8 @@ var Chains = /* @__PURE__ */ ((Chains2) => {
3313
3312
  Chains2["BSC_MAINNET"] = "EVM-56";
3314
3313
  Chains2["ARBITRUM_MAINNET"] = "EVM-42161";
3315
3314
  Chains2["BASE_MAINNET"] = "EVM-8453";
3315
+ Chains2["POLYGON_MAINNET"] = "EVM-137";
3316
+ Chains2["UNICHAIN_MAINNET"] = "EVM-130";
3316
3317
  return Chains2;
3317
3318
  })(Chains || {});
3318
3319
  var chainIds = {
@@ -3323,7 +3324,9 @@ var chainIds = {
3323
3324
  ["EVM-1" /* MAINNET */]: 1,
3324
3325
  ["EVM-56" /* BSC_MAINNET */]: 56,
3325
3326
  ["EVM-42161" /* ARBITRUM_MAINNET */]: 42161,
3326
- ["EVM-8453" /* BASE_MAINNET */]: 8453
3327
+ ["EVM-8453" /* BASE_MAINNET */]: 8453,
3328
+ ["EVM-137" /* POLYGON_MAINNET */]: 137,
3329
+ ["EVM-130" /* UNICHAIN_MAINNET */]: 130
3327
3330
  };
3328
3331
  var viemChains = {
3329
3332
  "97": bscTestnet,
@@ -3332,7 +3335,9 @@ var viemChains = {
3332
3335
  "1": mainnet,
3333
3336
  "56": bsc,
3334
3337
  "42161": arbitrum,
3335
- "8453": base
3338
+ "8453": base,
3339
+ "137": polygon,
3340
+ "130": unichain
3336
3341
  };
3337
3342
  var WrappedNativeDecimals = {
3338
3343
  ["EVM-97" /* BSC_CHAPEL */]: 18,
@@ -3341,7 +3346,9 @@ var WrappedNativeDecimals = {
3341
3346
  ["EVM-1" /* MAINNET */]: 18,
3342
3347
  ["EVM-56" /* BSC_MAINNET */]: 18,
3343
3348
  ["EVM-42161" /* ARBITRUM_MAINNET */]: 18,
3344
- ["EVM-8453" /* BASE_MAINNET */]: 18
3349
+ ["EVM-8453" /* BASE_MAINNET */]: 18,
3350
+ ["EVM-137" /* POLYGON_MAINNET */]: 18,
3351
+ ["EVM-130" /* UNICHAIN_MAINNET */]: 18
3345
3352
  };
3346
3353
  var assets = {
3347
3354
  ["EVM-97" /* BSC_CHAPEL */]: {
@@ -3385,6 +3392,18 @@ var assets = {
3385
3392
  DAI: "0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb".toLowerCase(),
3386
3393
  USDC: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913".toLowerCase(),
3387
3394
  USDT: "0xfde4C96c8593536E31F229EA8f37b2ADa2699bb2".toLowerCase()
3395
+ },
3396
+ ["EVM-137" /* POLYGON_MAINNET */]: {
3397
+ WETH: "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619".toLowerCase(),
3398
+ DAI: "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063".toLowerCase(),
3399
+ USDC: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359".toLowerCase(),
3400
+ USDT: "0xc2132D05D31c914a87C6611C10748AEb04B58e8F".toLowerCase()
3401
+ },
3402
+ ["EVM-130" /* UNICHAIN_MAINNET */]: {
3403
+ WETH: "0x4200000000000000000000000000000000000006".toLowerCase(),
3404
+ DAI: "0x0000000000000000000000000000000000000000".toLowerCase(),
3405
+ USDC: "0x078d782b760474a361dda0af3839290b0ef57ad6".toLowerCase(),
3406
+ USDT: "0x9151434b16b9763660705744891fa906f660ecc5".toLowerCase()
3388
3407
  }
3389
3408
  };
3390
3409
  var addresses = {
@@ -3395,7 +3414,9 @@ var addresses = {
3395
3414
  ["EVM-1" /* MAINNET */]: "0xd54165e45926720b062C192a5bacEC64d5bB08DA",
3396
3415
  ["EVM-56" /* BSC_MAINNET */]: "0xd54165e45926720b062C192a5bacEC64d5bB08DA",
3397
3416
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0xd54165e45926720b062C192a5bacEC64d5bB08DA",
3398
- ["EVM-8453" /* BASE_MAINNET */]: "0xd54165e45926720b062C192a5bacEC64d5bB08DA"
3417
+ ["EVM-8453" /* BASE_MAINNET */]: "0xd54165e45926720b062C192a5bacEC64d5bB08DA",
3418
+ ["EVM-137" /* POLYGON_MAINNET */]: "0xd54165e45926720b062C192a5bacEC64d5bB08DA",
3419
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0xd54165e45926720b062C192a5bacEC64d5bB08DA"
3399
3420
  },
3400
3421
  Host: {
3401
3422
  ["EVM-97" /* BSC_CHAPEL */]: "0x8Aa0Dea6D675d785A882967Bf38183f6117C09b7",
@@ -3404,7 +3425,9 @@ var addresses = {
3404
3425
  ["EVM-1" /* MAINNET */]: "0x792A6236AF69787C40cF76b69B4c8c7B28c4cA20",
3405
3426
  ["EVM-56" /* BSC_MAINNET */]: "0x24B5d421Ec373FcA57325dd2F0C074009Af021F7",
3406
3427
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0xE05AFD4Eb2ce6d65c40e1048381BD0Ef8b4B299e",
3407
- ["EVM-8453" /* BASE_MAINNET */]: "0x6FFe92e4d7a9D589549644544780e6725E84b248"
3428
+ ["EVM-8453" /* BASE_MAINNET */]: "0x6FFe92e4d7a9D589549644544780e6725E84b248",
3429
+ ["EVM-137" /* POLYGON_MAINNET */]: "0xD8d3db17C1dF65b301D45C84405CcAC1395C559a",
3430
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0x2A17C1c3616Bbc33FCe5aF5B965F166ba76cEDAf"
3408
3431
  },
3409
3432
  UniswapRouter02: {
3410
3433
  ["EVM-97" /* BSC_CHAPEL */]: "0x9639379819420704457B07A0C33B678D9E0F8Df0",
@@ -3413,7 +3436,9 @@ var addresses = {
3413
3436
  ["EVM-1" /* MAINNET */]: "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
3414
3437
  ["EVM-56" /* BSC_MAINNET */]: "0x10ED43C718714eb63d5aA57B78B54704E256024E",
3415
3438
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24",
3416
- ["EVM-8453" /* BASE_MAINNET */]: "0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24"
3439
+ ["EVM-8453" /* BASE_MAINNET */]: "0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24",
3440
+ ["EVM-137" /* POLYGON_MAINNET */]: "0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff",
3441
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0x284f11109359a7e1306c3e447ef14d38400063ff"
3417
3442
  },
3418
3443
  UniswapV2Factory: {
3419
3444
  ["EVM-97" /* BSC_CHAPEL */]: "0x12e036669DA18F4A2777853d6e2136b32AceEC86",
@@ -3422,56 +3447,72 @@ var addresses = {
3422
3447
  ["EVM-1" /* MAINNET */]: "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
3423
3448
  ["EVM-56" /* BSC_MAINNET */]: "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73",
3424
3449
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0xf1D7CC64Fb4452F05c498126312eBE29f30Fbcf9",
3425
- ["EVM-8453" /* BASE_MAINNET */]: "0x8909Dc15e40173Ff4699343b6eB8132c65e18eC6"
3450
+ ["EVM-8453" /* BASE_MAINNET */]: "0x8909Dc15e40173Ff4699343b6eB8132c65e18eC6",
3451
+ ["EVM-137" /* POLYGON_MAINNET */]: "0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32",
3452
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0x1F98400000000000000000000000000000000002"
3426
3453
  },
3427
3454
  BatchExecutor: {
3428
3455
  ["EVM-97" /* BSC_CHAPEL */]: "0x4CC58B5D8FBf838d062E4b21F75C327835B5F0ef",
3429
3456
  ["EVM-1" /* MAINNET */]: "0x0000000000000000000000000000000000000000",
3430
3457
  ["EVM-56" /* BSC_MAINNET */]: "0x0000000000000000000000000000000000000000",
3431
3458
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x0000000000000000000000000000000000000000",
3432
- ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000"
3459
+ ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000",
3460
+ ["EVM-137" /* POLYGON_MAINNET */]: "0x0000000000000000000000000000000000000000",
3461
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0x0000000000000000000000000000000000000000"
3433
3462
  },
3434
3463
  UniversalRouter: {
3435
3464
  ["EVM-97" /* BSC_CHAPEL */]: "0xcc6d5ece3d4a57245bf5a2f64f3ed9179b81f714",
3436
3465
  ["EVM-1" /* MAINNET */]: "0x0000000000000000000000000000000000000000",
3437
3466
  ["EVM-56" /* BSC_MAINNET */]: "0x0000000000000000000000000000000000000000",
3438
3467
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x0000000000000000000000000000000000000000",
3439
- ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000"
3468
+ ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000",
3469
+ ["EVM-137" /* POLYGON_MAINNET */]: "0x0000000000000000000000000000000000000000",
3470
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0x0000000000000000000000000000000000000000"
3440
3471
  },
3441
3472
  UniswapV3Router: {
3442
3473
  ["EVM-97" /* BSC_CHAPEL */]: "0x0000000000000000000000000000000000000000",
3443
3474
  ["EVM-1" /* MAINNET */]: "0x1F98431c8aD98523631AE4a59f267346ea31F984",
3444
3475
  ["EVM-56" /* BSC_MAINNET */]: "0x0000000000000000000000000000000000000000",
3445
3476
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x0000000000000000000000000000000000000000",
3446
- ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000"
3477
+ ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000",
3478
+ ["EVM-137" /* POLYGON_MAINNET */]: "0x0000000000000000000000000000000000000000",
3479
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0x0000000000000000000000000000000000000000"
3447
3480
  },
3448
3481
  UniswapV3Factory: {
3449
3482
  ["EVM-97" /* BSC_CHAPEL */]: "0x0000000000000000000000000000000000000000",
3450
3483
  ["EVM-1" /* MAINNET */]: "0x1F98431c8aD98523631AE4a59f267346ea31F984",
3451
3484
  ["EVM-56" /* BSC_MAINNET */]: "0x0000000000000000000000000000000000000000",
3452
3485
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x0000000000000000000000000000000000000000",
3453
- ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000"
3486
+ ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000",
3487
+ ["EVM-137" /* POLYGON_MAINNET */]: "0x0000000000000000000000000000000000000000",
3488
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0x0000000000000000000000000000000000000000"
3454
3489
  },
3455
3490
  UniswapV3Quoter: {
3456
3491
  ["EVM-97" /* BSC_CHAPEL */]: "0x0000000000000000000000000000000000000000",
3457
3492
  ["EVM-1" /* MAINNET */]: "0x61fFE014bA17989E743c5F6cB21bF9697530B21e",
3458
3493
  ["EVM-56" /* BSC_MAINNET */]: "0x0000000000000000000000000000000000000000",
3459
3494
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x0000000000000000000000000000000000000000",
3460
- ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000"
3495
+ ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000",
3496
+ ["EVM-137" /* POLYGON_MAINNET */]: "0x0000000000000000000000000000000000000000",
3497
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0x0000000000000000000000000000000000000000"
3461
3498
  },
3462
3499
  UniswapV4PoolManager: {
3463
3500
  ["EVM-97" /* BSC_CHAPEL */]: "0x0000000000000000000000000000000000000000",
3464
3501
  ["EVM-1" /* MAINNET */]: "0x0000000000000000000000000000000000000000",
3465
3502
  ["EVM-56" /* BSC_MAINNET */]: "0x0000000000000000000000000000000000000000",
3466
3503
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x0000000000000000000000000000000000000000",
3467
- ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000"
3504
+ ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000",
3505
+ ["EVM-137" /* POLYGON_MAINNET */]: "0x0000000000000000000000000000000000000000",
3506
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0x0000000000000000000000000000000000000000"
3468
3507
  },
3469
3508
  UniswapV4Quoter: {
3470
3509
  ["EVM-97" /* BSC_CHAPEL */]: "0x0000000000000000000000000000000000000000",
3471
3510
  ["EVM-1" /* MAINNET */]: "0x52f0e24d1c21c8a0cb1e5a5dd6198556bd9e1203",
3472
3511
  ["EVM-56" /* BSC_MAINNET */]: "0x0000000000000000000000000000000000000000",
3473
3512
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0x0000000000000000000000000000000000000000",
3474
- ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000"
3513
+ ["EVM-8453" /* BASE_MAINNET */]: "0x0000000000000000000000000000000000000000",
3514
+ ["EVM-137" /* POLYGON_MAINNET */]: "0x0000000000000000000000000000000000000000",
3515
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "0x0000000000000000000000000000000000000000"
3475
3516
  },
3476
3517
  Calldispatcher: {
3477
3518
  ["EVM-11155111" /* SEPOLIA */]: "0xC7f13b6D03A0A7F3239d38897503E90553ABe155"
@@ -3485,7 +3526,9 @@ var createRpcUrls = (env2) => ({
3485
3526
  ["EVM-1" /* MAINNET */]: env2.ETH_MAINNET || "https://eth-mainnet.g.alchemy.com/v2/demo",
3486
3527
  ["EVM-56" /* BSC_MAINNET */]: env2.BSC_MAINNET || "https://binance.llamarpc.com",
3487
3528
  ["EVM-42161" /* ARBITRUM_MAINNET */]: env2.ARBITRUM_MAINNET || "https://arbitrum-one.public.blastapi.io",
3488
- ["EVM-8453" /* BASE_MAINNET */]: env2.BASE_MAINNET || "https://base-mainnet.public.blastapi.io"
3529
+ ["EVM-8453" /* BASE_MAINNET */]: env2.BASE_MAINNET || "https://base-mainnet.public.blastapi.io",
3530
+ ["EVM-137" /* POLYGON_MAINNET */]: env2.POLYGON_MAINNET || "https://polygon-bor-rpc.publicnode.com",
3531
+ ["EVM-130" /* UNICHAIN_MAINNET */]: env2.UNICHAIN_MAINNET || "https://unichain.api.onfinality.io/public"
3489
3532
  });
3490
3533
  var consensusStateIds = {
3491
3534
  ["EVM-97" /* BSC_CHAPEL */]: "BSC0",
@@ -3495,7 +3538,9 @@ var consensusStateIds = {
3495
3538
  ["EVM-1" /* MAINNET */]: "ETH0",
3496
3539
  ["EVM-56" /* BSC_MAINNET */]: "BSC0",
3497
3540
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "ETH0",
3498
- ["EVM-8453" /* BASE_MAINNET */]: "ETH0"
3541
+ ["EVM-8453" /* BASE_MAINNET */]: "ETH0",
3542
+ ["EVM-137" /* POLYGON_MAINNET */]: "ETH0",
3543
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "ETH0"
3499
3544
  };
3500
3545
  var coingeckoIds = {
3501
3546
  ["EVM-97" /* BSC_CHAPEL */]: "binance-smart-chain",
@@ -3505,7 +3550,9 @@ var coingeckoIds = {
3505
3550
  ["EVM-1" /* MAINNET */]: "ethereum",
3506
3551
  ["EVM-56" /* BSC_MAINNET */]: "binance-smart-chain",
3507
3552
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "arbitrum-one",
3508
- ["EVM-8453" /* BASE_MAINNET */]: "base"
3553
+ ["EVM-8453" /* BASE_MAINNET */]: "base",
3554
+ ["EVM-137" /* POLYGON_MAINNET */]: "matic-network",
3555
+ ["EVM-130" /* UNICHAIN_MAINNET */]: "ethereum"
3509
3556
  };
3510
3557
 
3511
3558
  // src/configs/ChainConfigService.ts
@@ -4505,6 +4552,14 @@ function adjustFeeDecimals(feeInFeeToken, fromDecimals, toDecimals) {
4505
4552
  return (feeInFeeToken + scaleFactor - 1n) / scaleFactor;
4506
4553
  }
4507
4554
  }
4555
+ function replaceWebsocketWithHttp(url) {
4556
+ if (url.startsWith("ws://")) {
4557
+ return url.replace("ws://", "http://");
4558
+ } else if (url.startsWith("wss://")) {
4559
+ return url.replace("wss://", "https://");
4560
+ }
4561
+ return url;
4562
+ }
4508
4563
 
4509
4564
  // src/utils/exceptions.ts
4510
4565
  var AbortSignalInternal = class _AbortSignalInternal extends Error {
@@ -5014,14 +5069,52 @@ function deriveSecondLevelSlot(key, firstLevelSlot) {
5014
5069
  const combined = new Uint8Array([...keyBytes, ...slotBytes]);
5015
5070
  return keccak256(combined);
5016
5071
  }
5072
+ var HttpRpcClient = class {
5073
+ constructor(url) {
5074
+ this.url = url;
5075
+ }
5076
+ /**
5077
+ * Make an RPC call over HTTP
5078
+ * @param method - The RPC method name
5079
+ * @param params - The parameters for the RPC call
5080
+ * @returns Promise resolving to the RPC response
5081
+ */
5082
+ async call(method, params = []) {
5083
+ const body = JSON.stringify({
5084
+ jsonrpc: "2.0",
5085
+ id: Date.now(),
5086
+ method,
5087
+ params
5088
+ });
5089
+ const response = await fetch(this.url, {
5090
+ method: "POST",
5091
+ headers: {
5092
+ "Content-Type": "application/json"
5093
+ },
5094
+ body
5095
+ });
5096
+ if (!response.ok) {
5097
+ throw new Error(`HTTP error! status: ${response.status}`);
5098
+ }
5099
+ const result = await response.json();
5100
+ if (result.error) {
5101
+ throw new Error(`RPC error: ${result.error.message}`);
5102
+ }
5103
+ return result.result;
5104
+ }
5105
+ };
5017
5106
  var SubstrateChain = class {
5018
5107
  constructor(params) {
5019
5108
  this.params = params;
5109
+ const url = this.params.ws;
5110
+ const httpUrl = replaceWebsocketWithHttp(url);
5111
+ this.rpcClient = new HttpRpcClient(httpUrl);
5020
5112
  }
5021
5113
  /*
5022
5114
  * api: The Polkadot API instance for the Substrate chain.
5023
5115
  */
5024
5116
  api;
5117
+ rpcClient;
5025
5118
  /*
5026
5119
  * connect: Connects to the Substrate chain using the provided WebSocket URL.
5027
5120
  */
@@ -5081,9 +5174,7 @@ var SubstrateChain = class {
5081
5174
  async queryRequestCommitment(commitment) {
5082
5175
  const prefix = toHex(":child_storage:default:ISMP");
5083
5176
  const key = this.requestCommitmentKey(commitment);
5084
- const rpc = new RpcWebSocketClient();
5085
- await rpc.connect(this.params.ws);
5086
- const item = await rpc.call("childstate_getStorage", [prefix, key]);
5177
+ const item = await this.rpcClient.call("childstate_getStorage", [prefix, key]);
5087
5178
  return item;
5088
5179
  }
5089
5180
  /**
@@ -5094,9 +5185,7 @@ var SubstrateChain = class {
5094
5185
  async queryRequestReceipt(commitment) {
5095
5186
  const prefix = toHex(":child_storage:default:ISMP");
5096
5187
  const key = this.requestReceiptKey(commitment);
5097
- const rpc = new RpcWebSocketClient();
5098
- await rpc.connect(this.params.ws);
5099
- const item = await rpc.call("childstate_getStorage", [prefix, key]);
5188
+ const item = await this.rpcClient.call("childstate_getStorage", [prefix, key]);
5100
5189
  return item;
5101
5190
  }
5102
5191
  /**
@@ -5116,15 +5205,13 @@ var SubstrateChain = class {
5116
5205
  * @returns {Promise<HexString>} The proof.
5117
5206
  */
5118
5207
  async queryProof(message, counterparty, at) {
5119
- const rpc = new RpcWebSocketClient();
5120
- await rpc.connect(this.params.ws);
5121
5208
  if (isEvmChain(counterparty)) {
5122
- const proof = await rpc.call("mmr_queryProof", [Number(at), message]);
5209
+ const proof = await this.rpcClient.call("mmr_queryProof", [Number(at), message]);
5123
5210
  return toHex(proof.proof);
5124
5211
  }
5125
5212
  if (isSubstrateChain(counterparty)) {
5126
5213
  const childTrieKeys = "Requests" in message ? message.Requests.map(requestCommitmentStorageKey) : message.Responses.map(responseCommitmentStorageKey);
5127
- const proof = await rpc.call("ismp_queryChildTrieProof", [Number(at), childTrieKeys]);
5214
+ const proof = await this.rpcClient.call("ismp_queryChildTrieProof", [Number(at), childTrieKeys]);
5128
5215
  const basicProof = BasicProof.dec(toHex(proof.proof));
5129
5216
  const encoded = SubstrateStateProof.enc({
5130
5217
  tag: "OverlayProof",
@@ -5188,10 +5275,8 @@ var SubstrateChain = class {
5188
5275
  * @returns The state proof as a hexadecimal string.
5189
5276
  */
5190
5277
  async queryStateProof(at, keys) {
5191
- const rpc = new RpcWebSocketClient();
5192
- await rpc.connect(this.params.ws);
5193
5278
  const encodedKeys = keys.map((key) => Array.from(hexToBytes(key)));
5194
- const proof = await rpc.call("ismp_queryChildTrieProof", [Number(at), encodedKeys]);
5279
+ const proof = await this.rpcClient.call("ismp_queryChildTrieProof", [Number(at), encodedKeys]);
5195
5280
  const basicProof = BasicProof.dec(toHex(proof.proof));
5196
5281
  const encoded = SubstrateStateProof.enc({
5197
5282
  tag: "OverlayProof",
@@ -5211,9 +5296,16 @@ var SubstrateChain = class {
5211
5296
  * @returns {Promise<bigint>} The latest state machine height.
5212
5297
  */
5213
5298
  async latestStateMachineHeight(stateMachineId) {
5214
- if (!this.api) throw new Error("API not initialized");
5215
- const latestHeight = await this.api.query.ismp.latestStateMachineHeight(stateMachineId);
5216
- return BigInt(latestHeight.toString());
5299
+ const state_id = convertStateIdToStateMachineId(stateMachineId.stateId);
5300
+ const consensusStateIdToBytes = hexToBytes(stateMachineId.consensusStateId);
5301
+ const decoder = new TextDecoder("utf-8");
5302
+ const decodedConsensusStateId = decoder.decode(consensusStateIdToBytes);
5303
+ const payload = {
5304
+ state_id,
5305
+ consensus_state_id: decodedConsensusStateId
5306
+ };
5307
+ const latestHeight = await this.rpcClient.call("ismp_queryStateMachineLatestHeight", [payload]);
5308
+ return BigInt(latestHeight);
5217
5309
  }
5218
5310
  /**
5219
5311
  * Get the state machine update time for a given state machine height.
@@ -5221,9 +5313,20 @@ var SubstrateChain = class {
5221
5313
  * @returns {Promise<bigint>} The statemachine update time in seconds.
5222
5314
  */
5223
5315
  async stateMachineUpdateTime(stateMachineHeight) {
5224
- if (!this.api) throw new Error("API not initialized");
5225
- const updateTime = await this.api.query.ismp.stateMachineUpdateTime(stateMachineHeight);
5226
- return BigInt(updateTime.toString());
5316
+ const state_id = convertStateIdToStateMachineId(stateMachineHeight.id.stateId);
5317
+ const consensusStateIdToBytes = hexToBytes(stateMachineHeight.id.consensusStateId);
5318
+ const decoder = new TextDecoder("utf-8");
5319
+ const decodedConsensusStateId = decoder.decode(consensusStateIdToBytes);
5320
+ const stateMachineId = {
5321
+ state_id,
5322
+ consensus_state_id: decodedConsensusStateId
5323
+ };
5324
+ const payload = {
5325
+ id: stateMachineId,
5326
+ height: Number(stateMachineHeight.height)
5327
+ };
5328
+ const updateTime = await this.rpcClient.call("ismp_queryStateMachineUpdateTime", [payload]);
5329
+ return BigInt(updateTime);
5227
5330
  }
5228
5331
  /**
5229
5332
  * Get the challenge period for a given state machine id.
@@ -5231,9 +5334,16 @@ var SubstrateChain = class {
5231
5334
  * @returns {Promise<bigint>} The challenge period in seconds.
5232
5335
  */
5233
5336
  async challengePeriod(stateMachineId) {
5234
- if (!this.api) throw new Error("API not initialized");
5235
- const challengePeriod = await this.api.query.ismp.challengePeriod(stateMachineId);
5236
- return BigInt(challengePeriod.toString());
5337
+ const state_id = convertStateIdToStateMachineId(stateMachineId.stateId);
5338
+ const consensusStateIdToBytes = hexToBytes(stateMachineId.consensusStateId);
5339
+ const decoder = new TextDecoder("utf-8");
5340
+ const decodedConsensusStateId = decoder.decode(consensusStateIdToBytes);
5341
+ const payload = {
5342
+ state_id,
5343
+ consensus_state_id: decodedConsensusStateId
5344
+ };
5345
+ const challengePeriod = await this.rpcClient.call("ismp_queryChallengePeriod", [payload]);
5346
+ return BigInt(challengePeriod);
5237
5347
  }
5238
5348
  /**
5239
5349
  * Encode an ISMP calldata for a substrate chain.
@@ -5283,6 +5393,24 @@ function convertStateMachineIdToEnum(id) {
5283
5393
  }
5284
5394
  return { tag, value };
5285
5395
  }
5396
+ function convertStateIdToStateMachineId(stateId) {
5397
+ switch (true) {
5398
+ case stateId.Evm !== void 0:
5399
+ return `EVM-${stateId.Evm}`;
5400
+ case stateId.Polkadot !== void 0:
5401
+ return `POLKADOT-${stateId.Polkadot}`;
5402
+ case stateId.Kusama !== void 0:
5403
+ return `KUSAMA-${stateId.Kusama}`;
5404
+ case stateId.Substrate !== void 0: {
5405
+ const bytes = hexToBytes(stateId.Substrate);
5406
+ const decoder = new TextDecoder("utf-8");
5407
+ const decoded = decoder.decode(bytes);
5408
+ return `SUBSTRATE-${decoded}`;
5409
+ }
5410
+ default:
5411
+ throw new Error("Unsupported stateId variant");
5412
+ }
5413
+ }
5286
5414
  function convertIPostRequestToCodec(request) {
5287
5415
  return {
5288
5416
  tag: "Post",
@@ -11552,13 +11680,6 @@ var IntentGateway = class {
11552
11680
  this.source = source;
11553
11681
  this.dest = dest;
11554
11682
  }
11555
- destStateproofCache = /* @__PURE__ */ new Map();
11556
- getCachedProof(id) {
11557
- return this.destStateproofCache.get(id);
11558
- }
11559
- clearCachedProof(id) {
11560
- this.destStateproofCache.delete(id);
11561
- }
11562
11683
  /**
11563
11684
  * Estimates the total cost required to fill an order, including gas fees, relayer fees,
11564
11685
  * protocol fees, and swap operations.
@@ -12167,117 +12288,128 @@ var IntentGateway = class {
12167
12288
  });
12168
12289
  return filledStatus !== "0x0000000000000000000000000000000000000000000000000000000000000000";
12169
12290
  }
12170
- async *cancelOrder(order, hyperbridgeConfig, indexerClient) {
12171
- const hyperbridge = await getChain({
12172
- ...hyperbridgeConfig,
12173
- hasher: "Keccak"
12174
- });
12291
+ async submitAndConfirmReceipt(hyperbridge, commitment, message) {
12292
+ let storageValue = await hyperbridge.queryRequestReceipt(commitment);
12293
+ if (!storageValue) {
12294
+ console.log("No receipt found. Attempting to submit...");
12295
+ try {
12296
+ await hyperbridge.submitUnsigned(message);
12297
+ } catch {
12298
+ console.warn("Submission failed. Awaiting network confirmation...");
12299
+ }
12300
+ console.log("Waiting for network state update...");
12301
+ await sleep(3e4);
12302
+ storageValue = await retryPromise(
12303
+ async () => {
12304
+ const value = await hyperbridge.queryRequestReceipt(commitment);
12305
+ if (!value) throw new Error("Receipt not found");
12306
+ return value;
12307
+ },
12308
+ { maxRetries: 5, backoffMs: 5e3, logMessage: "Checking for receipt" }
12309
+ );
12310
+ }
12311
+ console.log("Hyperbridge Receipt confirmed.");
12312
+ }
12313
+ async *cancelOrder(order, hyperbridgeConfig, indexerClient, storedData) {
12314
+ const hyperbridge = await getChain({ ...hyperbridgeConfig, hasher: "Keccak" });
12175
12315
  const sourceStateMachine = hexToString$1(order.sourceChain);
12176
12316
  const destStateMachine = hexToString$1(order.destChain);
12177
12317
  const sourceConsensusStateId = this.source.config.getConsensusStateId(sourceStateMachine);
12178
12318
  const destConsensusStateId = this.dest.config.getConsensusStateId(destStateMachine);
12179
- let latestHeight = 0n;
12180
- while (latestHeight <= order.deadline) {
12181
- const { stateId } = parseStateMachineId(destStateMachine);
12182
- latestHeight = await hyperbridge.latestStateMachineHeight({
12183
- stateId,
12184
- consensusStateId: destConsensusStateId
12185
- });
12186
- yield {
12187
- status: "AWAITING_DESTINATION_FINALIZED",
12188
- data: { currentHeight: latestHeight, deadline: order.deadline }
12189
- };
12190
- if (latestHeight <= order.deadline) {
12191
- await sleep(1e4);
12319
+ let destIProof;
12320
+ if (storedData?.destIProof) {
12321
+ destIProof = storedData.destIProof;
12322
+ yield { status: "DESTINATION_FINALIZED", data: { proof: destIProof.proof, height: destIProof.height } };
12323
+ } else {
12324
+ let latestHeight = 0n;
12325
+ let lastFailedHeight = null;
12326
+ let proofHex = null;
12327
+ while (!proofHex) {
12328
+ latestHeight = await retryPromise(
12329
+ () => hyperbridge.latestStateMachineHeight({
12330
+ stateId: parseStateMachineId(destStateMachine).stateId,
12331
+ consensusStateId: destConsensusStateId
12332
+ }),
12333
+ { maxRetries: 5, backoffMs: 500, logMessage: "Failed to fetch latest state machine height" }
12334
+ );
12335
+ const shouldFetchProof = lastFailedHeight === null ? latestHeight > order.deadline : latestHeight > lastFailedHeight;
12336
+ if (!shouldFetchProof) {
12337
+ yield {
12338
+ status: "AWAITING_DESTINATION_FINALIZED",
12339
+ data: {
12340
+ currentHeight: latestHeight,
12341
+ deadline: order.deadline,
12342
+ ...lastFailedHeight && { lastFailedHeight }
12343
+ }
12344
+ };
12345
+ await sleep(1e4);
12346
+ continue;
12347
+ }
12348
+ try {
12349
+ const intentGatewayAddress = this.dest.config.getIntentGatewayAddress(destStateMachine);
12350
+ const orderId = orderCommitment(order);
12351
+ const slotHash = await this.dest.client.readContract({
12352
+ abi: IntentGateway_default.ABI,
12353
+ address: intentGatewayAddress,
12354
+ functionName: "calculateCommitmentSlotHash",
12355
+ args: [orderId]
12356
+ });
12357
+ proofHex = await this.dest.queryStateProof(latestHeight, [slotHash], intentGatewayAddress);
12358
+ } catch (error) {
12359
+ lastFailedHeight = latestHeight;
12360
+ yield {
12361
+ status: "PROOF_FETCH_FAILED",
12362
+ data: {
12363
+ failedHeight: latestHeight,
12364
+ error: error instanceof Error ? error.message : String(error),
12365
+ deadline: order.deadline
12366
+ }
12367
+ };
12368
+ await sleep(1e4);
12369
+ }
12192
12370
  }
12371
+ destIProof = {
12372
+ consensusStateId: destConsensusStateId,
12373
+ height: latestHeight,
12374
+ proof: proofHex,
12375
+ stateMachine: destStateMachine
12376
+ };
12377
+ yield { status: "DESTINATION_FINALIZED", data: { proof: destIProof.proof, height: destIProof.height } };
12193
12378
  }
12194
- yield { status: "DESTINATION_FINALIZED", data: { height: latestHeight } };
12195
- const intentGatewayAddress = this.dest.config.getIntentGatewayAddress(destStateMachine);
12196
- const orderId = orderCommitment(order);
12197
- const slotHash = await this.dest.client.readContract({
12198
- abi: IntentGateway_default.ABI,
12199
- address: intentGatewayAddress,
12200
- functionName: "calculateCommitmentSlotHash",
12201
- args: [orderId]
12202
- });
12203
- const proof = await this.dest.queryStateProof(latestHeight, [slotHash], intentGatewayAddress);
12204
- const destIProof = {
12205
- consensusStateId: destConsensusStateId,
12206
- height: latestHeight,
12207
- proof,
12208
- stateMachine: destStateMachine
12209
- };
12210
- this.destStateproofCache.set(order.id, destIProof);
12211
- yield { status: "STATE_PROOF_RECEIVED", data: { proof, height: latestHeight } };
12212
- const getRequest = yield { status: "AWAITING_GET_REQUEST" };
12213
- if (!getRequest) {
12214
- throw new Error("[Cancel Order]: Get Request not provided");
12215
- }
12216
- const commitment = getRequestCommitment({
12217
- ...getRequest,
12218
- keys: [...getRequest.keys]
12219
- });
12220
- const statusStream = indexerClient.getRequestStatusStream(commitment);
12221
- for await (const statusUpdate of statusStream) {
12379
+ const getRequest = storedData?.getRequest ?? (yield { status: "AWAITING_GET_REQUEST" });
12380
+ if (!getRequest) throw new Error("[Cancel Order]: Get Request not provided");
12381
+ const commitment = getRequestCommitment({ ...getRequest, keys: [...getRequest.keys] });
12382
+ const sourceStatusStream = indexerClient.getRequestStatusStream(commitment);
12383
+ for await (const statusUpdate of sourceStatusStream) {
12222
12384
  if (statusUpdate.status === RequestStatus.SOURCE_FINALIZED) {
12223
- const sourceHeight = statusUpdate.metadata.blockNumber;
12224
- const proof2 = await this.source.queryProof(
12385
+ const sourceHeight = BigInt(statusUpdate.metadata.blockNumber);
12386
+ const proof = await this.source.queryProof(
12225
12387
  { Requests: [commitment] },
12226
12388
  hyperbridgeConfig.stateMachineId,
12227
- BigInt(sourceHeight)
12389
+ sourceHeight
12228
12390
  );
12229
- const { stateId } = parseStateMachineId(sourceStateMachine);
12230
12391
  const sourceIProof = {
12231
- height: BigInt(sourceHeight),
12392
+ height: sourceHeight,
12232
12393
  stateMachine: sourceStateMachine,
12233
12394
  consensusStateId: sourceConsensusStateId,
12234
- proof: proof2
12395
+ proof
12235
12396
  };
12397
+ yield { status: "SOURCE_PROOF_RECEIVED", data: sourceIProof };
12236
12398
  const getRequestMessage = {
12237
12399
  kind: "GetRequest",
12238
12400
  requests: [getRequest],
12239
12401
  source: sourceIProof,
12240
- response: this.getCachedProof(order.id),
12402
+ response: destIProof,
12241
12403
  signer: pad("0x")
12242
12404
  };
12243
12405
  await waitForChallengePeriod(hyperbridge, {
12244
- height: BigInt(sourceHeight),
12406
+ height: sourceHeight,
12245
12407
  id: {
12246
- stateId,
12408
+ stateId: parseStateMachineId(sourceStateMachine).stateId,
12247
12409
  consensusStateId: sourceConsensusStateId
12248
12410
  }
12249
12411
  });
12250
- const receiptKey = hyperbridge.requestReceiptKey(commitment);
12251
- const { api } = hyperbridge;
12252
- if (!api) {
12253
- throw new Error("Hyperbridge API is not available");
12254
- }
12255
- let storageValue = await api.rpc.childstate.getStorage(":child_storage:default:ISMP", receiptKey);
12256
- if (storageValue.isNone) {
12257
- console.log("No receipt found. Attempting to submit...");
12258
- try {
12259
- await hyperbridge.submitUnsigned(getRequestMessage);
12260
- } catch {
12261
- console.warn("Submission failed. Awaiting network confirmation...");
12262
- }
12263
- console.log("Waiting for network state update...");
12264
- await sleep(3e4);
12265
- storageValue = await retryPromise(
12266
- async () => {
12267
- const value = await api.rpc.childstate.getStorage(":child_storage:default:ISMP", receiptKey);
12268
- if (value.isNone) {
12269
- throw new Error("Receipt not found");
12270
- }
12271
- return value;
12272
- },
12273
- {
12274
- maxRetries: 10,
12275
- backoffMs: 5e3,
12276
- logMessage: "Checking for receipt"
12277
- }
12278
- );
12279
- }
12280
- console.log("Receipt confirmed on Hyperbridge. Proceeding.");
12412
+ await this.submitAndConfirmReceipt(hyperbridge, commitment, getRequestMessage);
12281
12413
  }
12282
12414
  yield statusUpdate;
12283
12415
  }
@@ -12653,6 +12785,6 @@ async function teleportDot(param_) {
12653
12785
  return stream;
12654
12786
  }
12655
12787
 
12656
- export { ADDRESS_ZERO, ChainConfigService, Chains, DEFAULT_ADDRESS, DEFAULT_GRAFFITI, DUMMY_PRIVATE_KEY, ERC20Method, EvmChain, HyperClientStatus, IndexerClient, IntentGateway, OrderStatus, REQUEST_COMMITMENTS_SLOT, REQUEST_RECEIPTS_SLOT, RESPONSE_COMMITMENTS_SLOT, RESPONSE_RECEIPTS_SLOT, RequestKind, RequestStatus, STATE_COMMITMENTS_SLOT, SubstrateChain, TeleportStatus, TimeoutStatus, WrappedNativeDecimals, __test, addresses, adjustFeeDecimals, assets, bytes20ToBytes32, bytes32ToBytes20, chainIds, coingeckoIds, consensusStateIds, constructRedeemEscrowRequestBody, convertStateMachineIdToEnum, createQueryClient, createRpcUrls, encodeISMPMessage, estimateGasForPost, fetchPrice, generateRootWithProof, getChain, getRequestCommitment, getStateCommitmentFieldSlot, getStateCommitmentSlot, getStorageSlot, hexToString, orderCommitment, postRequestCommitment, queryAssetTeleported, queryGetRequest, queryPostRequest, teleport, teleportDot, viemChains };
12788
+ export { ADDRESS_ZERO, ChainConfigService, Chains, DEFAULT_ADDRESS, DEFAULT_GRAFFITI, DUMMY_PRIVATE_KEY, ERC20Method, EvmChain, HyperClientStatus, IndexerClient, IntentGateway, OrderStatus, REQUEST_COMMITMENTS_SLOT, REQUEST_RECEIPTS_SLOT, RESPONSE_COMMITMENTS_SLOT, RESPONSE_RECEIPTS_SLOT, RequestKind, RequestStatus, STATE_COMMITMENTS_SLOT, SubstrateChain, TeleportStatus, TimeoutStatus, WrappedNativeDecimals, __test, addresses, adjustFeeDecimals, assets, bytes20ToBytes32, bytes32ToBytes20, chainIds, coingeckoIds, consensusStateIds, constructRedeemEscrowRequestBody, convertStateIdToStateMachineId, convertStateMachineIdToEnum, createQueryClient, createRpcUrls, encodeISMPMessage, estimateGasForPost, fetchPrice, generateRootWithProof, getChain, getRequestCommitment, getStateCommitmentFieldSlot, getStateCommitmentSlot, getStorageSlot, hexToString, orderCommitment, postRequestCommitment, queryAssetTeleported, queryGetRequest, queryPostRequest, teleport, teleportDot, viemChains };
12657
12789
  //# sourceMappingURL=index.js.map
12658
12790
  //# sourceMappingURL=index.js.map