@hyperbridge/sdk 1.3.9 → 1.3.10

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.
@@ -1,5 +1,6 @@
1
1
  import { ConsolaInstance } from 'consola';
2
2
  import { GraphQLClient } from 'graphql-request';
3
+ import * as viem from 'viem';
3
4
  import { Hex, ContractFunctionArgs, Log, PublicClient } from 'viem';
4
5
  import { ApiPromise } from '@polkadot/api';
5
6
  import { SignerOptions } from '@polkadot/api/types';
@@ -1650,9 +1651,10 @@ declare class EvmChain implements IChain {
1650
1651
  * Query and return the encoded storage proof for the provided keys at the given height.
1651
1652
  * @param {bigint} at - The block height at which to query the storage proof.
1652
1653
  * @param {HexString[]} keys - The keys for which to query the storage proof.
1654
+ * @param {HexString} address - Optional contract address to fetch storage proof else default to host contract
1653
1655
  * @returns {Promise<HexString>} The encoded storage proof.
1654
1656
  */
1655
- queryStateProof(at: bigint, keys: HexString[]): Promise<HexString>;
1657
+ queryStateProof(at: bigint, keys: HexString[], address?: HexString): Promise<HexString>;
1656
1658
  /**
1657
1659
  * Returns the current timestamp of the chain.
1658
1660
  * @returns {Promise<bigint>} The current timestamp.
@@ -2573,6 +2575,9 @@ declare function queryGetRequest(params: {
2573
2575
  declare class IntentGateway {
2574
2576
  readonly source: EvmChain;
2575
2577
  readonly dest: EvmChain;
2578
+ private destStateproofCache;
2579
+ getCachedProof(id: HexString): IProof | undefined;
2580
+ clearCachedProof(id: HexString): void;
2576
2581
  /**
2577
2582
  * Creates a new IntentGateway instance for cross-chain operations.
2578
2583
  * @param source - The source EVM chain
@@ -2658,6 +2663,115 @@ declare class IntentGateway {
2658
2663
  * @returns True if the order has been filled, false otherwise
2659
2664
  */
2660
2665
  isOrderFilled(order: Order): Promise<boolean>;
2666
+ cancelOrder(order: Order, hyperbridgeConfig: IHyperbridgeConfig, indexerClient: IndexerClient): AsyncGenerator<{
2667
+ status: RequestStatus["SOURCE"];
2668
+ metadata: {
2669
+ blockHash: string;
2670
+ blockNumber: number;
2671
+ transactionHash: string;
2672
+ timestamp?: number;
2673
+ };
2674
+ } | {
2675
+ status: RequestStatus["SOURCE_FINALIZED"];
2676
+ metadata: {
2677
+ blockHash: string;
2678
+ blockNumber: number;
2679
+ transactionHash: string;
2680
+ timestamp?: number;
2681
+ };
2682
+ } | {
2683
+ status: RequestStatus["HYPERBRIDGE_DELIVERED"];
2684
+ metadata: {
2685
+ blockHash: string;
2686
+ blockNumber: number;
2687
+ transactionHash: string;
2688
+ timestamp?: number;
2689
+ };
2690
+ } | {
2691
+ status: RequestStatus["HYPERBRIDGE_FINALIZED"];
2692
+ metadata: {
2693
+ calldata: viem.Hex;
2694
+ blockHash: string;
2695
+ blockNumber: number;
2696
+ transactionHash: string;
2697
+ timestamp?: number;
2698
+ };
2699
+ } | {
2700
+ status: RequestStatus["DESTINATION"];
2701
+ metadata: {
2702
+ blockHash: string;
2703
+ blockNumber: number;
2704
+ transactionHash: string;
2705
+ timestamp?: number;
2706
+ };
2707
+ } | {
2708
+ status: TimeoutStatus["PENDING_TIMEOUT"];
2709
+ metadata: {
2710
+ blockHash: string;
2711
+ blockNumber: number;
2712
+ transactionHash: string;
2713
+ };
2714
+ } | {
2715
+ status: TimeoutStatus["DESTINATION_FINALIZED_TIMEOUT"];
2716
+ metadata: {
2717
+ blockHash: string;
2718
+ blockNumber: number;
2719
+ transactionHash: string;
2720
+ timestamp?: number;
2721
+ };
2722
+ } | {
2723
+ status: TimeoutStatus["HYPERBRIDGE_TIMED_OUT"];
2724
+ metadata: {
2725
+ blockHash: string;
2726
+ blockNumber: number;
2727
+ transactionHash: string;
2728
+ timestamp?: number;
2729
+ };
2730
+ } | {
2731
+ status: TimeoutStatus["HYPERBRIDGE_FINALIZED_TIMEOUT"];
2732
+ metadata: {
2733
+ calldata: viem.Hex;
2734
+ blockHash: string;
2735
+ blockNumber: number;
2736
+ transactionHash: string;
2737
+ timestamp?: number;
2738
+ };
2739
+ } | {
2740
+ status: TimeoutStatus["TIMED_OUT"];
2741
+ metadata: {
2742
+ blockHash: string;
2743
+ blockNumber: number;
2744
+ transactionHash: string;
2745
+ timestamp?: number;
2746
+ };
2747
+ } | {
2748
+ status: string;
2749
+ data: {
2750
+ currentHeight: bigint;
2751
+ deadline: bigint;
2752
+ height?: undefined;
2753
+ proof?: undefined;
2754
+ };
2755
+ } | {
2756
+ status: string;
2757
+ data: {
2758
+ height: bigint;
2759
+ currentHeight?: undefined;
2760
+ deadline?: undefined;
2761
+ proof?: undefined;
2762
+ };
2763
+ } | {
2764
+ status: string;
2765
+ data: {
2766
+ proof: `0x${string}`;
2767
+ height: bigint;
2768
+ currentHeight?: undefined;
2769
+ deadline?: undefined;
2770
+ };
2771
+ } | {
2772
+ status: string;
2773
+ data?: undefined;
2774
+ }, void, IGetRequest | undefined>;
2661
2775
  /**
2662
2776
  * Returns the tick spacing for a given fee tier in Uniswap V4
2663
2777
  * @param fee - The fee tier in basis points
@@ -1,6 +1,6 @@
1
1
  import { createConsola, LogLevels } from 'consola';
2
2
  import { flatten, zip, capitalize, maxBy, isNil } from 'lodash-es';
3
- import { toHex, hexToBytes, encodePacked, keccak256, encodeAbiParameters, bytesToHex, concatHex, createPublicClient, http, encodeFunctionData, erc20Abi, bytesToBigInt, pad, toBytes, maxUint256, formatUnits, parseUnits } from 'viem';
3
+ import { toHex, hexToBytes, encodePacked, keccak256, encodeAbiParameters, bytesToHex, concatHex, createPublicClient, http, encodeFunctionData, erc20Abi, bytesToBigInt, pad, toBytes, maxUint256, formatUnits, parseUnits, hexToString as hexToString$1 } from 'viem';
4
4
  import mergeRace from '@async-generator/merge-race';
5
5
  import { gnosisChiado, gnosis, bscTestnet, bsc, soneium, baseSepolia, base, optimismSepolia, optimism, arbitrumSepolia, arbitrum, mainnet, sepolia } from 'viem/chains';
6
6
  import { hasWindow, isNode, env } from 'std-env';
@@ -3440,7 +3440,7 @@ var addresses = {
3440
3440
  IntentGateway: {
3441
3441
  ["EVM-97" /* BSC_CHAPEL */]: "0x016b6ffC9f890d1e28f9Fdb9eaDA776b02F89509",
