@xyo-network/chain-bridge 1.19.17 → 1.20.0

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.
Files changed (146) hide show
  1. package/dist/node/config/getTestGateway.d.ts +4 -0
  2. package/dist/node/config/getTestGateway.d.ts.map +1 -0
  3. package/dist/node/config/index.d.ts +1 -1
  4. package/dist/node/config/index.d.ts.map +1 -1
  5. package/dist/node/index.mjs +767 -520
  6. package/dist/node/index.mjs.map +1 -1
  7. package/dist/node/modules/index.d.ts +0 -1
  8. package/dist/node/modules/index.d.ts.map +1 -1
  9. package/dist/node/queue/flowProducer.d.ts +2 -1
  10. package/dist/node/queue/flowProducer.d.ts.map +1 -1
  11. package/dist/node/queue/flows/createEthToXl1BridgeJob.d.ts +10 -0
  12. package/dist/node/queue/flows/createEthToXl1BridgeJob.d.ts.map +1 -0
  13. package/dist/node/queue/flows/createXl1ToEthBridgeJob.d.ts.map +1 -1
  14. package/dist/node/queue/flows/index.d.ts +1 -0
  15. package/dist/node/queue/flows/index.d.ts.map +1 -1
  16. package/dist/node/queue/getXl1ToEthQueueJobs.d.ts +20 -0
  17. package/dist/node/queue/getXl1ToEthQueueJobs.d.ts.map +1 -0
  18. package/dist/node/queue/getXl1ToEthQueues.d.ts +14 -0
  19. package/dist/node/queue/getXl1ToEthQueues.d.ts.map +1 -0
  20. package/dist/node/queue/index.d.ts +4 -0
  21. package/dist/node/queue/index.d.ts.map +1 -1
  22. package/dist/node/queue/prefix.d.ts +2 -0
  23. package/dist/node/queue/prefix.d.ts.map +1 -0
  24. package/dist/node/queue/telemetry.d.ts +3 -0
  25. package/dist/node/queue/telemetry.d.ts.map +1 -0
  26. package/dist/node/queue/workers/EthToXl1BridgeParent.d.ts +8 -0
  27. package/dist/node/queue/workers/EthToXl1BridgeParent.d.ts.map +1 -0
  28. package/dist/node/queue/workers/EthTransactionMonitor.d.ts +11 -0
  29. package/dist/node/queue/workers/EthTransactionMonitor.d.ts.map +1 -1
  30. package/dist/node/queue/workers/EthTransactionPreparation.d.ts +9 -1
  31. package/dist/node/queue/workers/EthTransactionPreparation.d.ts.map +1 -1
  32. package/dist/node/queue/workers/EthTransactionSubmission.d.ts +10 -0
  33. package/dist/node/queue/workers/EthTransactionSubmission.d.ts.map +1 -1
  34. package/dist/node/queue/workers/EthTransactionSubmissionStorage.d.ts +13 -0
  35. package/dist/node/queue/workers/EthTransactionSubmissionStorage.d.ts.map +1 -0
  36. package/dist/node/queue/workers/WorkerDescription.d.ts +2 -1
  37. package/dist/node/queue/workers/WorkerDescription.d.ts.map +1 -1
  38. package/dist/node/queue/workers/Xl1ToEthBridgeParent.d.ts +8 -0
  39. package/dist/node/queue/workers/Xl1ToEthBridgeParent.d.ts.map +1 -1
  40. package/dist/node/queue/workers/Xl1TransactionMonitor.d.ts +10 -0
  41. package/dist/node/queue/workers/Xl1TransactionMonitor.d.ts.map +1 -1
  42. package/dist/node/queue/workers/Xl1TransactionPreparation.d.ts +9 -0
  43. package/dist/node/queue/workers/Xl1TransactionPreparation.d.ts.map +1 -1
  44. package/dist/node/queue/workers/Xl1TransactionSubmission.d.ts +10 -0
  45. package/dist/node/queue/workers/Xl1TransactionSubmission.d.ts.map +1 -1
  46. package/dist/node/queue/workers/Xl1TransactionSubmissionStorage.d.ts +13 -0
  47. package/dist/node/queue/workers/Xl1TransactionSubmissionStorage.d.ts.map +1 -0
  48. package/dist/node/queue/workers/createWorkers.d.ts +2 -1
  49. package/dist/node/queue/workers/createWorkers.d.ts.map +1 -1
  50. package/dist/node/queue/workers/index.d.ts +3 -0
  51. package/dist/node/queue/workers/index.d.ts.map +1 -1
  52. package/dist/node/queue/workers/util/index.d.ts +0 -2
  53. package/dist/node/queue/workers/util/index.d.ts.map +1 -1
  54. package/dist/node/server/addFlowProducer.d.ts.map +1 -1
  55. package/dist/node/server/addWorkers.d.ts.map +1 -1
  56. package/dist/node/server/index.d.ts +2 -0
  57. package/dist/node/server/index.d.ts.map +1 -1
  58. package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeToRemote.d.ts.map +1 -1
  59. package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeToRemoteEstimate.d.ts +1 -1
  60. package/dist/node/server/routes/bridge/routeDefinitions/routes/bridgeToRemoteStatus.d.ts.map +1 -1
  61. package/dist/node/server/server.d.ts.map +1 -1
  62. package/dist/node/services/IBridgeServiceCollection.d.ts +1 -1
  63. package/dist/node/services/IBridgeServiceCollection.d.ts.map +1 -1
  64. package/dist/node/services/TxState.d.ts +0 -7
  65. package/dist/node/services/TxState.d.ts.map +1 -1
  66. package/dist/node/util/index.d.ts +0 -3
  67. package/dist/node/util/index.d.ts.map +1 -1
  68. package/dist/node/validation/AsyncLogger.d.ts +5 -0
  69. package/dist/node/validation/AsyncLogger.d.ts.map +1 -0
  70. package/dist/node/validation/index.d.ts +7 -0
  71. package/dist/node/validation/index.d.ts.map +1 -0
  72. package/dist/node/validation/validateBridgeEstimate.d.ts.map +1 -0
  73. package/dist/node/validation/validateBridgeEstimateExact.d.ts.map +1 -0
  74. package/dist/node/validation/validateBridgeTransaction.d.ts.map +1 -0
  75. package/dist/node/{queue/workers/util/validateSufficientAllowance.d.ts → validation/validateSufficientLiquiditySourceAllowance.d.ts} +3 -7
  76. package/dist/node/validation/validateSufficientLiquiditySourceAllowance.d.ts.map +1 -0
  77. package/dist/node/{queue/workers/util/validateSufficientBalance.d.ts → validation/validateSufficientLiquiditySourceBalance.d.ts} +3 -7
  78. package/dist/node/validation/validateSufficientLiquiditySourceBalance.d.ts.map +1 -0
  79. package/dist/node/validation/validateSufficientRunnerEthBalanceForGas.d.ts +14 -0
  80. package/dist/node/validation/validateSufficientRunnerEthBalanceForGas.d.ts.map +1 -0
  81. package/dist/node/validation/validateSufficientXL1SourceAddressBalance.d.ts +15 -0
  82. package/dist/node/validation/validateSufficientXL1SourceAddressBalance.d.ts.map +1 -0
  83. package/package.json +31 -35
  84. package/src/config/getBridgeWalletAccount.ts +1 -1
  85. package/src/config/getTestGateway.ts +24 -0
  86. package/src/config/index.ts +1 -1
  87. package/src/modules/index.ts +0 -1
  88. package/src/queue/flowProducer.ts +7 -2
  89. package/src/queue/flows/createEthToXl1BridgeJob.ts +71 -0
  90. package/src/queue/flows/createXl1ToEthBridgeJob.ts +48 -20
  91. package/src/queue/flows/index.ts +1 -0
  92. package/src/queue/getXl1ToEthQueueJobs.ts +57 -0
  93. package/src/queue/getXl1ToEthQueues.ts +39 -0
  94. package/src/queue/index.ts +4 -0
  95. package/src/queue/prefix.ts +1 -0
  96. package/src/queue/telemetry.ts +12 -0
  97. package/src/queue/workers/EthToXl1BridgeParent.ts +40 -0
  98. package/src/queue/workers/EthTransactionMonitor.ts +14 -6
  99. package/src/queue/workers/EthTransactionPreparation.ts +21 -9
  100. package/src/queue/workers/EthTransactionSubmission.ts +10 -38
  101. package/src/queue/workers/EthTransactionSubmissionStorage.ts +76 -0
  102. package/src/queue/workers/WorkerDescription.ts +2 -1
  103. package/src/queue/workers/Xl1ToEthBridgeParent.ts +13 -5
  104. package/src/queue/workers/Xl1TransactionMonitor.ts +18 -12
  105. package/src/queue/workers/Xl1TransactionPreparation.ts +11 -7
  106. package/src/queue/workers/Xl1TransactionSubmission.ts +12 -12
  107. package/src/queue/workers/Xl1TransactionSubmissionStorage.ts +77 -0
  108. package/src/queue/workers/createWorkers.ts +15 -8
  109. package/src/queue/workers/index.ts +3 -0
  110. package/src/queue/workers/util/index.ts +0 -2
  111. package/src/server/addFlowProducer.ts +5 -2
  112. package/src/server/addWorkers.ts +6 -2
  113. package/src/server/index.ts +3 -2
  114. package/src/server/routes/bridge/routeDefinitions/routes/bridgeToRemote.ts +11 -1
  115. package/src/server/routes/bridge/routeDefinitions/routes/bridgeToRemoteStatus.ts +27 -101
  116. package/src/server/server.ts +1 -0
  117. package/src/services/IBridgeServiceCollection.ts +1 -1
  118. package/src/services/TxState.ts +0 -18
  119. package/src/services/getServices.ts +1 -1
  120. package/src/util/index.ts +0 -3
  121. package/src/validation/AsyncLogger.ts +5 -0
  122. package/src/validation/index.ts +6 -0
  123. package/src/{util → validation}/validateBridgeEstimate.ts +1 -1
  124. package/src/{util → validation}/validateBridgeEstimateExact.ts +1 -1
  125. package/src/{util → validation}/validateBridgeTransaction.ts +1 -2
  126. package/src/{queue/workers/util/validateSufficientAllowance.ts → validation/validateSufficientLiquiditySourceAllowance.ts} +3 -6
  127. package/src/{queue/workers/util/validateSufficientBalance.ts → validation/validateSufficientLiquiditySourceBalance.ts} +3 -6
  128. package/src/validation/validateSufficientRunnerEthBalanceForGas.ts +57 -0
  129. package/src/validation/validateSufficientXL1SourceAddressBalance.ts +39 -0
  130. package/dist/node/config/getGateway.d.ts +0 -4
  131. package/dist/node/config/getGateway.d.ts.map +0 -1
  132. package/dist/node/modules/XL1TransactionCompletionMonitorSentinel/XL1TransactionCompletionMonitorSentinel.d.ts +0 -60
  133. package/dist/node/modules/XL1TransactionCompletionMonitorSentinel/XL1TransactionCompletionMonitorSentinel.d.ts.map +0 -1
  134. package/dist/node/modules/XL1TransactionCompletionMonitorSentinel/index.d.ts +0 -2
  135. package/dist/node/modules/XL1TransactionCompletionMonitorSentinel/index.d.ts.map +0 -1
  136. package/dist/node/queue/workers/util/validateSufficientAllowance.d.ts.map +0 -1
  137. package/dist/node/queue/workers/util/validateSufficientBalance.d.ts.map +0 -1
  138. package/dist/node/util/validateBridgeEstimate.d.ts.map +0 -1
  139. package/dist/node/util/validateBridgeEstimateExact.d.ts.map +0 -1
  140. package/dist/node/util/validateBridgeTransaction.d.ts.map +0 -1
  141. package/src/config/getGateway.ts +0 -23
  142. package/src/modules/XL1TransactionCompletionMonitorSentinel/XL1TransactionCompletionMonitorSentinel.ts +0 -165
  143. package/src/modules/XL1TransactionCompletionMonitorSentinel/index.ts +0 -1
  144. /package/dist/node/{util → validation}/validateBridgeEstimate.d.ts +0 -0
  145. /package/dist/node/{util → validation}/validateBridgeEstimateExact.d.ts +0 -0
  146. /package/dist/node/{util → validation}/validateBridgeTransaction.d.ts +0 -0
