@@ -25,6 +25,8 @@ import {
2525 type RequestStatusKey ,
2626 type TimeoutStatusKey ,
2727 type PostRequestStatus ,
28+ type OrderWithStatus ,
29+ OrderStatus ,
2830} from "@/types"
2931import {
3032 STATE_MACHINE_UPDATES_BY_HEIGHT ,
@@ -45,7 +47,7 @@ import {
4547 waitForChallengePeriod ,
4648} from "@/utils"
4749import { getChain , type IChain , type SubstrateChain } from "@/chain"
48- import { _queryGetRequestInternal , _queryRequestInternal } from "./query-client"
50+ import { _queryGetRequestInternal , _queryRequestInternal , _queryOrderInternal } from "./query-client"
4951
5052/**
5153 * IndexerClient provides methods for interacting with the Hyperbridge indexer.
@@ -1316,16 +1318,6 @@ export class IndexerClient {
13161318 }
13171319 }
13181320
1319- /**
1320- * Executes an async operation with exponential backoff retry
1321- * @param operation - Async function to execute
1322- * @param retryConfig - Optional retry configuration
1323- * @returns Result of the operation
1324- * @throws Last encountered error after all retries are exhausted
1325- *
1326- * @example
1327- * const result = await this.withRetry(() => this.queryStatus(hash));
1328- */
13291321 /**
13301322 * Query for asset teleported events by sender, recipient, and destination chain
13311323 * @param from - The sender address
@@ -1367,6 +1359,107 @@ export class IndexerClient {
13671359 ...retryConfig ,
13681360 } )
13691361 }
1362+
1363+ /**
1364+ * Query for an order by its commitment hash
1365+ * @param commitment - The commitment hash of the order
1366+ * @returns The order with its status if found, undefined otherwise
1367+ */
1368+ async queryOrder ( commitment : HexString ) : Promise < OrderWithStatus | undefined > {
1369+ return _queryOrderInternal ( {
1370+ commitmentHash : commitment ,
1371+ queryClient : this . client ,
1372+ logger : this . logger ,
1373+ } )
1374+ }
1375+
1376+ /**
1377+ * Create a Stream of status updates for an order.
1378+ * Stream ends when the order reaches a terminal state (FILLED, REDEEMED, or REFUNDED).
1379+ * @param commitment - The commitment hash of the order
1380+ * @returns AsyncGenerator that emits status updates until a terminal state is reached
1381+ * @example
1382+ *
1383+ * let client = new IndexerClient(config)
1384+ * let stream = client.orderStatusStream(commitment)
1385+ *
1386+ * // you can use a for-await-of loop
1387+ * for await (const status of stream) {
1388+ * console.log(status)
1389+ * }
1390+ *
1391+ * // you can also use a while loop
1392+ * while (true) {
1393+ * const status = await stream.next()
1394+ * if (status.done) {
1395+ * break
1396+ * }
1397+ * console.log(status.value)
1398+ * }
1399+ */
1400+ async * orderStatusStream ( commitment : HexString ) : AsyncGenerator <
1401+ {
1402+ status : OrderStatus
1403+ metadata : {
1404+ blockHash : string
1405+ blockNumber : number
1406+ transactionHash : string
1407+ timestamp : bigint
1408+ filler ?: string
1409+ }
1410+ } ,
1411+ void
1412+ > {
1413+ const logger = this . logger . withTag ( "[orderStatusStream]" )
1414+
1415+ let order : OrderWithStatus | undefined
1416+
1417+ while ( ! order ) {
1418+ await this . sleep_for_interval ( )
1419+ order = await _queryOrderInternal ( {
1420+ commitmentHash : commitment ,
1421+ queryClient : this . client ,
1422+ logger : this . logger ,
1423+ } )
1424+ }
1425+
1426+ logger . trace ( "`Order` found" )
1427+ // Yield initial status
1428+ const latestStatus = order . statuses [ order . statuses . length - 1 ]
1429+ yield {
1430+ status : latestStatus . status ,
1431+ metadata : latestStatus . metadata ,
1432+ }
1433+
1434+ // If we're already in a terminal state, end the stream
1435+ if ( [ OrderStatus . FILLED , OrderStatus . REDEEMED , OrderStatus . REFUNDED ] . includes ( latestStatus . status ) ) {
1436+ return
1437+ }
1438+
1439+ while ( true ) {
1440+ await this . sleep_for_interval ( )
1441+ const updatedOrder = await _queryOrderInternal ( {
1442+ commitmentHash : commitment ,
1443+ queryClient : this . client ,
1444+ logger : this . logger ,
1445+ } )
1446+
1447+ if ( ! updatedOrder ) continue
1448+
1449+ const newLatestStatus = updatedOrder . statuses [ updatedOrder . statuses . length - 1 ]
1450+
1451+ if ( newLatestStatus . status !== latestStatus . status ) {
1452+ yield {
1453+ status : newLatestStatus . status ,
1454+ metadata : newLatestStatus . metadata ,
1455+ }
1456+
1457+ if ( [ OrderStatus . FILLED , OrderStatus . REDEEMED , OrderStatus . REFUNDED ] . includes ( newLatestStatus . status ) ) {
1458+ return
1459+ }
1460+ }
1461+ }
1462+ }
13701463}
13711464
13721465interface PartialClientConfig extends Omit < ClientConfig , "pollInterval" > {
0 commit comments