@tetherto/wdk-wallet 1.0.0-beta.2 → 1.0.0-beta.21

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 (50) hide show
  1. package/AGENTS.md +49 -0
  2. package/README.md +2 -2
  3. package/bare.js +3 -3
  4. package/index.js +14 -2
  5. package/package.json +18 -4
  6. package/src/disposable.js +26 -0
  7. package/src/errors.js +318 -3
  8. package/src/multisig/errors.js +54 -0
  9. package/src/multisig/index.js +42 -0
  10. package/src/multisig/multisig-message-signing-read-only.js +63 -0
  11. package/src/multisig/multisig-message-signing.js +64 -0
  12. package/src/multisig/multisig-owner-management.js +94 -0
  13. package/src/multisig/wallet-account-multisig.js +144 -0
  14. package/src/multisig/wallet-account-read-only-multisig.js +98 -0
  15. package/src/protocols/bridge-protocol.js +35 -1
  16. package/src/protocols/errors.js +510 -0
  17. package/src/protocols/fiat-protocol.js +401 -0
  18. package/src/protocols/index.js +76 -0
  19. package/src/protocols/lending-protocol.js +114 -2
  20. package/src/protocols/sda-protocol.js +643 -0
  21. package/src/protocols/swap-protocol.js +36 -1
  22. package/src/protocols/swidge-protocol.js +516 -0
  23. package/src/signer.js +89 -0
  24. package/src/wallet-account-read-only-simple.js +162 -0
  25. package/src/wallet-account-read-only.js +186 -40
  26. package/src/wallet-account.js +59 -22
  27. package/src/wallet-manager.js +152 -25
  28. package/types/index.d.ts +13 -4
  29. package/types/src/disposable.d.ts +7 -0
  30. package/types/src/errors.d.ts +220 -2
  31. package/types/src/multisig/errors.d.ts +33 -0
  32. package/types/src/multisig/index.d.ts +15 -0
  33. package/types/src/multisig/multisig-message-signing-read-only.d.ts +53 -0
  34. package/types/src/multisig/multisig-message-signing.d.ts +40 -0
  35. package/types/src/multisig/multisig-owner-management.d.ts +65 -0
  36. package/types/src/multisig/wallet-account-multisig.d.ts +101 -0
  37. package/types/src/multisig/wallet-account-read-only-multisig.d.ts +83 -0
  38. package/types/src/protocols/bridge-protocol.d.ts +39 -3
  39. package/types/src/protocols/errors.d.ts +366 -0
  40. package/types/src/protocols/fiat-protocol.d.ts +409 -0
  41. package/types/src/protocols/index.d.ts +64 -0
  42. package/types/src/protocols/lending-protocol.d.ts +118 -4
  43. package/types/src/protocols/sda-protocol.d.ts +737 -0
  44. package/types/src/protocols/swap-protocol.d.ts +44 -4
  45. package/types/src/protocols/swidge-protocol.d.ts +483 -0
  46. package/types/src/signer.d.ts +53 -0
  47. package/types/src/wallet-account-read-only-simple.d.ts +144 -0
  48. package/types/src/wallet-account-read-only.d.ts +104 -27
  49. package/types/src/wallet-account.d.ts +55 -20
  50. package/types/src/wallet-manager.d.ts +109 -17
@@ -13,12 +13,21 @@
13
13
  // limitations under the License.
14
14
  'use strict'
15
15
 
16
- import { NotImplementedError } from '../errors.js'
16
+ import { NotImplementedError } from './errors.js'
17
17
 
18
18
  /** @typedef {import('../wallet-account-read-only.js').IWalletAccountReadOnly} IWalletAccountReadOnly */
19
19
 
20
20
  /** @typedef {import('../wallet-account.js').IWalletAccount} IWalletAccount */
21
21
 
22
+ /** @typedef {import('./errors.js').AccountRequiredError} AccountRequiredError */
23
+ /** @typedef {import('./errors.js').InvalidTokenError} InvalidTokenError */
24
+ /** @typedef {import('./errors.js').MaximumFeeExceededError} MaximumFeeExceededError */
25
+ /** @typedef {import('./errors.js').ReadOnlyAccountRequiredError} ReadOnlyAccountRequiredError */
26
+ /** @typedef {import('./errors.js').ProviderError} ProviderError */
27
+ /** @typedef {import('./errors.js').ProviderRequiredError} ProviderRequiredError */
28
+ /** @typedef {import('./errors.js').SwapError} SwapError */
29
+ /** @typedef {import('./errors.js').ValueError} ValueError */
30
+
22
31
  /**
23
32
  * @typedef {Object} SwapProtocolConfig
24
33
  * @property {number | bigint} [swapMaxFee] - The maximum fee amount for swap operations.
@@ -33,6 +42,7 @@ import { NotImplementedError } from '../errors.js'
33
42
  * @property {string} tokenIn - The address of the token to sell.
34
43
  * @property {string} tokenOut - The address of the token to buy.
35
44
  * @property {string} [to] - The address that will receive the output tokens. If not set, the account itself will receive the funds.
45
+ * @property {number | bigint} [minAmountOut] - The minimum acceptable amount of destination tokens to receive (in base unit).
36
46
  */
