@hyperbridge/sdk 2.3.0 → 2.3.1

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.
@@ -2150,7 +2150,7 @@ var ABI2 = [
2150
2150
  { internalType: "bytes", name: "source", type: "bytes" },
2151
2151
  { internalType: "bytes", name: "dest", type: "bytes" },
2152
2152
  { internalType: "uint64", name: "nonce", type: "uint64" },
2153
- { internalType: "address", name: "from", type: "address" },
2153
+ { internalType: "bytes", name: "from", type: "bytes" },
2154
2154
  { internalType: "uint64", name: "timeoutTimestamp", type: "uint64" },
2155
2155
  { internalType: "bytes[]", name: "keys", type: "bytes[]" },
2156
2156
  { internalType: "uint64", name: "height", type: "uint64" },
@@ -2217,7 +2217,7 @@ var ABI2 = [
2217
2217
  { internalType: "bytes", name: "source", type: "bytes" },
2218
2218
  { internalType: "bytes", name: "dest", type: "bytes" },
2219
2219
  { internalType: "uint64", name: "nonce", type: "uint64" },
2220
- { internalType: "address", name: "from", type: "address" },
2220
+ { internalType: "bytes", name: "from", type: "bytes" },
2221
2221
  { internalType: "uint64", name: "timeoutTimestamp", type: "uint64" },
2222
2222
  { internalType: "bytes[]", name: "keys", type: "bytes[]" },
2223
2223
  { internalType: "uint64", name: "height", type: "uint64" },
@@ -6442,28 +6442,60 @@ var EvmChain = class _EvmChain {
6442
6442
  }
6443
6443
  /**
6444
6444
  * Query and return the encoded storage proof for the provided keys at the given height.
6445
+ *
6446
+ * Keys may be either:
6447
+ * - 32-byte storage slots — read from `address` (or the host contract when omitted), or
6448
+ * - 52-byte cross-chain GET keys encoded as `address(20) || slot(32)`, where the target
6449
+ * contract is embedded in the key. These may span multiple contracts.
6450
+ *
6445
6451
  * @param {bigint} at - The block height at which to query the storage proof.
6446
6452
  * @param {HexString[]} keys - The keys for which to query the storage proof.
6447
- * @param {HexString} address - Optional contract address to fetch storage proof else default to host contract
6453
+ * @param {HexString} address - Optional contract address; forces all keys to be read as slots
6454
+ * of this contract. Omit to let 52-byte keys carry their own contract address.
6448
6455
  * @returns {Promise<HexString>} The encoded storage proof.
6449
6456
  */
6450
6457
  async queryStateProof(at, keys, address) {
6451
- const config = {
6452
- address: address ?? this.params.host,
6453
- storageKeys: keys
6454
- };
6455
- if (!at) {
6456
- config.blockTag = "latest";
6457
- } else {
6458
- config.blockNumber = at;
6459
- }
6460
- const proof = await this.publicClient.getProof(config);
6461
- const flattenedProof = Array.from(new Set(flatten(proof.storageProof.map((item) => item.proof))));
6458
+ const slotsByContract = /* @__PURE__ */ new Map();
6459
+ for (const key of keys) {
6460
+ let contract;
6461
+ let slot;
6462
+ if (address) {
6463
+ contract = address.toLowerCase();
6464
+ slot = key;
6465
+ } else if ((key.length - 2) / 2 === 52) {
6466
+ contract = key.slice(0, 42).toLowerCase();
6467
+ slot = `0x${key.slice(42)}`;
6468
+ } else {
6469
+ contract = this.params.host.toLowerCase();
6470
+ slot = key;
6471
+ }
6472
+ const slots = slotsByContract.get(contract) ?? [];
6473
+ slots.push(slot);
6474
+ slotsByContract.set(contract, slots);
6475
+ }
6476
+ const contracts = Array.from(slotsByContract.entries());
6477
+ const proofs = await Promise.all(
6478
+ contracts.map(([contract, slots]) => {
6479
+ const config = { address: contract, storageKeys: slots };
6480
+ if (!at) {
6481
+ config.blockTag = "latest";
6482
+ } else {
6483
+ config.blockNumber = at;
6484
+ }
6485
+ return this.publicClient.getProof(config);
6486
+ })
6487
+ );
6488
+ const contractProof = Array.from(new Set(flatten(proofs.map((proof) => proof.accountProof))));
6489
+ const storageProof = contracts.map(([contract], i) => {
6490
+ const flattened = Array.from(new Set(flatten(proofs[i].storageProof.map((item) => item.proof))));
6491
+ return [
6492
+ Array.from(hexToBytes(contract)),
6493
+ flattened.map((item) => Array.from(hexToBytes(item)))
6494
+ ];
6495
+ });
6462
6496
  const encoded = EvmStateProof.enc({
6463
- contractProof: proof.accountProof.map((item) => Array.from(hexToBytes(item))),
6464
- storageProof: [
6465
- [Array.from(hexToBytes(config.address)), flattenedProof.map((item) => Array.from(hexToBytes(item)))]
6466
- ]
6497
+ contractProof: contractProof.map((item) => Array.from(hexToBytes(item))),
6498
+ storageProof
6467
6499
  });
6468
6500
  return toHex(encoded);
6469
6501
  }
@@ -7185,6 +7217,49 @@ var SubstrateChain = class _SubstrateChain {
7185
7217
  const item = await this.rpcClient.call("childstate_getStorage", [prefix, key]);
7186
7218
  return item;
7187
7219
  }
7220
+ /**
7221
+ * Returns the storage key for a response receipt in the child trie.
7222
+ * A response receipt is keyed by the originating *request* commitment.
7223
+ * @param {HexString} key - The request commitment (0x-prefixed H256 hex string)
7224
+ * @returns {HexString} The storage key as a hex string
7225
+ */
7226
+ responseReceiptKey(key) {
7227
+ const prefix = new TextEncoder().encode("ResponseReceipts");
7228
+ const keyBytes = hexToBytes(key);
7229
+ return bytesToHex(new Uint8Array([...prefix, ...keyBytes]));
7230
+ }
7231
+ /**
7232
+ * Queries the response receipt for a request commitment. For a GET, Hyperbridge
7233
+ * produces the response as it processes the request, so the presence of a response
7234
+ * receipt indicates the request has already been delivered and handled.
7235
+ * @param {HexString} commitment - The originating request commitment to query.
7236
+ * @returns {Promise<HexString | undefined>} The receipt data if present, otherwise undefined.
7237
+ */
7238
+ async queryResponseReceipt(commitment) {
7239
+ const prefix = toHex(":child_storage:default:ISMP");
7240
+ const key = this.responseReceiptKey(commitment);
7241
+ const item = await this.rpcClient.call("childstate_getStorage", [prefix, key]);
7242
+ return item;
7243
+ }
7244
+ /**
7245
+ * Queries the state-machine commitment Hyperbridge holds for a counterparty chain at an
7246
+ * exact height — the `BoundedStateCommitments` map that `state_machine_commitment` (and thus
7247
+ * proof verification) reads. Returns the committed `stateRoot`, or undefined if Hyperbridge
7248
+ * has not finalized that chain at exactly that height.
7249
+ * @param {StateMachineHeight} height - The counterparty state machine id + height.
7250
+ * @returns {Promise<HexString | undefined>} The committed state root, or undefined if absent.
7251
+ */
7252
+ async queryStateMachineCommitment(height) {
7253
+ if (!this.api) throw new Error("API not initialized");
7254
+ const id = {
7255
+ stateId: height.id.stateId,
7256
+ // on-chain StateMachineId encodes consensusStateId as [u8; 4]
7257
+ consensusStateId: toHex(toBytes(height.id.consensusStateId))
7258
+ };
7259
+ const commitment = await this.api.query.ismp.boundedStateCommitments(id, Number(height.height));
7260
+ if (commitment.isNone) return void 0;
7261
+ return commitment.toJSON()?.stateRoot;
7262
+ }
7188
7263
  /**
7189
7264
  * Returns the current timestamp of the chain.
7190
7265
  * @returns {Promise<bigint>} The current timestamp.
@@ -7812,6 +7887,23 @@ var IntentsCoprocessor = class _IntentsCoprocessor {
7812
7887
  } else if (result.status.isInBlock || result.status.isFinalized) {
7813
7888
  resolved = true;
7814
7889
  clearTimeout(timeoutId);
7890
+ const interrupted = result.events.find(
7891
+ ({ event }) => event.section === "utility" && event.method === "BatchInterrupted"
7892
+ );
7893
+ if (interrupted) {
7894
+ const [indexCodec, dispatchError] = interrupted.event.data;
7895
+ if (Number(indexCodec.toString()) === 0) {
7896
+ let errorMsg;
7897
+ if (dispatchError?.isModule) {
7898
+ const decoded = this.api.registry.findMetaError(dispatchError.asModule);
7899
+ errorMsg = `Dispatch error: ${decoded.section}::${decoded.name}`;
7900
+ } else {
7901
+ errorMsg = `Dispatch error: batch interrupted (${dispatchError?.toString()})`;
7902
+ }
7903
+ resolve({ success: false, error: errorMsg });
7904
+ return;
7905
+ }
7906
+ }
7815
7907
  resolve({
7816
7908
  success: true,
7817
7909
  blockHash: (result.status.isInBlock ? result.status.asInBlock : result.status.asFinalized).toHex(),
@@ -7871,10 +7963,19 @@ var IntentsCoprocessor = class _IntentsCoprocessor {
7871
7963
  }
7872
7964
  }
7873
7965
  /**
7874
- * Retracts a previous bid and places a new one in a single transaction via utility.batch.
7875
- * The retraction runs first, so the old deposit is reclaimed even if the new bid then fails
7876
- * (batch is non-atomic a failing call interrupts the batch without reverting the calls that
7877
- * already succeeded, unlike batchAll which would roll the retraction back too).
7966
+ * Places a new bid and retracts a previous one in a single transaction via utility.batch.
7967
+ *
7968
+ * The new bid is the primary operation, so `placeBid` MUST run first. `utility.batch` is
7969
+ * non-atomic: a failing call interrupts the batch (via a BatchInterrupted event) without
7970
+ * reverting the calls that already succeeded. Placing first guarantees the new bid lands even
7971
+ * when the retraction then fails — which it routinely does, because a previous commitment's bid
7972
+ * may already be gone (or was itself never placed), making `retractBid` return `BidNotFound`.
7973
+ *
7974
+ * Ordering retraction first (the previous behaviour) caused a self-sustaining cascade: a
7975
+ * `BidNotFound` on the leading retract skipped the trailing `placeBid`, so the current bid never
7976
+ * landed, so the *next* interval's retract of that never-placed commitment also failed, and so
7977
+ * on — silently, because the batch extrinsic itself reports success. The deposit reclaim is
7978
+ * best-effort; landing the bid is not.
7878
7979
  *
7879
7980
  * @param retractCommitment - The order commitment of the bid to retract (bytes32)
7880
7981
  * @param bidCommitment - The order commitment of the new bid (bytes32)
@@ -7884,8 +7985,8 @@ var IntentsCoprocessor = class _IntentsCoprocessor {
7884
7985
  async submitBidWithRetraction(retractCommitment, bidCommitment, userOp) {
7885
7986
  try {
7886
7987
  const batch = this.api.tx.utility.batch([
7887
- this.api.tx.intentsCoprocessor.retractBid(retractCommitment),
7888
- this.api.tx.intentsCoprocessor.placeBid(bidCommitment, userOp)
7988
+ this.api.tx.intentsCoprocessor.placeBid(bidCommitment, userOp),
7989
+ this.api.tx.intentsCoprocessor.retractBid(retractCommitment)
7889
7990
  ]);
7890
7991
  return await this.signAndSendExtrinsic(batch);
7891
7992
  } catch (error) {
@@ -9991,6 +10092,7 @@ var GetRequestClient = class {
9991
10092
  const latestMetadata = request.statuses[request.statuses.length - 1];
9992
10093
  status = maxBy([status, latestMetadata.status], (item) => REQUEST_STATUS_WEIGHTS[item]);
9993
10094
  if (!status) return;
10095
+ let sourceFinalizedHeight;
9994
10096
  while (true) {
9995
10097
  switch (status) {
9996
10098
  case RequestStatus.SOURCE: {
@@ -10002,6 +10104,7 @@ var GetRequestClient = class {
10002
10104
  chain: this.ctx.config.hyperbridge.config.stateMachineId
10003
10105
  })
10004
10106
  });
10107
+ sourceFinalizedHeight = BigInt(sourceUpdate.height);
10005
10108
  yield {
10006
10109
  status: RequestStatus.SOURCE_FINALIZED,
10007
10110
  metadata: {
@@ -10015,6 +10118,15 @@ var GetRequestClient = class {
10015
10118
  break;
10016
10119
  }
10017
10120
  case RequestStatus.SOURCE_FINALIZED: {
10121
+ if (request.source !== this.ctx.config.hyperbridge.config.stateMachineId && sourceFinalizedHeight !== void 0) {
10122
+ try {
10123
+ await this.deliverToHyperbridge(request, sourceFinalizedHeight);
10124
+ } catch (error) {
10125
+ this.logger.warn(
10126
+ `Self-delivery to Hyperbridge failed; waiting for a relayer instead: ${error instanceof Error ? error.message : error}`
10127
+ );
10128
+ }
10129
+ }
10018
10130
  request = await waitOrAbort(this.ctx, {
10019
10131
  signal,
10020
10132
  promise: () => this.queries.queryGetRequest(hash),
@@ -10035,7 +10147,10 @@ var GetRequestClient = class {
10035
10147
  }
10036
10148
  case RequestStatus.HYPERBRIDGE_DELIVERED: {
10037
10149
  if (request.source === this.ctx.config.hyperbridge.config.stateMachineId) return;
10038
- const response = await this.queries.queryResponseByRequestId(hash);
10150
+ const response = await waitOrAbort(this.ctx, {
10151
+ signal,
10152
+ promise: () => this.queries.queryResponseByRequestId(hash)
10153
+ });
10039
10154
  yield await this.streamFinalized(signal, request, 1, response);
10040
10155
  status = RequestStatus.HYPERBRIDGE_FINALIZED;
10041
10156
  break;
@@ -10065,6 +10180,102 @@ var GetRequestClient = class {
10065
10180
  }
10066
10181
  }
10067
10182
  }
10183
+ /**
10184
+ * Self-delivers a GET request to Hyperbridge — the request→Hyperbridge hop that would
10185
+ * otherwise require an external relayer.
10186
+ *
10187
+ * Mirrors the relayer path (and {@link OrderCanceller}): prove the request commitment on
10188
+ * the source chain at the Hyperbridge-finalized source height, prove the requested keys on
10189
+ * the destination chain at the request's height, wait out the source challenge period, then
10190
+ * submit an unsigned `GetRequest` message. Source and destination may each be EVM or
10191
+ * substrate — proofs are built via the chain-agnostic {@link IChain} methods.
10192
+ *
10193
+ * Idempotent: returns early if Hyperbridge already holds the response receipt. The caller
10194
+ * wraps this best-effort, so a failure leaves the stream observing as before.
10195
+ */
10196
+ async deliverToHyperbridge(request, sourceFinalizedHeight) {
10197
+ const sourceChain = this.ctx.config.source;
10198
+ const destChain = this.ctx.config.dest;
10199
+ const hyperbridge = this.ctx.config.hyperbridge;
10200
+ const commitment = getRequestCommitment({ ...request, keys: [...request.keys] });
10201
+ const retry = { maxRetries: 5, backoffMs: 2e3 };
10202
+ if (await withRetry(this.ctx, () => hyperbridge.queryResponseReceipt(commitment), retry)) return;
10203
+ this.logger.info(
10204
+ `Delivering GET ${commitment} to Hyperbridge (${request.source}@${sourceFinalizedHeight} \u2192 ${request.dest}@${request.height})`
10205
+ );
10206
+ const sourceProof = {
10207
+ height: sourceFinalizedHeight,
10208
+ stateMachine: request.source,
10209
+ consensusStateId: sourceChain.config.consensusStateId,
10210
+ proof: await withRetry(
10211
+ this.ctx,
10212
+ () => sourceChain.queryProof(
10213
+ { Requests: [commitment] },
10214
+ this.ctx.config.hyperbridge.config.stateMachineId,
10215
+ sourceFinalizedHeight
10216
+ ),
10217
+ retry
10218
+ )
10219
+ };
10220
+ this.logger.info(` \u2713 built source proof: ${(sourceProof.proof.length - 2) / 2} bytes @ ${request.source}#${sourceFinalizedHeight}`);
10221
+ const destCommitment = await withRetry(
10222
+ this.ctx,
10223
+ () => hyperbridge.queryStateMachineCommitment({
10224
+ id: {
10225
+ stateId: parseStateMachineId(request.dest).stateId,
10226
+ consensusStateId: destChain.config.consensusStateId
10227
+ },
10228
+ height: request.height
10229
+ }),
10230
+ retry
10231
+ );
10232
+ if (!destCommitment) {
10233
+ throw new Error(`Hyperbridge has no state commitment for ${request.dest} at height ${request.height}`);
10234
+ }
10235
+ const responseProof = {
10236
+ height: request.height,
10237
+ stateMachine: request.dest,
10238
+ consensusStateId: destChain.config.consensusStateId,
10239
+ proof: await withRetry(this.ctx, () => destChain.queryStateProof(request.height, [...request.keys]), retry)
10240
+ };
10241
+ this.logger.info(
10242
+ ` \u2713 built response proof: ${(responseProof.proof.length - 2) / 2} bytes (${request.keys.length} key(s) @ ${request.dest}#${request.height})`
10243
+ );
10244
+ await withRetry(
10245
+ this.ctx,
10246
+ () => waitForChallengePeriod(hyperbridge, {
10247
+ height: sourceFinalizedHeight,
10248
+ id: {
10249
+ stateId: parseStateMachineId(request.source).stateId,
10250
+ consensusStateId: sourceChain.config.consensusStateId
10251
+ }
10252
+ }),
10253
+ retry
10254
+ );
10255
+ const message = {
10256
+ kind: "GetRequest",
10257
+ requests: [
10258
+ {
10259
+ source: request.source,
10260
+ dest: request.dest,
10261
+ nonce: request.nonce,
10262
+ from: request.from,
10263
+ timeoutTimestamp: request.timeoutTimestamp,
10264
+ keys: [...request.keys],
10265
+ height: request.height,
10266
+ context: request.context
10267
+ }
10268
+ ],
10269
+ source: sourceProof,
10270
+ response: responseProof,
10271
+ signer: pad("0x")
10272
+ };
10273
+ this.logger.info(" \u2192 submitting GetRequest message (source + response proofs) to Hyperbridge\u2026");
10274
+ const result = await withRetry(this.ctx, () => hyperbridge.submitUnsigned(message), retry);
10275
+ this.logger.info(
10276
+ ` \u2713 delivered GET ${commitment} in Hyperbridge block #${result.blockNumber} (tx ${result.transactionHash})`
10277
+ );
10278
+ }
10068
10279
  /**
10069
10280
  * Snapshot helper: returns the `HYPERBRIDGE_FINALIZED` event with source-chain
10070
10281
  * calldata if prerequisites are met, or `undefined` if we're still waiting
@@ -10078,7 +10289,7 @@ var GetRequestClient = class {
10078
10289
  const finality = await this.queries.queryStateMachineUpdateByHeight({
10079
10290
  statemachineId: config.stateMachineId,
10080
10291
  height: hyperbridgeDelivered.metadata.blockNumber,
10081
- chain: config.stateMachineId
10292
+ chain: request.source
10082
10293
  });
10083
10294
  if (finality) {
10084
10295
  const proof = await hyperbridge.queryProof(
@@ -10149,14 +10360,19 @@ var GetRequestClient = class {
10149
10360
  ],
10150
10361
  signer: pad("0x")
10151
10362
  });
10363
+ const hyperbridgeFinality = await this.queries.queryStateMachineUpdateByHeight({
10364
+ statemachineId: config.stateMachineId,
10365
+ height: hyperbridgeDelivered.metadata.blockNumber,
10366
+ chain: config.stateMachineId
10367
+ });
10368
+ if (!hyperbridgeFinality) return void 0;
10152
10369
  return {
10153
10370
  status: RequestStatus.HYPERBRIDGE_FINALIZED,
10154
10371
  metadata: {
10155
- blockHash: hyperbridgeDelivered.metadata.blockHash,
10156
- blockNumber: Number(consensusResult.provenHeight),
10157
- transactionHash: hyperbridgeDelivered.metadata.transactionHash,
10158
- // @ts-ignore
10159
- timestamp: hyperbridgeDelivered.metadata.timestamp,
10372
+ blockHash: hyperbridgeFinality.blockHash,
10373
+ blockNumber: hyperbridgeFinality.height,
10374
+ transactionHash: hyperbridgeFinality.transactionHash,
10375
+ timestamp: hyperbridgeFinality.timestamp,
10160
10376
  calldata
10161
10377
  }
10162
10378
  };
@@ -10176,12 +10392,44 @@ var GetRequestClient = class {
10176
10392
  const hyperbridge = this.ctx.config.hyperbridge;
10177
10393
  const stateMachineId = this.ctx.config.hyperbridge.config.stateMachineId;
10178
10394
  const neededHeight = BigInt(request.statuses[hyperbridgeDeliveredIndex].metadata.blockNumber);
10395
+ const consensusStateId = this.ctx.config.hyperbridge.config.consensusStateId;
10396
+ const encodeGetResponse = (height, proof2) => sourceChain.encode({
10397
+ kind: "GetResponse",
10398
+ proof: { stateMachine: stateMachineId, consensusStateId, proof: proof2, height },
10399
+ responses: [
10400
+ {
10401
+ get: request,
10402
+ values: request.keys.map((key, index) => ({
10403
+ key,
10404
+ value: response?.values[index] || "0x"
10405
+ }))
10406
+ }
10407
+ ],
10408
+ signer: pad("0x")
10409
+ });
10179
10410
  let finality = await this.queries.queryStateMachineUpdateByHeight({
10180
10411
  statemachineId: stateMachineId,
10181
10412
  height: Number(neededHeight),
10182
- chain: stateMachineId
10413
+ chain: request.source
10183
10414
  });
10184
- if (!finality && sourceChain instanceof EvmChain) {
10415
+ if (finality) {
10416
+ const proof2 = await hyperbridge.queryProof(
10417
+ { Responses: [response?.commitment] },
10418
+ request.source,
10419
+ BigInt(finality.height)
10420
+ );
10421
+ return {
10422
+ status: RequestStatus.HYPERBRIDGE_FINALIZED,
10423
+ metadata: {
10424
+ blockHash: finality.blockHash,
10425
+ blockNumber: finality.height,
10426
+ transactionHash: finality.transactionHash,
10427
+ timestamp: finality.timestamp,
10428
+ calldata: encodeGetResponse(BigInt(finality.height), proof2)
10429
+ }
10430
+ };
10431
+ }
10432
+ if (sourceChain instanceof EvmChain) {
10185
10433
  const hyperbridgeSubstrate = hyperbridge;
10186
10434
  const currentEpoch = await sourceChain.currentEpoch();
10187
10435
  const consensusResult = await waitOrAbort(this.ctx, {
@@ -10193,12 +10441,12 @@ var GetRequestClient = class {
10193
10441
  request.source,
10194
10442
  consensusResult.provenHeight
10195
10443
  );
10196
- const calldata2 = sourceChain.encode({
10444
+ const calldata = sourceChain.encode({
10197
10445
  kind: "BatchConsensusAndGetResponse",
10198
10446
  consensusProofs: consensusResult.proofs,
10199
10447
  proof: {
10200
10448
  stateMachine: stateMachineId,
10201
- consensusStateId: this.ctx.config.hyperbridge.config.consensusStateId,
10449
+ consensusStateId,
10202
10450
  proof: proof2,
10203
10451
  height: consensusResult.provenHeight
10204
10452
  },
@@ -10213,20 +10461,7 @@ var GetRequestClient = class {
10213
10461
  ],
10214
10462
  signer: pad("0x")
10215
10463
  });
10216
- return {
10217
- status: RequestStatus.HYPERBRIDGE_FINALIZED,
10218
- metadata: {
10219
- blockHash: request.statuses[hyperbridgeDeliveredIndex].metadata.blockHash,
10220
- blockNumber: Number(consensusResult.provenHeight),
10221
- transactionHash: request.statuses[hyperbridgeDeliveredIndex].metadata.transactionHash,
10222
- // @ts-ignore
10223
- timestamp: request.statuses[hyperbridgeDeliveredIndex].metadata.timestamp,
10224
- calldata: calldata2
10225
- }
10226
- };
10227
- }
10228
- if (!finality) {
10229
- finality = await waitOrAbort(this.ctx, {
10464
+ const hyperbridgeFinality = await waitOrAbort(this.ctx, {
10230
10465
  signal,
10231
10466
  promise: () => this.queries.queryStateMachineUpdateByHeight({
10232
10467
  statemachineId: stateMachineId,
@@ -10234,31 +10469,30 @@ var GetRequestClient = class {
10234
10469
  chain: stateMachineId
10235
10470
  })
10236
10471
  });
10472
+ return {
10473
+ status: RequestStatus.HYPERBRIDGE_FINALIZED,
10474
+ metadata: {
10475
+ blockHash: hyperbridgeFinality.blockHash,
10476
+ blockNumber: hyperbridgeFinality.height,
10477
+ transactionHash: hyperbridgeFinality.transactionHash,
10478
+ timestamp: hyperbridgeFinality.timestamp,
10479
+ calldata
10480
+ }
10481
+ };
10237
10482
  }
10483
+ finality = await waitOrAbort(this.ctx, {
10484
+ signal,
10485
+ promise: () => this.queries.queryStateMachineUpdateByHeight({
10486
+ statemachineId: stateMachineId,
10487
+ height: Number(neededHeight),
10488
+ chain: request.source
10489
+ })
10490
+ });
10238
10491
  const proof = await hyperbridge.queryProof(
10239
10492
  { Responses: [response?.commitment] },
10240
10493
  request.source,
10241
10494
  BigInt(finality.height)
10242
10495
  );
10243
- const calldata = sourceChain.encode({
10244
- kind: "GetResponse",
10245
- proof: {
10246
- stateMachine: stateMachineId,
10247
- consensusStateId: this.ctx.config.hyperbridge.config.consensusStateId,
10248
- proof,
10249
- height: BigInt(finality.height)
10250
- },
10251
- responses: [
10252
- {
10253
- get: request,
10254
- values: request.keys.map((key, index) => ({
10255
- key,
10256
- value: response?.values[index] || "0x"
10257
- }))
10258
- }
10259
- ],
10260
- signer: pad("0x")
10261
- });
10262
10496
  return {
10263
10497
  status: RequestStatus.HYPERBRIDGE_FINALIZED,
10264
10498
  metadata: {
@@ -10266,7 +10500,7 @@ var GetRequestClient = class {
10266
10500
  blockNumber: finality.height,
10267
10501
  transactionHash: finality.transactionHash,
10268
10502
  timestamp: finality.timestamp,
10269
- calldata
10503
+ calldata: encodeGetResponse(BigInt(finality.height), proof)
10270
10504
  }
10271
10505
  };
10272
10506
  }
@@ -10769,7 +11003,7 @@ var PostRequestClient = class {
10769
11003
  const finality = await this.queries.queryStateMachineUpdateByHeight({
10770
11004
  statemachineId: config.stateMachineId,
10771
11005
  height: hyperbridgeDelivered.metadata.blockNumber,
10772
- chain: config.stateMachineId
11006
+ chain: request.dest
10773
11007
  });
10774
11008
  if (finality) {
10775
11009
  const proof = await hyperbridge.queryProof(
@@ -10824,14 +11058,19 @@ var PostRequestClient = class {
10824
11058
  requests: [request],
10825
11059
  signer: pad("0x")
10826
11060
  });
11061
+ const hyperbridgeFinality = await this.queries.queryStateMachineUpdateByHeight({
11062
+ statemachineId: config.stateMachineId,
11063
+ height: hyperbridgeDelivered.metadata.blockNumber,
11064
+ chain: config.stateMachineId
11065
+ });
11066
+ if (!hyperbridgeFinality) return void 0;
10827
11067
  return {
10828
11068
  status: RequestStatus.HYPERBRIDGE_FINALIZED,
10829
11069
  metadata: {
10830
- blockHash: hyperbridgeDelivered.metadata.blockHash,
10831
- blockNumber: Number(consensusResult.provenHeight),
10832
- transactionHash: hyperbridgeDelivered.metadata.transactionHash,
10833
- // @ts-ignore
10834
- timestamp: hyperbridgeDelivered.metadata.timestamp,
11070
+ blockHash: hyperbridgeFinality.blockHash,
11071
+ blockNumber: hyperbridgeFinality.height,
11072
+ transactionHash: hyperbridgeFinality.transactionHash,
11073
+ timestamp: hyperbridgeFinality.timestamp,
10835
11074
  calldata
10836
11075
  }
10837
11076
  };
@@ -10854,7 +11093,7 @@ var PostRequestClient = class {
10854
11093
  let finality = await this.queries.queryStateMachineUpdateByHeight({
10855
11094
  statemachineId: stateMachineId,
10856
11095
  height: Number(neededHeight),
10857
- chain: stateMachineId
11096
+ chain: request.dest
10858
11097
  });
10859
11098
  if (!finality && destChain instanceof EvmChain) {
10860
11099
  const hyperbridgeSubstrate = hyperbridge;
@@ -10882,14 +11121,21 @@ var PostRequestClient = class {
10882
11121
  requests: [request],
10883
11122
  signer: pad("0x")
10884
11123
  });
11124
+ const hyperbridgeFinality = await waitOrAbort(this.ctx, {
11125
+ signal,
11126
+ promise: () => this.queries.queryStateMachineUpdateByHeight({
11127
+ statemachineId: stateMachineId,
11128
+ height: Number(neededHeight),
11129
+ chain: stateMachineId
11130
+ })
11131
+ });
10885
11132
  return {
10886
11133
  status: RequestStatus.HYPERBRIDGE_FINALIZED,
10887
11134
  metadata: {
10888
- blockHash: request.statuses[hyperbridgeDeliveredIndex].metadata.blockHash,
10889
- blockNumber: Number(consensusResult.provenHeight),
10890
- transactionHash: request.statuses[hyperbridgeDeliveredIndex].metadata.transactionHash,
10891
- // @ts-ignore
10892
- timestamp: request.statuses[hyperbridgeDeliveredIndex].metadata.timestamp,
11135
+ blockHash: hyperbridgeFinality.blockHash,
11136
+ blockNumber: hyperbridgeFinality.height,
11137
+ transactionHash: hyperbridgeFinality.transactionHash,
11138
+ timestamp: hyperbridgeFinality.timestamp,
10893
11139
  calldata: calldata2
10894
11140
  }
10895
11141
  };
@@ -10900,7 +11146,7 @@ var PostRequestClient = class {
10900
11146
  promise: () => this.queries.queryStateMachineUpdateByHeight({
10901
11147
  statemachineId: stateMachineId,
10902
11148
  height: Number(neededHeight),
10903
- chain: stateMachineId
11149
+ chain: request.dest
10904
11150
  })
10905
11151
  });
10906
11152
  }