3442
3442
  ["EVM-10200" /* GNOSIS_CHIADO */]: "0x016b6ffC9f890d1e28f9Fdb9eaDA776b02F89509",
3443
- ["EVM-11155111" /* SEPOLIA */]: "0x0000000000000000000000000000000000000000",
3443
+ ["EVM-11155111" /* SEPOLIA */]: "0x016b6ffC9f890d1e28f9Fdb9eaDA776b02F89509",
3444
3444
  ["EVM-1" /* MAINNET */]: "0xd54165e45926720b062C192a5bacEC64d5bB08DA",
3445
3445
  ["EVM-56" /* BSC_MAINNET */]: "0xd54165e45926720b062C192a5bacEC64d5bB08DA",
3446
3446
  ["EVM-42161" /* ARBITRUM_MAINNET */]: "0xd54165e45926720b062C192a5bacEC64d5bB08DA",
@@ -4667,11 +4667,12 @@ var EvmChain = class {
4667
4667
  * Query and return the encoded storage proof for the provided keys at the given height.
4668
4668
  * @param {bigint} at - The block height at which to query the storage proof.
4669
4669
  * @param {HexString[]} keys - The keys for which to query the storage proof.
4670
+ * @param {HexString} address - Optional contract address to fetch storage proof else default to host contract
4670
4671
  * @returns {Promise<HexString>} The encoded storage proof.
4671
4672
  */
4672
- async queryStateProof(at, keys) {
4673
+ async queryStateProof(at, keys, address) {
4673
4674
  const config = {
4674
- address: this.params.host,
4675
+ address: address ?? this.params.host,
4675
4676
  storageKeys: keys
4676
4677
  };
4677
4678
  if (!at) {
@@ -5535,7 +5536,7 @@ query StateMachineUpdatesByHeight($statemachineId: String!, $height: Int!, $chai
5535
5536
  { chain: { equalTo: $chain } }
5536
5537
  ]
5537
5538
  }
5538
- orderBy: HEIGHT_ASC
5539
+ orderBy: HEIGHT_DESC
5539
5540
  first: 1
5540
5541
  ) {
5541
5542
  nodes {
@@ -11600,6 +11601,13 @@ var IntentGateway = class {
11600
11601
  this.source = source;
11601
11602
  this.dest = dest;
11602
11603
  }
11604
+ destStateproofCache = /* @__PURE__ */ new Map();
11605
+ getCachedProof(id) {
11606
+ return this.destStateproofCache.get(id);
11607
+ }
11608
+ clearCachedProof(id) {
11609
+ this.destStateproofCache.delete(id);
11610
+ }
11603
11611
  /**
11604
11612
  * Estimates the total cost required to fill an order, including gas fees, relayer fees,
11605
11613
  * protocol fees, and swap operations.
@@ -12208,6 +12216,121 @@ var IntentGateway = class {
12208
12216
  });
12209
12217
  return filledStatus !== "0x0000000000000000000000000000000000000000000000000000000000000000";
12210
12218
  }
12219
+ async *cancelOrder(order, hyperbridgeConfig, indexerClient) {
12220
+ const hyperbridge = await getChain({
12221
+ ...hyperbridgeConfig,
12222
+ hasher: "Keccak"
12223
+ });
12224
+ const sourceStateMachine = hexToString$1(order.sourceChain);
12225
+ const destStateMachine = hexToString$1(order.destChain);
12226
+ const sourceConsensusStateId = this.source.config.getConsensusStateId(sourceStateMachine);
12227
+ const destConsensusStateId = this.dest.config.getConsensusStateId(destStateMachine);
12228
+ let latestHeight = 0n;
12229
+ while (latestHeight <= order.deadline) {
12230
+ const { stateId } = parseStateMachineId(destStateMachine);
12231
+ latestHeight = await hyperbridge.latestStateMachineHeight({
12232
+ stateId,
12233
+ consensusStateId: destConsensusStateId
12234
+ });
12235
+ yield {
12236
+ status: "AWAITING_DESTINATION_FINALIZED",
12237
+ data: { currentHeight: latestHeight, deadline: order.deadline }
12238
+ };
12239
+ if (latestHeight <= order.deadline) {
12240
+ await sleep(1e4);
12241
+ }
12242
+ }
12243
+ yield { status: "DESTINATION_FINALIZED", data: { height: latestHeight } };
12244
+ const intentGatewayAddress = this.dest.config.getIntentGatewayAddress(destStateMachine);
12245
+ const orderId = orderCommitment(order);
12246
+ const slotHash = await this.dest.client.readContract({
12247
+ abi: IntentGateway_default.ABI,
12248
+ address: intentGatewayAddress,
12249
+ functionName: "calculateCommitmentSlotHash",
12250
+ args: [orderId]
12251
+ });
12252
+ const proof = await this.dest.queryStateProof(latestHeight, [slotHash], intentGatewayAddress);
12253
+ const destIProof = {
12254
+ consensusStateId: destConsensusStateId,
12255
+ height: latestHeight,
12256
+ proof,
12257
+ stateMachine: destStateMachine
12258
+ };
12259
+ this.destStateproofCache.set(order.id, destIProof);
12260
+ yield { status: "STATE_PROOF_RECEIVED", data: { proof, height: latestHeight } };
12261
+ const getRequest = yield { status: "AWAITING_GET_REQUEST" };
12262
+ if (!getRequest) {
12263
+ throw new Error("[Cancel Order]: Get Request not provided");
12264
+ }
12265
+ const commitment = getRequestCommitment({
12266
+ ...getRequest,
12267
+ keys: [...getRequest.keys]
12268
+ });
12269
+ const statusStream = indexerClient.getRequestStatusStream(commitment);
12270
+ for await (const statusUpdate of statusStream) {
12271
+ if (statusUpdate.status === RequestStatus.SOURCE_FINALIZED) {
12272
+ const sourceHeight = statusUpdate.metadata.blockNumber;
12273
+ const proof2 = await this.source.queryProof(
12274
+ { Requests: [commitment] },
12275
+ hyperbridgeConfig.stateMachineId,
12276
+ BigInt(sourceHeight)
12277
+ );
12278
+ const { stateId } = parseStateMachineId(sourceStateMachine);
12279
+ const sourceIProof = {
12280
+ height: BigInt(sourceHeight),
12281
+ stateMachine: sourceStateMachine,
12282
+ consensusStateId: sourceConsensusStateId,
12283
+ proof: proof2
12284
+ };
12285
+ const getRequestMessage = {
12286
+ kind: "GetRequest",
12287
+ requests: [getRequest],
12288
+ source: sourceIProof,
12289
+ response: this.getCachedProof(order.id),
12290
+ signer: pad("0x")
12291
+ };
12292
+ await waitForChallengePeriod(hyperbridge, {
12293
+ height: BigInt(sourceHeight),
12294
+ id: {
12295
+ stateId,
12296
+ consensusStateId: sourceConsensusStateId
12297
+ }
12298
+ });
12299
+ const receiptKey = hyperbridge.requestReceiptKey(commitment);
12300
+ const { api } = hyperbridge;
12301
+ if (!api) {
12302
+ throw new Error("Hyperbridge API is not available");
12303
+ }
12304
+ let storageValue = await api.rpc.childstate.getStorage(":child_storage:default:ISMP", receiptKey);
12305
+ if (storageValue.isNone) {
12306
+ console.log("No receipt found. Attempting to submit...");
12307
+ try {
12308
+ await hyperbridge.submitUnsigned(getRequestMessage);
12309
+ } catch {
12310
+ console.warn("Submission failed. Awaiting network confirmation...");
12311
+ }
12312
+ console.log("Waiting for network state update...");
12313
+ await sleep(3e4);
12314
+ storageValue = await retryPromise(
12315
+ async () => {
12316
+ const value = await api.rpc.childstate.getStorage(":child_storage:default:ISMP", receiptKey);
12317
+ if (value.isNone) {
12318
+ throw new Error("Receipt not found");
12319
+ }
12320
+ return value;
12321
+ },
12322
+ {
12323
+ maxRetries: 10,
12324
+ backoffMs: 5e3,
12325
+ logMessage: "Checking for receipt"
12326
+ }
12327
+ );
12328
+ }
12329
+ console.log("Receipt confirmed on Hyperbridge. Proceeding.");
12330
+ }
12331
+ yield statusUpdate;
12332
+ }
12333
+ }
12211
12334
  /**
12212
12335
  * Returns the tick spacing for a given fee tier in Uniswap V4
12213
12336
  * @param fee - The fee tier in basis points