@@ -3,38 +3,43 @@ import { PayloadBuilder } from '@xyo-network/sdk-js'
3
3
  import { isBridgeIntent, type SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
4
4
  import type { Job } from 'bullmq'
5
5
  import { Worker } from 'bullmq'
6
+ import type { BullMQOtel } from 'bullmq-otel'
6
7
  import { getAddress } from 'ethers'
7
8
  import type { Redis } from 'ioredis'
8
9
 
9
10
  import type { IBridgeServiceCollection } from '../../services/index.ts'
10
- import { validateSufficientAllowance, validateSufficientBalance } from './util/index.ts'
11
+ import {
12
+ validateSufficientLiquiditySourceAllowance, validateSufficientLiquiditySourceBalance, validateSufficientRunnerEthBalanceForGas,
13
+ } from '../../validation/index.ts'
14
+ import { prefix } from '../prefix.ts'
11
15
  import type { WorkerDescription } from './WorkerDescription.ts'
12
16
 
13
- type JobData = { tx: SignedHydratedTransaction }
14
-
15
- interface ReturnType {}
17
+ export type EthTransactionPreparationJobData = { tx: SignedHydratedTransaction }
18
+ export type EthTransactionPreparationJobReturn = Record<string, never>
19
+ export type EthTransactionPreparationJob = Job<EthTransactionPreparationJobData, EthTransactionPreparationJobReturn>
16
20
 
17
21
  const name = 'Prepare ETH Transaction'
18
22
  const queueName = 'eth-tx-prepare'
19
23
 
20
- export const createWorker = (connection: Redis, services?: IBridgeServiceCollection) => {
24
+ export const createWorker = (connection: Redis, telemetry?: BullMQOtel, services?: IBridgeServiceCollection) => {
21
25
  const bridge = assertEx(services?.bridge, () => 'bridge service not provided')
22
26
  const bridgeableToken = assertEx(services?.bridgeableToken, () => 'bridgeableToken service not provided')
23
27
  const stateMap = assertEx(services?.ethTxStateMap, () => 'ethTxStateMap service not provided')
28
+ const wallet = assertEx(services?.wallet, () => 'wallet service not provided')
24
29
 
25
30
  const worker = new Worker(
26
31
  queueName,
27
- async (job: Job<JobData, ReturnType>) => {
32
+ async (job: EthTransactionPreparationJob) => {
28
33
  const { tx } = job.data
29
34
  const hash = await PayloadBuilder.hash(tx[0])
30
35
  await job.log(`[${hash}] preparing ETH transaction`)
31
36
  await job.log(`[${hash}] validating liquiditySource has sufficient allowance`)
32
- if (!await validateSufficientAllowance(tx, bridgeableToken, bridge, job)) {
37
+ if (!await validateSufficientLiquiditySourceAllowance(tx, bridgeableToken, bridge, job)) {
33
38
  throw new Error('Liquidity source does not have sufficient allowance for the bridge to execute the transaction')
34
39
  }
35
40
  await job.log(`[${hash}] validated liquiditySource has sufficient allowance`)
36
41
  await job.log(`[${hash}] validating liquiditySource has sufficient balance`)
37
- if (!await validateSufficientBalance(tx, bridgeableToken, bridge, job)) {
42
+ if (!await validateSufficientLiquiditySourceBalance(tx, bridgeableToken, bridge, job)) {
38
43
  throw new Error('Liquidity source does not have sufficient balance for the bridge to execute the transaction')
39
44
  }
40
45
  await job.log(`[${hash}] validated liquiditySource has sufficient balance`)
@@ -46,13 +51,20 @@ export const createWorker = (connection: Redis, services?: IBridgeServiceCollect
46
51
  const nonce = hexToBigInt(await PayloadBuilder.hash(tx[0]))
47
52
  const preparedTx = await bridge.getFunction('bridgeFromRemote').populateTransaction(srcAddress, destAddress, amount, nonce)
48
53
  await job.log(`[${hash}] built ETH transaction`)
54
+ await job.log(`[${hash}] validating tx runner has sufficient ETH for gas`)
55
+ if (!await validateSufficientRunnerEthBalanceForGas(preparedTx, wallet, job)) {
56
+ throw new Error('Transaction runner does not have sufficient ETH to cover estimated gas (with buffer)')
57
+ }
58
+ await job.log(`[${hash}] validated tx runner has sufficient ETH for gas`)
49
59
  await job.log(`[${hash}] storing ETH preparedTx`)
50
60
  await stateMap.set(hash, { preparedTx })
51
61
  await job.log(`[${hash}] stored ETH preparedTx`)
52
62
  await job.log(`[${hash}] prepared ETH transaction`)
53
63
  return {}
54
64
  },
55
- { connection },
65
+ {
66
+ connection, telemetry, prefix,
67
+ },
56
68
  )
57
69
 
58
70
  worker.on('failed', (job, err) => {
@@ -3,21 +3,23 @@ import { PayloadBuilder } from '@xyo-network/sdk-js'
3
3
  import { type SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
4
4
  import type { Job } from 'bullmq'
5
5
  import { Worker } from 'bullmq'
6
+ import type { BullMQOtel } from 'bullmq-otel'
6
7
  import type { Redis } from 'ioredis'
7
8
 
8
9
  import type { EthTxState, IBridgeServiceCollection } from '../../services/index.ts'
10
+ import { prefix } from '../prefix.ts'
9
11
  import { submitEthTransaction } from './util/index.ts'
10
12
  import type { WorkerDescription } from './WorkerDescription.ts'
11
13
 
12
- type JobData = { tx: SignedHydratedTransaction }
13
-
14
- interface ReturnType {
14
+ export type EthTransactionSubmissionJobData = { tx: SignedHydratedTransaction }
15
+ export interface EthTransactionSubmissionJobReturn {
15
16
  submissionHash: Required<EthTxState>['submissionHash']
16
17
  }
18
+ export type EthTransactionSubmissionJob = Job<EthTransactionSubmissionJobData, EthTransactionSubmissionJobReturn>
17
19
 
18
20
  const name = 'Submit ETH Transaction'
19
21
  const queueName = 'eth-tx-submit'
20
- const createWorker = (connection: Redis, services?: IBridgeServiceCollection) => {
22
+ const createWorker = (connection: Redis, telemetry?: BullMQOtel, services?: IBridgeServiceCollection) => {
21
23
  const bridge = assertEx(services?.bridge, () => 'bridge service not provided')
22
24
  const bridgeableToken = assertEx(services?.bridgeableToken, () => 'bridgeableToken service not provided')
23
25
  const wallet = assertEx(services?.wallet, () => 'wallet service not provided')
@@ -25,11 +27,10 @@ const createWorker = (connection: Redis, services?: IBridgeServiceCollection) =>
25
27
 
26
28
  const worker = new Worker(
27
29
  queueName,
28
- async (job: Job<JobData, ReturnType>) => {
30
+ async (job: EthTransactionSubmissionJob) => {
29
31
  const { tx } = job.data
30
32
  const hash = await PayloadBuilder.hash(tx[0])
31
33
  const state = assertEx(await stateMap.get(hash), () => `[${hash}] state not found`)
32
- const preparedTx = assertEx(state?.preparedTx, () => `[${hash}] preparedTx not found`)
33
34
 
34
35
  // Idempotency check against resubmission
35
36
  const { submissionHash: existingSubmissionHash } = state
@@ -40,42 +41,13 @@ const createWorker = (connection: Redis, services?: IBridgeServiceCollection) =>
40
41
 
41
42
  // Submit the transaction to the Ethereum network
42
43
  await job.log(`[${hash}] Submitting ETH tx`)
43
- // const submissionResponse = await wallet.sendTransaction(preparedTx)
44
- // const submissionHash = submissionResponse.hash
45
-
46
44
  const submissionHash = assertEx(await submitEthTransaction(tx, bridgeableToken, bridge, wallet), () => `[${hash}] submissionHash not found in receipt`)
47
-
48
45
  await job.log(`[${hash}] Submitted ETH tx and received submission response hash ${submissionHash}`)
49
-
50
- // Store the submission hash in the state
51
- await job.log(`[${hash}] Storing ETH submissionHash`)
52
- state.submissionHash = submissionHash
53
- await stateMap.set(hash, state)
54
- await job.log(`[${hash}] Stored ETH submissionHash`)
55
-
56
46
  return { submissionHash }
57
- // const hash = await PayloadBuilder.hash(tx[0])
58
- // await job.log(`[${hash}] Obtaining bridge intent from tx`)
59
- // const bridgeIntent = assertEx(tx[1].find(isBridgeIntent), () => 'No bridge intent found in transaction payload')
60
- // await job.log(`[${hash}] submitting ETH transaction`)
61
- // const amount = hexToBigInt(bridgeIntent.destAmount)
62
- // const srcAddress = getAddress(bridgeIntent.srcAddress)
63
- // const destAddress = getAddress(bridgeIntent.destAddress)
64
- // const tx = await bridge.bridgeFromRemote(srcAddress, destAddress, amount)
65
- // // const nonce = await wallet.getNonce()
66
- // // const tx = await bridge.bridgeFromRemote(srcAddress, destAddress, amount, { nonce })
67
- // await job.log(`[${hash}] submitted ETH transaction`)
68
- // const confirmation = await tx.wait()
69
- // const transactionResponse = await confirmation?.getTransaction()
70
- // const destConfirmation = asHex(transactionResponse?.hash ?? '', true)
71
- // const block = await transactionResponse?.getBlock()
72
- // await job.log(`[${hash}] confirmed ETH transaction with hash ${destConfirmation} in block ${block?.number}`)
73
- // const { schema, ...rest } = bridgeIntent
74
- // const result: BridgeDestinationObservation = new PayloadBuilder<BridgeDestinationObservation>({ schema: BridgeDestinationObservationSchema })
75
- // .fields({ ...rest, destConfirmation }).build()
76
- // return result
77
47
  },
78
- { connection, concurrency: 1 },
48
+ {
49
+ connection, telemetry, concurrency: 1, prefix,
50
+ },
79
51
  )
80
52
 
81
53
  worker.on('failed', (job, err) => {
@@ -0,0 +1,76 @@
1
+ import { assertEx, isDefined } from '@xylabs/sdk-js'
2
+ import { PayloadBuilder } from '@xyo-network/sdk-js'
3
+ import type { SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
4
+ import type { Job } from 'bullmq'
5
+ import { Worker } from 'bullmq'
6
+ import type { BullMQOtel } from 'bullmq-otel'
7
+ import type { Redis } from 'ioredis'
8
+
9
+ import type { EthTxState, IBridgeServiceCollection } from '../../services/index.ts'
10
+ import { prefix } from '../prefix.ts'
11
+ import type { EthTransactionSubmissionJobReturn } from './EthTransactionSubmission.ts'
12
+ import { EthTransactionSubmission } from './EthTransactionSubmission.ts'
13
+ import type { WorkerDescription } from './WorkerDescription.ts'
14
+
15
+ export type EthTransactionSubmissionStorageJobData = { tx: SignedHydratedTransaction }
16
+ export interface EthTransactionSubmissionStorageJobReturn {
17
+ submissionHash: Required<EthTxState>['submissionHash']
18
+ }
19
+ export type EthTransactionSubmissionStorageJob = Job<EthTransactionSubmissionStorageJobData, EthTransactionSubmissionStorageJobReturn>
20
+
21
+ const name = 'Store ETH Transaction Submission'
22
+ const queueName = 'eth-tx-store-submission'
23
+
24
+ const createWorker = (connection: Redis, telemetry?: BullMQOtel, services?: IBridgeServiceCollection) => {
25
+ const stateMap = assertEx(services?.ethTxStateMap, () => 'ethTxStateMap service not provided')
26
+
27
+ const worker = new Worker(
28
+ queueName,
29
+ async (job: EthTransactionSubmissionStorageJob) => {
30
+ const { tx } = job.data
31
+ // Get the hash of the transaction
32
+ const hash = await PayloadBuilder.hash(tx[0])
33
+
34
+ // Get the state of the transaction
35
+ const state = assertEx(await stateMap.get(hash), () => `[${hash}] state not found`)
36
+
37
+ // Idempotency check against re-store
38
+ const { submissionHash: existingSubmissionHash } = state
39
+ if (isDefined(existingSubmissionHash)) {
40
+ await job.log(`[${hash}] submissionHash already stored as ${existingSubmissionHash}`)
41
+ return { submissionHash: existingSubmissionHash }
42
+ }
43
+
44
+ // In a Flow, this job should be the PARENT and the submit job should be a CHILD.
45
+ // That lets us read the child job’s return value(s) here.
46
+ const childrenValues = await job.getChildrenValues()
47
+ const jobKey = `${prefix}:${EthTransactionSubmission.queueName}:${hash}`
48
+ const childValues = childrenValues?.[jobKey] as EthTransactionSubmissionJobReturn | undefined
49
+ const submissionHash = childValues?.submissionHash
50
+
51
+ const resolvedSubmissionHash = assertEx(submissionHash, () => `[${hash}] child submissionHash not found in children values`)
52
+
53
+ // Store the submission hash in the state
54
+ await job.log(`[${hash}] Storing ETH submissionHash`)
55
+ state.submissionHash = resolvedSubmissionHash
56
+ await stateMap.set(hash, state)
57
+ await job.log(`[${hash}] Stored ETH submissionHash`)
58
+ return { submissionHash: resolvedSubmissionHash }
59
+ },
60
+ {
61
+ connection, telemetry, prefix,
62
+ },
63
+ )
64
+
65
+ worker.on('failed', (job, err) => {
66
+ console.error(`[${name}] Job ${job?.id} failed:`, err.message)
67
+ })
68
+
69
+ worker.on('error', (err) => {
70
+ console.error(`[${name}] Worker error:`, err)
71
+ })
72
+ }
73
+
74
+ export const EthTransactionSubmissionStorage: WorkerDescription = {
75
+ createWorker, name, queueName,
76
+ }
@@ -1,9 +1,10 @@
1
+ import type { BullMQOtel } from 'bullmq-otel'
1
2
  import type { Redis } from 'ioredis'
2
3
 
3
4
  import type { IBridgeServiceCollection } from '../../services/index.ts'
4
5
 
5
6
  export interface WorkerDescription {
6
- createWorker: (connection: Redis, services?: IBridgeServiceCollection) => void
7
+ createWorker: (connection: Redis, telemetry?: BullMQOtel, services?: IBridgeServiceCollection) => void
7
8
  name: string
8
9
  queueName: string
9
10
  }
@@ -1,22 +1,30 @@
1
+ import type { SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
1
2
  import type { Job } from 'bullmq'
2
3
  import { Worker } from 'bullmq'
4
+ import type { BullMQOtel } from 'bullmq-otel'
3
5
  import type { Redis } from 'ioredis'
4
6
 
7
+ import { prefix } from '../prefix.ts'
5
8
  import type { WorkerDescription } from './WorkerDescription.ts'
6
9
 
10
+ export type Xl1ToEthBridgeParentJobData = { tx: SignedHydratedTransaction }
11
+ export interface Xl1ToEthBridgeParentJobReturn { }
12
+ export type Xl1ToEthBridgeParentJob = Job<Xl1ToEthBridgeParentJobData, Xl1ToEthBridgeParentJobReturn>
13
+
7
14
  const name = 'Bridge XL1 to Ethereum'
8
15
  const queueName = 'xl1-to-eth-bridge'
9
- const createWorker = (connection: Redis) => {
16
+ const createWorker = (connection: Redis, telemetry?: BullMQOtel) => {
10
17
  const worker = new Worker(
11
18
  queueName,
12
- async (job: Job) => {
19
+ async (job: Xl1ToEthBridgeParentJob) => {
13
20
  await job.log(`[${job.name}] start`)
14
21
  // Parent job has no work other than waiting on children
15
22
  await job.log(`[${job.name}] done`)
16
- // const values = await job.getChildrenValues()
17
- return { ok: true }
23
+ return {}
24
+ },
25
+ {
26
+ connection, telemetry, prefix,
18
27
  },
19
- { connection },
20
28
  )
21
29
 
22
30
  worker.on('failed', (job, err) => {
@@ -1,3 +1,4 @@
1
+ import type { BrandedHash } from '@xylabs/sdk-js'
1
2
  import {
2
3
  assertEx, isDefined, isNull,
3
4
  } from '@xylabs/sdk-js'
@@ -5,44 +6,47 @@ import { PayloadBuilder } from '@xyo-network/sdk-js'
5
6
  import type { SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
6
7
  import type { Job } from 'bullmq'
7
8
  import { UnrecoverableError, Worker } from 'bullmq'
9
+ import type { BullMQOtel } from 'bullmq-otel'
8
10
  import type { Redis } from 'ioredis'
9
11
 
10
12
  import type { IBridgeServiceCollection } from '../../services/index.ts'
13
+ import { prefix } from '../prefix.ts'
11
14
  import type { WorkerDescription } from './WorkerDescription.ts'
12
15
 
13
- type JobData = { tx: SignedHydratedTransaction }
14
-
15
- interface ReturnType {
16
+ export type Xl1TransactionMonitorJobData = { tx: SignedHydratedTransaction }
17
+ export interface Xl1TransactionMonitorJobReturn {
18
+ submissionHash: BrandedHash
16
19
  }
20
+ export type Xl1TransactionMonitorJob = Job<Xl1TransactionMonitorJobData, Xl1TransactionMonitorJobReturn>
17
21
 
18
22
  const name = 'Monitor Submitted XL1 Transaction'
19
23
  const queueName = 'xl1-tx-monitor'
20
- const createWorker = (connection: Redis, services?: IBridgeServiceCollection) => {
24
+ const createWorker = (connection: Redis, telemetry?: BullMQOtel, services?: IBridgeServiceCollection) => {
21
25
  const gateway = assertEx(services?.gateway, () => 'gateway service not provided')
22
26
  const stateMap = assertEx(services?.xl1TxStateMap, () => 'xl1TxStateMap service not provided')
27
+ const viewer = assertEx(gateway.connection.viewer, () => 'viewer not defined on gateway')
23
28
 
24
29
  const worker = new Worker(
25
30
  queueName,
26
- async (job: Job<JobData, ReturnType>) => {
31
+ async (job: Xl1TransactionMonitorJob) => {
27
32
  const { tx } = job.data
28
33
  // Get the hash of the transaction
29
34
  const hash = await PayloadBuilder.hash(tx[0])
30
35
  // Get the state of the transaction
31
- const viewer = assertEx(gateway.connection.viewer, () => `[${hash}] viewer not defined on gateway`)
32
36
  const state = assertEx(await stateMap.get(hash), () => `[${hash}] state not found`)
33
- const submissionHash = assertEx(state?.submissionHash, () => `[${hash}] submissionHash not found`)
37
+ const submissionHash = assertEx(state.submissionHash, () => `[${hash}] submissionHash not found`)
34
38
 
35
39
  // Check for transaction inclusion on chain
36
40
  await job.log(`[${hash}] Checking for XL1 transaction inclusion on chain`)
37
- const foundTx = await viewer.transactionByHash(submissionHash)
41
+ const foundTx = await viewer.transaction.byHash(submissionHash)
42
+ // const foundTx = await viewer.transaction.byHash(submissionHash)
38
43
 
39
44
  // Transaction found on chain
40
45
  if (isDefined(foundTx) && !isNull(foundTx)) {
41
46
  await job.log(`[${hash}] Found transaction on chain`)
42
47
  // Store the block hash
43
- state.confirmationHash = await PayloadBuilder.hash(foundTx[0])
44
- await stateMap.set(hash, state)
45
- return {}
48
+ const submissionHash = await PayloadBuilder.hash(foundTx[0])
49
+ return { submissionHash }
46
50
  }
47
51
 
48
52
  // Check for transaction expiration
@@ -59,7 +63,9 @@ const createWorker = (connection: Redis, services?: IBridgeServiceCollection) =>
59
63
  await job.log(`[${hash}] Transaction not yet included, retrying later`)
60
64
  throw new Error(`[${hash}] Transaction not yet included`)
61
65
  },
62
- { connection },
66
+ {
67
+ connection, telemetry, prefix,
68
+ },
63
69
  )
64
70
 
65
71
  worker.on('failed', (job, err) => {
@@ -3,23 +3,25 @@ import { PayloadBuilder } from '@xyo-network/sdk-js'
3
3
  import type { SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
4
4
  import type { Job } from 'bullmq'
5
5
  import { Worker } from 'bullmq'
6
+ import type { BullMQOtel } from 'bullmq-otel'
6
7
  import type { Redis } from 'ioredis'
7
8
 
8
9
  import type { IBridgeServiceCollection } from '../../services/index.ts'
10
+ import { prefix } from '../prefix.ts'
9
11
  import type { WorkerDescription } from './WorkerDescription.ts'
10
12
 
11
- type JobData = { tx: SignedHydratedTransaction }
12
-
13
- interface ReturnType {}
13
+ export type Xl1TransactionPreparationJobData = { tx: SignedHydratedTransaction }
14
+ export interface Xl1TransactionPreparationJobReturn { preparedTx: SignedHydratedTransaction }
15
+ export type Xl1TransactionPreparationJob = Job<Xl1TransactionPreparationJobData, Xl1TransactionPreparationJobReturn>
14
16
 
15
17
  const name = 'Prepare XL1 Transaction'
16
18
  const queueName = 'xl1-tx-prepare'
17
- const createWorker = (connection: Redis, services?: IBridgeServiceCollection) => {
19
+ const createWorker = (connection: Redis, telemetry?: BullMQOtel, services?: IBridgeServiceCollection) => {
18
20
  const stateMap = assertEx(services?.xl1TxStateMap, () => 'xl1TxStateMap service not provided')
19
21
 
20
22
  const worker = new Worker(
21
23
  queueName,
22
- async (job: Job<JobData, ReturnType>) => {
24
+ async (job: Xl1TransactionPreparationJob) => {
23
25
  const { tx } = job.data
24
26
  const hash = await PayloadBuilder.hash(tx[0])
25
27
  await job.log(`[${hash}] preparing XL1 transaction`)
@@ -28,9 +30,11 @@ const createWorker = (connection: Redis, services?: IBridgeServiceCollection) =>
28
30
  await stateMap.set(hash, { preparedTx })
29
31
  await job.log(`[${hash}] stored XL1 preparedTx`)
30
32
  await job.log(`[${hash}] prepared XL1 transaction`)
31
- return {}
33
+ return { preparedTx }
34
+ },
35
+ {
36
+ connection, telemetry, prefix,
32
37
  },
33
- { connection },
34
38
  )
35
39
 
36
40
  worker.on('failed', (job, err) => {
@@ -3,27 +3,30 @@ import { PayloadBuilder } from '@xyo-network/sdk-js'
3
3
  import { type SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
4
4
  import type { Job } from 'bullmq'
5
5
  import { Worker } from 'bullmq'
6
+ import type { BullMQOtel } from 'bullmq-otel'
6
7
  import type { Redis } from 'ioredis'
7
8
 
8
9
  import type { IBridgeServiceCollection, Xl1TxState } from '../../services/index.ts'
10
+ import { prefix } from '../prefix.ts'
9
11
  import { submitXl1Transaction } from './util/index.ts'
10
12
  import type { WorkerDescription } from './WorkerDescription.ts'
11
13
 
12
- type JobData = { tx: SignedHydratedTransaction }
13
-
14
- interface ReturnType {
14
+ export type Xl1TransactionSubmissionJobData = { tx: SignedHydratedTransaction }
15
+ export interface Xl1TransactionSubmissionJobReturn {
15
16
  submissionHash: Required<Xl1TxState>['submissionHash']
16
17
  }
18
+ export type Xl1TransactionSubmissionJob = Job<Xl1TransactionSubmissionJobData, Xl1TransactionSubmissionJobReturn>
17
19
 
18
20
  const name = 'Submit XL1 Transaction'
19
21
  const queueName = 'xl1-tx-submit'
20
- const createWorker = (connection: Redis, services?: IBridgeServiceCollection) => {
22
+
23
+ const createWorker = (connection: Redis, telemetry?: BullMQOtel, services?: IBridgeServiceCollection) => {
21
24
  const gateway = assertEx(services?.gateway, () => 'gateway service not provided')
22
25
  const stateMap = assertEx(services?.xl1TxStateMap, () => 'xl1TxStateMap service not provided')
23
26
 
24
27
  const worker = new Worker(
25
28
  queueName,
26
- async (job: Job<JobData, ReturnType>) => {
29
+ async (job: Xl1TransactionSubmissionJob) => {
27
30
  const { tx } = job.data
28
31
  // Get the hash of the transaction
29
32
  const hash = await PayloadBuilder.hash(tx[0])
@@ -46,15 +49,12 @@ const createWorker = (connection: Redis, services?: IBridgeServiceCollection) =>
46
49
  // assertEx(submissionHash === hash, () => `[${hash}] Submitted transaction hash ${submissionHash} does not match expected hash`)
47
50
  await job.log(`[${hash}] Submitted XL1 tx`)
48
51
 
49
- // Store the submission hash in the state
50
- await job.log(`[${hash}] Storing XL1 submissionHash`)
51
- state.submissionHash = submissionHash
52
- await stateMap.set(hash, state)
53
- await job.log(`[${hash}] Stored XL1 submissionHash`)
54
-
52
+ // This worker intentionally does NOT store state as subsequent steps will do that.
55
53
  return { submissionHash }
56
54
  },
57
- { connection },
55
+ {
56
+ connection, telemetry, prefix,
57
+ },
58
58
  )
59
59
 
60
60
  worker.on('failed', (job, err) => {
@@ -0,0 +1,77 @@
1
+ import { assertEx, isDefined } from '@xylabs/sdk-js'
2
+ import { PayloadBuilder } from '@xyo-network/sdk-js'
3
+ import { type SignedHydratedTransaction } from '@xyo-network/xl1-sdk'
4
+ import type { Job } from 'bullmq'
5
+ import { Worker } from 'bullmq'
6
+ import type { BullMQOtel } from 'bullmq-otel'
7
+ import type { Redis } from 'ioredis'
8
+
9
+ import type { IBridgeServiceCollection, Xl1TxState } from '../../services/index.ts'
10
+ import { prefix } from '../prefix.ts'
11
+ import type { WorkerDescription } from './WorkerDescription.ts'
12
+ import type { Xl1TransactionSubmissionJobReturn } from './Xl1TransactionSubmission.ts'
13
+ import { Xl1TransactionSubmission } from './Xl1TransactionSubmission.ts'
14
+
15
+ export type Xl1TransactionSubmissionStorageJobData = { tx: SignedHydratedTransaction }
16
+ export interface Xl1TransactionSubmissionStorageJobReturn {
17
+ submissionHash: Required<Xl1TxState>['submissionHash']
18
+ }
19
+ export type Xl1TransactionSubmissionStorageJob = Job<Xl1TransactionSubmissionStorageJobData, Xl1TransactionSubmissionStorageJobReturn>
20
+
21
+ const name = 'Store XL1 Transaction Submission'
22
+ const queueName = 'xl1-tx-store-submission'
23
+
24
+ const createWorker = (connection: Redis, telemetry?: BullMQOtel, services?: IBridgeServiceCollection) => {
25
+ const stateMap = assertEx(services?.xl1TxStateMap, () => 'xl1TxStateMap service not provided')
26
+
27
+ const worker = new Worker(
28
+ queueName,
29
+ async (job: Xl1TransactionSubmissionStorageJob) => {
30
+ const { tx } = job.data
31
+ // Get the hash of the transaction
32
+ const hash = await PayloadBuilder.hash(tx[0])
33
+
34
+ // Get the state of the transaction
35
+ const state = assertEx(await stateMap.get(hash), () => `[${hash}] state not found`)
36
+
37
+ // Idempotency check against re-store
38
+ const { submissionHash: existingSubmissionHash } = state
39
+ if (isDefined(existingSubmissionHash)) {
40
+ await job.log(`[${hash}] submissionHash already stored as ${existingSubmissionHash}`)
41
+ return { submissionHash: existingSubmissionHash }
42
+ }
43
+
44
+ // In a Flow, this job should be the PARENT and the submit job should be a CHILD.
45
+ // That lets us read the child job’s return value(s) here.
46
+ const childrenValues = await job.getChildrenValues()
47
+ const jobKey = `${prefix}:${Xl1TransactionSubmission.queueName}:${hash}`
48
+ const childValues = childrenValues?.[jobKey] as Xl1TransactionSubmissionJobReturn | undefined
49
+ const submissionHash = childValues?.submissionHash
50
+
51
+ const resolvedSubmissionHash = assertEx(submissionHash, () => `[${hash}] child submissionHash not found in children values`)
52
+
53
+ // Store the submission hash in the state
54
+ await job.log(`[${hash}] Storing XL1 submissionHash`)
55
+ state.submissionHash = resolvedSubmissionHash
56
+ await stateMap.set(hash, state)
57
+ await job.log(`[${hash}] Stored XL1 submissionHash`)
58
+
59
+ return { submissionHash: resolvedSubmissionHash }
60
+ },
61
+ {
62
+ connection, telemetry, prefix,
63
+ },
64
+ )
65
+
66
+ worker.on('failed', (job, err) => {
67
+ console.error(`[${name}] Job ${job?.id} failed:`, err.message)
68
+ })
69
+
70
+ worker.on('error', (err) => {
71
+ console.error(`[${name}] Worker error:`, err)
72
+ })
73
+ }
74
+
75
+ export const Xl1TransactionSubmissionStorage: WorkerDescription = {
76
+ createWorker, name, queueName,
77
+ }
@@ -1,3 +1,4 @@
1
+ import type { BullMQOtel } from 'bullmq-otel'
1
2
  import type { Redis } from 'ioredis'
2
3
 
3
4
  import type { IBridgeServiceCollection } from '../../services/index.ts'
@@ -5,18 +6,24 @@ import {
5
6
  EthTransactionMonitor,
6
7
  EthTransactionPreparation,
7
8
  EthTransactionSubmission,
9
+ EthTransactionSubmissionStorage,
8
10
  Xl1ToEthBridgeParent,
9
11
  Xl1TransactionMonitor,
10
12
  Xl1TransactionPreparation,
11
13
  Xl1TransactionSubmission,
14
+ Xl1TransactionSubmissionStorage,
12
15
  } from './index.ts'
13
16
 
14
- export const createWorkers = (connection: Redis, services: IBridgeServiceCollection) => {
15
- Xl1ToEthBridgeParent.createWorker(connection)
16
- Xl1TransactionPreparation.createWorker(connection, services)
17
- Xl1TransactionSubmission.createWorker(connection, services)
18
- Xl1TransactionMonitor.createWorker(connection, services)
19
- EthTransactionPreparation.createWorker(connection, services)
20
- EthTransactionSubmission.createWorker(connection, services)
21
- EthTransactionMonitor.createWorker(connection, services)
17
+ export const createWorkers = (connection: Redis, telemetry: BullMQOtel, services: IBridgeServiceCollection) => {
18
+ // TODO: Uncomment when EthToXl1 flow is ready
19
+ // EthToXl1BridgeParent.createWorker(connection, telemetry)
20
+ EthTransactionMonitor.createWorker(connection, telemetry, services)
21
+ EthTransactionPreparation.createWorker(connection, telemetry, services)
22
+ EthTransactionSubmission.createWorker(connection, telemetry, services)
23
+ EthTransactionSubmissionStorage.createWorker(connection, telemetry, services)
24
+ Xl1ToEthBridgeParent.createWorker(connection, telemetry)
25
+ Xl1TransactionMonitor.createWorker(connection, telemetry, services)
26
+ Xl1TransactionPreparation.createWorker(connection, telemetry, services)
27
+ Xl1TransactionSubmission.createWorker(connection, telemetry, services)
28
+ Xl1TransactionSubmissionStorage.createWorker(connection, telemetry, services)
22
29
  }
@@ -1,8 +1,11 @@
1
1
  export * from './createWorkers.ts'
2
+ export * from './EthToXl1BridgeParent.ts'
2
3
  export * from './EthTransactionMonitor.ts'
3
4
  export * from './EthTransactionPreparation.ts'
4
5
  export * from './EthTransactionSubmission.ts'
6
+ export * from './EthTransactionSubmissionStorage.ts'
5
7
  export * from './Xl1ToEthBridgeParent.ts'
6
8
  export * from './Xl1TransactionMonitor.ts'
7
9
  export * from './Xl1TransactionPreparation.ts'
8
10
  export * from './Xl1TransactionSubmission.ts'
11
+ export * from './Xl1TransactionSubmissionStorage.ts'
@@ -1,4 +1,2 @@
1
1
  export * from './submitEthTransaction.ts'
2
2
  export * from './submitXl1Transaction.ts'
3
- export * from './validateSufficientAllowance.ts'
4
- export * from './validateSufficientBalance.ts'
@@ -1,11 +1,14 @@
1
1
  import type { BridgeConfig } from '@xyo-network/chain-orchestration'
2
2
  import type { Express } from 'express'
3
3
 
4
- import { getConnection, getFlowProducer } from '../queue/index.ts'
4
+ import {
5
+ getConnection, getFlowProducer, getTelemetry,
6
+ } from '../queue/index.ts'
5
7
 
6
8
  export const addFlowProducer = (app: Express, config: BridgeConfig): Express => {
7
9
  const connection = getConnection(config)
8
- const flowProducer = getFlowProducer(connection)
10
+ const telemetry = getTelemetry()
11
+ const flowProducer = getFlowProducer(connection, telemetry)
9
12
  app.flowProducer = flowProducer
10
13
  return app
11
14
  }
@@ -1,9 +1,13 @@
1
1
  import type { BridgeConfig } from '@xyo-network/chain-orchestration'
2
2
 
3
- import { createWorkers, getConnection } from '../queue/index.ts'
3
+ import {
4
+ createWorkers, getConnection,
5
+ getTelemetry,
6
+ } from '../queue/index.ts'
4
7
  import type { IBridgeServiceCollection } from '../services/index.ts'
5
8
 
6
9
  export const addWorkers = (config: BridgeConfig, services: IBridgeServiceCollection) => {
7
10
  const connection = getConnection(config)
8
- createWorkers(connection, services)
11
+ const telemetry = getTelemetry()
12
+ createWorkers(connection, telemetry, services)
9
13
  }
@@ -4,6 +4,8 @@ export * from './server.ts'
4
4
  // import type { NodeInstance } from '@xyo-network/sdk-js'
5
5
  import type { FlowProducer } from 'bullmq'
6
6
 
7
+ import type { IBridgeServiceCollection } from '../services/index.ts'
8
+
7
9
  // import type { IBridgeServiceCollection } from '../services/index.ts'
8
10
 
9
11
  declare global {
@@ -11,8 +13,7 @@ declare global {
11
13
  namespace Express {
12
14
  interface Application {
13
15
  flowProducer: FlowProducer
14
- // node: NodeInstance
15
- // services: IBridgeServiceCollection
16
+ services: IBridgeServiceCollection
16
17
  }
17
18
  }
18
19
  }