0xtrails 0.2.6 → 0.4.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 (218) hide show
  1. package/dist/aave.d.ts +2 -0
  2. package/dist/aave.d.ts.map +1 -1
  3. package/dist/{ccip-Xjh9d1gb.js → ccip-BpQGQiWq.js} +7 -7
  4. package/dist/config.d.ts +0 -5
  5. package/dist/config.d.ts.map +1 -1
  6. package/dist/constants.d.ts +2 -4
  7. package/dist/constants.d.ts.map +1 -1
  8. package/dist/error.d.ts +4 -1
  9. package/dist/error.d.ts.map +1 -1
  10. package/dist/fees.d.ts +2 -2
  11. package/dist/fees.d.ts.map +1 -1
  12. package/dist/{index-BnhdZ8Ho.js → index-DsJM5F-V.js} +46084 -48697
  13. package/dist/index.d.ts +9 -8
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +741 -923
  16. package/dist/intentReceiptMonitor.d.ts +24 -0
  17. package/dist/intentReceiptMonitor.d.ts.map +1 -0
  18. package/dist/intentReceiptPoller.d.ts +69 -0
  19. package/dist/intentReceiptPoller.d.ts.map +1 -0
  20. package/dist/intents.d.ts +15 -11
  21. package/dist/intents.d.ts.map +1 -1
  22. package/dist/morpho.d.ts +6 -5
  23. package/dist/morpho.d.ts.map +1 -1
  24. package/dist/mutations.d.ts +16 -0
  25. package/dist/mutations.d.ts.map +1 -0
  26. package/dist/preconditions.d.ts +5 -4
  27. package/dist/preconditions.d.ts.map +1 -1
  28. package/dist/prepareSend.d.ts +5 -190
  29. package/dist/prepareSend.d.ts.map +1 -1
  30. package/dist/prices.d.ts +9 -6
  31. package/dist/prices.d.ts.map +1 -1
  32. package/dist/sequenceWallet.d.ts +3 -16
  33. package/dist/sequenceWallet.d.ts.map +1 -1
  34. package/dist/tokenBalances.d.ts +17 -13
  35. package/dist/tokenBalances.d.ts.map +1 -1
  36. package/dist/trails.d.ts +24 -40
  37. package/dist/trails.d.ts.map +1 -1
  38. package/dist/trailsClient.d.ts +5 -6
  39. package/dist/trailsClient.d.ts.map +1 -1
  40. package/dist/transactionIntent/constants.d.ts +7 -0
  41. package/dist/transactionIntent/constants.d.ts.map +1 -0
  42. package/dist/transactionIntent/deposits/depositOrchestrator.d.ts +44 -0
  43. package/dist/transactionIntent/deposits/depositOrchestrator.d.ts.map +1 -0
  44. package/dist/transactionIntent/deposits/gaslessDeposit.d.ts +30 -0
  45. package/dist/transactionIntent/deposits/gaslessDeposit.d.ts.map +1 -0
  46. package/dist/transactionIntent/deposits/index.d.ts +4 -0
  47. package/dist/transactionIntent/deposits/index.d.ts.map +1 -0
  48. package/dist/transactionIntent/deposits/standardDeposit.d.ts +30 -0
  49. package/dist/transactionIntent/deposits/standardDeposit.d.ts.map +1 -0
  50. package/dist/transactionIntent/execution/index.d.ts +2 -0
  51. package/dist/transactionIntent/execution/index.d.ts.map +1 -0
  52. package/dist/transactionIntent/execution/transactionState.d.ts +5 -0
  53. package/dist/transactionIntent/execution/transactionState.d.ts.map +1 -0
  54. package/dist/transactionIntent/handlers/crossChain.d.ts +82 -0
  55. package/dist/transactionIntent/handlers/crossChain.d.ts.map +1 -0
  56. package/dist/transactionIntent/handlers/index.d.ts +4 -0
  57. package/dist/transactionIntent/handlers/index.d.ts.map +1 -0
  58. package/dist/transactionIntent/handlers/sameChainDifferentToken.d.ts +62 -0
  59. package/dist/transactionIntent/handlers/sameChainDifferentToken.d.ts.map +1 -0
  60. package/dist/transactionIntent/handlers/sameChainSameToken.d.ts +72 -0
  61. package/dist/transactionIntent/handlers/sameChainSameToken.d.ts.map +1 -0
  62. package/dist/transactionIntent/index.d.ts +9 -0
  63. package/dist/transactionIntent/index.d.ts.map +1 -0
  64. package/dist/transactionIntent/quote/feeExtractors.d.ts +17 -0
  65. package/dist/transactionIntent/quote/feeExtractors.d.ts.map +1 -0
  66. package/dist/transactionIntent/quote/index.d.ts +4 -0
  67. package/dist/transactionIntent/quote/index.d.ts.map +1 -0
  68. package/dist/transactionIntent/quote/normalizeQuote.d.ts +34 -0
  69. package/dist/transactionIntent/quote/normalizeQuote.d.ts.map +1 -0
  70. package/dist/transactionIntent/quote/quoteHelpers.d.ts +5 -0
  71. package/dist/transactionIntent/quote/quoteHelpers.d.ts.map +1 -0
  72. package/dist/transactionIntent/types.d.ts +131 -0
  73. package/dist/transactionIntent/types.d.ts.map +1 -0
  74. package/dist/transactionIntent/utils/balanceChecker.d.ts +18 -0
  75. package/dist/transactionIntent/utils/balanceChecker.d.ts.map +1 -0
  76. package/dist/transactionIntent/utils/index.d.ts +4 -0
  77. package/dist/transactionIntent/utils/index.d.ts.map +1 -0
  78. package/dist/transactionIntent/utils/lifiHelpers.d.ts +10 -0
  79. package/dist/transactionIntent/utils/lifiHelpers.d.ts.map +1 -0
  80. package/dist/transactionIntent/utils/testnetHelpers.d.ts +3 -0
  81. package/dist/transactionIntent/utils/testnetHelpers.d.ts.map +1 -0
  82. package/dist/transactionIntent/validators.d.ts +6 -0
  83. package/dist/transactionIntent/validators.d.ts.map +1 -0
  84. package/dist/transactions.d.ts +2 -1
  85. package/dist/transactions.d.ts.map +1 -1
  86. package/dist/widget/components/AccountSettings.d.ts.map +1 -1
  87. package/dist/widget/components/ClassicSwap.d.ts +0 -1
  88. package/dist/widget/components/ClassicSwap.d.ts.map +1 -1
  89. package/dist/widget/components/ConfigDisplay.d.ts.map +1 -1
  90. package/dist/widget/components/ConnectedWallets.d.ts.map +1 -1
  91. package/dist/widget/components/DynamicSizeInputField.d.ts +13 -0
  92. package/dist/widget/components/DynamicSizeInputField.d.ts.map +1 -0
  93. package/dist/widget/components/Earn.d.ts +0 -1
  94. package/dist/widget/components/Earn.d.ts.map +1 -1
  95. package/dist/widget/components/FeeOptions.d.ts +5 -13
  96. package/dist/widget/components/FeeOptions.d.ts.map +1 -1
  97. package/dist/widget/components/Fund.d.ts +0 -1
  98. package/dist/widget/components/Fund.d.ts.map +1 -1
  99. package/dist/widget/components/FundMethods.d.ts.map +1 -1
  100. package/dist/widget/components/FundSwap.d.ts +0 -1
  101. package/dist/widget/components/FundSwap.d.ts.map +1 -1
  102. package/dist/widget/components/Pay.d.ts +0 -1
  103. package/dist/widget/components/Pay.d.ts.map +1 -1
  104. package/dist/widget/components/PoolDeposit.d.ts +0 -1
  105. package/dist/widget/components/PoolDeposit.d.ts.map +1 -1
  106. package/dist/widget/components/PoolWithdraw.d.ts +0 -18
  107. package/dist/widget/components/PoolWithdraw.d.ts.map +1 -1
  108. package/dist/widget/components/QuoteDetails.d.ts +1 -0
  109. package/dist/widget/components/QuoteDetails.d.ts.map +1 -1
  110. package/dist/widget/components/Swap.d.ts +0 -1
  111. package/dist/widget/components/Swap.d.ts.map +1 -1
  112. package/dist/widget/components/TransferPendingVertical.d.ts.map +1 -1
  113. package/dist/widget/components/WaasFeeOptions.d.ts.map +1 -1
  114. package/dist/widget/css/compiled.css +2 -2
  115. package/dist/widget/hooks/useCheckout.d.ts +17 -4
  116. package/dist/widget/hooks/useCheckout.d.ts.map +1 -1
  117. package/dist/widget/hooks/useDefaultTokenSelection.d.ts.map +1 -1
  118. package/dist/widget/hooks/useQuote.d.ts +3 -4
  119. package/dist/widget/hooks/useQuote.d.ts.map +1 -1
  120. package/dist/widget/hooks/useSelectedFeeToken.d.ts +1 -0
  121. package/dist/widget/hooks/useSelectedFeeToken.d.ts.map +1 -1
  122. package/dist/widget/hooks/useSendForm.d.ts +3 -4
  123. package/dist/widget/hooks/useSendForm.d.ts.map +1 -1
  124. package/dist/widget/hooks/useTokenList.d.ts.map +1 -1
  125. package/dist/widget/hooks/useWalletConnectionContext.d.ts +25 -0
  126. package/dist/widget/hooks/useWalletConnectionContext.d.ts.map +1 -0
  127. package/dist/widget/index.js +1 -1
  128. package/dist/widget/widget.d.ts +12 -7
  129. package/dist/widget/widget.d.ts.map +1 -1
  130. package/package.json +21 -23
  131. package/src/aave.ts +54 -1
  132. package/src/config.ts +57 -58
  133. package/src/constants.ts +8 -9
  134. package/src/error.ts +21 -3
  135. package/src/fees.ts +53 -42
  136. package/src/index.ts +35 -13
  137. package/src/intentReceiptMonitor.ts +102 -0
  138. package/src/intentReceiptPoller.ts +299 -0
  139. package/src/intents.ts +206 -172
  140. package/src/morpho.ts +58 -9
  141. package/src/mutations.ts +129 -0
  142. package/src/preconditions.ts +16 -21
  143. package/src/prepareSend.ts +80 -4514
  144. package/src/prices.ts +26 -22
  145. package/src/relaySdk.ts +2 -2
  146. package/src/sequenceWallet.ts +6 -73
  147. package/src/tokenBalances.ts +175 -69
  148. package/src/trails.ts +230 -722
  149. package/src/trailsClient.ts +10 -23
  150. package/src/transactionIntent/constants.ts +11 -0
  151. package/src/transactionIntent/deposits/depositOrchestrator.ts +210 -0
  152. package/src/transactionIntent/deposits/gaslessDeposit.ts +588 -0
  153. package/src/transactionIntent/deposits/index.ts +3 -0
  154. package/src/transactionIntent/deposits/standardDeposit.ts +379 -0
  155. package/src/transactionIntent/execution/index.ts +1 -0
  156. package/src/transactionIntent/execution/transactionState.ts +35 -0
  157. package/src/transactionIntent/handlers/crossChain.ts +1707 -0
  158. package/src/transactionIntent/handlers/index.ts +3 -0
  159. package/src/transactionIntent/handlers/sameChainDifferentToken.ts +323 -0
  160. package/src/transactionIntent/handlers/sameChainSameToken.ts +712 -0
  161. package/src/transactionIntent/index.ts +9 -0
  162. package/src/transactionIntent/quote/feeExtractors.ts +81 -0
  163. package/src/transactionIntent/quote/index.ts +3 -0
  164. package/src/transactionIntent/quote/normalizeQuote.ts +367 -0
  165. package/src/transactionIntent/quote/quoteHelpers.ts +53 -0
  166. package/src/transactionIntent/types.ts +157 -0
  167. package/src/transactionIntent/utils/balanceChecker.ts +96 -0
  168. package/src/transactionIntent/utils/index.ts +3 -0
  169. package/src/transactionIntent/utils/lifiHelpers.ts +68 -0
  170. package/src/transactionIntent/utils/testnetHelpers.ts +10 -0
  171. package/src/transactionIntent/validators.ts +57 -0
  172. package/src/transactions.ts +36 -53
  173. package/src/widget/compiled.css +2 -2
  174. package/src/widget/components/AccountIntentTransactionHistory.tsx +36 -36
  175. package/src/widget/components/AccountSettings.tsx +23 -6
  176. package/src/widget/components/ClassicSwap.tsx +28 -53
  177. package/src/widget/components/ConfigDisplay.tsx +0 -11
  178. package/src/widget/components/ConnectedWallets.tsx +30 -4
  179. package/src/widget/components/DynamicSizeInputField.tsx +109 -0
  180. package/src/widget/components/Earn.tsx +0 -16
  181. package/src/widget/components/FeeBreakdown.tsx +3 -3
  182. package/src/widget/components/FeeOption.tsx +2 -2
  183. package/src/widget/components/FeeOptions.tsx +151 -112
  184. package/src/widget/components/Fund.tsx +0 -3
  185. package/src/widget/components/FundMethods.tsx +4 -3
  186. package/src/widget/components/FundSwap.tsx +0 -1
  187. package/src/widget/components/Pay.tsx +11 -16
  188. package/src/widget/components/PoolDeposit.tsx +35 -32
  189. package/src/widget/components/PoolWithdraw.tsx +153 -256
  190. package/src/widget/components/QuoteDetails.tsx +899 -494
  191. package/src/widget/components/Swap.tsx +0 -1
  192. package/src/widget/components/TransferPendingVertical.tsx +12 -8
  193. package/src/widget/components/WaasFeeOptions.tsx +23 -7
  194. package/src/widget/components/WalletConfirmation.tsx +1 -1
  195. package/src/widget/hooks/useAmountUsd.ts +9 -9
  196. package/src/widget/hooks/useCheckout.ts +97 -9
  197. package/src/widget/hooks/useDefaultTokenSelection.tsx +27 -21
  198. package/src/widget/hooks/useQuote.ts +86 -33
  199. package/src/widget/hooks/useSelectedFeeToken.tsx +32 -37
  200. package/src/widget/hooks/useSendForm.ts +37 -47
  201. package/src/widget/hooks/useTokenList.ts +34 -26
  202. package/src/widget/hooks/useWalletConnectionContext.tsx +128 -0
  203. package/src/widget/widget.tsx +197 -207
  204. package/dist/apiClient.d.ts +0 -9
  205. package/dist/apiClient.d.ts.map +0 -1
  206. package/dist/intentEntrypoint.d.ts +0 -114
  207. package/dist/intentEntrypoint.d.ts.map +0 -1
  208. package/dist/metaTxnMonitor.d.ts +0 -15
  209. package/dist/metaTxnMonitor.d.ts.map +0 -1
  210. package/dist/metaTxns.d.ts +0 -11
  211. package/dist/metaTxns.d.ts.map +0 -1
  212. package/dist/relayer.d.ts +0 -43
  213. package/dist/relayer.d.ts.map +0 -1
  214. package/src/apiClient.ts +0 -35
  215. package/src/intentEntrypoint.ts +0 -203
  216. package/src/metaTxnMonitor.ts +0 -171
  217. package/src/metaTxns.ts +0 -45
  218. package/src/relayer.ts +0 -289
