@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.
@@ -2100,7 +2100,7 @@ var ABI2 = [
2100
2100
  { internalType: "bytes", name: "source", type: "bytes" },
2101
2101
  { internalType: "bytes", name: "dest", type: "bytes" },
2102
2102
  { internalType: "uint64", name: "nonce", type: "uint64" },
2103
- { internalType: "address", name: "from", type: "address" },
2103
+ { internalType: "bytes", name: "from", type: "bytes" },
2104
2104
  { internalType: "uint64", name: "timeoutTimestamp", type: "uint64" },
2105
2105
  { internalType: "bytes[]", name: "keys", type: "bytes[]" },
2106
2106
  { internalType: "uint64", name: "height", type: "uint64" },
@@ -2167,7 +2167,7 @@ var ABI2 = [
2167
2167
  { internalType: "bytes", name: "source", type: "bytes" },
2168
2168
  { internalType: "bytes", name: "dest", type: "bytes" },
2169
2169
  { internalType: "uint64", name: "nonce", type: "uint64" },
2170
- { internalType: "address", name: "from", type: "address" },
2170
+ { internalType: "bytes", name: "from", type: "bytes" },
2171
2171
  { internalType: "uint64", name: "timeoutTimestamp", type: "uint64" },
2172
2172
  { internalType: "bytes[]", name: "keys", type: "bytes[]" },
2173
2173
  { internalType: "uint64", name: "height", type: "uint64" },
@@ -6392,28 +6392,60 @@ var EvmChain = class _EvmChain {
6392
6392
  }
6393
6393
  /**
6394
6394
  * Query and return the encoded storage proof for the provided keys at the given height.
6395
+ *
6396
+ * Keys may be either:
6397
+ * - 32-byte storage slots — read from `address` (or the host contract when omitted), or
6398
+ * - 52-byte cross-chain GET keys encoded as `address(20) || slot(32)`, where the target
6399
+ * contract is embedded in the key. These may span multiple contracts.
6400
+ *
6395
6401
  * @param {bigint} at - The block height at which to query the storage proof.
6396
6402
  * @param {HexString[]} keys - The keys for which to query the storage proof.
6397
- * @param {HexString} address - Optional contract address to fetch storage proof else default to host contract
6403
+ * @param {HexString} address - Optional contract address; forces all keys to be read as slots
6404
+ * of this contract. Omit to let 52-byte keys carry their own contract address.
6398
6405
  * @returns {Promise<HexString>} The encoded storage proof.
6399
6406
  */
6400
6407
  async queryStateProof(at, keys, address) {
6401
- const config = {
6402
- address: address ?? this.params.host,
6403
- storageKeys: keys
6404
- };
6405
- if (!at) {
6406
- config.blockTag = "latest";
6407
- } else {
6408
- config.blockNumber = at;
6409
- }
6410
- const proof = await this.publicClient.getProof(config);
6411
- const flattenedProof = Array.from(new Set(flatten(proof.storageProof.map((item) => item.proof))));
6408
+ const slotsByContract = /* @__PURE__ */ new Map();
6409
+ for (const key of keys) {
6410
+ let contract;
6411
+ let slot;
6412
+ if (address) {
6413
+ contract = address.toLowerCase();
6414
+ slot = key;
6415
+ } else if ((key.length - 2) / 2 === 52) {
6416
+ contract = key.slice(0, 42).toLowerCase();
6417
+ slot = `0x${key.slice(42)}`;
6418
+ } else {
6419
+ contract = this.params.host.toLowerCase();
6420
+ slot = key;
6421
+ }
6422
+ const slots = slotsByContract.get(contract) ?? [];
6423
+ slots.push(slot);
6424
+ slotsByContract.set(contract, slots);
6425
+ }
6426
+ const contracts = Array.from(slotsByContract.entries());
6427
+ const proofs = await Promise.all(
6428
+ contracts.map(([contract, slots]) => {
6429
+ const config = { address: contract, storageKeys: slots };
6430
+ if (!at) {
6431
+ config.blockTag = "latest";
6432
+ } else {
6433
+ config.blockNumber = at;
6434
+ }
6435
+ return this.publicClient.getProof(config);
6436
+ })
6437
+ );
6438
+ const contractProof = Array.from(new Set(flatten(proofs.map((proof) => proof.accountProof))));
6439
+ const storageProof = contracts.map(([contract], i) => {
6440
+ const flattened = Array.from(new Set(flatten(proofs[i].storageProof.map((item) => item.proof))));
6441
+ return [
6442
+ Array.from(hexToBytes(contract)),
6443
+ flattened.map((item) => Array.from(hexToBytes(item)))
6444
+ ];
6445
+ });
6412
6446
  const encoded = EvmStateProof.enc({
6413
- contractProof: proof.accountProof.map((item) => Array.from(hexToBytes(item))),
6414
- storageProof: [
6415
- [Array.from(hexToBytes(config.address)), flattenedProof.map((item) => Array.from(hexToBytes(item)))]
6416
- ]
6447
+ contractProof: contractProof.map((item) => Array.from(hexToBytes(item))),
6448
+ storageProof
6417
6449
  });
6418
6450
  return toHex(encoded);
6419
6451
  }
@@ -7135,6 +7167,49 @@ var SubstrateChain = class _SubstrateChain {
7135
7167
  const item = await this.rpcClient.call("childstate_getStorage", [prefix, key]);
7136
7168
  return item;
7137
7169
  }
7170
+ /**
7171
+ * Returns the storage key for a response receipt in the child trie.
7172
+ * A response receipt is keyed by the originating *request* commitment.
7173
+ * @param {HexString} key - The request commitment (0x-prefixed H256 hex string)
7174
+ * @returns {HexString} The storage key as a hex string
7175
+ */
7176
+ responseReceiptKey(key) {
7177
+ const prefix = new TextEncoder().encode("ResponseReceipts");
7178
+ const keyBytes = hexToBytes(key);
7179
+ return bytesToHex(new Uint8Array([...prefix, ...keyBytes]));
7180
+ }
7181
+ /**
7182
+ * Queries the response receipt for a request commitment. For a GET, Hyperbridge
7183
+ * produces the response as it processes the request, so the presence of a response
7184
+ * receipt indicates the request has already been delivered and handled.
7185
+ * @param {HexString} commitment - The originating request commitment to query.
7186
+ * @returns {Promise<HexString | undefined>} The receipt data if present, otherwise undefined.
7187
+ */
7188
+ async queryResponseReceipt(commitment) {
7189
+ const prefix = toHex(":child_storage:default:ISMP");
7190
+ const key = this.responseReceiptKey(commitment);
7191
+ const item = await this.rpcClient.call("childstate_getStorage", [prefix, key]);
7192
+ return item;
7193
+ }
7194
+ /**
7195
+ * Queries the state-machine commitment Hyperbridge holds for a counterparty chain at an
7196
+ * exact height — the `BoundedStateCommitments` map that `state_machine_commitment` (and thus
7197
+ * proof verification) reads. Returns the committed `stateRoot`, or undefined if Hyperbridge
7198
+ * has not finalized that chain at exactly that height.
7199
+ * @param {StateMachineHeight} height - The counterparty state machine id + height.
7200
+ * @returns {Promise<HexString | undefined>} The committed state root, or undefined if absent.
7201
+ */
7202
+ async queryStateMachineCommitment(height) {
7203
+ if (!this.api) throw new Error("API not initialized");
7204
+ const id = {
7205
+ stateId: height.id.stateId,
7206
+ // on-chain StateMachineId encodes consensusStateId as [u8; 4]
7207
+ consensusStateId: toHex(toBytes(height.id.consensusStateId))
7208
+ };
7209
+ const commitment = await this.api.query.ismp.boundedStateCommitments(id, Number(height.height));
7210
+ if (commitment.isNone) return void 0;
7211
+ return commitment.toJSON()?.stateRoot;
7212
+ }
7138
7213
  /**
7139
7214
  * Returns the current timestamp of the chain.
7140
7215
  * @returns {Promise<bigint>} The current timestamp.
@@ -7762,6 +7837,23 @@ var IntentsCoprocessor = class _IntentsCoprocessor {
7762
7837
  } else if (result.status.isInBlock || result.status.isFinalized) {
7763
7838
  resolved = true;
7764
7839
  clearTimeout(timeoutId);
7840
+ const interrupted = result.events.find(
7841
+ ({ event }) => event.section === "utility" && event.method === "BatchInterrupted"
7842
+ );
7843
+ if (interrupted) {
7844
+ const [indexCodec, dispatchError] = interrupted.event.data;
7845
+ if (Number(indexCodec.toString()) === 0) {
7846
+ let errorMsg;
7847
+ if (dispatchError?.isModule) {
7848
+ const decoded = this.api.registry.findMetaError(dispatchError.asModule);
7849
+ errorMsg = `Dispatch error: ${decoded.section}::${decoded.name}`;
7850
+ } else {
7851
+ errorMsg = `Dispatch error: batch interrupted (${dispatchError?.toString()})`;
7852
+ }
7853
+ resolve({ success: false, error: errorMsg });
7854
+ return;
7855
+ }
7856
+ }
7765
7857
  resolve({
7766
7858
  success: true,
7767
7859
  blockHash: (result.status.isInBlock ? result.status.asInBlock : result.status.asFinalized).toHex(),
@@ -7821,10 +7913,19 @@ var IntentsCoprocessor = class _IntentsCoprocessor {
7821
7913
  }
7822
7914
  }
7823
7915
  /**
7824
- * Retracts a previous bid and places a new one in a single transaction via utility.batch.
7825
- * The retraction runs first, so the old deposit is reclaimed even if the new bid then fails
7826
- * (batch is non-atomic a failing call interrupts the batch without reverting the calls that
7827
- * already succeeded, unlike batchAll which would roll the retraction back too).
7916
+ * Places a new bid and retracts a previous one in a single transaction via utility.batch.
7917
+ *
7918
+ * The new bid is the primary operation, so `placeBid` MUST run first. `utility.batch` is
7919
+ * non-atomic: a failing call interrupts the batch (via a BatchInterrupted event) without
7920
+ * reverting the calls that already succeeded. Placing first guarantees the new bid lands even
7921
+ * when the retraction then fails — which it routinely does, because a previous commitment's bid
7922
+ * may already be gone (or was itself never placed), making `retractBid` return `BidNotFound`.
7923
+ *
7924
+ * Ordering retraction first (the previous behaviour) caused a self-sustaining cascade: a
7925
+ * `BidNotFound` on the leading retract skipped the trailing `placeBid`, so the current bid never
7926
+ * landed, so the *next* interval's retract of that never-placed commitment also failed, and so
7927
+ * on — silently, because the batch extrinsic itself reports success. The deposit reclaim is
7928
+ * best-effort; landing the bid is not.
7828
7929
  *
7829
7930
  * @param retractCommitment - The order commitment of the bid to retract (bytes32)
7830
7931
  * @param bidCommitment - The order commitment of the new bid (bytes32)
@@ -7834,8 +7935,8 @@ var IntentsCoprocessor = class _IntentsCoprocessor {
7834
7935
  async submitBidWithRetraction(retractCommitment, bidCommitment, userOp) {
7835
7936
  try {
7836
7937
  const batch = this.api.tx.utility.batch([
7837
- this.api.tx.intentsCoprocessor.retractBid(retractCommitment),
7838
- this.api.tx.intentsCoprocessor.placeBid(bidCommitment, userOp)
7938
+ this.api.tx.intentsCoprocessor.placeBid(bidCommitment, userOp),
7939
+ this.api.tx.intentsCoprocessor.retractBid(retractCommitment)
7839
7940
  ]);
7840
7941
  return await this.signAndSendExtrinsic(batch);
7841
7942
  } catch (error) {
@@ -9941,6 +10042,7 @@ var GetRequestClient = class {
9941
10042
  const latestMetadata = request.statuses[request.statuses.length - 1];
9942
10043
  status = maxBy([status, latestMetadata.status], (item) => REQUEST_STATUS_WEIGHTS[item]);
9943
10044
  if (!status) return;
10045
+ let sourceFinalizedHeight;
9944
10046
  while (true) {
9945
10047
  switch (status) {
9946
10048
  case RequestStatus.SOURCE: {
@@ -9952,6 +10054,7 @@ var GetRequestClient = class {
9952
10054
  chain: this.ctx.config.hyperbridge.config.stateMachineId
9953
10055
  })
9954
10056
  });
10057
+ sourceFinalizedHeight = BigInt(sourceUpdate.height);
9955
10058
  yield {
9956
10059
  status: RequestStatus.SOURCE_FINALIZED,
9957
10060
  metadata: {
@@ -9965,6 +10068,15 @@ var GetRequestClient = class {
9965
10068
  break;
9966
10069
  }
9967
10070
  case RequestStatus.SOURCE_FINALIZED: {
10071
+ if (request.source !== this.ctx.config.hyperbridge.config.stateMachineId && sourceFinalizedHeight !== void 0) {
10072
+ try {
10073
+ await this.deliverToHyperbridge(request, sourceFinalizedHeight);
10074
+ } catch (error) {
10075
+ this.logger.warn(
10076
+ `Self-delivery to Hyperbridge failed; waiting for a relayer instead: ${error instanceof Error ? error.message : error}`
10077
+ );
10078
+ }
10079
+ }
9968
10080
  request = await waitOrAbort(this.ctx, {
9969
10081
  signal,
9970
10082
  promise: () => this.queries.queryGetRequest(hash),
@@ -9985,7 +10097,10 @@ var GetRequestClient = class {
9985
10097
  }
9986
10098
  case RequestStatus.HYPERBRIDGE_DELIVERED: {
9987
10099
  if (request.source === this.ctx.config.hyperbridge.config.stateMachineId) return;
9988
- const response = await this.queries.queryResponseByRequestId(hash);
10100
+ const response = await waitOrAbort(this.ctx, {
10101
+ signal,
10102
+ promise: () => this.queries.queryResponseByRequestId(hash)
10103
+ });
9989
10104
  yield await this.streamFinalized(signal, request, 1, response);
9990
10105
  status = RequestStatus.HYPERBRIDGE_FINALIZED;
9991
10106
  break;
@@ -10015,6 +10130,102 @@ var GetRequestClient = class {
10015
10130
  }
10016
10131
  }
10017
10132
  }
10133
+ /**
10134
+ * Self-delivers a GET request to Hyperbridge — the request→Hyperbridge hop that would
10135
+ * otherwise require an external relayer.
10136
+ *
10137
+ * Mirrors the relayer path (and {@link OrderCanceller}): prove the request commitment on
10138
+ * the source chain at the Hyperbridge-finalized source height, prove the requested keys on
10139
+ * the destination chain at the request's height, wait out the source challenge period, then
10140
+ * submit an unsigned `GetRequest` message. Source and destination may each be EVM or
10141
+ * substrate — proofs are built via the chain-agnostic {@link IChain} methods.
10142
+ *
10143
+ * Idempotent: returns early if Hyperbridge already holds the response receipt. The caller
10144
+ * wraps this best-effort, so a failure leaves the stream observing as before.
10145
+ */
10146
+ async deliverToHyperbridge(request, sourceFinalizedHeight) {
10147
+ const sourceChain = this.ctx.config.source;
10148
+ const destChain = this.ctx.config.dest;
10149
+ const hyperbridge = this.ctx.config.hyperbridge;
10150
+ const commitment = getRequestCommitment({ ...request, keys: [...request.keys] });
10151
+ const retry = { maxRetries: 5, backoffMs: 2e3 };
10152
+ if (await withRetry(this.ctx, () => hyperbridge.queryResponseReceipt(commitment), retry)) return;
10153
+ this.logger.info(
10154
+ `Delivering GET ${commitment} to Hyperbridge (${request.source}@${sourceFinalizedHeight} \u2192 ${request.dest}@${request.height})`
10155
+ );
10156
+ const sourceProof = {
10157
+ height: sourceFinalizedHeight,
10158
+ stateMachine: request.source,
10159
+ consensusStateId: sourceChain.config.consensusStateId,
10160
+ proof: await withRetry(
10161
+ this.ctx,
10162
+ () => sourceChain.queryProof(
10163
+ { Requests: [commitment] },
10164
+ this.ctx.config.hyperbridge.config.stateMachineId,
10165
+ sourceFinalizedHeight
10166
+ ),
10167
+ retry
10168
+ )
10169
+ };
10170
+ this.logger.info(` \u2713 built source proof: ${(sourceProof.proof.length - 2) / 2} bytes @ ${request.source}#${sourceFinalizedHeight}`);
10171
+ const destCommitment = await withRetry(
10172
+ this.ctx,
10173
+ () => hyperbridge.queryStateMachineCommitment({
10174
+ id: {
10175
+ stateId: parseStateMachineId(request.dest).stateId,
10176
+ consensusStateId: destChain.config.consensusStateId
10177
+ },
10178
+ height: request.height
10179
+ }),
10180
+ retry
10181
+ );
10182
+ if (!destCommitment) {
10183
+ throw new Error(`Hyperbridge has no state commitment for ${request.dest} at height ${request.height}`);
10184
+ }
10185
+ const responseProof = {
10186
+ height: request.height,
10187
+ stateMachine: request.dest,
10188
+ consensusStateId: destChain.config.consensusStateId,
10189
+ proof: await withRetry(this.ctx, () => destChain.queryStateProof(request.height, [...request.keys]), retry)
10190
+ };
10191
+ this.logger.info(
10192
+ ` \u2713 built response proof: ${(responseProof.proof.length - 2) / 2} bytes (${request.keys.length} key(s) @ ${request.dest}#${request.height})`
10193
+ );
10194
+ await withRetry(
10195
+ this.ctx,
10196
+ () => waitForChallengePeriod(hyperbridge, {
10197
+ height: sourceFinalizedHeight,
10198
+ id: {
10199
+ stateId: parseStateMachineId(request.source).stateId,
10200
+ consensusStateId: sourceChain.config.consensusStateId
10201
+ }
10202
+ }),
10203
+ retry
10204
+ );
10205
+ const message = {
10206
+ kind: "GetRequest",
10207
+ requests: [
10208
+ {
10209
+ source: request.source,
10210
+ dest: request.dest,
10211
+ nonce: request.nonce,
10212
+ from: request.from,
10213
+ timeoutTimestamp: request.timeoutTimestamp,
10214
+ keys: [...request.keys],
10215
+ height: request.height,
10216
+ context: request.context
10217
+ }
10218
+ ],
10219
+ source: sourceProof,
10220
+ response: responseProof,
10221
+ signer: pad("0x")
10222
+ };
10223
+ this.logger.info(" \u2192 submitting GetRequest message (source + response proofs) to Hyperbridge\u2026");
10224
+ const result = await withRetry(this.ctx, () => hyperbridge.submitUnsigned(message), retry);
10225
+ this.logger.info(
10226
+ ` \u2713 delivered GET ${commitment} in Hyperbridge block #${result.blockNumber} (tx ${result.transactionHash})`
10227
+ );
10228
+ }
10018
10229
  /**
10019
10230
  * Snapshot helper: returns the `HYPERBRIDGE_FINALIZED` event with source-chain
10020
10231
  * calldata if prerequisites are met, or `undefined` if we're still waiting
@@ -10028,7 +10239,7 @@ var GetRequestClient = class {
10028
10239
  const finality = await this.queries.queryStateMachineUpdateByHeight({
10029
10240
  statemachineId: config.stateMachineId,
10030
10241
  height: hyperbridgeDelivered.metadata.blockNumber,
10031
- chain: config.stateMachineId
10242
+ chain: request.source
10032
10243
  });
10033
10244
  if (finality) {
10034
10245
  const proof = await hyperbridge.queryProof(
@@ -10099,14 +10310,19 @@ var GetRequestClient = class {
10099
10310
  ],
10100
10311
  signer: pad("0x")
10101
10312
  });
10313
+ const hyperbridgeFinality = await this.queries.queryStateMachineUpdateByHeight({
10314
+ statemachineId: config.stateMachineId,
10315
+ height: hyperbridgeDelivered.metadata.blockNumber,
10316
+ chain: config.stateMachineId
10317
+ });
10318
+ if (!hyperbridgeFinality) return void 0;
10102
10319
  return {
10103
10320
  status: RequestStatus.HYPERBRIDGE_FINALIZED,
10104
10321
  metadata: {
10105
- blockHash: hyperbridgeDelivered.metadata.blockHash,
10106
- blockNumber: Number(consensusResult.provenHeight),
10107
- transactionHash: hyperbridgeDelivered.metadata.transactionHash,
10108
- // @ts-ignore
10109
- timestamp: hyperbridgeDelivered.metadata.timestamp,
10322
+ blockHash: hyperbridgeFinality.blockHash,
10323
+ blockNumber: hyperbridgeFinality.height,
10324
+ transactionHash: hyperbridgeFinality.transactionHash,
10325
+ timestamp: hyperbridgeFinality.timestamp,
10110
10326
  calldata
10111
10327
  }
10112
10328
  };
@@ -10126,12 +10342,44 @@ var GetRequestClient = class {
10126
10342
  const hyperbridge = this.ctx.config.hyperbridge;
10127
10343
  const stateMachineId = this.ctx.config.hyperbridge.config.stateMachineId;
10128
10344
  const neededHeight = BigInt(request.statuses[hyperbridgeDeliveredIndex].metadata.blockNumber);
10345
+ const consensusStateId = this.ctx.config.hyperbridge.config.consensusStateId;
10346
+ const encodeGetResponse = (height, proof2) => sourceChain.encode({
10347
+ kind: "GetResponse",
10348
+ proof: { stateMachine: stateMachineId, consensusStateId, proof: proof2, height },
10349
+ responses: [
10350
+ {
10351
+ get: request,
10352
+ values: request.keys.map((key, index) => ({
10353
+ key,
10354
+ value: response?.values[index] || "0x"
10355
+ }))
10356
+ }
10357
+ ],
10358
+ signer: pad("0x")
10359
+ });
10129
10360
  let finality = await this.queries.queryStateMachineUpdateByHeight({
10130
10361
  statemachineId: stateMachineId,
10131
10362
  height: Number(neededHeight),
10132
- chain: stateMachineId
10363
+ chain: request.source
10133
10364
  });
10134
- if (!finality && sourceChain instanceof EvmChain) {
10365
+ if (finality) {
10366
+ const proof2 = await hyperbridge.queryProof(
10367
+ { Responses: [response?.commitment] },
10368
+ request.source,
10369
+ BigInt(finality.height)
10370
+ );
10371
+ return {
10372
+ status: RequestStatus.HYPERBRIDGE_FINALIZED,
10373
+ metadata: {
10374
+ blockHash: finality.blockHash,
10375
+ blockNumber: finality.height,
10376
+ transactionHash: finality.transactionHash,
10377
+ timestamp: finality.timestamp,
10378
+ calldata: encodeGetResponse(BigInt(finality.height), proof2)
10379
+ }
10380
+ };
10381
+ }
10382
+ if (sourceChain instanceof EvmChain) {
10135
10383
  const hyperbridgeSubstrate = hyperbridge;
10136
10384
  const currentEpoch = await sourceChain.currentEpoch();
10137
10385
  const consensusResult = await waitOrAbort(this.ctx, {
@@ -10143,12 +10391,12 @@ var GetRequestClient = class {
10143
10391
  request.source,
10144
10392
  consensusResult.provenHeight
10145
10393
  );
10146
- const calldata2 = sourceChain.encode({
10394
+ const calldata = sourceChain.encode({
10147
10395
  kind: "BatchConsensusAndGetResponse",
10148
10396
  consensusProofs: consensusResult.proofs,
10149
10397
  proof: {
10150
10398
  stateMachine: stateMachineId,
10151
- consensusStateId: this.ctx.config.hyperbridge.config.consensusStateId,
10399
+ consensusStateId,
10152
10400
  proof: proof2,
10153
10401
  height: consensusResult.provenHeight
10154
10402
  },
@@ -10163,20 +10411,7 @@ var GetRequestClient = class {
10163
10411
  ],
10164
10412
  signer: pad("0x")
10165
10413
  });
10166
- return {
10167
- status: RequestStatus.HYPERBRIDGE_FINALIZED,
10168
- metadata: {
10169
- blockHash: request.statuses[hyperbridgeDeliveredIndex].metadata.blockHash,
10170
- blockNumber: Number(consensusResult.provenHeight),
10171
- transactionHash: request.statuses[hyperbridgeDeliveredIndex].metadata.transactionHash,
10172
- // @ts-ignore
10173
- timestamp: request.statuses[hyperbridgeDeliveredIndex].metadata.timestamp,
10174
- calldata: calldata2
10175
- }
10176
- };
10177
- }
10178
- if (!finality) {
10179
- finality = await waitOrAbort(this.ctx, {
10414
+ const hyperbridgeFinality = await waitOrAbort(this.ctx, {
10180
10415
  signal,
10181
10416
  promise: () => this.queries.queryStateMachineUpdateByHeight({
10182
10417
  statemachineId: stateMachineId,
@@ -10184,31 +10419,30 @@ var GetRequestClient = class {
10184
10419
  chain: stateMachineId
10185
10420
  })
10186
10421
  });
10422
+ return {
10423
+ status: RequestStatus.HYPERBRIDGE_FINALIZED,
10424
+ metadata: {
10425
+ blockHash: hyperbridgeFinality.blockHash,
10426
+ blockNumber: hyperbridgeFinality.height,
10427
+ transactionHash: hyperbridgeFinality.transactionHash,
10428
+ timestamp: hyperbridgeFinality.timestamp,
10429
+ calldata
10430
+ }
10431
+ };
10187
10432
  }
10433
+ finality = await waitOrAbort(this.ctx, {
10434
+ signal,
10435
+ promise: () => this.queries.queryStateMachineUpdateByHeight({
10436
+ statemachineId: stateMachineId,
10437
+ height: Number(neededHeight),
10438
+ chain: request.source
10439
+ })
10440
+ });
10188
10441
  const proof = await hyperbridge.queryProof(
10189
10442
  { Responses: [response?.commitment] },
10190
10443
  request.source,
10191
10444
  BigInt(finality.height)
10192
10445
  );
10193
- const calldata = sourceChain.encode({
10194
- kind: "GetResponse",
10195
- proof: {
10196
- stateMachine: stateMachineId,
10197
- consensusStateId: this.ctx.config.hyperbridge.config.consensusStateId,
10198
- proof,
10199
- height: BigInt(finality.height)
10200
- },
10201
- responses: [
10202
- {
10203
- get: request,
10204
- values: request.keys.map((key, index) => ({
10205
- key,
10206
- value: response?.values[index] || "0x"
10207
- }))
10208
- }
10209
- ],
10210
- signer: pad("0x")
10211
- });
10212
10446
  return {
10213
10447
  status: RequestStatus.HYPERBRIDGE_FINALIZED,
10214
10448
  metadata: {
@@ -10216,7 +10450,7 @@ var GetRequestClient = class {
10216
10450
  blockNumber: finality.height,
10217
10451
  transactionHash: finality.transactionHash,
10218
10452
  timestamp: finality.timestamp,
10219
- calldata
10453
+ calldata: encodeGetResponse(BigInt(finality.height), proof)
10220
10454
  }
10221
10455
  };
10222
10456
  }
@@ -10719,7 +10953,7 @@ var PostRequestClient = class {
10719
10953
  const finality = await this.queries.queryStateMachineUpdateByHeight({
10720
10954
  statemachineId: config.stateMachineId,
10721
10955
  height: hyperbridgeDelivered.metadata.blockNumber,
10722
- chain: config.stateMachineId
10956
+ chain: request.dest
10723
10957
  });
10724
10958
  if (finality) {
10725
10959
  const proof = await hyperbridge.queryProof(
@@ -10774,14 +11008,19 @@ var PostRequestClient = class {
10774
11008
  requests: [request],
10775
11009
  signer: pad("0x")
10776
11010
  });
11011
+ const hyperbridgeFinality = await this.queries.queryStateMachineUpdateByHeight({
11012
+ statemachineId: config.stateMachineId,
11013
+ height: hyperbridgeDelivered.metadata.blockNumber,
11014
+ chain: config.stateMachineId
11015
+ });
11016
+ if (!hyperbridgeFinality) return void 0;
10777
11017
  return {
10778
11018
  status: RequestStatus.HYPERBRIDGE_FINALIZED,
10779
11019
  metadata: {
10780
- blockHash: hyperbridgeDelivered.metadata.blockHash,
10781
- blockNumber: Number(consensusResult.provenHeight),
10782
- transactionHash: hyperbridgeDelivered.metadata.transactionHash,
10783
- // @ts-ignore
10784
- timestamp: hyperbridgeDelivered.metadata.timestamp,
11020
+ blockHash: hyperbridgeFinality.blockHash,
11021
+ blockNumber: hyperbridgeFinality.height,
11022
+ transactionHash: hyperbridgeFinality.transactionHash,
11023
+ timestamp: hyperbridgeFinality.timestamp,
10785
11024
  calldata
10786
11025
  }
10787
11026
  };
@@ -10804,7 +11043,7 @@ var PostRequestClient = class {
10804
11043
  let finality = await this.queries.queryStateMachineUpdateByHeight({
10805
11044
  statemachineId: stateMachineId,
10806
11045
  height: Number(neededHeight),
10807
- chain: stateMachineId
11046
+ chain: request.dest
10808
11047
  });
10809
11048
  if (!finality && destChain instanceof EvmChain) {
10810
11049
  const hyperbridgeSubstrate = hyperbridge;
@@ -10832,14 +11071,21 @@ var PostRequestClient = class {
10832
11071
  requests: [request],
10833
11072
  signer: pad("0x")
10834
11073
  });
11074
+ const hyperbridgeFinality = await waitOrAbort(this.ctx, {
11075
+ signal,
11076
+ promise: () => this.queries.queryStateMachineUpdateByHeight({
11077
+ statemachineId: stateMachineId,
11078
+ height: Number(neededHeight),
11079
+ chain: stateMachineId
11080
+ })
11081
+ });
10835
11082
  return {
10836
11083
  status: RequestStatus.HYPERBRIDGE_FINALIZED,
10837
11084
  metadata: {
10838
- blockHash: request.statuses[hyperbridgeDeliveredIndex].metadata.blockHash,
10839
- blockNumber: Number(consensusResult.provenHeight),
10840
- transactionHash: request.statuses[hyperbridgeDeliveredIndex].metadata.transactionHash,
10841
- // @ts-ignore
10842
- timestamp: request.statuses[hyperbridgeDeliveredIndex].metadata.timestamp,
11085
+ blockHash: hyperbridgeFinality.blockHash,
11086
+ blockNumber: hyperbridgeFinality.height,
11087
+ transactionHash: hyperbridgeFinality.transactionHash,
11088
+ timestamp: hyperbridgeFinality.timestamp,
10843
11089
  calldata: calldata2
10844
11090
  }
10845
11091
  };
@@ -10850,7 +11096,7 @@ var PostRequestClient = class {
10850
11096
  promise: () => this.queries.queryStateMachineUpdateByHeight({
10851
11097
  statemachineId: stateMachineId,
10852
11098
  height: Number(neededHeight),
10853
- chain: stateMachineId
11099
+ chain: request.dest
10854
11100
  })
10855
11101
  });
10856
11102
  }