37
47
 
38
48
  /**
@@ -62,6 +72,13 @@ export class ISwapProtocol {
62
72
  *
63
73
  * @param {SwapOptions} options - The swap's options.
64
74
  * @returns {Promise<SwapResult>} The swap's result.
75
+ * @throws {AccountRequiredError} If the protocol requires a full account to perform a swap.
76
+ * @throws {ValueError} If the swap options are not valid.
77
+ * @throws {InvalidTokenError} If the input or output tokens are not valid ERC 20 token's addresses.
78
+ * @throws {ProviderRequiredError} If the method requires a provider.
79
+ * @throws {ProviderError} If the provider fails to perform the swap.
80
+ * @throws {SwapError} If the swap fails with an error.
81
+ * @throws {MaximumFeeExceededError} If the the costs of the transaction exceeds the swap max. fee option.
65
82
  */
66
83
  async swap (options) {
67
84
  throw new NotImplementedError('swap(options)')
@@ -72,6 +89,12 @@ export class ISwapProtocol {
72
89
  *
73
90
  * @param {SwapOptions} options - The swap's options.
74
91
  * @returns {Promise<Omit<SwapResult, 'hash'>>} The swap's quotes.
92
+ * @throws {ReadOnlyAccountRequiredError} If the protocol requires a read-only or full account to quote the costs of a swap.
93
+ * @throws {ValueError} If the swap options are not valid.
94
+ * @throws {InvalidTokenError} If the input or output tokens are not valid ERC 20 token's addresses.
95
+ * @throws {ProviderRequiredError} If the method requires a provider.
96
+ * @throws {ProviderError} If the provider fails to estimate the costs of the swap.
97
+ * @throws {SwapError} If the swap fails with an error.
75
98
  */
76
99
  async quoteSwap (options) {
77
100
  throw new NotImplementedError('quoteSwap(options)')
@@ -122,6 +145,13 @@ export default class SwapProtocol {
122
145
  * @abstract
123
146
  * @param {SwapOptions} options - The swap's options.
124
147
  * @returns {Promise<SwapResult>} The swap's result.
148
+ * @throws {AccountRequiredError} If the protocol requires a full account to perform a swap.
149
+ * @throws {ValueError} If the swap options are not valid.
150
+ * @throws {InvalidTokenError} If the input or output tokens are not valid ERC 20 token's addresses.
151
+ * @throws {ProviderRequiredError} If the method requires a provider.
152
+ * @throws {ProviderError} If the provider fails to perform the swap.
153
+ * @throws {SwapError} If the swap fails with an error.
154
+ * @throws {MaximumFeeExceededError} If the the costs of the transaction exceeds the swap max. fee option.
125
155
  */
126
156
  async swap (options) {
127
157
  throw new NotImplementedError('swap(options)')
@@ -133,6 +163,11 @@ export default class SwapProtocol {
133
163
  * @abstract
134
164
  * @param {SwapOptions} options - The swap's options.
135
165
  * @returns {Promise<Omit<SwapResult, 'hash'>>} The swap's quotes.
166
+ * @throws {ValueError} If the swap options are not valid.
167
+ * @throws {InvalidTokenError} If the input or output tokens are not valid ERC 20 token's addresses.
168
+ * @throws {ProviderRequiredError} If the method requires a provider.
169
+ * @throws {ProviderError} If the provider fails to estimate the costs of the swap.
170
+ * @throws {SwapError} If the swap fails with an error.
136
171
  */
137
172
  async quoteSwap (options) {
138
173
  throw new NotImplementedError('quoteSwap(options)')
@@ -0,0 +1,516 @@
1
+ // Copyright 2024 Tether Operations Limited
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ 'use strict'
15
+
16
+ import { BridgeError, NotImplementedError, SwapError, SwidgeError } from './errors.js'
17
+
18
+ /** @typedef {import('../wallet-account-read-only.js').IWalletAccountReadOnly} IWalletAccountReadOnly */
19
+
20
+ /** @typedef {import('../wallet-account.js').IWalletAccount} IWalletAccount */
21
+
22
+ /** @typedef {import('./swap-protocol.js').ISwapProtocol} ISwapProtocol */
23
+ /** @typedef {import('./swap-protocol.js').SwapOptions} SwapOptions */
24
+ /** @typedef {import('./swap-protocol.js').SwapResult} SwapResult */
25
+
26
+ /** @typedef {import('./bridge-protocol.js').IBridgeProtocol} IBridgeProtocol */
27
+ /** @typedef {import('./bridge-protocol.js').BridgeOptions} BridgeOptions */
28
+ /** @typedef {import('./bridge-protocol.js').BridgeResult} BridgeResult */
29
+
30
+ /** @typedef {import('./errors.js').AccountRequiredError} AccountRequiredError */
31
+ /** @typedef {import('./errors.js').InvalidTokenError} InvalidTokenError */
32
+ /** @typedef {import('./errors.js').MaximumFeeExceededError} MaximumFeeExceededError */
33
+ /** @typedef {import('./errors.js').NoSuchElementError} NoSuchElementError */
34
+ /** @typedef {import('./errors.js').ReadOnlyAccountRequiredError} ReadOnlyAccountRequiredError */
35
+ /** @typedef {import('./errors.js').ProviderError} ProviderError */
36
+ /** @typedef {import('./errors.js').ProviderRequiredError} ProviderRequiredError */
37
+ /** @typedef {import('./errors.js').ValueError} ValueError */
38
+
39
+ /**
40
+ * @typedef {'pending' | 'action-required' | 'completed' | 'failed'
41
+ * | 'refund-pending' | 'refunded' | 'cancelled' | 'expired' | 'partial'} SwidgeStatus
42
+ */
43
+
44
+ /**
45
+ * @typedef {'network' | 'protocol' | 'affiliate' | 'other'} SwidgeFeeType
46
+ */
47
+
48
+ /**
49
+ * @typedef {Object} SwidgeProtocolConfig
50
+ * @property {number | bigint} [maxNetworkFeeBps] - Maximum acceptable network fee in basis points of the input amount.
51
+ * @property {number | bigint} [maxProtocolFeeBps] - Maximum acceptable protocol fee in basis points of the input amount.
52
+ */
53
+
54
+ /**
55
+ * @typedef {SwidgeCommonOptions & (SwidgeExactInOptions | SwidgeExactOutOptions)} SwidgeOptions
56
+ */
57
+
58
+ /**
59
+ * @typedef {Object} SwidgeCommonOptions
60
+ * @property {string} fromToken - The provider-specific identifier or address of the source token.
61
+ * @property {string} toToken - The provider-specific identifier or address of the destination token.
62
+ * @property {string | number} [toChain] - The identifier of the destination chain. If omitted, defaults to the source chain (same-chain swap).
63
+ * @property {string} [recipient] - The address that will receive the output tokens.
64
+ * @property {string} [refundAddress] - The address that will receive refunds if the tx cannot complete.
65
+ * @property {number} [slippage] - The maximum acceptable slippage as a decimal (e.g., 0.01 for 1%).
66
+ * @property {number | bigint} [minAmountOut] - The minimum acceptable amount of destination tokens to receive (in base unit).
67
+ */
68
+
69
+ /**
70
+ * @typedef {Object} SwidgeExactInOptions
71
+ * @property {number | bigint} fromTokenAmount - The exact amount of source tokens to spend (in base unit).
72
+ * @property {never} [toTokenAmount] - Not applicable for exact-in operations.
73
+ */
74
+
75
+ /**
76
+ * @typedef {Object} SwidgeExactOutOptions
77
+ * @property {never} [fromTokenAmount] - Not applicable for exact-out operations.
78
+ * @property {number | bigint} toTokenAmount - The exact amount of destination tokens to receive (in base unit).
79
+ */
80
+
81
+ /**
82
+ * @typedef {Object} SwidgeFee
83
+ * @property {SwidgeFeeType} type - The category of the fee.
84
+ * @property {bigint} amount - The fee amount in base units.
85
+ * @property {string} token - The token in which the fee is denominated.
86
+ * @property {string | number} [chain] - The chain on which the fee is charged.
87
+ * @property {boolean} [included] - Whether the fee is already included in the quoted amounts.
88
+ * @property {string} [description] - A human-readable description of the fee.
89
+ */
90
+
91
+ /**
92
+ * @typedef {Object} SwidgeTransaction
93
+ * @property {string} hash - The transaction hash.
94
+ * @property {string | number} [chain] - The chain on which the transaction occurred.
95
+ * @property {'source' | 'destination' | 'approval' | 'refund' | 'other'} [type] - The role of the transaction within the swidge.
96
+ */
97
+
98
+ /**
99
+ * Non-binding quote for a swidge operation.
100
+ * Providers that internally decompose the operation into multiple sequential transactions
101
+ * should encapsulate that decomposition behind a single quote.
102
+ *
103
+ * @typedef {Object} SwidgeQuote
104
+ * @property {bigint} fromTokenAmount - The amount of source tokens to spend.
105
+ * @property {bigint} toTokenAmount - The estimated amount of destination tokens to receive.
106
+ * @property {bigint} toTokenAmountMin - The minimum guaranteed amount after slippage.
107
+ * @property {SwidgeFee[]} fees - Itemised fee breakdown.
108
+ * @property {number} [estimatedDuration] - Estimated duration in seconds.
109
+ * @property {number} [expiry] - Unix timestamp (seconds) at which the quote expires.
110
+ * @property {number} [priceImpact] - Provider-reported estimated price impact as a decimal (e.g., 0.01 for 1%).
111
+ */
112
+
113
+ /**
114
+ * @typedef {Object} SwidgeResult
115
+ * @property {string} id - The unique swidge execution identifier.
116
+ * @property {string} [hash] - The primary transaction hash (if available immediately).
117
+ * @property {SwidgeFee[]} fees - Itemised fee breakdown.
118
+ * @property {SwidgeTransaction[]} [transactions] - Transactions produced by the swidge execution.
119
+ * @property {bigint} fromTokenAmount - The actual amount of source tokens spent.
120
+ * @property {bigint} toTokenAmount - The actual or expected amount of destination tokens.
121
+ * @property {bigint} [toTokenAmountMin] - The minimum guaranteed amount after slippage.
122
+ */
123
+
124
+ /**
125
+ * @typedef {Object} SwidgeStatusOptions
126
+ * @property {string | number} [fromChain] - The source chain identifier.
127
+ * @property {string | number} [toChain] - The destination chain identifier.
128
+ */
129
+
130
+ /**
131
+ * @typedef {Object} SwidgeStatusResult
132
+ * @property {SwidgeStatus} status - The current status of the swidge.
133
+ * @property {SwidgeTransaction[]} [transactions] - Transactions associated with the swidge.
134
+ */
135
+
136
+ /**
137
+ * @typedef {Object} SwidgeSupportedChain
138
+ * @property {string | number} id - The provider-specific chain identifier.
139
+ * @property {string} name - The human-readable chain name.
140
+ * @property {string} type - The chain or virtual machine type (e.g., 'evm', 'svm', 'utxo').
141
+ * @property {string} nativeToken - The symbol of the chain's native token.
142
+ */
143
+
144
+ /**
145
+ * @typedef {Object} SwidgeSupportedToken
146
+ * @property {string} token - The provider-specific token identifier to use in swidge operations.
147
+ * @property {string | number} chain - The chain on which the token is available.
148
+ * @property {string} symbol - The token symbol.
149
+ * @property {number} decimals - The number of decimal places for the token's base unit.
150
+ * @property {string} [address] - The token contract address, if applicable.
151
+ * @property {string} [name] - The token's full name.
152
+ */
153
+
154
+ /**
155
+ * @typedef {Object} SwidgeSupportedTokensOptions
156
+ * @property {string | number} [fromChain] - The optional source chain for route-scoped discovery.
157
+ * @property {string} [fromToken] - The optional source token for route-scoped discovery.
158
+ * @property {string | number} [toChain] - The optional destination chain for route-scoped discovery.
159
+ */
160
+
161
+ /**
162
+ * @interface
163
+ * @extends {ISwapProtocol}
164
+ * @extends {IBridgeProtocol}
165
+ */
166
+ export class ISwidgeProtocol {
167
+ /**
168
+ * Quotes the estimated costs and output of a cross-chain swap/bridge operation.
169
+ * Returns a non-binding quote; the actual execution is performed
170
+ * by {@link swidge}.
171
+ *
172
+ * @param {SwidgeOptions} options - The swidge options.
173
+ * @returns {Promise<SwidgeQuote>} The quoted swidge details.
174
+ * @throws {ReadOnlyAccountRequiredError} If the protocol requires a read-only or full account to quote the costs of a swidge.
175
+ * @throws {ValueError} If the swidge options are not valid.
176
+ * @throws {InvalidTokenError} If the from or to tokens are not valid ERC 20 token's addresses.
177
+ * @throws {ProviderRequiredError} If the method requires a provider.
178
+ * @throws {ProviderError} If the provider fails to perform the swidge.
179
+ * @throws {SwidgeError} If the swidge fails with an error.
180
+ */
181
+ async quoteSwidge (options) {
182
+ throw new NotImplementedError('quoteSwidge(options)')
183
+ }
184
+
185
+ /**
186
+ * Executes a swidge operation.
187
+ *
188
+ * @param {SwidgeOptions} options - The swidge options.
189
+ * @param {SwidgeProtocolConfig} [config] - Optional provider-specific execution configuration.
190
+ * @returns {Promise<SwidgeResult>} The swidge execution result.
191
+ * @throws {AccountRequiredError} If the protocol requires a full account to perform a swidge.
192
+ * @throws {ValueError} If the swidge options are not valid.
193
+ * @throws {InvalidTokenError} If the from or to tokens are not valid ERC 20 token's addresses.
194
+ * @throws {ProviderRequiredError} If the method requires a provider.
195
+ * @throws {ProviderError} If the provider fails to perform the swidge.
196
+ * @throws {SwidgeError} If the swidge fails with an error.
197
+ * @throws {MaximumFeeExceededError} If the the costs of the transaction exceeds the max. network or protocol fee options.
198
+ */
199
+ async swidge (options, config) {
200
+ throw new NotImplementedError('swidge(options, config)')
201
+ }
202
+
203
+ /**
204
+ * Retrieves the current status of an in-flight swidge.
205
+ *
206
+ * @param {string} id - The swidge execution identifier returned by swidge.
207
+ * @param {SwidgeStatusOptions} [options] - Optional hints to assist provider lookups.
208
+ * @returns {Promise<SwidgeStatusResult>} The current swidge status.
209
+ * @throws {ValueError} If the id is not valid.
210
+ * @throws {NoSuchElementError} If no swidge exists for the given id.
211
+ * @throws {ProviderRequiredError} If the method requires a provider.
212
+ * @throws {ProviderError} If the provider fails to fetch the swidge's status.
213
+ */
214
+ async getSwidgeStatus (id, options) {
215
+ throw new NotImplementedError('getSwidgeStatus(id, options)')
216
+ }
217
+
218
+ /**
219
+ * Retrieves the chains supported by the provider for swidge operations.
220
+ *
221
+ * @returns {Promise<SwidgeSupportedChain[]>} The supported chains.
222
+ * @throws {ProviderRequiredError} If the method requires a provider.
223
+ * @throws {ProviderError} If the provider fails to fetch the available blockchains.
224
+ */
225
+ async getSupportedChains () {
226
+ throw new NotImplementedError('getSupportedChains()')
227
+ }
228
+
229
+ /**
230
+ * Retrieves the tokens supported by the provider for swidge operations.
231
+ *
232
+ * @param {SwidgeSupportedTokensOptions} [options] - Optional filters for chain- or route-scoped token discovery.
233
+ * @returns {Promise<SwidgeSupportedToken[]>} The supported tokens.
234
+ * @throws {ProviderRequiredError} If the method requires a provider.
235
+ * @throws {ProviderError} If the provider fails to fetch the available tokens.
236
+ */
237
+ async getSupportedTokens (options) {
238
+ throw new NotImplementedError('getSupportedTokens(options)')
239
+ }
240
+ }
241
+
242
+ /**
243
+ * @abstract
244
+ * @implements {ISwidgeProtocol}
245
+ * @implements {ISwapProtocol}
246
+ * @implements {IBridgeProtocol}
247
+ */
248
+ export default class SwidgeProtocol {
249
+ /**
250
+ * Creates a new swidge protocol without binding it to a wallet account.
251
+ *
252
+ * @overload
253
+ * @param {undefined} [account] - The wallet account to use to interact with the protocol.
254
+ * @param {SwidgeProtocolConfig} [config] - The swidge protocol configuration.
255
+ */
256
+
257
+ /**
258
+ * Creates a new read-only swidge protocol.
259
+ *
260
+ * @overload
261
+ * @param {IWalletAccountReadOnly} account - The wallet account to use to interact with the protocol.
262
+ * @param {SwidgeProtocolConfig} [config] - The swidge protocol configuration.
263
+ */
264
+
265
+ /**
266
+ * Creates a new swidge protocol.
267
+ *
268
+ * @overload
269
+ * @param {IWalletAccount} account - The wallet account to use to interact with the protocol.
270
+ * @param {SwidgeProtocolConfig} [config] - The swidge protocol configuration.
271
+ */
272
+ constructor (account, config = {}) {
273
+ /**
274
+ * The wallet account to use to interact with the protocol.
275
+ *
276
+ * @protected
277
+ * @type {IWalletAccountReadOnly | IWalletAccount | undefined}
278
+ */
279
+ this._account = account
280
+
281
+ /**
282
+ * The swidge protocol configuration.
283
+ *
284
+ * @protected
285
+ * @type {SwidgeProtocolConfig}
286
+ */
287
+ this._config = config
288
+ }
289
+
290
+ /**
291
+ * Swaps a pair of tokens by delegating to {@link swidge}.
292
+ *
293
+ * @param {SwapOptions} options - The swap's options.
294
+ * @returns {Promise<SwapResult>} The swap's result.
295
+ * @throws {AccountRequiredError} If the protocol requires a full account to perform a swap.
296
+ * @throws {ValueError} If the swap options are not valid.
297
+ * @throws {InvalidTokenError} If the input or output tokens are not valid ERC 20 token's addresses.
298
+ * @throws {ProviderRequiredError} If the method requires a provider.
299
+ * @throws {ProviderError} If the provider fails to perform the swap.
300
+ * @throws {SwapError} If the swap fails with an error.
301
+ * @throws {MaximumFeeExceededError} If the the costs of the transaction exceeds the max. network or protocol fee options.
302
+ */
303
+ async swap (options) {
304
+ try {
305
+ const result = await this.swidge({
306
+ fromToken: options.tokenIn,
307
+ toToken: options.tokenOut,
308
+ recipient: options.to,
309
+ fromTokenAmount: options.tokenInAmount,
310
+ toTokenAmount: options.tokenOutAmount,
311
+ minAmountOut: options.minAmountOut
312
+ })
313
+
314
+ const fee = result.fees.reduce((acc, f) => acc + f.amount, 0n)
315
+
316
+ return { hash: result.id, fee, tokenInAmount: result.fromTokenAmount, tokenOutAmount: result.toTokenAmount }
317
+ } catch (error) {
318
+ if (error instanceof SwidgeError) {
319
+ throw new SwapError(error.message, { reason: error.reason, cause: error })
320
+ }
321
+
322
+ throw error
323
+ }
324
+ }
325
+
326
+ /**
327
+ * Quotes the costs of a swap operation by delegating to {@link quoteSwidge}.
328
+ *
329
+ * @param {SwapOptions} options - The swap's options.
330
+ * @returns {Promise<Omit<SwapResult, 'hash'>>} The swap's quotes.
331
+ * @throws {ReadOnlyAccountRequiredError} If the protocol requires a read-only or full account to quote the costs of a swap.
332
+ * @throws {ValueError} If the swap options are not valid.
333
+ * @throws {InvalidTokenError} If the input or output tokens are not valid ERC 20 token's addresses.
334
+ * @throws {ProviderRequiredError} If the method requires a provider.
335
+ * @throws {ProviderError} If the provider fails to estimate the costs of the swap.
336
+ * @throws {SwapError} If the swap fails with an error.
337
+ */
338
+ async quoteSwap (options) {
339
+ try {
340
+ const result = await this.quoteSwidge({
341
+ fromToken: options.tokenIn,
342
+ toToken: options.tokenOut,
343
+ recipient: options.to,
344
+ fromTokenAmount: options.tokenInAmount,
345
+ toTokenAmount: options.tokenOutAmount,
346
+ minAmountOut: options.minAmountOut
347
+ })
348
+
349
+ const fee = result.fees.reduce((acc, f) => acc + f.amount, 0n)
350
+
351
+ return { fee, tokenInAmount: result.fromTokenAmount, tokenOutAmount: result.toTokenAmount }
352
+ } catch (error) {
353
+ if (error instanceof SwidgeError) {
354
+ throw new SwapError(error.message, { reason: error.reason, cause: error })
355
+ }
356
+
357
+ throw error
358
+ }
359
+ }
360
+
361
+ /**
362
+ * Bridges a token to a different blockchain by delegating to {@link swidge}.
363
+ *
364
+ * @param {BridgeOptions} options - The bridge's options.
365
+ * @returns {Promise<BridgeResult>} The bridge's result.
366
+ * @throws {AccountRequiredError} If the protocol requires a full account to perform a bridge.
367
+ * @throws {ValueError} If the bridge options are not valid.
368
+ * @throws {InvalidTokenError} If the token is not a valid ERC 20 token's address.
369
+ * @throws {ProviderRequiredError} If the method requires a provider.
370
+ * @throws {ProviderError} If the provider fails to perform the bridge.
371
+ * @throws {BridgeError} If the bridge fails with an error.
372
+ * @throws {MaximumFeeExceededError} If the the costs of the transaction exceeds the max. network or protocol fee options.
373
+ */
374
+ async bridge (options) {
375
+ try {
376
+ const { id: hash, fees } = await this.swidge({
377
+ fromToken: options.token,
378
+ toToken: options.token,
379
+ toChain: options.targetChain,
380
+ recipient: options.recipient,
381
+ fromTokenAmount: options.amount
382
+ })
383
+
384
+ const fee = fees.filter(f => f.type === 'network')
385
+ .reduce((acc, { amount }) => acc + amount, 0n)
386
+
387
+ const bridgeFee = fees.filter(f => f.type === 'protocol')
388
+ .reduce((acc, { amount }) => acc + amount, 0n)
389
+
390
+ return { hash, fee, bridgeFee }
391
+ } catch (error) {
392
+ if (error instanceof SwidgeError) {
393
+ throw new BridgeError(error.message, { reason: error.reason, cause: error })
394
+ }
395
+
396
+ throw error
397
+ }
398
+ }
399
+
400
+ /**
401
+ * Quotes the costs of a bridge operation by delegating to {@link quoteSwidge}.
402
+ *
403
+ * @param {BridgeOptions} options - The bridge's options.
404
+ * @returns {Promise<Omit<BridgeResult, 'hash'>>} The bridge's quotes.
405
+ * @throws {ReadOnlyAccountRequiredError} If the protocol requires a read-only or full account to quote the costs of a swap.
406
+ * @throws {ValueError} If the bridge options are not valid.
407
+ * @throws {InvalidTokenError} If the token is not a valid ERC 20 token's address.
408
+ * @throws {ProviderRequiredError} If the method requires a provider.
409
+ * @throws {ProviderError} If the provider fails to perform the bridge.
410
+ * @throws {BridgeError} If the bridge fails with an error.
411
+ */
412
+ async quoteBridge (options) {
413
+ try {
414
+ const { fees } = await this.quoteSwidge({
415
+ fromToken: options.token,
416
+ toToken: options.token,
417
+ toChain: options.targetChain,
418
+ recipient: options.recipient,
419
+ fromTokenAmount: options.amount
420
+ })
421
+
422
+ const fee = fees.filter(f => f.type === 'network')
423
+ .reduce((acc, { amount }) => acc + amount, 0n)
424
+
425
+ const bridgeFee = fees.filter(f => f.type === 'protocol')
426
+ .reduce((acc, { amount }) => acc + amount, 0n)
427
+
428
+ return { fee, bridgeFee }
429
+ } catch (error) {
430
+ if (error instanceof SwidgeError) {
431
+ throw new BridgeError(error.message, { reason: error.reason, cause: error })
432
+ }
433
+
434
+ throw error
435
+ }
436
+ }
437
+
438
+ /**
439
+ * Quotes the estimated costs and output of a swidge operation.
440
+ * Returns a non-binding quote; the actual execution is performed
441
+ * by {@link swidge}.
442
+ *
443
+ * @abstract
444
+ * @param {SwidgeOptions} options - The swidge options.
445
+ * @returns {Promise<SwidgeQuote>} The quoted swidge details.
446
+ * @throws {ReadOnlyAccountRequiredError} If the protocol requires a read-only or full account to quote the costs of a swidge.
447
+ * @throws {ValueError} If the swidge options are not valid.
448
+ * @throws {InvalidTokenError} If the from or to tokens are not valid ERC 20 token's addresses.
449
+ * @throws {ProviderRequiredError} If the method requires a provider.
450
+ * @throws {ProviderError} If the provider fails to perform the swidge.
451
+ * @throws {SwidgeError} If the swidge fails with an error.
452
+ */
453
+ async quoteSwidge (options) {
454
+ throw new NotImplementedError('quoteSwidge(options)')
455
+ }
456
+
457
+ /**
458
+ * Executes a swidge operation.
459
+ *
460
+ * @abstract
461
+ * @param {SwidgeOptions} options - The swidge options.
462
+ * @param {SwidgeProtocolConfig} [config] - Optional provider-specific execution configuration.
463
+ * @returns {Promise<SwidgeResult>} The swidge execution result.
464
+ * @throws {AccountRequiredError} If the protocol requires a full account to perform a swidge.
465
+ * @throws {ValueError} If the swidge options are not valid.
466
+ * @throws {InvalidTokenError} If the from or to tokens are not valid ERC 20 token's addresses.
467
+ * @throws {ProviderRequiredError} If the method requires a provider.
468
+ * @throws {ProviderError} If the provider fails to perform the swidge.
469
+ * @throws {SwidgeError} If the swidge fails with an error.
470
+ * @throws {MaximumFeeExceededError} If the the costs of the transaction exceeds the max. network or protocol fee options.
471
+ */
472
+ async swidge (options, config) {
473
+ throw new NotImplementedError('swidge(options, config)')
474
+ }
475
+
476
+ /**
477
+ * Retrieves the current status of an in-flight swidge.
478
+ *
479
+ * @abstract
480
+ * @param {string} id - The swidge execution identifier returned by swidge.
481
+ * @param {SwidgeStatusOptions} [options] - Optional hints to assist provider lookups.
482
+ * @returns {Promise<SwidgeStatusResult>} The current swidge status.
483
+ * @throws {ValueError} If the id is not valid.
484
+ * @throws {NoSuchElementError} If no swidge exists for the given id.
485
+ * @throws {ProviderRequiredError} If the method requires a provider.
486
+ * @throws {ProviderError} If the provider fails to fetch the swidge's status.
487
+ */
488
+ async getSwidgeStatus (id, options) {
489
+ throw new NotImplementedError('getSwidgeStatus(id, options)')
490
+ }
491
+
492
+ /**
493
+ * Retrieves the chains supported by the provider for swidge operations.
494
+ *
495
+ * @abstract
496
+ * @returns {Promise<SwidgeSupportedChain[]>} The supported chains.
497
+ * @throws {ProviderRequiredError} If the method requires a provider.
498
+ * @throws {ProviderError} If the provider fails to fetch the available blockchains.
499
+ */
500
+ async getSupportedChains () {
501
+ throw new NotImplementedError('getSupportedChains()')
502
+ }
503
+
504
+ /**
505
+ * Retrieves the tokens supported by the provider for swidge operations.
506
+ *
507
+ * @abstract
508
+ * @param {SwidgeSupportedTokensOptions} [options] - Optional filters for chain- or route-scoped token discovery.
509
+ * @returns {Promise<SwidgeSupportedToken[]>} The supported tokens.
510
+ * @throws {ProviderRequiredError} If the method requires a provider.
511
+ * @throws {ProviderError} If the provider fails to fetch the available tokens.
512
+ */
513
+ async getSupportedTokens (options) {
514
+ throw new NotImplementedError('getSupportedTokens(options)')
515
+ }
516
+ }
package/src/signer.js ADDED
@@ -0,0 +1,89 @@
1
+ // Copyright 2024 Tether Operations Limited
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ 'use strict'
15
+
16
+ import { NotImplementedError } from './errors.js'
17
+
18
+ import { IDisposable } from './disposable.js'
19
+
20
+ /** @typedef {import('./wallet-account.js').KeyPair} KeyPair */
21
+ /** @typedef {import('./errors.js').UnsupportedOperationError} UnsupportedOperationError */
22
+ /** @typedef {import('./errors.js').ValueError} ValueError */
23
+
24
+ /**
25
+ * A minimal, cross-chain signer interface.
26
+ *
27
+ * @interface
28
+ */
29
+ export class ISigner extends IDisposable {
30
+ /**
31
+ * Whether the signer supports account derivation via {@link derive}.
32
+ *
33
+ * @type {boolean}
34
+ */
35
+ get isDerivable () {
36
+ throw new NotImplementedError('isDerivable')
37
+ }
38
+
39
+ /**
40
+ * The signer's key pair, or null if the signer does not allow retrieving
41
+ * key material (e.g. hardware signers).
42
+ *
43
+ * @type {KeyPair | null}
44
+ */
45
+ get keyPair () {
46
+ throw new NotImplementedError('keyPair')
47
+ }
48
+
49
+ /**
50
+ * The BIP 0032 derivation path, or null if the signer is not bound to a derivation position
51
+ * (e.g. private-key signers).
52
+ *
53
+ * @type {string | null}
54
+ */
55
+ get path () {
56
+ throw new NotImplementedError('path')
57
+ }
58
+
59
+ /**
60
+ * Derive a child signer using a relative path (e.g., "0'/0/0").
61
+ *
62
+ * @param {string} path - The relative derivation path.
63
+ * @returns {Promise<ISigner>} The derived signer.
64
+ * @throws {UnsupportedOperationError} If the signer does not support account derivation.
65
+ * @throws {ValueError} If the path is not valid.
66
+ */
67
+ async derive (path) {
68
+ throw new NotImplementedError('derive(path)')
69
+ }
70
+
71
+ /**
72
+ * Returns the signer's address.
73
+ *
74
+ * @returns {Promise<string>} The address.
75
+ */
76
+ async getAddress () {
77
+ throw new NotImplementedError('getAddress()')
78
+ }
79
+
80
+ /**
81
+ * Signs a message.
82
+ *
83
+ * @param {string} message - The message to sign.
84
+ * @returns {Promise<string>} The message's signature.
85
+ */
86
+ async sign (message) {
87
+ throw new NotImplementedError('sign(message)')
88
+ }
89
+ }