package/src/fees.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { GetIntentCallsPayloadsReturn } from "./intents.js"
1
+ import type { Intent } from "@0xsequence/trails-api"
2
2
  import { getTokenInfo } from "./tokens.js"
3
3
  import { formatRawAmount, formatUsdAmountDisplay } from "./tokenBalances.js"
4
4
  import { zeroAddress } from "viem"
@@ -51,27 +51,21 @@ async function formatTokenAmount(
51
51
 
52
52
  // Extract fee breakdown from intent data
53
53
  export async function extractTrailsFeeBreakdown(
54
- intent: GetIntentCallsPayloadsReturn,
54
+ intent: Intent,
55
55
  ): Promise<TrailsFeeBreakdown | null> {
56
- const trailsFee = intent.payloads?.trailsFee
57
- if (!trailsFee?.executeQuote?.chainQuotes) {
56
+ const fees = intent?.fees
57
+ if (!fees) {
58
58
  return null
59
59
  }
60
60
 
61
- const chainQuotes = trailsFee.executeQuote.chainQuotes
62
-
63
61
  // Get chain IDs from the calls array to determine origin and destination chains
64
- const calls = intent.payloads?.calls || []
65
- const originChainId = calls[0]?.chainId // First call is origin chain
66
- const destinationChainId = calls[1]?.chainId // Second call is destination chain
67
-
68
- // Find quotes by chainId
69
- const originChainQuote = chainQuotes.find(
70
- (quote: any) => quote.chainId === originChainId,
71
- )
72
- const destinationChainQuote = chainQuotes.find(
73
- (quote: any) => quote.chainId === destinationChainId,
74
- )
62
+ const calls = intent?.calls || []
63
+ const originChainId: number | undefined = calls[0]?.chainId // First call is origin chain
64
+ const destinationChainId: number | undefined = calls[1]?.chainId // Second call is destination chain
65
+
66
+ // Use the origin and destination gas fees
67
+ const originChainQuote = fees.originGas
68
+ const destinationChainQuote = fees.destinationGas
75
69
 
76
70
  // If we can't find any chain quotes, return null
77
71
  if (!originChainQuote && !destinationChainQuote) {
@@ -80,7 +74,6 @@ export async function extractTrailsFeeBreakdown(
80
74
  {
81
75
  originChainId,
82
76
  destinationChainId,
83
- availableChainIds: chainQuotes.map((q: any) => q.chainId),
84
77
  },
85
78
  )
86
79
  return null
@@ -89,52 +82,60 @@ export async function extractTrailsFeeBreakdown(
89
82
  const breakdown: TrailsFeeBreakdown = {}
90
83
 
91
84
  // Extract origin relay fee if available
92
- if (originChainQuote) {
85
+ if (originChainQuote && originChainId !== undefined) {
93
86
  const originTokenSymbol = originChainQuote.nativeTokenSymbol || "ETH"
94
- const originChainIdNum = parseInt(originChainId || "0", 10)
87
+ const originChainIdNum =
88
+ typeof originChainId === "bigint" ? Number(originChainId) : originChainId
95
89
  const nativeTokenAddress = zeroAddress
96
90
 
97
91
  breakdown.originRelayFee = {
98
92
  amount: await formatTokenAmount(
99
- originChainQuote.totalFeeAmount,
93
+ originChainQuote.totalFeeAmount.toString(),
100
94
  nativeTokenAddress,
101
95
  originChainIdNum,
102
96
  ),
103
97
  tokenSymbol: originTokenSymbol,
104
98
  tokenAddress: nativeTokenAddress,
105
99
  chainId: originChainIdNum,
106
- usdValue: formatUsdAmountDisplay(originChainQuote.totalFeeUSD),
100
+ usdValue: formatUsdAmountDisplay(originChainQuote.totalFeeUsd),
107
101
  }
108
102
  breakdown.originChainId = originChainIdNum
109
103
  }
110
104
 
111
105
  // Extract destination relay fee if available
112
- if (destinationChainQuote) {
106
+ if (destinationChainQuote && destinationChainId !== undefined) {
113
107
  const destinationTokenSymbol =
114
108
  destinationChainQuote.nativeTokenSymbol || "ETH"
115
- const destinationChainIdNum = parseInt(destinationChainId || "0", 10)
109
+ const destinationChainIdNum =
110
+ typeof destinationChainId === "bigint"
111
+ ? Number(destinationChainId)
112
+ : destinationChainId
116
113
  const nativeTokenAddress = zeroAddress
117
114
 
118
115
  breakdown.destinationRelayFee = {
119
116
  amount: await formatTokenAmount(
120
- destinationChainQuote.totalFeeAmount,
117
+ destinationChainQuote.totalFeeAmount.toString(),
121
118
  nativeTokenAddress,
122
119
  destinationChainIdNum,
123
120
  ),
124
121
  tokenSymbol: destinationTokenSymbol,
125
122
  tokenAddress: nativeTokenAddress,
126
123
  chainId: destinationChainIdNum,
127
- usdValue: formatUsdAmountDisplay(destinationChainQuote.totalFeeUSD),
124
+ usdValue: formatUsdAmountDisplay(destinationChainQuote.totalFeeUsd),
128
125
  }
129
126
  breakdown.destinationChainId = destinationChainIdNum
130
127
  }
131
128
 
132
- // Extract provider fee from crossChainFee if available
133
- const providerFeeAmount = trailsFee.crossChainFee?.providerFeeUSD
134
- const providerFeeRawAmount = trailsFee.crossChainFee?.providerFee
135
- if (providerFeeAmount !== undefined || providerFeeRawAmount !== undefined) {
136
- const feeTokenAddress = trailsFee.feeToken || zeroAddress
137
- const originChainIdNum = parseInt(originChainId || "0", 10)
129
+ // Extract provider fee from provider fees if available
130
+ const providerFeeAmount = fees.provider?.quoteProviderFeeUsd
131
+ const providerFeeRawAmount = fees.provider?.quoteProviderFee
132
+ if (
133
+ (providerFeeAmount !== undefined || providerFeeRawAmount !== undefined) &&
134
+ originChainId !== undefined
135
+ ) {
136
+ const feeTokenAddress = fees.feeTokenAddress || zeroAddress
137
+ const originChainIdNum =
138
+ typeof originChainId === "bigint" ? Number(originChainId) : originChainId
138
139
 
139
140
  // Get token info to determine the actual token symbol
140
141
  const feeTokenInfo = await getTokenInfo(originChainIdNum, feeTokenAddress)
@@ -146,7 +147,7 @@ export async function extractTrailsFeeBreakdown(
146
147
 
147
148
  breakdown.providerFee = {
148
149
  amount: await formatTokenAmount(
149
- providerFeeRawAmount,
150
+ providerFeeRawAmount?.toString(),
150
151
  feeTokenAddress,
151
152
  originChainIdNum,
152
153
  ),
@@ -157,12 +158,16 @@ export async function extractTrailsFeeBreakdown(
157
158
  }
158
159
  }
159
160
 
160
- // Extract Trails platform fee from crossChainFee if available
161
- const trailsFeeAmount = trailsFee.crossChainFee?.trailsSwapFeeUSD
162
- const trailsFeeRawAmount = trailsFee.crossChainFee?.trailsSwapFee
163
- if (trailsFeeAmount !== undefined || trailsFeeRawAmount !== undefined) {
164
- const feeTokenAddress = trailsFee.feeToken || zeroAddress
165
- const originChainIdNum = parseInt(originChainId || "0", 10)
161
+ // Extract Trails platform fee from provider fees if available
162
+ const trailsFeeAmount = fees.provider?.trailsFeeUsd
163
+ const trailsFeeRawAmount = fees.provider?.trailsFee
164
+ if (
165
+ (trailsFeeAmount !== undefined || trailsFeeRawAmount !== undefined) &&
166
+ originChainId !== undefined
167
+ ) {
168
+ const feeTokenAddress = fees.feeTokenAddress || zeroAddress
169
+ const originChainIdNum =
170
+ typeof originChainId === "bigint" ? Number(originChainId) : originChainId
166
171
 
167
172
  // Get token info to determine the actual token symbol
168
173
  const feeTokenInfo = await getTokenInfo(originChainIdNum, feeTokenAddress)
@@ -174,7 +179,7 @@ export async function extractTrailsFeeBreakdown(
174
179
 
175
180
  breakdown.trailsFee = {
176
181
  amount: await formatTokenAmount(
177
- trailsFeeRawAmount,
182
+ trailsFeeRawAmount?.toString(),
178
183
  feeTokenAddress,
179
184
  originChainIdNum,
180
185
  ),
@@ -186,8 +191,14 @@ export async function extractTrailsFeeBreakdown(
186
191
  }
187
192
 
188
193
  // Calculate total USD value from the actual data
189
- const originUsd = parseFloat(originChainQuote?.totalFeeUSD || "0")
190
- const destinationUsd = parseFloat(destinationChainQuote?.totalFeeUSD || "0")
194
+ const originUsd =
195
+ typeof originChainQuote?.totalFeeUsd === "string"
196
+ ? parseFloat(originChainQuote.totalFeeUsd)
197
+ : originChainQuote?.totalFeeUsd || 0
198
+ const destinationUsd =
199
+ typeof destinationChainQuote?.totalFeeUsd === "string"
200
+ ? parseFloat(destinationChainQuote.totalFeeUsd)
201
+ : destinationChainQuote?.totalFeeUsd || 0
191
202
  const providerUsd =
192
203
  typeof providerFeeAmount === "number" ? providerFeeAmount : 0
193
204
  const trailsUsd = typeof trailsFeeAmount === "number" ? trailsFeeAmount : 0
package/src/index.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  // Be explicit about what we export
2
2
 
3
- export { getAPIClient, useAPIClient } from "./apiClient.js"
3
+ export {
4
+ InsufficientBalanceError,
5
+ UserRejectionError,
6
+ getIsUserRejectionError,
7
+ } from "./error.js"
4
8
  export {
5
9
  getSupportedChains,
6
10
  useSupportedChains,
@@ -13,7 +17,6 @@ export {
13
17
  useIndexerGatewayClient,
14
18
  } from "./indexerClient.js"
15
19
  export type {
16
- GetIntentCallsPayloadsReturn,
17
20
  OriginCallParams,
18
21
  QuoteProvider,
19
22
  TrailsFee,
@@ -21,15 +24,20 @@ export type {
21
24
  export {
22
25
  calculateIntentAddress,
23
26
  calculateOriginAndDestinationIntentAddresses,
24
- commitIntentConfig,
25
- getIntentCallsPayloads,
27
+ commitIntent,
28
+ getIntent,
26
29
  sendOriginTransaction,
27
30
  } from "./intents.js"
28
- export type {
29
- MetaTxn,
30
- MetaTxnStatus,
31
- } from "./metaTxnMonitor.js"
32
- export { useMetaTxnsMonitor } from "./metaTxnMonitor.js"
31
+ export type { IntentReceiptStatus } from "./intentReceiptMonitor.js"
32
+ export { useIntentReceiptMonitor } from "./intentReceiptMonitor.js"
33
+ export {
34
+ pollIntentReceipt,
35
+ type IntentReceiptPollerOptions,
36
+ type IntentReceiptPollerCallbacks,
37
+ type OnOriginTransactionFound,
38
+ type OnDestinationTransactionFound,
39
+ type OnReceiptUpdate,
40
+ } from "./intentReceiptPoller.js"
33
41
  export {
34
42
  prepareSend,
35
43
  useQuote,
@@ -82,20 +90,20 @@ export type { SupportedToken } from "./tokens.js"
82
90
  export { getTxTimeDiff } from "./transactions.js"
83
91
  export { useTrails } from "./trails.js"
84
92
  export type { WagmiAccount } from "./trails.js"
93
+ export {
94
+ useCommitIntent,
95
+ useExecuteIntent,
96
+ } from "./mutations.js"
85
97
  export type { Mode } from "./mode.js"
86
98
  export { TRAILS_ROUTER_PLACEHOLDER_AMOUNT } from "./trailsRouter.js"
87
99
  export { getERC20TransferData } from "./encoders.js"
88
- export { getRelayer, useRelayers } from "./relayer.js"
89
- export { relayerSendMetaTx, getMetaTxnReceipt } from "./metaTxns.js"
90
100
  export {
91
101
  declareSequenceEnv,
92
102
  getSequenceProjectAccessKey,
93
- getSequenceApiUrl,
94
103
  getTrailsApiUrl,
95
104
  getSequenceIndexerUrl,
96
105
  getSequenceEnv,
97
106
  setSequenceProjectAccessKey,
98
- setSequenceApiUrl,
99
107
  setTrailsApiUrl,
100
108
  setSequenceIndexerUrl,
101
109
  setSequenceNodeGatewayUrl,
@@ -115,3 +123,17 @@ export {
115
123
  useTrailsClient,
116
124
  TrailsClient,
117
125
  } from "./trailsClient.js"
126
+ export type {
127
+ IntentReceipt,
128
+ IntentTransaction as IntentTransactionReceipt,
129
+ IntentStatus,
130
+ TransactionStatus,
131
+ WaitIntentReceiptRequest,
132
+ WaitIntentReceiptResponse,
133
+ GetIntentReceiptRequest,
134
+ GetIntentReceiptResponse,
135
+ QuoteIntentRequest,
136
+ Intent,
137
+ ExecuteIntentRequest,
138
+ ExecuteIntentResponse,
139
+ } from "@0xsequence/trails-api"
@@ -0,0 +1,102 @@
1
+ import type { TrailsClient } from "./trailsClient.js"
2
+ import type {
3
+ IntentReceipt,
4
+ WaitIntentReceiptResponse,
5
+ } from "@0xsequence/trails-api"
6
+ import { type Query, useQuery } from "@tanstack/react-query"
7
+ import { logger } from "./logger.js"
8
+
9
+ const POLL_INTERVAL = 150 // 150 ms
10
+
11
+ export type IntentReceiptStatus = {
12
+ intentReceipt: IntentReceipt | null
13
+ done: boolean
14
+ isLoading: boolean
15
+ isError: boolean
16
+ error: Error | null
17
+ }
18
+
19
+ /**
20
+ * Hook to monitor intent execution status using the WaitIntentReceipt API.
21
+ * This replaces the old useMetaTxnsMonitor approach which polled individual meta-transactions.
22
+ *
23
+ * The WaitIntentReceipt API is a long-polling endpoint that:
24
+ * - Blocks up to 28 seconds waiting for status changes
25
+ * - Returns unified status for both origin and destination transactions
26
+ * - Sets done=true when intent reaches terminal status (SUCCEEDED or FAILED)
27
+ *
28
+ * @param intentId - The intent ID to monitor (usually committedOriginIntentAddress)
29
+ * @param trailsClient - TrailsClient instance for API calls
30
+ * @returns IntentReceiptStatus with current receipt, done flag, and loading states
31
+ */
32
+ export const useIntentReceiptMonitor = (
33
+ intentId: string | undefined,
34
+ trailsClient: TrailsClient,
35
+ ): IntentReceiptStatus => {
36
+ const queryResult = useQuery({
37
+ queryKey: ["waitIntentReceipt", intentId],
38
+ queryFn: async (): Promise<WaitIntentReceiptResponse> => {
39
+ if (!intentId) {
40
+ throw new Error("Intent ID is required for monitoring")
41
+ }
42
+
43
+ logger.console.log(
44
+ `[trails-sdk] 🔍 Monitoring intent receipt for ${intentId}`,
45
+ )
46
+
47
+ const response = await trailsClient.waitIntentReceipt({
48
+ intentId,
49
+ })
50
+
51
+ logger.console.log(
52
+ `[trails-sdk] 📊 Intent ${intentId} status: ${response.intentReceipt.status}, done: ${response.done}`,
53
+ )
54
+
55
+ return response
56
+ },
57
+ enabled: !!intentId,
58
+ refetchIntervalInBackground: true,
59
+ refetchInterval: (
60
+ query: Query<
61
+ WaitIntentReceiptResponse,
62
+ Error,
63
+ WaitIntentReceiptResponse,
64
+ ReadonlyArray<unknown>
65
+ >,
66
+ ) => {
67
+ const data = query.state.data
68
+
69
+ // If no data yet, continue polling
70
+ if (!data) return POLL_INTERVAL
71
+
72
+ // If done flag is true, stop polling
73
+ if (data.done) {
74
+ logger.console.log(
75
+ `[trails-sdk] ✅ Intent monitoring complete for ${intentId}`,
76
+ )
77
+ return false
78
+ }
79
+
80
+ // Otherwise continue polling
81
+ return POLL_INTERVAL
82
+ },
83
+ retry: (failureCount: number, error: Error) => {
84
+ if (failureCount >= 30) {
85
+ logger.console.error(
86
+ `[trails-sdk] ❌ Giving up on intent ${intentId} after 30 failed API attempts:`,
87
+ error,
88
+ )
89
+ return false
90
+ }
91
+ return true
92
+ },
93
+ })
94
+
95
+ return {
96
+ intentReceipt: queryResult.data?.intentReceipt ?? null,
97
+ done: queryResult.data?.done ?? false,
98
+ isLoading: queryResult.isLoading,
99
+ isError: queryResult.isError,
100
+ error: queryResult.error,
101
+ }
102
+ }
@@ -0,0 +1,299 @@
1
+ import type { TrailsClient } from "./trailsClient.js"
2
+ import type {
3
+ IntentReceipt,
4
+ WaitIntentReceiptResponse,
5
+ TransactionStatus,
6
+ } from "@0xsequence/trails-api"
7
+ import { logger } from "./logger.js"
8
+
9
+ const POLL_INTERVAL = 150 // 150 ms
10
+ const MAX_WAIT_TIME = 5 * 60 * 1000 // 5 minutes
11
+
12
+ /**
13
+ * Callback invoked when the deposit transaction hash is discovered (for gasless flows).
14
+ * @param txHash - The transaction hash of the deposit transaction
15
+ * @param receipt - The full IntentReceipt containing additional metadata
16
+ */
17
+ export type OnDepositTransactionFound = (
18
+ txHash: string,
19
+ receipt: IntentReceipt,
20
+ ) => Promise<void>
21
+
22
+ /**
23
+ * Callback invoked when the origin transaction hash is discovered.
24
+ * @param txHash - The transaction hash of the origin transaction
25
+ * @param receipt - The full IntentReceipt containing additional metadata
26
+ */
27
+ export type OnOriginTransactionFound = (
28
+ txHash: string,
29
+ receipt: IntentReceipt,
30
+ ) => Promise<void>
31
+
32
+ /**
33
+ * Callback invoked when the destination transaction hash is discovered.
34
+ * @param txHash - The transaction hash of the destination transaction
35
+ * @param receipt - The full IntentReceipt containing additional metadata
36
+ */
37
+ export type OnDestinationTransactionFound = (
38
+ txHash: string,
39
+ receipt: IntentReceipt,
40
+ ) => Promise<void>
41
+
42
+ /**
43
+ * Callback invoked when the intent receipt is updated (regardless of which field changed).
44
+ * @param receipt - The full IntentReceipt
45
+ * @param done - Whether the intent has reached a terminal status
46
+ */
47
+ export type OnReceiptUpdate = (
48
+ receipt: IntentReceipt,
49
+ done: boolean,
50
+ ) => Promise<void>
51
+
52
+ export interface IntentReceiptPollerCallbacks {
53
+ onDepositTransactionFound?: OnDepositTransactionFound
54
+ onOriginTransactionFound?: OnOriginTransactionFound
55
+ onDestinationTransactionFound?: OnDestinationTransactionFound
56
+ onReceiptUpdate?: OnReceiptUpdate
57
+ }
58
+
59
+ export interface IntentReceiptPollerOptions {
60
+ intentId: string
61
+ trailsClient: TrailsClient
62
+ callbacks?: IntentReceiptPollerCallbacks
63
+ maxWaitTime?: number
64
+ pollInterval?: number
65
+ abortSignal?: AbortSignal
66
+ }
67
+
68
+ /**
69
+ * Unified intent receipt poller that monitors a single intent ID and invokes callbacks
70
+ * as origin and destination transactions are discovered. This consolidates what was
71
+ * previously done via separate polling loops.
72
+ *
73
+ * @param options - Configuration for polling including intent ID, client, and callbacks
74
+ * @returns A promise that resolves when the intent reaches a terminal status or times out
75
+ *
76
+ * Usage:
77
+ * ```
78
+ * const poller = pollIntentReceipt({
79
+ * intentId: intent.intentId,
80
+ * trailsClient,
81
+ * callbacks: {
82
+ * onOriginTransactionFound: async (txHash, receipt) => {
83
+ * // Handle origin transaction discovery
84
+ * },
85
+ * onDestinationTransactionFound: async (txHash, receipt) => {
86
+ * // Handle destination transaction discovery
87
+ * },
88
+ * onReceiptUpdate: async (receipt, done) => {
89
+ * // Handle any receipt update
90
+ * },
91
+ * },
92
+ * })
93
+ * ```
94
+ */
95
+ export async function pollIntentReceipt(
96
+ options: IntentReceiptPollerOptions,
97
+ ): Promise<IntentReceipt | null> {
98
+ const {
99
+ intentId,
100
+ trailsClient,
101
+ callbacks,
102
+ maxWaitTime = MAX_WAIT_TIME,
103
+ pollInterval = POLL_INTERVAL,
104
+ abortSignal,
105
+ } = options
106
+
107
+ const startTime = Date.now()
108
+ let lastDepositTxHash: string | undefined
109
+ let lastOriginTxHash: string | undefined
110
+ let lastDestinationTxHash: string | undefined
111
+ let lastReceipt: IntentReceipt | null = null
112
+ let lastReceiptStates: TransactionStatus[] = []
113
+
114
+ logger.console.log("[trails-sdk] Starting unified intent receipt polling", {
115
+ intentId,
116
+ maxWaitTime,
117
+ pollInterval,
118
+ })
119
+
120
+ try {
121
+ while (Date.now() - startTime < maxWaitTime) {
122
+ // Check abort signal
123
+ if (abortSignal?.aborted) {
124
+ logger.console.log(
125
+ "[trails-sdk] Intent receipt polling aborted via signal",
126
+ {
127
+ intentId,
128
+ },
129
+ )
130
+ return lastReceipt
131
+ }
132
+
133
+ try {
134
+ // Perform single waitIntentReceipt call
135
+ const response: WaitIntentReceiptResponse =
136
+ await trailsClient.waitIntentReceipt({
137
+ intentId,
138
+ lastReceiptStates,
139
+ })
140
+
141
+ const receipt = response.intentReceipt
142
+ lastReceipt = receipt
143
+ lastReceiptStates = response.receiptStates
144
+
145
+ logger.console.log("[trails-sdk] Unified intent receipt update", {
146
+ intentId,
147
+ status: receipt.status,
148
+ done: response.done,
149
+ depositTxHash: receipt.depositTransaction?.txnHash,
150
+ originTxHash: receipt.originTransaction?.txnHash,
151
+ destinationTxHash: receipt.destinationTransaction?.txnHash,
152
+ })
153
+
154
+ // Check if deposit transaction was discovered (and not already reported)
155
+ // This is used for gasless flows where the user's deposit is tracked separately
156
+ if (
157
+ receipt.depositTransaction?.txnHash &&
158
+ receipt.depositTransaction.txnHash !== lastDepositTxHash
159
+ ) {
160
+ lastDepositTxHash = receipt.depositTransaction.txnHash
161
+ logger.console.log(
162
+ "[trails-sdk] Deposit transaction discovered via unified poller",
163
+ {
164
+ intentId,
165
+ txHash: lastDepositTxHash,
166
+ },
167
+ )
168
+
169
+ if (callbacks?.onDepositTransactionFound) {
170
+ try {
171
+ await callbacks.onDepositTransactionFound(
172
+ lastDepositTxHash,
173
+ receipt,
174
+ )
175
+ } catch (error) {
176
+ logger.console.error(
177
+ "[trails-sdk] Error in onDepositTransactionFound callback",
178
+ {
179
+ error: error instanceof Error ? error.message : String(error),
180
+ },
181
+ )
182
+ }
183
+ }
184
+ }
185
+
186
+ // Check if origin transaction was discovered (and not already reported)
187
+ if (
188
+ receipt.originTransaction?.txnHash &&
189
+ receipt.originTransaction.txnHash !== lastOriginTxHash
190
+ ) {
191
+ lastOriginTxHash = receipt.originTransaction.txnHash
192
+ logger.console.log(
193
+ "[trails-sdk] Origin transaction discovered via unified poller",
194
+ {
195
+ intentId,
196
+ txHash: lastOriginTxHash,
197
+ },
198
+ )
199
+
200
+ if (callbacks?.onOriginTransactionFound) {
201
+ try {
202
+ await callbacks.onOriginTransactionFound(
203
+ lastOriginTxHash,
204
+ receipt,
205
+ )
206
+ } catch (error) {
207
+ logger.console.error(
208
+ "[trails-sdk] Error in onOriginTransactionFound callback",
209
+ {
210
+ error: error instanceof Error ? error.message : String(error),
211
+ },
212
+ )
213
+ }
214
+ }
215
+ }
216
+
217
+ // Check if destination transaction was discovered (and not already reported)
218
+ if (
219
+ receipt.destinationTransaction?.txnHash &&
220
+ receipt.destinationTransaction.txnHash !== lastDestinationTxHash
221
+ ) {
222
+ lastDestinationTxHash = receipt.destinationTransaction.txnHash
223
+ logger.console.log(
224
+ "[trails-sdk] Destination transaction discovered via unified poller",
225
+ {
226
+ intentId,
227
+ txHash: lastDestinationTxHash,
228
+ },
229
+ )
230
+
231
+ if (callbacks?.onDestinationTransactionFound) {
232
+ try {
233
+ await callbacks.onDestinationTransactionFound(
234
+ lastDestinationTxHash,
235
+ receipt,
236
+ )
237
+ } catch (error) {
238
+ logger.console.error(
239
+ "[trails-sdk] Error in onDestinationTransactionFound callback",
240
+ {
241
+ error: error instanceof Error ? error.message : String(error),
242
+ },
243
+ )
244
+ }
245
+ }
246
+ }
247
+
248
+ // Invoke generic receipt update callback
249
+ if (callbacks?.onReceiptUpdate) {
250
+ try {
251
+ await callbacks.onReceiptUpdate(receipt, response.done)
252
+ } catch (error) {
253
+ logger.console.error(
254
+ "[trails-sdk] Error in onReceiptUpdate callback",
255
+ {
256
+ error: error instanceof Error ? error.message : String(error),
257
+ },
258
+ )
259
+ }
260
+ }
261
+
262
+ // If intent is done, stop polling
263
+ if (response.done) {
264
+ logger.console.log("[trails-sdk] Intent receipt polling complete", {
265
+ intentId,
266
+ status: receipt.status,
267
+ })
268
+ return receipt
269
+ }
270
+ } catch (error) {
271
+ logger.console.warn("[trails-sdk] Error polling intent receipt", {
272
+ intentId,
273
+ error: error instanceof Error ? error.message : String(error),
274
+ })
275
+ // Continue polling on error
276
+ }
277
+
278
+ // Wait a very short period of time before next poll.
279
+ // We do not want to wait long, as we want quick feedback to users
280
+ // on state changes.
281
+ await new Promise((resolve) => setTimeout(resolve, pollInterval))
282
+ }
283
+
284
+ logger.console.warn("[trails-sdk] Intent receipt polling timeout", {
285
+ intentId,
286
+ maxWaitTime,
287
+ })
288
+ return lastReceipt
289
+ } catch (error) {
290
+ logger.console.error(
291
+ "[trails-sdk] Unexpected error in intent receipt polling",
292
+ {
293
+ intentId,
294
+ error: error instanceof Error ? error.message : String(error),
295
+ },
296
+ )
297
+ return lastReceipt
298
+ }
299
+ }