@defuse-protocol/intents-sdk 0.14.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.
package/dist/index.cjs ADDED
@@ -0,0 +1,2069 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ AssertionError: () => import_internal_utils21.AssertionError,
34
+ BaseError: () => import_internal_utils18.BaseError,
35
+ BridgeNameEnum: () => BridgeNameEnum,
36
+ Chains: () => Chains,
37
+ FeeExceedsAmountError: () => FeeExceedsAmountError,
38
+ HotWithdrawalCancelledError: () => HotWithdrawalCancelledError,
39
+ HotWithdrawalNotFoundError: () => HotWithdrawalNotFoundError,
40
+ HotWithdrawalPendingError: () => HotWithdrawalPendingError,
41
+ HttpRequestError: () => import_internal_utils20.HttpRequestError,
42
+ IntentSettlementError: () => import_internal_utils22.IntentSettlementError,
43
+ IntentsSDK: () => IntentsSDK,
44
+ MinWithdrawalAmountError: () => MinWithdrawalAmountError,
45
+ PoaWithdrawalInvariantError: () => import_internal_utils19.PoaWithdrawalInvariantError,
46
+ PoaWithdrawalNotFoundError: () => import_internal_utils19.PoaWithdrawalNotFoundError,
47
+ PoaWithdrawalPendingError: () => import_internal_utils19.PoaWithdrawalPendingError,
48
+ QuoteError: () => import_internal_utils22.QuoteError,
49
+ RelayPublishError: () => import_internal_utils22.RelayPublishError,
50
+ RouteEnum: () => RouteEnum,
51
+ RpcRequestError: () => import_internal_utils20.RpcRequestError,
52
+ TimeoutError: () => import_internal_utils20.TimeoutError,
53
+ TrustlineNotFoundError: () => TrustlineNotFoundError,
54
+ UnsupportedDestinationMemoError: () => UnsupportedDestinationMemoError,
55
+ createDefaultRoute: () => createDefaultRoute,
56
+ createHotBridgeRoute: () => createHotBridgeRoute,
57
+ createIntentSignerNEP413: () => createIntentSignerNEP413,
58
+ createIntentSignerNearKeyPair: () => createIntentSignerNearKeyPair,
59
+ createIntentSignerViem: () => createIntentSignerViem,
60
+ createInternalTransferRoute: () => createInternalTransferRoute,
61
+ createNearWithdrawalRoute: () => createNearWithdrawalRoute,
62
+ createPoaBridgeRoute: () => createPoaBridgeRoute,
63
+ createVirtualChainRoute: () => createVirtualChainRoute
64
+ });
65
+ module.exports = __toCommonJS(index_exports);
66
+
67
+ // src/sdk.ts
68
+ var import_internal_utils16 = require("@defuse-protocol/internal-utils");
69
+ var import_omni_sdk3 = __toESM(require("@hot-labs/omni-sdk"), 1);
70
+ var import_viem2 = require("viem");
71
+
72
+ // src/bridges/aurora-engine-bridge/aurora-engine-bridge.ts
73
+ var import_internal_utils2 = require("@defuse-protocol/internal-utils");
74
+
75
+ // src/constants/route-enum.ts
76
+ var RouteEnum = {
77
+ HotBridge: "hot_bridge",
78
+ PoaBridge: "poa_bridge",
79
+ NearWithdrawal: "near_withdrawal",
80
+ VirtualChain: "virtual_chain",
81
+ InternalTransfer: "internal_transfer"
82
+ };
83
+
84
+ // src/bridges/aurora-engine-bridge/aurora-engine-bridge-constants.ts
85
+ var NEAR_NATIVE_ASSET_ID = "nep141:wrap.near";
86
+
87
+ // src/bridges/aurora-engine-bridge/aurora-engine-bridge-utils.ts
88
+ var import_internal_utils = require("@defuse-protocol/internal-utils");
89
+ var import_viem = require("viem");
90
+ function createWithdrawIntentPrimitive(params) {
91
+ const { contractId: tokenAccountId, standard } = import_internal_utils.utils.parseDefuseAssetId(
92
+ params.assetId
93
+ );
94
+ (0, import_internal_utils.assert)(standard === "nep141", "Only NEP-141 is supported");
95
+ if (params.proxyTokenContractId == null) {
96
+ return {
97
+ intent: "ft_withdraw",
98
+ token: tokenAccountId,
99
+ receiver_id: params.auroraEngineContractId,
100
+ amount: params.amount.toString(),
101
+ msg: makeAuroraEngineDepositMsg(params.destinationAddress),
102
+ storage_deposit: params.storageDeposit > 0n ? params.storageDeposit.toString() : null
103
+ };
104
+ }
105
+ return {
106
+ intent: "ft_withdraw",
107
+ token: tokenAccountId,
108
+ receiver_id: params.proxyTokenContractId,
109
+ amount: params.amount.toString(),
110
+ msg: `${params.auroraEngineContractId}:${makeAuroraEngineDepositMsg(params.destinationAddress)}`,
111
+ storage_deposit: params.storageDeposit > 0n ? params.storageDeposit.toString() : null
112
+ };
113
+ }
114
+ function makeAuroraEngineDepositMsg(recipientAddress) {
115
+ const parsedRecipientAddress = (0, import_viem.getAddress)(recipientAddress);
116
+ return parsedRecipientAddress.slice(2).toLowerCase();
117
+ }
118
+ function withdrawalParamsInvariant(params) {
119
+ (0, import_internal_utils.assert)(params.routeConfig != null, "Bridge config is required");
120
+ (0, import_internal_utils.assert)(
121
+ params.routeConfig.route === RouteEnum.VirtualChain,
122
+ "Bridge is not aurora_engine"
123
+ );
124
+ }
125
+
126
+ // src/bridges/aurora-engine-bridge/aurora-engine-bridge.ts
127
+ var AuroraEngineBridge = class {
128
+ constructor({
129
+ env,
130
+ nearProvider
131
+ }) {
132
+ this.env = env;
133
+ this.nearProvider = nearProvider;
134
+ }
135
+ is(routeConfig) {
136
+ return routeConfig.route === RouteEnum.VirtualChain;
137
+ }
138
+ supports(params) {
139
+ if ("routeConfig" in params && params.routeConfig != null) {
140
+ return this.is(params.routeConfig);
141
+ }
142
+ return false;
143
+ }
144
+ parseAssetId() {
145
+ return null;
146
+ }
147
+ createWithdrawalIntents(args) {
148
+ withdrawalParamsInvariant(args.withdrawalParams);
149
+ const intents = [];
150
+ if (args.feeEstimation.quote != null) {
151
+ intents.push({
152
+ intent: "token_diff",
153
+ diff: {
154
+ [args.feeEstimation.quote.defuse_asset_identifier_in]: `-${args.feeEstimation.quote.amount_in}`,
155
+ [args.feeEstimation.quote.defuse_asset_identifier_out]: args.feeEstimation.quote.amount_out
156
+ },
157
+ referral: args.referral
158
+ });
159
+ }
160
+ const intent = createWithdrawIntentPrimitive({
161
+ assetId: args.withdrawalParams.assetId,
162
+ auroraEngineContractId: args.withdrawalParams.routeConfig.auroraEngineContractId,
163
+ proxyTokenContractId: args.withdrawalParams.routeConfig.proxyTokenContractId,
164
+ destinationAddress: args.withdrawalParams.destinationAddress,
165
+ amount: args.withdrawalParams.amount,
166
+ storageDeposit: args.feeEstimation.quote ? BigInt(args.feeEstimation.quote.amount_out) : args.feeEstimation.amount
167
+ });
168
+ intents.push(intent);
169
+ return Promise.resolve(intents);
170
+ }
171
+ /**
172
+ * Aurora Engine bridge doesn't have withdrawal restrictions.
173
+ */
174
+ async validateWithdrawal(_args) {
175
+ return;
176
+ }
177
+ async estimateWithdrawalFee(args) {
178
+ withdrawalParamsInvariant(args.withdrawalParams);
179
+ const { contractId: tokenAccountId, standard } = import_internal_utils2.utils.parseDefuseAssetId(
180
+ args.withdrawalParams.assetId
181
+ );
182
+ (0, import_internal_utils2.assert)(standard === "nep141", "Only NEP-141 is supported");
183
+ const [minStorageBalance, userStorageBalance] = await Promise.all([
184
+ (0, import_internal_utils2.getNearNep141MinStorageBalance)({
185
+ contractId: tokenAccountId,
186
+ nearProvider: this.nearProvider
187
+ }),
188
+ (0, import_internal_utils2.getNearNep141StorageBalance)({
189
+ contractId: tokenAccountId,
190
+ accountId: args.withdrawalParams.routeConfig.auroraEngineContractId,
191
+ nearProvider: this.nearProvider
192
+ })
193
+ ]);
194
+ if (minStorageBalance <= userStorageBalance) {
195
+ return {
196
+ amount: 0n,
197
+ quote: null
198
+ };
199
+ }
200
+ const feeAssetId = NEAR_NATIVE_ASSET_ID;
201
+ const feeAmount = minStorageBalance - userStorageBalance;
202
+ const feeQuote = args.withdrawalParams.assetId === feeAssetId ? null : await import_internal_utils2.solverRelay.getQuote({
203
+ quoteParams: {
204
+ defuse_asset_identifier_in: args.withdrawalParams.assetId,
205
+ defuse_asset_identifier_out: feeAssetId,
206
+ exact_amount_out: feeAmount.toString(),
207
+ wait_ms: args.quoteOptions?.waitMs
208
+ },
209
+ config: {
210
+ baseURL: import_internal_utils2.configsByEnvironment[this.env].solverRelayBaseURL,
211
+ logBalanceSufficient: false,
212
+ logger: args.logger
213
+ }
214
+ });
215
+ return {
216
+ amount: feeQuote ? BigInt(feeQuote.amount_in) : feeAmount,
217
+ quote: feeQuote
218
+ };
219
+ }
220
+ async waitForWithdrawalCompletion(_args) {
221
+ return { hash: null };
222
+ }
223
+ };
224
+
225
+ // src/bridges/direct-bridge/direct-bridge.ts
226
+ var import_internal_utils5 = require("@defuse-protocol/internal-utils");
227
+
228
+ // src/constants/bridge-name-enum.ts
229
+ var BridgeNameEnum = {
230
+ Hot: "hot",
231
+ Poa: "poa",
232
+ None: null
233
+ };
234
+
235
+ // src/lib/caip2.ts
236
+ var import_internal_utils3 = require("@defuse-protocol/internal-utils");
237
+ var Chains = {
238
+ Bitcoin: "bip122:000000000019d6689c085ae165831e93",
239
+ Ethereum: "eip155:1",
240
+ Base: "eip155:8453",
241
+ Arbitrum: "eip155:42161",
242
+ BNB: "eip155:56",
243
+ Polygon: "eip155:137",
244
+ Near: "near:mainnet",
245
+ Solana: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
246
+ Tron: "tron:27Lqcw",
247
+ Gnosis: "eip155:100",
248
+ XRPL: "xrpl:0",
249
+ Dogecoin: "bip122:1a91e3dace36e2be3bf030a65679fe82",
250
+ Zcash: "zcash:0",
251
+ Berachain: "eip155:80085",
252
+ TON: "tvm:-239",
253
+ Optimism: "eip155:10",
254
+ Avalanche: "eip155:43114",
255
+ Sui: "sui:mainnet",
256
+ Aptos: "aptos:mainnet",
257
+ Stellar: "stellar:pubnet",
258
+ Cardano: "cip34:1-764824073"
259
+ };
260
+ function getEIP155ChainId(chain) {
261
+ (0, import_internal_utils3.assert)(chain.startsWith("eip155:"), "Chain is not an EIP-155 chain");
262
+ const chainIdStr = chain.slice(7);
263
+ (0, import_internal_utils3.assert)(chainIdStr.length > 0, "Chain is not an EIP-155 chain");
264
+ const chainId = Number(chainIdStr);
265
+ (0, import_internal_utils3.assert)(!Number.isNaN(chainId), "Chain is not an EIP-155 chain");
266
+ return chainId;
267
+ }
268
+
269
+ // src/bridges/direct-bridge/direct-bridge-constants.ts
270
+ var NEAR_NATIVE_ASSET_ID2 = "nep141:wrap.near";
271
+
272
+ // src/bridges/direct-bridge/direct-bridge-utils.ts
273
+ var import_internal_utils4 = require("@defuse-protocol/internal-utils");
274
+ function createWithdrawIntentPrimitive2(params) {
275
+ if (params.assetId === NEAR_NATIVE_ASSET_ID2 && // Ensure `msg` is not passed, because `native_withdraw` intent doesn't support `msg`
276
+ params.msg === void 0) {
277
+ return {
278
+ intent: "native_withdraw",
279
+ receiver_id: params.destinationAddress,
280
+ amount: params.amount.toString()
281
+ };
282
+ }
283
+ const { contractId: tokenAccountId, standard } = import_internal_utils4.utils.parseDefuseAssetId(
284
+ params.assetId
285
+ );
286
+ (0, import_internal_utils4.assert)(standard === "nep141", "Only NEP-141 is supported");
287
+ return {
288
+ intent: "ft_withdraw",
289
+ token: tokenAccountId,
290
+ receiver_id: params.destinationAddress,
291
+ amount: params.amount.toString(),
292
+ storage_deposit: params.storageDeposit > 0n ? params.storageDeposit.toString() : null,
293
+ msg: params.msg
294
+ };
295
+ }
296
+ function withdrawalParamsInvariant2(params) {
297
+ (0, import_internal_utils4.assert)(
298
+ !params.routeConfig ? true : params.routeConfig.route === RouteEnum.NearWithdrawal,
299
+ "Bridge is not direct"
300
+ );
301
+ }
302
+
303
+ // src/bridges/direct-bridge/direct-bridge.ts
304
+ var DirectBridge = class {
305
+ constructor({
306
+ env,
307
+ nearProvider
308
+ }) {
309
+ this.env = env;
310
+ this.nearProvider = nearProvider;
311
+ }
312
+ is(routeConfig) {
313
+ return routeConfig.route === RouteEnum.NearWithdrawal;
314
+ }
315
+ supports(params) {
316
+ let result = true;
317
+ if ("routeConfig" in params && params.routeConfig != null) {
318
+ result && (result = this.is(params.routeConfig));
319
+ }
320
+ try {
321
+ return result && this.parseAssetId(params.assetId) != null;
322
+ } catch {
323
+ return false;
324
+ }
325
+ }
326
+ parseAssetId(assetId) {
327
+ const parsed = import_internal_utils5.utils.parseDefuseAssetId(assetId);
328
+ if (parsed.standard === "nep141") {
329
+ return Object.assign(parsed, {
330
+ blockchain: Chains.Near,
331
+ bridgeName: BridgeNameEnum.None,
332
+ address: parsed.contractId
333
+ });
334
+ }
335
+ return null;
336
+ }
337
+ createWithdrawalIntents(args) {
338
+ withdrawalParamsInvariant2(args.withdrawalParams);
339
+ const intents = [];
340
+ if (args.feeEstimation.quote != null) {
341
+ intents.push({
342
+ intent: "token_diff",
343
+ diff: {
344
+ [args.feeEstimation.quote.defuse_asset_identifier_in]: `-${args.feeEstimation.quote.amount_in}`,
345
+ [args.feeEstimation.quote.defuse_asset_identifier_out]: args.feeEstimation.quote.amount_out
346
+ },
347
+ referral: args.referral
348
+ });
349
+ }
350
+ const intent = createWithdrawIntentPrimitive2({
351
+ assetId: args.withdrawalParams.assetId,
352
+ destinationAddress: args.withdrawalParams.destinationAddress,
353
+ amount: args.withdrawalParams.amount,
354
+ storageDeposit: args.feeEstimation.quote ? BigInt(args.feeEstimation.quote.amount_out) : args.feeEstimation.amount,
355
+ msg: args.withdrawalParams.routeConfig?.msg
356
+ });
357
+ intents.push(intent);
358
+ return Promise.resolve(intents);
359
+ }
360
+ /**
361
+ * Direct bridge doesn't have withdrawal restrictions.
362
+ */
363
+ async validateWithdrawal(_args) {
364
+ return;
365
+ }
366
+ async estimateWithdrawalFee(args) {
367
+ withdrawalParamsInvariant2(args.withdrawalParams);
368
+ const { contractId: tokenAccountId, standard } = import_internal_utils5.utils.parseDefuseAssetId(
369
+ args.withdrawalParams.assetId
370
+ );
371
+ (0, import_internal_utils5.assert)(standard === "nep141", "Only NEP-141 is supported");
372
+ if (
373
+ // We don't directly withdraw `wrap.near`, we unwrap it first, so it doesn't require storage
374
+ args.withdrawalParams.assetId === NEAR_NATIVE_ASSET_ID2 && // Ensure `msg` is not passed, because `native_withdraw` intent doesn't support `msg`
375
+ args.withdrawalParams.routeConfig?.msg === void 0
376
+ )
377
+ return {
378
+ amount: 0n,
379
+ quote: null
380
+ };
381
+ const [minStorageBalance, userStorageBalance] = await Promise.all([
382
+ (0, import_internal_utils5.getNearNep141MinStorageBalance)({
383
+ contractId: tokenAccountId,
384
+ nearProvider: this.nearProvider
385
+ }),
386
+ (0, import_internal_utils5.getNearNep141StorageBalance)({
387
+ contractId: tokenAccountId,
388
+ accountId: args.withdrawalParams.destinationAddress,
389
+ nearProvider: this.nearProvider
390
+ })
391
+ ]);
392
+ if (minStorageBalance <= userStorageBalance) {
393
+ return {
394
+ amount: 0n,
395
+ quote: null
396
+ };
397
+ }
398
+ const feeAssetId = NEAR_NATIVE_ASSET_ID2;
399
+ const feeAmount = minStorageBalance - userStorageBalance;
400
+ const feeQuote = args.withdrawalParams.assetId === feeAssetId ? null : await import_internal_utils5.solverRelay.getQuote({
401
+ quoteParams: {
402
+ defuse_asset_identifier_in: args.withdrawalParams.assetId,
403
+ defuse_asset_identifier_out: feeAssetId,
404
+ exact_amount_out: feeAmount.toString(),
405
+ wait_ms: args.quoteOptions?.waitMs
406
+ },
407
+ config: {
408
+ baseURL: import_internal_utils5.configsByEnvironment[this.env].solverRelayBaseURL,
409
+ logBalanceSufficient: false,
410
+ logger: args.logger
411
+ }
412
+ });
413
+ return {
414
+ amount: feeQuote ? BigInt(feeQuote.amount_in) : feeAmount,
415
+ quote: feeQuote
416
+ };
417
+ }
418
+ async waitForWithdrawalCompletion(args) {
419
+ return { hash: args.tx.hash };
420
+ }
421
+ };
422
+
423
+ // src/bridges/hot-bridge/hot-bridge.ts
424
+ var import_internal_utils9 = require("@defuse-protocol/internal-utils");
425
+ var import_omni_sdk2 = require("@hot-labs/omni-sdk");
426
+ var import_attempt = require("@lifeomic/attempt");
427
+
428
+ // src/classes/errors.ts
429
+ var import_internal_utils6 = require("@defuse-protocol/internal-utils");
430
+ var FeeExceedsAmountError = class extends import_internal_utils6.BaseError {
431
+ constructor(feeEstimation, amount) {
432
+ super("Amount too small to pay fee.", {
433
+ metaMessages: [
434
+ `Required fee: ${feeEstimation.amount}`,
435
+ `Withdrawal amount: ${amount}`
436
+ ],
437
+ name: "FeeExceedsAmountError"
438
+ });
439
+ this.feeEstimation = feeEstimation;
440
+ this.amount = amount;
441
+ }
442
+ };
443
+ var MinWithdrawalAmountError = class extends import_internal_utils6.BaseError {
444
+ constructor(minAmount, requestedAmount, assetId) {
445
+ super("Withdrawal amount is below minimum required by the bridge.", {
446
+ metaMessages: [
447
+ `Asset ID: ${assetId}`,
448
+ `Minimum amount: ${minAmount}`,
449
+ `Requested amount: ${requestedAmount}`
450
+ ],
451
+ name: "MinWithdrawalAmountError"
452
+ });
453
+ this.minAmount = minAmount;
454
+ this.requestedAmount = requestedAmount;
455
+ this.assetId = assetId;
456
+ }
457
+ };
458
+ var UnsupportedDestinationMemoError = class extends import_internal_utils6.BaseError {
459
+ constructor(blockchain, assetId) {
460
+ super("Destination memo is not supported for this blockchain.", {
461
+ details: "Destination memo is only supported for XRP Ledger withdrawals.",
462
+ metaMessages: [`Blockchain: ${blockchain}`, `Asset ID: ${assetId}`],
463
+ name: "UnsupportedDestinationMemoError"
464
+ });
465
+ this.blockchain = blockchain;
466
+ this.assetId = assetId;
467
+ }
468
+ };
469
+ var TrustlineNotFoundError = class extends import_internal_utils6.BaseError {
470
+ constructor(destinationAddress, assetId, blockchain, tokenAddress) {
471
+ super("Destination address does not have a trustline for this asset.", {
472
+ details: "The destination address must establish a trustline before receiving this asset.",
473
+ metaMessages: [
474
+ `Blockchain: ${blockchain}`,
475
+ `Asset ID: ${assetId}`,
476
+ `Destination address: ${destinationAddress}`,
477
+ `Token address: ${tokenAddress}`
478
+ ],
479
+ name: "TrustlineNotFoundError"
480
+ });
481
+ this.destinationAddress = destinationAddress;
482
+ this.assetId = assetId;
483
+ this.blockchain = blockchain;
484
+ this.tokenAddress = tokenAddress;
485
+ }
486
+ };
487
+
488
+ // src/bridges/hot-bridge/error.ts
489
+ var import_internal_utils7 = require("@defuse-protocol/internal-utils");
490
+ var HotWithdrawalPendingError = class extends import_internal_utils7.BaseError {
491
+ constructor(txHash, index) {
492
+ super("Withdrawal is still pending.", {
493
+ metaMessages: [`TxHash: ${txHash}`, `Index: ${index}`],
494
+ name: "HotWithdrawalPendingError"
495
+ });
496
+ this.txHash = txHash;
497
+ this.index = index;
498
+ }
499
+ };
500
+ var HotWithdrawalNotFoundError = class extends import_internal_utils7.BaseError {
501
+ constructor(txHash, index) {
502
+ super("Withdrawal with given index is not found.", {
503
+ metaMessages: [`TxHash: ${txHash}`, `Index: ${index}`],
504
+ name: "HotWithdrawalNotFoundError"
505
+ });
506
+ this.txHash = txHash;
507
+ this.index = index;
508
+ }
509
+ };
510
+ var HotWithdrawalCancelledError = class extends import_internal_utils7.BaseError {
511
+ constructor(txHash, index) {
512
+ super("Gasless withdrawal was canceled.", {
513
+ metaMessages: [`TxHash: ${txHash}`, `Index: ${index}`],
514
+ name: "HotWithdrawalCancelledError"
515
+ });
516
+ this.txHash = txHash;
517
+ this.index = index;
518
+ }
519
+ };
520
+
521
+ // src/bridges/hot-bridge/hot-bridge-constants.ts
522
+ var HotWithdrawStatus = {
523
+ Completed: "COMPLETED",
524
+ Canceled: "CANCELED"
525
+ };
526
+
527
+ // src/bridges/hot-bridge/hot-bridge-utils.ts
528
+ var import_internal_utils8 = require("@defuse-protocol/internal-utils");
529
+ var import_omni_sdk = require("@hot-labs/omni-sdk");
530
+
531
+ // src/bridges/hot-bridge/hot-bridge-chains.ts
532
+ var HotBridgeChains = [
533
+ Chains.Polygon,
534
+ Chains.BNB,
535
+ Chains.TON,
536
+ Chains.Optimism,
537
+ Chains.Avalanche,
538
+ Chains.Stellar
539
+ ];
540
+ var HotBridgeEVMChains = HotBridgeChains.filter((a) => a.startsWith("eip155:"));
541
+
542
+ // src/bridges/hot-bridge/hot-bridge-utils.ts
543
+ var nativeTokenMapping = {
544
+ [Chains.BNB]: "nep245:v2_1.omni.hot.tg:56_11111111111111111111",
545
+ [Chains.Polygon]: "nep245:v2_1.omni.hot.tg:137_11111111111111111111",
546
+ [Chains.TON]: "nep245:v2_1.omni.hot.tg:1117_",
547
+ [Chains.Optimism]: "nep245:v2_1.omni.hot.tg:10_11111111111111111111",
548
+ [Chains.Avalanche]: "nep245:v2_1.omni.hot.tg:43114_11111111111111111111",
549
+ [Chains.Stellar]: "nep245:v2_1.omni.hot.tg:1100_111bzQBB5v7AhLyPMDwS8uJgQV24KaAPXtwyVWu2KXbbfQU6NXRCz"
550
+ };
551
+ var caip2NetworkIdMapping = {
552
+ [Chains.BNB]: import_omni_sdk.Network.Bnb,
553
+ [Chains.Polygon]: import_omni_sdk.Network.Polygon,
554
+ [Chains.TON]: import_omni_sdk.Network.Ton,
555
+ [Chains.Optimism]: import_omni_sdk.Network.Optimism,
556
+ [Chains.Avalanche]: import_omni_sdk.Network.Avalanche,
557
+ [Chains.Stellar]: import_omni_sdk.Network.Stellar
558
+ };
559
+ var networkIdCAIP2Mapping = Object.fromEntries(
560
+ Object.entries(caip2NetworkIdMapping).map(([k, v]) => [v, k])
561
+ );
562
+ function getFeeAssetIdForChain(caip2) {
563
+ return nativeTokenMapping[caip2];
564
+ }
565
+ function toHotNetworkId(caip2) {
566
+ hotBlockchainInvariant(caip2);
567
+ return caip2NetworkIdMapping[caip2];
568
+ }
569
+ function hotNetworkIdToCAIP2(network) {
570
+ if (networkIdCAIP2Mapping[network] != null) {
571
+ return networkIdCAIP2Mapping[network];
572
+ }
573
+ throw new Error(`Unsupported HOT Bridge chain = ${network}`);
574
+ }
575
+ function formatTxHash(txHash, caip2) {
576
+ if (caip2.startsWith("eip155:")) {
577
+ return `0x${txHash}`;
578
+ }
579
+ return txHash;
580
+ }
581
+ function hotBlockchainInvariant(blockchain) {
582
+ (0, import_internal_utils8.assert)(
583
+ HotBridgeChains.includes(blockchain),
584
+ `${blockchain} is not a valid HOT Bridge blockchain. Supported values: ${HotBridgeChains.join()}`
585
+ );
586
+ }
587
+
588
+ // src/bridges/hot-bridge/hot-bridge.ts
589
+ var HotBridge = class {
590
+ constructor({ env, hotSdk }) {
591
+ this.env = env;
592
+ this.hotSdk = hotSdk;
593
+ }
594
+ is(routeConfig) {
595
+ return routeConfig.route === RouteEnum.HotBridge;
596
+ }
597
+ supports(params) {
598
+ let result = true;
599
+ if ("routeConfig" in params && params.routeConfig != null) {
600
+ result && (result = this.is(params.routeConfig));
601
+ }
602
+ try {
603
+ return result && this.parseAssetId(params.assetId) != null;
604
+ } catch {
605
+ return false;
606
+ }
607
+ }
608
+ parseAssetId(assetId) {
609
+ const parsed = import_internal_utils9.utils.parseDefuseAssetId(assetId);
610
+ if (parsed.contractId === import_omni_sdk2.utils.OMNI_HOT_V2) {
611
+ (0, import_internal_utils9.assert)(
612
+ parsed.standard === "nep245",
613
+ "NEP-245 is supported only for HOT bridge"
614
+ );
615
+ const [chainId, address] = import_omni_sdk2.utils.fromOmni(parsed.tokenId).split(":");
616
+ (0, import_internal_utils9.assert)(chainId != null, "Chain ID is not found");
617
+ (0, import_internal_utils9.assert)(address != null, "Address is not found");
618
+ return Object.assign(
619
+ parsed,
620
+ {
621
+ blockchain: hotNetworkIdToCAIP2(chainId),
622
+ bridgeName: BridgeNameEnum.Hot
623
+ },
624
+ address === "native" ? { native: true } : { address }
625
+ );
626
+ }
627
+ return null;
628
+ }
629
+ async createWithdrawalIntents(args) {
630
+ const assetInfo = this.parseAssetId(args.withdrawalParams.assetId);
631
+ (0, import_internal_utils9.assert)(assetInfo != null, "Asset is not supported");
632
+ if (args.withdrawalParams.destinationMemo != null && args.withdrawalParams.destinationMemo !== "") {
633
+ throw new UnsupportedDestinationMemoError(
634
+ assetInfo.blockchain,
635
+ args.withdrawalParams.assetId
636
+ );
637
+ }
638
+ const intents = [];
639
+ let feeAmount;
640
+ if (args.feeEstimation.quote == null) {
641
+ feeAmount = args.feeEstimation.amount;
642
+ } else {
643
+ feeAmount = BigInt(args.feeEstimation.quote.amount_out);
644
+ intents.push({
645
+ intent: "token_diff",
646
+ diff: {
647
+ [args.feeEstimation.quote.defuse_asset_identifier_in]: `-${args.feeEstimation.quote.amount_in}`,
648
+ [args.feeEstimation.quote.defuse_asset_identifier_out]: args.feeEstimation.quote.amount_out
649
+ },
650
+ referral: args.referral
651
+ });
652
+ }
653
+ const isNative = "native" in assetInfo;
654
+ const amount = args.withdrawalParams.amount + (isNative ? feeAmount : 0n);
655
+ const intent = await this.hotSdk.buildGaslessWithdrawIntent({
656
+ feeToken: "native",
657
+ feeAmount,
658
+ chain: toHotNetworkId(assetInfo.blockchain),
659
+ token: isNative ? "native" : assetInfo.address,
660
+ amount,
661
+ receiver: args.withdrawalParams.destinationAddress,
662
+ intentAccount: ""
663
+ // it is not used inside the function
664
+ });
665
+ (0, import_internal_utils9.assert)(intent.amounts[0] === amount.toString(), "Amount is not correct");
666
+ if (intent.amounts.length === 2) {
667
+ (0, import_internal_utils9.assert)(
668
+ intent.amounts[1] === feeAmount.toString(),
669
+ "Amount is not correct"
670
+ );
671
+ }
672
+ intents.push(intent);
673
+ return intents;
674
+ }
675
+ /**
676
+ * Hot bridge validates trustlines for Stellar addresses.
677
+ * For Stellar chains, checks if the destination address has the required trustline.
678
+ * @throws {TrustlineNotFoundError} If Stellar destination address lacks required trustline
679
+ */
680
+ async validateWithdrawal(args) {
681
+ const assetInfo = this.parseAssetId(args.assetId);
682
+ (0, import_internal_utils9.assert)(assetInfo != null, "Asset is not supported");
683
+ hotBlockchainInvariant(assetInfo.blockchain);
684
+ if (assetInfo.blockchain === Chains.Stellar) {
685
+ const token = "native" in assetInfo ? "native" : assetInfo.address;
686
+ const hasTrustline = await this.hotSdk.stellar.isTrustlineExists(
687
+ args.destinationAddress,
688
+ token
689
+ );
690
+ if (!hasTrustline) {
691
+ throw new TrustlineNotFoundError(
692
+ args.destinationAddress,
693
+ args.assetId,
694
+ assetInfo.blockchain,
695
+ token
696
+ );
697
+ }
698
+ }
699
+ }
700
+ async estimateWithdrawalFee(args) {
701
+ const assetInfo = this.parseAssetId(args.withdrawalParams.assetId);
702
+ (0, import_internal_utils9.assert)(assetInfo != null, "Asset is not supported");
703
+ hotBlockchainInvariant(assetInfo.blockchain);
704
+ const { gasPrice: feeAmount } = await this.hotSdk.getGaslessWithdrawFee(
705
+ toHotNetworkId(assetInfo.blockchain),
706
+ args.withdrawalParams.destinationAddress
707
+ );
708
+ const feeAssetId = getFeeAssetIdForChain(assetInfo.blockchain);
709
+ const feeQuote = args.withdrawalParams.assetId === feeAssetId || feeAmount === 0n ? null : await import_internal_utils9.solverRelay.getQuote({
710
+ quoteParams: {
711
+ defuse_asset_identifier_in: args.withdrawalParams.assetId,
712
+ defuse_asset_identifier_out: feeAssetId,
713
+ exact_amount_out: feeAmount.toString(),
714
+ wait_ms: args.quoteOptions?.waitMs
715
+ },
716
+ config: {
717
+ baseURL: import_internal_utils9.configsByEnvironment[this.env].solverRelayBaseURL,
718
+ logBalanceSufficient: false,
719
+ logger: args.logger
720
+ }
721
+ });
722
+ return {
723
+ amount: feeQuote ? BigInt(feeQuote.amount_in) : feeAmount,
724
+ quote: feeQuote
725
+ };
726
+ }
727
+ async waitForWithdrawalCompletion(args) {
728
+ const nonces = await this.hotSdk.near.parseWithdrawalNonces(
729
+ args.tx.hash,
730
+ args.tx.accountId
731
+ );
732
+ const nonce = nonces[args.index];
733
+ if (nonce == null) {
734
+ throw new HotWithdrawalNotFoundError(args.tx.hash, args.index);
735
+ }
736
+ return (0, import_attempt.retry)(
737
+ async () => {
738
+ if (args.signal?.aborted) {
739
+ throw args.signal.reason;
740
+ }
741
+ const status = await this.hotSdk.getGaslessWithdrawStatus(
742
+ nonce.toString()
743
+ );
744
+ if (status === HotWithdrawStatus.Canceled) {
745
+ throw new HotWithdrawalCancelledError(args.tx.hash, args.index);
746
+ }
747
+ if (status === HotWithdrawStatus.Completed) {
748
+ return { hash: null };
749
+ }
750
+ if (typeof status === "string") {
751
+ return {
752
+ hash: "chain" in args.routeConfig ? formatTxHash(status, args.routeConfig.chain) : status
753
+ };
754
+ }
755
+ throw new HotWithdrawalPendingError(args.tx.hash, args.index);
756
+ },
757
+ {
758
+ ...args.retryOptions ?? import_internal_utils9.RETRY_CONFIGS.TWO_MINS_GRADUAL,
759
+ handleError: (err, ctx) => {
760
+ if (err instanceof HotWithdrawalCancelledError || err === args.signal?.reason) {
761
+ ctx.abort();
762
+ }
763
+ }
764
+ }
765
+ );
766
+ }
767
+ };
768
+
769
+ // src/bridges/intents-bridge/intents-bridge.ts
770
+ var IntentsBridge = class {
771
+ is(routeConfig) {
772
+ return routeConfig.route === RouteEnum.InternalTransfer;
773
+ }
774
+ supports(params) {
775
+ if ("routeConfig" in params && params.routeConfig != null) {
776
+ return this.is(params.routeConfig);
777
+ }
778
+ return false;
779
+ }
780
+ parseAssetId() {
781
+ return null;
782
+ }
783
+ createWithdrawalIntents(args) {
784
+ const intents = [
785
+ {
786
+ intent: "transfer",
787
+ receiver_id: args.withdrawalParams.destinationAddress,
788
+ tokens: {
789
+ [args.withdrawalParams.assetId]: args.withdrawalParams.amount.toString()
790
+ }
791
+ }
792
+ ];
793
+ return Promise.resolve(intents);
794
+ }
795
+ /**
796
+ * Intents bridge doesn't have withdrawal restrictions.
797
+ */
798
+ async validateWithdrawal(_args) {
799
+ return;
800
+ }
801
+ async estimateWithdrawalFee() {
802
+ return {
803
+ amount: 0n,
804
+ quote: null
805
+ };
806
+ }
807
+ async waitForWithdrawalCompletion(args) {
808
+ return { hash: args.tx.hash };
809
+ }
810
+ };
811
+
812
+ // src/bridges/poa-bridge/poa-bridge.ts
813
+ var import_internal_utils11 = require("@defuse-protocol/internal-utils");
814
+ var import_ttlcache = __toESM(require("@isaacs/ttlcache"), 1);
815
+
816
+ // src/bridges/poa-bridge/poa-bridge-utils.ts
817
+ var import_internal_utils10 = require("@defuse-protocol/internal-utils");
818
+ function createWithdrawIntentPrimitive3(params) {
819
+ const { contractId: tokenAccountId } = import_internal_utils10.utils.parseDefuseAssetId(
820
+ params.assetId
821
+ );
822
+ return {
823
+ intent: "ft_withdraw",
824
+ token: tokenAccountId,
825
+ receiver_id: tokenAccountId,
826
+ amount: params.amount.toString(),
827
+ memo: createWithdrawMemo({
828
+ receiverAddress: params.destinationAddress,
829
+ xrpMemo: params.destinationMemo
830
+ })
831
+ };
832
+ }
833
+ function createWithdrawMemo({
834
+ receiverAddress,
835
+ xrpMemo
836
+ }) {
837
+ const memo = ["WITHDRAW_TO", receiverAddress];
838
+ if (xrpMemo != null && xrpMemo !== "") {
839
+ memo.push(xrpMemo);
840
+ }
841
+ return memo.join(":");
842
+ }
843
+ var caip2Mapping = {
844
+ [Chains.Ethereum]: "eth:1",
845
+ [Chains.Base]: "eth:8453",
846
+ [Chains.Arbitrum]: "eth:42161",
847
+ [Chains.Bitcoin]: "btc:mainnet",
848
+ [Chains.Solana]: "sol:mainnet",
849
+ [Chains.Dogecoin]: "doge:mainnet",
850
+ [Chains.XRPL]: "xrp:mainnet",
851
+ [Chains.Zcash]: "zec:mainnet",
852
+ [Chains.Gnosis]: "eth:100",
853
+ [Chains.Berachain]: "eth:80094",
854
+ [Chains.Tron]: "tron:mainnet",
855
+ [Chains.Sui]: "sui:mainnet",
856
+ [Chains.Aptos]: "aptos:mainnet",
857
+ [Chains.Cardano]: "cardano:mainnet"
858
+ };
859
+ function toPoaNetwork(caip2) {
860
+ if (caip2Mapping[caip2] == null) {
861
+ throw new Error(`Unsupported POA Bridge chain = ${caip2}`);
862
+ }
863
+ return caip2Mapping[caip2];
864
+ }
865
+ var tokenPrefixMapping = {
866
+ eth: Chains.Ethereum,
867
+ base: Chains.Base,
868
+ arb: Chains.Arbitrum,
869
+ btc: Chains.Bitcoin,
870
+ sol: Chains.Solana,
871
+ doge: Chains.Dogecoin,
872
+ xrp: Chains.XRPL,
873
+ zec: Chains.Zcash,
874
+ gnosis: Chains.Gnosis,
875
+ bera: Chains.Berachain,
876
+ tron: Chains.Tron,
877
+ sui: Chains.Sui,
878
+ aptos: Chains.Aptos,
879
+ cardano: Chains.Cardano
880
+ };
881
+ function contractIdToCaip2(contractId) {
882
+ for (const [prefix, caip2] of Object.entries(tokenPrefixMapping)) {
883
+ if (contractId.startsWith(`${prefix}.`) || contractId.startsWith(`${prefix}-`)) {
884
+ return caip2;
885
+ }
886
+ }
887
+ throw new Error(`Unsupported POA Bridge contractId = ${contractId}`);
888
+ }
889
+
890
+ // src/bridges/poa-bridge/poa-bridge.ts
891
+ var PoaBridge = class {
892
+ constructor({ env }) {
893
+ // TTL cache for supported tokens with 30-second TTL
894
+ this.supportedTokensCache = new import_ttlcache.default({ ttl: 30 * 1e3 });
895
+ this.env = env;
896
+ }
897
+ is(routeConfig) {
898
+ return routeConfig.route === RouteEnum.PoaBridge;
899
+ }
900
+ supports(params) {
901
+ let result = true;
902
+ if ("routeConfig" in params && params.routeConfig != null) {
903
+ result && (result = this.is(params.routeConfig));
904
+ }
905
+ try {
906
+ return result && this.parseAssetId(params.assetId) != null;
907
+ } catch {
908
+ return false;
909
+ }
910
+ }
911
+ parseAssetId(assetId) {
912
+ const parsed = import_internal_utils11.utils.parseDefuseAssetId(assetId);
913
+ if (parsed.contractId.endsWith(
914
+ `.${import_internal_utils11.configsByEnvironment[this.env].poaTokenFactoryContractID}`
915
+ )) {
916
+ return Object.assign(parsed, {
917
+ blockchain: contractIdToCaip2(parsed.contractId),
918
+ bridgeName: BridgeNameEnum.Poa,
919
+ address: ""
920
+ // todo: derive address (or native)
921
+ });
922
+ }
923
+ return null;
924
+ }
925
+ createWithdrawalIntents(args) {
926
+ const intent = createWithdrawIntentPrimitive3({
927
+ ...args.withdrawalParams,
928
+ amount: args.withdrawalParams.amount + args.feeEstimation.amount,
929
+ destinationMemo: args.withdrawalParams.destinationMemo
930
+ });
931
+ return Promise.resolve([intent]);
932
+ }
933
+ /**
934
+ * Validates minimum withdrawal amount for POA bridge tokens.
935
+ * Checks the bridge's supported tokens API to ensure the withdrawal amount
936
+ * meets the minimum required amount for the specific token and blockchain.
937
+ * @throws {MinWithdrawalAmountError} If the amount is below the minimum required
938
+ */
939
+ async validateWithdrawal(args) {
940
+ const assetInfo = this.parseAssetId(args.assetId);
941
+ (0, import_internal_utils11.assert)(assetInfo != null, "Asset is not supported");
942
+ const { tokens } = await this.getCachedSupportedTokens(
943
+ [toPoaNetwork(assetInfo.blockchain)],
944
+ args.logger
945
+ );
946
+ const tokenInfo = tokens.find(
947
+ (token) => token.intents_token_id === args.assetId
948
+ );
949
+ if (tokenInfo != null) {
950
+ const minWithdrawalAmount = BigInt(tokenInfo.min_withdrawal_amount);
951
+ if (args.amount < minWithdrawalAmount) {
952
+ throw new MinWithdrawalAmountError(
953
+ minWithdrawalAmount,
954
+ args.amount,
955
+ args.assetId
956
+ );
957
+ }
958
+ }
959
+ }
960
+ async estimateWithdrawalFee(args) {
961
+ const assetInfo = this.parseAssetId(args.withdrawalParams.assetId);
962
+ (0, import_internal_utils11.assert)(assetInfo != null, "Asset is not supported");
963
+ const estimation = await import_internal_utils11.poaBridge.httpClient.getWithdrawalEstimate(
964
+ {
965
+ token: import_internal_utils11.utils.getTokenAccountId(args.withdrawalParams.assetId),
966
+ address: args.withdrawalParams.destinationAddress,
967
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
968
+ chain: toPoaNetwork(assetInfo.blockchain)
969
+ },
970
+ {
971
+ baseURL: import_internal_utils11.configsByEnvironment[this.env].poaBridgeBaseURL,
972
+ logger: args.logger
973
+ }
974
+ );
975
+ return {
976
+ amount: BigInt(estimation.withdrawalFee),
977
+ quote: null
978
+ };
979
+ }
980
+ async waitForWithdrawalCompletion(args) {
981
+ const withdrawalStatus = await import_internal_utils11.poaBridge.waitForWithdrawalCompletion({
982
+ txHash: args.tx.hash,
983
+ index: args.index,
984
+ signal: args.signal ?? new AbortController().signal,
985
+ retryOptions: args.retryOptions,
986
+ baseURL: import_internal_utils11.configsByEnvironment[this.env].poaBridgeBaseURL,
987
+ logger: args.logger
988
+ });
989
+ return { hash: withdrawalStatus.destinationTxHash };
990
+ }
991
+ /**
992
+ * Gets supported tokens with caching to avoid frequent API calls.
993
+ * Cache expires after 30 seconds using TTL cache.
994
+ */
995
+ async getCachedSupportedTokens(chains, logger) {
996
+ const cacheKey = chains.sort().join(",");
997
+ const cached = this.supportedTokensCache.get(cacheKey);
998
+ if (cached != null) {
999
+ return cached;
1000
+ }
1001
+ const data = await import_internal_utils11.poaBridge.httpClient.getSupportedTokens(
1002
+ { chains },
1003
+ {
1004
+ baseURL: import_internal_utils11.configsByEnvironment[this.env].poaBridgeBaseURL,
1005
+ logger
1006
+ }
1007
+ );
1008
+ this.supportedTokensCache.set(cacheKey, data);
1009
+ return data;
1010
+ }
1011
+ };
1012
+
1013
+ // src/constants/public-rpc-urls.ts
1014
+ var PUBLIC_EVM_RPC_URLS = {
1015
+ [Chains.BNB]: ["https://bsc-rpc.publicnode.com"],
1016
+ [Chains.Polygon]: ["https://polygon-bor-rpc.publicnode.com"],
1017
+ [Chains.Optimism]: ["https://optimism-rpc.publicnode.com"],
1018
+ [Chains.Avalanche]: ["https://avalanche-c-chain-rpc.publicnode.com"]
1019
+ };
1020
+ var PUBLIC_STELLAR_RPC_URLS = {
1021
+ soroban: ["https://mainnet.sorobanrpc.com"],
1022
+ horizon: ["https://horizon.stellar.org"]
1023
+ };
1024
+
1025
+ // src/intents/intent-executer-impl/intent-executer.ts
1026
+ var import_internal_utils12 = require("@defuse-protocol/internal-utils");
1027
+
1028
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/_u64.js
1029
+ var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
1030
+ var _32n = /* @__PURE__ */ BigInt(32);
1031
+ function fromBig(n, le = false) {
1032
+ if (le)
1033
+ return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
1034
+ return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
1035
+ }
1036
+ function split(lst, le = false) {
1037
+ const len = lst.length;
1038
+ let Ah = new Uint32Array(len);
1039
+ let Al = new Uint32Array(len);
1040
+ for (let i = 0; i < len; i++) {
1041
+ const { h, l } = fromBig(lst[i], le);
1042
+ [Ah[i], Al[i]] = [h, l];
1043
+ }
1044
+ return [Ah, Al];
1045
+ }
1046
+ var rotlSH = (h, l, s) => h << s | l >>> 32 - s;
1047
+ var rotlSL = (h, l, s) => l << s | h >>> 32 - s;
1048
+ var rotlBH = (h, l, s) => l << s - 32 | h >>> 64 - s;
1049
+ var rotlBL = (h, l, s) => h << s - 32 | l >>> 64 - s;
1050
+
1051
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/utils.js
1052
+ function isBytes(a) {
1053
+ return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
1054
+ }
1055
+ function anumber(n) {
1056
+ if (!Number.isSafeInteger(n) || n < 0)
1057
+ throw new Error("positive integer expected, got " + n);
1058
+ }
1059
+ function abytes(b, ...lengths) {
1060
+ if (!isBytes(b))
1061
+ throw new Error("Uint8Array expected");
1062
+ if (lengths.length > 0 && !lengths.includes(b.length))
1063
+ throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
1064
+ }
1065
+ function aexists(instance, checkFinished = true) {
1066
+ if (instance.destroyed)
1067
+ throw new Error("Hash instance has been destroyed");
1068
+ if (checkFinished && instance.finished)
1069
+ throw new Error("Hash#digest() has already been called");
1070
+ }
1071
+ function aoutput(out, instance) {
1072
+ abytes(out);
1073
+ const min = instance.outputLen;
1074
+ if (out.length < min) {
1075
+ throw new Error("digestInto() expects output buffer of length at least " + min);
1076
+ }
1077
+ }
1078
+ function u32(arr) {
1079
+ return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
1080
+ }
1081
+ function clean(...arrays) {
1082
+ for (let i = 0; i < arrays.length; i++) {
1083
+ arrays[i].fill(0);
1084
+ }
1085
+ }
1086
+ var isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
1087
+ function byteSwap(word) {
1088
+ return word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
1089
+ }
1090
+ function byteSwap32(arr) {
1091
+ for (let i = 0; i < arr.length; i++) {
1092
+ arr[i] = byteSwap(arr[i]);
1093
+ }
1094
+ return arr;
1095
+ }
1096
+ var swap32IfBE = isLE ? (u) => u : byteSwap32;
1097
+ function utf8ToBytes(str) {
1098
+ if (typeof str !== "string")
1099
+ throw new Error("string expected");
1100
+ return new Uint8Array(new TextEncoder().encode(str));
1101
+ }
1102
+ function toBytes(data) {
1103
+ if (typeof data === "string")
1104
+ data = utf8ToBytes(data);
1105
+ abytes(data);
1106
+ return data;
1107
+ }
1108
+ var Hash = class {
1109
+ };
1110
+ function createHasher(hashCons) {
1111
+ const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
1112
+ const tmp = hashCons();
1113
+ hashC.outputLen = tmp.outputLen;
1114
+ hashC.blockLen = tmp.blockLen;
1115
+ hashC.create = () => hashCons();
1116
+ return hashC;
1117
+ }
1118
+
1119
+ // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/sha3.js
1120
+ var _0n = BigInt(0);
1121
+ var _1n = BigInt(1);
1122
+ var _2n = BigInt(2);
1123
+ var _7n = BigInt(7);
1124
+ var _256n = BigInt(256);
1125
+ var _0x71n = BigInt(113);
1126
+ var SHA3_PI = [];
1127
+ var SHA3_ROTL = [];
1128
+ var _SHA3_IOTA = [];
1129
+ for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
1130
+ [x, y] = [y, (2 * x + 3 * y) % 5];
1131
+ SHA3_PI.push(2 * (5 * y + x));
1132
+ SHA3_ROTL.push((round + 1) * (round + 2) / 2 % 64);
1133
+ let t = _0n;
1134
+ for (let j = 0; j < 7; j++) {
1135
+ R = (R << _1n ^ (R >> _7n) * _0x71n) % _256n;
1136
+ if (R & _2n)
1137
+ t ^= _1n << (_1n << /* @__PURE__ */ BigInt(j)) - _1n;
1138
+ }
1139
+ _SHA3_IOTA.push(t);
1140
+ }
1141
+ var IOTAS = split(_SHA3_IOTA, true);
1142
+ var SHA3_IOTA_H = IOTAS[0];
1143
+ var SHA3_IOTA_L = IOTAS[1];
1144
+ var rotlH = (h, l, s) => s > 32 ? rotlBH(h, l, s) : rotlSH(h, l, s);
1145
+ var rotlL = (h, l, s) => s > 32 ? rotlBL(h, l, s) : rotlSL(h, l, s);
1146
+ function keccakP(s, rounds = 24) {
1147
+ const B = new Uint32Array(5 * 2);
1148
+ for (let round = 24 - rounds; round < 24; round++) {
1149
+ for (let x = 0; x < 10; x++)
1150
+ B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
1151
+ for (let x = 0; x < 10; x += 2) {
1152
+ const idx1 = (x + 8) % 10;
1153
+ const idx0 = (x + 2) % 10;
1154
+ const B0 = B[idx0];
1155
+ const B1 = B[idx0 + 1];
1156
+ const Th = rotlH(B0, B1, 1) ^ B[idx1];
1157
+ const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
1158
+ for (let y = 0; y < 50; y += 10) {
1159
+ s[x + y] ^= Th;
1160
+ s[x + y + 1] ^= Tl;
1161
+ }
1162
+ }
1163
+ let curH = s[2];
1164
+ let curL = s[3];
1165
+ for (let t = 0; t < 24; t++) {
1166
+ const shift = SHA3_ROTL[t];
1167
+ const Th = rotlH(curH, curL, shift);
1168
+ const Tl = rotlL(curH, curL, shift);
1169
+ const PI = SHA3_PI[t];
1170
+ curH = s[PI];
1171
+ curL = s[PI + 1];
1172
+ s[PI] = Th;
1173
+ s[PI + 1] = Tl;
1174
+ }
1175
+ for (let y = 0; y < 50; y += 10) {
1176
+ for (let x = 0; x < 10; x++)
1177
+ B[x] = s[y + x];
1178
+ for (let x = 0; x < 10; x++)
1179
+ s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
1180
+ }
1181
+ s[0] ^= SHA3_IOTA_H[round];
1182
+ s[1] ^= SHA3_IOTA_L[round];
1183
+ }
1184
+ clean(B);
1185
+ }
1186
+ var Keccak = class _Keccak extends Hash {
1187
+ // NOTE: we accept arguments in bytes instead of bits here.
1188
+ constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
1189
+ super();
1190
+ this.pos = 0;
1191
+ this.posOut = 0;
1192
+ this.finished = false;
1193
+ this.destroyed = false;
1194
+ this.enableXOF = false;
1195
+ this.blockLen = blockLen;
1196
+ this.suffix = suffix;
1197
+ this.outputLen = outputLen;
1198
+ this.enableXOF = enableXOF;
1199
+ this.rounds = rounds;
1200
+ anumber(outputLen);
1201
+ if (!(0 < blockLen && blockLen < 200))
1202
+ throw new Error("only keccak-f1600 function is supported");
1203
+ this.state = new Uint8Array(200);
1204
+ this.state32 = u32(this.state);
1205
+ }
1206
+ clone() {
1207
+ return this._cloneInto();
1208
+ }
1209
+ keccak() {
1210
+ swap32IfBE(this.state32);
1211
+ keccakP(this.state32, this.rounds);
1212
+ swap32IfBE(this.state32);
1213
+ this.posOut = 0;
1214
+ this.pos = 0;
1215
+ }
1216
+ update(data) {
1217
+ aexists(this);
1218
+ data = toBytes(data);
1219
+ abytes(data);
1220
+ const { blockLen, state } = this;
1221
+ const len = data.length;
1222
+ for (let pos = 0; pos < len; ) {
1223
+ const take = Math.min(blockLen - this.pos, len - pos);
1224
+ for (let i = 0; i < take; i++)
1225
+ state[this.pos++] ^= data[pos++];
1226
+ if (this.pos === blockLen)
1227
+ this.keccak();
1228
+ }
1229
+ return this;
1230
+ }
1231
+ finish() {
1232
+ if (this.finished)
1233
+ return;
1234
+ this.finished = true;
1235
+ const { state, suffix, pos, blockLen } = this;
1236
+ state[pos] ^= suffix;
1237
+ if ((suffix & 128) !== 0 && pos === blockLen - 1)
1238
+ this.keccak();
1239
+ state[blockLen - 1] ^= 128;
1240
+ this.keccak();
1241
+ }
1242
+ writeInto(out) {
1243
+ aexists(this, false);
1244
+ abytes(out);
1245
+ this.finish();
1246
+ const bufferOut = this.state;
1247
+ const { blockLen } = this;
1248
+ for (let pos = 0, len = out.length; pos < len; ) {
1249
+ if (this.posOut >= blockLen)
1250
+ this.keccak();
1251
+ const take = Math.min(blockLen - this.posOut, len - pos);
1252
+ out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
1253
+ this.posOut += take;
1254
+ pos += take;
1255
+ }
1256
+ return out;
1257
+ }
1258
+ xofInto(out) {
1259
+ if (!this.enableXOF)
1260
+ throw new Error("XOF is not possible for this instance");
1261
+ return this.writeInto(out);
1262
+ }
1263
+ xof(bytes) {
1264
+ anumber(bytes);
1265
+ return this.xofInto(new Uint8Array(bytes));
1266
+ }
1267
+ digestInto(out) {
1268
+ aoutput(out, this);
1269
+ if (this.finished)
1270
+ throw new Error("digest() was already called");
1271
+ this.writeInto(out);
1272
+ this.destroy();
1273
+ return out;
1274
+ }
1275
+ digest() {
1276
+ return this.digestInto(new Uint8Array(this.outputLen));
1277
+ }
1278
+ destroy() {
1279
+ this.destroyed = true;
1280
+ clean(this.state);
1281
+ }
1282
+ _cloneInto(to) {
1283
+ const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
1284
+ to || (to = new _Keccak(blockLen, suffix, outputLen, enableXOF, rounds));
1285
+ to.state32.set(this.state32);
1286
+ to.pos = this.pos;
1287
+ to.posOut = this.posOut;
1288
+ to.finished = this.finished;
1289
+ to.rounds = rounds;
1290
+ to.suffix = suffix;
1291
+ to.outputLen = outputLen;
1292
+ to.enableXOF = enableXOF;
1293
+ to.destroyed = this.destroyed;
1294
+ return to;
1295
+ }
1296
+ };
1297
+ var gen = (suffix, blockLen, outputLen) => createHasher(() => new Keccak(blockLen, suffix, outputLen));
1298
+ var keccak_256 = /* @__PURE__ */ (() => gen(1, 136, 256 / 8))();
1299
+
1300
+ // src/intents/intent-hash.ts
1301
+ var import_base = require("@scure/base");
1302
+
1303
+ // src/lib/nep413.ts
1304
+ var import_borsher = require("borsher");
1305
+ var nep413PayloadSchema = import_borsher.BorshSchema.Struct({
1306
+ message: import_borsher.BorshSchema.String,
1307
+ nonce: import_borsher.BorshSchema.Array(import_borsher.BorshSchema.u8, 32),
1308
+ recipient: import_borsher.BorshSchema.String,
1309
+ callback_url: import_borsher.BorshSchema.Option(import_borsher.BorshSchema.String)
1310
+ });
1311
+ async function hashNEP413Message({
1312
+ message,
1313
+ recipient,
1314
+ nonce,
1315
+ callback_url
1316
+ }) {
1317
+ const payload = {
1318
+ message,
1319
+ nonce: Array.from(nonce),
1320
+ recipient,
1321
+ callback_url
1322
+ };
1323
+ const payloadSerialized = (0, import_borsher.borshSerialize)(nep413PayloadSchema, payload);
1324
+ const baseInt = 2 ** 31 + 413;
1325
+ const baseIntSerialized = (0, import_borsher.borshSerialize)(import_borsher.BorshSchema.u32, baseInt);
1326
+ const combinedData = new Uint8Array(
1327
+ baseIntSerialized.length + payloadSerialized.length
1328
+ );
1329
+ combinedData.set(baseIntSerialized);
1330
+ combinedData.set(payloadSerialized, baseIntSerialized.length);
1331
+ const hashBuffer = await crypto.subtle.digest("SHA-256", combinedData);
1332
+ return new Uint8Array(hashBuffer);
1333
+ }
1334
+
1335
+ // src/intents/intent-hash.ts
1336
+ async function computeIntentHash(multiPayload) {
1337
+ const hashBytes = await computeIntentHashBytes(multiPayload);
1338
+ return import_base.base58.encode(hashBytes);
1339
+ }
1340
+ async function computeIntentHashBytes(multiPayload) {
1341
+ switch (multiPayload.standard) {
1342
+ case "nep413":
1343
+ return computeNep413Hash(
1344
+ multiPayload
1345
+ );
1346
+ case "erc191":
1347
+ return computeErc191Hash(
1348
+ multiPayload
1349
+ );
1350
+ default:
1351
+ throw new Error(
1352
+ `Standard is not yet supported: ${multiPayload.standard}`
1353
+ );
1354
+ }
1355
+ }
1356
+ async function computeNep413Hash(multiPayload) {
1357
+ const payload = multiPayload.payload;
1358
+ return hashNEP413Message({
1359
+ message: payload.message,
1360
+ recipient: payload.recipient,
1361
+ nonce: Array.from(import_base.base64.decode(payload.nonce)),
1362
+ callback_url: payload.callbackUrl
1363
+ });
1364
+ }
1365
+ function computeErc191Hash(multiPayload) {
1366
+ const message = multiPayload.payload;
1367
+ const prefix = "Ethereum Signed Message:\n";
1368
+ const messageWithPrefix = prefix + message.length.toString() + message;
1369
+ return keccak_256(new TextEncoder().encode(messageWithPrefix));
1370
+ }
1371
+
1372
+ // src/intents/intent-payload-factory.ts
1373
+ var import_base2 = require("@scure/base");
1374
+ function defaultIntentPayloadFactory({
1375
+ intents,
1376
+ verifying_contract,
1377
+ ...params
1378
+ }) {
1379
+ params = Object.fromEntries(
1380
+ Object.entries(params).filter(([, value]) => value !== void 0)
1381
+ );
1382
+ return {
1383
+ verifying_contract,
1384
+ deadline: new Date(Date.now() + 60 * 1e3).toISOString(),
1385
+ nonce: import_base2.base64.encode(crypto.getRandomValues(new Uint8Array(32))),
1386
+ intents: intents == null ? [] : intents,
1387
+ signer_id: void 0,
1388
+ // or you can specify intent user id
1389
+ ...params
1390
+ };
1391
+ }
1392
+
1393
+ // src/intents/intent-executer-impl/intent-executer.ts
1394
+ var IntentExecuter = class {
1395
+ constructor(args) {
1396
+ this.env = args.env;
1397
+ this.logger = args.logger;
1398
+ this.intentPayloadFactory = args.intentPayloadFactory;
1399
+ this.intentRelayer = args.intentRelayer;
1400
+ this.intentSigner = args.intentSigner;
1401
+ this.onBeforePublishIntent = args.onBeforePublishIntent;
1402
+ }
1403
+ async signAndSendIntent({
1404
+ relayParams: relayParamsFactory,
1405
+ ...intentParams
1406
+ }) {
1407
+ const verifyingContract = import_internal_utils12.configsByEnvironment[this.env].contractID;
1408
+ let intentPayload = defaultIntentPayloadFactory({
1409
+ verifying_contract: verifyingContract,
1410
+ ...intentParams
1411
+ });
1412
+ if (this.intentPayloadFactory) {
1413
+ intentPayload = await mergeIntentPayloads(
1414
+ intentPayload,
1415
+ this.intentPayloadFactory
1416
+ );
1417
+ }
1418
+ const multiPayload = await this.intentSigner.signIntent(intentPayload);
1419
+ const relayParams = relayParamsFactory ? await relayParamsFactory() : {};
1420
+ if (this.onBeforePublishIntent) {
1421
+ const intentHash = await computeIntentHash(multiPayload);
1422
+ await this.onBeforePublishIntent({
1423
+ intentHash,
1424
+ intentPayload,
1425
+ multiPayload,
1426
+ relayParams
1427
+ });
1428
+ }
1429
+ const ticket = await this.intentRelayer.publishIntent(
1430
+ {
1431
+ multiPayload,
1432
+ ...relayParams
1433
+ },
1434
+ { logger: this.logger }
1435
+ );
1436
+ return { ticket };
1437
+ }
1438
+ async waitForSettlement(ticket) {
1439
+ return this.intentRelayer.waitForSettlement(ticket, {
1440
+ logger: this.logger
1441
+ });
1442
+ }
1443
+ };
1444
+ async function mergeIntentPayloads(basePayload, intentPayloadFactory) {
1445
+ const customPayload = await intentPayloadFactory(basePayload);
1446
+ const customPayloadIntents = customPayload.intents ?? [];
1447
+ return {
1448
+ ...basePayload,
1449
+ ...customPayload,
1450
+ intents: Array.from(
1451
+ /* @__PURE__ */ new Set([...customPayloadIntents, ...basePayload.intents])
1452
+ )
1453
+ };
1454
+ }
1455
+
1456
+ // src/intents/intent-relayer-impl/intent-relayer-public.ts
1457
+ var import_internal_utils13 = require("@defuse-protocol/internal-utils");
1458
+ var IntentRelayerPublic = class {
1459
+ constructor({ env }) {
1460
+ this.env = env;
1461
+ }
1462
+ async publishIntent({
1463
+ multiPayload,
1464
+ quoteHashes
1465
+ }, ctx = {}) {
1466
+ return (await this.publishIntents(
1467
+ {
1468
+ multiPayloads: [multiPayload],
1469
+ quoteHashes: quoteHashes ?? []
1470
+ },
1471
+ ctx
1472
+ ))[0];
1473
+ }
1474
+ // как прокидывать доп. параметры, например, quoteHashes (или специфичные параметры для каждого релея?)
1475
+ async publishIntents({
1476
+ multiPayloads,
1477
+ quoteHashes
1478
+ }, ctx = {}) {
1479
+ const a = await import_internal_utils13.solverRelay.publishIntents(
1480
+ {
1481
+ quote_hashes: quoteHashes,
1482
+ signed_datas: multiPayloads
1483
+ },
1484
+ {
1485
+ baseURL: import_internal_utils13.configsByEnvironment[this.env].solverRelayBaseURL,
1486
+ logger: ctx.logger
1487
+ }
1488
+ );
1489
+ if (a.isOk()) {
1490
+ return a.unwrap();
1491
+ }
1492
+ throw a.unwrapErr();
1493
+ }
1494
+ async waitForSettlement(ticket, ctx = {}) {
1495
+ const result = await import_internal_utils13.solverRelay.waitForIntentSettlement({
1496
+ intentHash: ticket,
1497
+ signal: new AbortController().signal,
1498
+ baseURL: import_internal_utils13.configsByEnvironment[this.env].solverRelayBaseURL,
1499
+ logger: ctx.logger
1500
+ });
1501
+ return {
1502
+ tx: {
1503
+ hash: result.txHash,
1504
+ // Usually relayer's account id is the verifying contract (`intents.near`),
1505
+ // but it is not set in stone and may change in the future.
1506
+ accountId: import_internal_utils13.configsByEnvironment[this.env].contractID
1507
+ }
1508
+ };
1509
+ }
1510
+ };
1511
+
1512
+ // src/intents/intent-signer-impl/intent-signer-noop.ts
1513
+ var noopIntentSigner = {
1514
+ signIntent() {
1515
+ throw new Error("Not implemented");
1516
+ }
1517
+ };
1518
+
1519
+ // src/lib/array.ts
1520
+ var import_internal_utils14 = require("@defuse-protocol/internal-utils");
1521
+ function zip(arr1, arr2) {
1522
+ (0, import_internal_utils14.assert)(arr1.length === arr2.length, "Arrays must have the same length");
1523
+ return arr1.map((v, i) => [v, arr2[i]]);
1524
+ }
1525
+
1526
+ // src/lib/configure-rpc-config.ts
1527
+ var import_internal_utils15 = require("@defuse-protocol/internal-utils");
1528
+
1529
+ // src/lib/object.ts
1530
+ function pick(obj, keys) {
1531
+ const result = {};
1532
+ for (const key of keys) {
1533
+ if (key in obj) {
1534
+ result[key] = obj[key];
1535
+ }
1536
+ }
1537
+ return result;
1538
+ }
1539
+
1540
+ // src/lib/configure-rpc-config.ts
1541
+ function configureEvmRpcUrls(defaultRpcUrls, userRpcUrls, supportedChains) {
1542
+ const evmRpcUrls = Object.fromEntries(
1543
+ Object.entries(
1544
+ pick(
1545
+ Object.assign({}, defaultRpcUrls, userRpcUrls ?? {}),
1546
+ supportedChains
1547
+ )
1548
+ ).map(([caip2, urls]) => [getEIP155ChainId(caip2), urls])
1549
+ );
1550
+ for (const [chainId, urls] of Object.entries(evmRpcUrls)) {
1551
+ (0, import_internal_utils15.assert)(
1552
+ urls.length > 0,
1553
+ `EVM RPC URLs for chain ${chainId} are not provided`
1554
+ );
1555
+ }
1556
+ return evmRpcUrls;
1557
+ }
1558
+ function configureStellarRpcUrls(defaultRpcUrls, userRpcUrls) {
1559
+ const stellarRpcUrls = Object.assign(
1560
+ {},
1561
+ defaultRpcUrls,
1562
+ userRpcUrls?.[Chains.Stellar] ?? {}
1563
+ );
1564
+ for (const [key, value] of Object.entries(stellarRpcUrls)) {
1565
+ (0, import_internal_utils15.assert)(value.length > 0, `Stellar RPC URL for ${key} is not provided`);
1566
+ }
1567
+ return stellarRpcUrls;
1568
+ }
1569
+
1570
+ // src/lib/route-config-factory.ts
1571
+ function createInternalTransferRoute() {
1572
+ return { route: RouteEnum.InternalTransfer };
1573
+ }
1574
+ function createNearWithdrawalRoute(msg) {
1575
+ return { route: RouteEnum.NearWithdrawal, msg };
1576
+ }
1577
+ function createVirtualChainRoute(auroraEngineContractId, proxyTokenContractId) {
1578
+ return {
1579
+ route: RouteEnum.VirtualChain,
1580
+ auroraEngineContractId,
1581
+ proxyTokenContractId
1582
+ };
1583
+ }
1584
+ function createPoaBridgeRoute(chain) {
1585
+ return {
1586
+ route: RouteEnum.PoaBridge,
1587
+ chain
1588
+ };
1589
+ }
1590
+ function createHotBridgeRoute(chain) {
1591
+ return {
1592
+ route: RouteEnum.HotBridge,
1593
+ chain
1594
+ };
1595
+ }
1596
+ function createDefaultRoute() {
1597
+ }
1598
+
1599
+ // src/lib/route-config.ts
1600
+ function determineRouteConfig(sdk, withdrawalParams) {
1601
+ if (withdrawalParams.routeConfig != null) {
1602
+ return withdrawalParams.routeConfig;
1603
+ }
1604
+ const parseAssetId = sdk.parseAssetId(withdrawalParams.assetId);
1605
+ const bridgeName = parseAssetId.bridgeName;
1606
+ switch (bridgeName) {
1607
+ case BridgeNameEnum.Hot:
1608
+ return {
1609
+ route: RouteEnum.HotBridge,
1610
+ chain: parseAssetId.blockchain
1611
+ };
1612
+ case BridgeNameEnum.Poa:
1613
+ return {
1614
+ route: RouteEnum.PoaBridge,
1615
+ chain: parseAssetId.blockchain
1616
+ };
1617
+ case BridgeNameEnum.None:
1618
+ return createNearWithdrawalRoute();
1619
+ default:
1620
+ bridgeName;
1621
+ throw new Error(`Unexpected bridge = ${bridgeName}`);
1622
+ }
1623
+ }
1624
+
1625
+ // src/sdk.ts
1626
+ var IntentsSDK = class {
1627
+ constructor(args) {
1628
+ this.env = args.env ?? "production";
1629
+ this.referral = args.referral;
1630
+ const nearRpcUrls = args.rpc?.[Chains.Near] ?? import_internal_utils16.PUBLIC_NEAR_RPC_URLS;
1631
+ (0, import_internal_utils16.assert)(nearRpcUrls.length > 0, "NEAR RPC URLs are not provided");
1632
+ const nearProvider = (0, import_internal_utils16.nearFailoverRpcProvider)({ urls: nearRpcUrls });
1633
+ const stellarRpcUrls = configureStellarRpcUrls(
1634
+ PUBLIC_STELLAR_RPC_URLS,
1635
+ args.rpc
1636
+ );
1637
+ const evmRpcUrls = configureEvmRpcUrls(
1638
+ PUBLIC_EVM_RPC_URLS,
1639
+ args.rpc,
1640
+ HotBridgeEVMChains
1641
+ );
1642
+ this.bridges = [
1643
+ new IntentsBridge(),
1644
+ new AuroraEngineBridge({
1645
+ env: this.env,
1646
+ nearProvider
1647
+ }),
1648
+ new PoaBridge({ env: this.env }),
1649
+ new HotBridge({
1650
+ env: this.env,
1651
+ hotSdk: new import_omni_sdk3.default.HotBridge({
1652
+ logger: console,
1653
+ evmRpc: evmRpcUrls,
1654
+ // 1. HotBridge from omni-sdk does not support FailoverProvider.
1655
+ // 2. omni-sdk has near-api-js@5.0.1, and it uses `instanceof` which doesn't work when multiple versions of packages are installed
1656
+ nearRpc: nearRpcUrls,
1657
+ stellarRpc: stellarRpcUrls.soroban,
1658
+ stellarHorizonRpc: stellarRpcUrls.horizon,
1659
+ async executeNearTransaction() {
1660
+ throw new Error("not implemented");
1661
+ }
1662
+ })
1663
+ }),
1664
+ new DirectBridge({
1665
+ env: this.env,
1666
+ nearProvider
1667
+ })
1668
+ ];
1669
+ this.intentRelayer = new IntentRelayerPublic({ env: this.env });
1670
+ this.intentSigner = args.intentSigner;
1671
+ }
1672
+ setIntentSigner(signer) {
1673
+ this.intentSigner = signer;
1674
+ }
1675
+ async createWithdrawalIntents(args) {
1676
+ for (const bridge of this.bridges) {
1677
+ if (bridge.supports(args.withdrawalParams)) {
1678
+ const actualAmount = args.withdrawalParams.feeInclusive ? args.withdrawalParams.amount - args.feeEstimation.amount : args.withdrawalParams.amount;
1679
+ await bridge.validateWithdrawal({
1680
+ assetId: args.withdrawalParams.assetId,
1681
+ amount: actualAmount,
1682
+ destinationAddress: args.withdrawalParams.destinationAddress,
1683
+ logger: args.logger
1684
+ });
1685
+ return bridge.createWithdrawalIntents({
1686
+ withdrawalParams: {
1687
+ ...args.withdrawalParams,
1688
+ amount: actualAmount
1689
+ },
1690
+ feeEstimation: args.feeEstimation,
1691
+ referral: args.referral ?? this.referral
1692
+ });
1693
+ }
1694
+ }
1695
+ throw new Error(
1696
+ `Cannot determine bridge for withdrawal = ${(0, import_viem2.stringify)(args.withdrawalParams)}`
1697
+ );
1698
+ }
1699
+ estimateWithdrawalFee(args) {
1700
+ if (!Array.isArray(args.withdrawalParams)) {
1701
+ return this._estimateWithdrawalFee({
1702
+ ...args,
1703
+ withdrawalParams: args.withdrawalParams
1704
+ });
1705
+ }
1706
+ return Promise.all(
1707
+ args.withdrawalParams.map(
1708
+ (withdrawalParams) => this._estimateWithdrawalFee({
1709
+ ...args,
1710
+ withdrawalParams
1711
+ })
1712
+ )
1713
+ );
1714
+ }
1715
+ async _estimateWithdrawalFee(args) {
1716
+ for (const bridge of this.bridges) {
1717
+ if (bridge.supports(args.withdrawalParams)) {
1718
+ const fee = await bridge.estimateWithdrawalFee({
1719
+ withdrawalParams: args.withdrawalParams,
1720
+ quoteOptions: args.quoteOptions,
1721
+ logger: args.logger
1722
+ });
1723
+ if (args.withdrawalParams.feeInclusive) {
1724
+ if (args.withdrawalParams.amount <= fee.amount) {
1725
+ throw new FeeExceedsAmountError(fee, args.withdrawalParams.amount);
1726
+ }
1727
+ }
1728
+ return fee;
1729
+ }
1730
+ }
1731
+ throw new Error(
1732
+ `Cannot determine bridge for withdrawal = ${(0, import_viem2.stringify)(args.withdrawalParams)}`
1733
+ );
1734
+ }
1735
+ getWithdrawalsIdentifiers({
1736
+ withdrawalParams,
1737
+ intentTx
1738
+ }) {
1739
+ const indexes = new Map(
1740
+ zip(
1741
+ withdrawalParams.map((w) => {
1742
+ const routeConfig = determineRouteConfig(this, w);
1743
+ return routeConfig.route;
1744
+ }),
1745
+ Array(withdrawalParams.length).fill(0)
1746
+ )
1747
+ );
1748
+ return withdrawalParams.map((w) => {
1749
+ const routeConfig = determineRouteConfig(this, w);
1750
+ const route = routeConfig.route;
1751
+ const index = indexes.get(route);
1752
+ (0, import_internal_utils16.assert)(index != null, "Index is not found for route");
1753
+ indexes.set(route, index + 1);
1754
+ return {
1755
+ routeConfig,
1756
+ index,
1757
+ tx: intentTx
1758
+ };
1759
+ });
1760
+ }
1761
+ async waitForWithdrawalCompletion(args) {
1762
+ const wids = this.getWithdrawalsIdentifiers({
1763
+ withdrawalParams: Array.isArray(args.withdrawalParams) ? args.withdrawalParams : [args.withdrawalParams],
1764
+ intentTx: args.intentTx
1765
+ });
1766
+ const result = await Promise.all(
1767
+ wids.map((wid) => {
1768
+ for (const bridge of this.bridges) {
1769
+ if (bridge.is(wid.routeConfig)) {
1770
+ return bridge.waitForWithdrawalCompletion({
1771
+ tx: args.intentTx,
1772
+ index: wid.index,
1773
+ routeConfig: wid.routeConfig,
1774
+ signal: args.signal,
1775
+ retryOptions: args.retryOptions,
1776
+ logger: args.logger
1777
+ });
1778
+ }
1779
+ }
1780
+ throw new Error(`Unsupported route = ${(0, import_viem2.stringify)(wid.routeConfig)}`);
1781
+ })
1782
+ );
1783
+ if (Array.isArray(args.withdrawalParams)) {
1784
+ return result;
1785
+ }
1786
+ (0, import_internal_utils16.assert)(result.length === 1, "Unexpected result length");
1787
+ return result[0];
1788
+ }
1789
+ parseAssetId(assetId) {
1790
+ for (const bridge of this.bridges) {
1791
+ const parsed = bridge.parseAssetId(assetId);
1792
+ if (parsed != null) {
1793
+ return parsed;
1794
+ }
1795
+ }
1796
+ throw new Error(`Cannot determine bridge for assetId = ${assetId}`);
1797
+ }
1798
+ async signAndSendIntent(args) {
1799
+ const intentSigner = args.signer ?? this.intentSigner;
1800
+ (0, import_internal_utils16.assert)(intentSigner != null, "Intent signer is not provided");
1801
+ const intentExecuter = new IntentExecuter({
1802
+ env: this.env,
1803
+ logger: args.logger,
1804
+ intentSigner,
1805
+ intentRelayer: this.intentRelayer,
1806
+ intentPayloadFactory: args.payload,
1807
+ onBeforePublishIntent: args.onBeforePublishIntent
1808
+ });
1809
+ const { ticket } = await intentExecuter.signAndSendIntent({
1810
+ intents: args.intents,
1811
+ relayParams: args.relayParams
1812
+ });
1813
+ return { intentHash: ticket };
1814
+ }
1815
+ async signAndSendWithdrawalIntent(args) {
1816
+ let withdrawalParamsArray;
1817
+ let feeEstimations;
1818
+ if (isBatchMode(args)) {
1819
+ withdrawalParamsArray = args.withdrawalParams;
1820
+ feeEstimations = args.feeEstimation;
1821
+ } else {
1822
+ withdrawalParamsArray = [args.withdrawalParams];
1823
+ feeEstimations = [args.feeEstimation];
1824
+ }
1825
+ const intentsP = zip(withdrawalParamsArray, feeEstimations).map(
1826
+ ([withdrawalParams, feeEstimation]) => {
1827
+ return this.createWithdrawalIntents({
1828
+ withdrawalParams,
1829
+ feeEstimation,
1830
+ referral: args.referral ?? this.referral,
1831
+ logger: args.logger
1832
+ });
1833
+ }
1834
+ );
1835
+ const intents = (await Promise.all(intentsP)).flat();
1836
+ const relayParamsFn = async () => {
1837
+ const relayParams = args.intent?.relayParams != null ? await args.intent?.relayParams() : { quoteHashes: void 0 };
1838
+ const quoteHashes = relayParams.quoteHashes ?? [];
1839
+ for (const fee of feeEstimations) {
1840
+ if (fee.quote != null) {
1841
+ quoteHashes.push(fee.quote.quote_hash);
1842
+ }
1843
+ }
1844
+ return { ...relayParams, quoteHashes };
1845
+ };
1846
+ return this.signAndSendIntent({
1847
+ intents,
1848
+ signer: args.intent?.signer,
1849
+ onBeforePublishIntent: args.intent?.onBeforePublishIntent,
1850
+ relayParams: relayParamsFn,
1851
+ payload: args.intent?.payload,
1852
+ logger: args.logger
1853
+ });
1854
+ }
1855
+ async waitForIntentSettlement(args) {
1856
+ const intentExecuter = new IntentExecuter({
1857
+ env: this.env,
1858
+ logger: args.logger,
1859
+ intentSigner: noopIntentSigner,
1860
+ intentRelayer: this.intentRelayer
1861
+ });
1862
+ const { tx } = await intentExecuter.waitForSettlement(args.intentHash);
1863
+ return tx;
1864
+ }
1865
+ async getIntentStatus({
1866
+ intentHash,
1867
+ logger
1868
+ }) {
1869
+ return import_internal_utils16.solverRelay.getStatus(
1870
+ {
1871
+ intent_hash: intentHash
1872
+ },
1873
+ {
1874
+ baseURL: import_internal_utils16.configsByEnvironment[this.env].solverRelayBaseURL,
1875
+ logger
1876
+ }
1877
+ );
1878
+ }
1879
+ async processWithdrawal(args) {
1880
+ const withdrawalParams = Array.isArray(args.withdrawalParams) ? args.withdrawalParams : [args.withdrawalParams];
1881
+ const feeEstimation = await (() => {
1882
+ if (args.feeEstimation != null) {
1883
+ return Array.isArray(args.feeEstimation) ? args.feeEstimation : [args.feeEstimation];
1884
+ }
1885
+ return this.estimateWithdrawalFee({
1886
+ withdrawalParams,
1887
+ logger: args.logger
1888
+ });
1889
+ })();
1890
+ const { intentHash } = await this.signAndSendWithdrawalIntent({
1891
+ withdrawalParams,
1892
+ feeEstimation,
1893
+ referral: args.referral,
1894
+ intent: args.intent,
1895
+ logger: args.logger
1896
+ });
1897
+ const intentTx = await this.waitForIntentSettlement({
1898
+ intentHash,
1899
+ logger: args.logger
1900
+ });
1901
+ const destinationTx = await this.waitForWithdrawalCompletion({
1902
+ withdrawalParams,
1903
+ intentTx,
1904
+ logger: args.logger,
1905
+ retryOptions: import_internal_utils16.RETRY_CONFIGS.FIVE_MINS_STEADY
1906
+ });
1907
+ if (!Array.isArray(args.withdrawalParams)) {
1908
+ return {
1909
+ // biome-ignore lint/style/noNonNullAssertion: <explanation>
1910
+ feeEstimation: feeEstimation[0],
1911
+ intentHash,
1912
+ intentTx,
1913
+ // biome-ignore lint/style/noNonNullAssertion: <explanation>
1914
+ destinationTx: destinationTx[0]
1915
+ };
1916
+ }
1917
+ return {
1918
+ feeEstimation,
1919
+ intentHash,
1920
+ intentTx,
1921
+ destinationTx
1922
+ };
1923
+ }
1924
+ };
1925
+ function isBatchMode(args) {
1926
+ return Array.isArray(args.withdrawalParams);
1927
+ }
1928
+
1929
+ // src/intents/intent-signer-impl/intent-signer-near-keypair.ts
1930
+ var import_base4 = require("@scure/base");
1931
+
1932
+ // src/intents/intent-signer-impl/intent-signer-nep413.ts
1933
+ var import_base3 = require("@scure/base");
1934
+ var IntentSignerNEP413 = class {
1935
+ constructor({ signMessage, accountId }) {
1936
+ this.signMessage = signMessage;
1937
+ this.accountId = accountId;
1938
+ }
1939
+ async signIntent(intent) {
1940
+ const nep413Payload = {
1941
+ recipient: intent.verifying_contract,
1942
+ nonce: Array.from(import_base3.base64.decode(intent.nonce)),
1943
+ message: JSON.stringify({
1944
+ deadline: intent.deadline,
1945
+ intents: intent.intents,
1946
+ signer_id: intent.signer_id ?? this.accountId
1947
+ })
1948
+ };
1949
+ const nep413Hash = await hashNEP413Message(nep413Payload);
1950
+ const { publicKey, signature } = await this.signMessage(
1951
+ nep413Payload,
1952
+ nep413Hash
1953
+ );
1954
+ const signatureFormatted = signature.startsWith("ed25519:") ? signature : `ed25519:${import_base3.base58.encode(import_base3.base64.decode(signature))}`;
1955
+ return {
1956
+ standard: "nep413",
1957
+ payload: {
1958
+ ...nep413Payload,
1959
+ nonce: intent.nonce
1960
+ },
1961
+ public_key: publicKey,
1962
+ signature: signatureFormatted
1963
+ };
1964
+ }
1965
+ };
1966
+
1967
+ // src/intents/intent-signer-impl/intent-signer-near-keypair.ts
1968
+ var IntentSignerNearKeypair = class extends IntentSignerNEP413 {
1969
+ constructor({ keypair, accountId }) {
1970
+ super({
1971
+ signMessage: (_nep413Payload, nep413Hash) => {
1972
+ const { publicKey, signature } = keypair.sign(nep413Hash);
1973
+ return {
1974
+ publicKey: publicKey.toString(),
1975
+ signature: import_base4.base64.encode(signature)
1976
+ };
1977
+ },
1978
+ accountId
1979
+ });
1980
+ }
1981
+ };
1982
+
1983
+ // src/intents/intent-signer-impl/intent-signer-viem.ts
1984
+ var import_internal_utils17 = require("@defuse-protocol/internal-utils");
1985
+ var IntentSignerViem = class {
1986
+ constructor(account) {
1987
+ this.account = account;
1988
+ }
1989
+ async signIntent(intent) {
1990
+ const payload = JSON.stringify({
1991
+ signer_id: intent.signer_id ?? import_internal_utils17.utils.authHandleToIntentsUserId({
1992
+ identifier: this.account.address,
1993
+ method: "evm"
1994
+ }),
1995
+ verifying_contract: intent.verifying_contract,
1996
+ deadline: intent.deadline,
1997
+ nonce: intent.nonce,
1998
+ intents: intent.intents
1999
+ });
2000
+ const signature = await this.account.signMessage?.({
2001
+ message: payload
2002
+ });
2003
+ if (signature == null) {
2004
+ throw new Error("No signature is returned");
2005
+ }
2006
+ return {
2007
+ standard: "erc191",
2008
+ payload,
2009
+ signature: import_internal_utils17.utils.transformERC191Signature(signature)
2010
+ };
2011
+ }
2012
+ };
2013
+
2014
+ // src/intents/intent-signer-impl/factories.ts
2015
+ function createIntentSignerNEP413(config) {
2016
+ return new IntentSignerNEP413(config);
2017
+ }
2018
+ function createIntentSignerNearKeyPair(config) {
2019
+ return new IntentSignerNearKeypair(config);
2020
+ }
2021
+ function createIntentSignerViem(config) {
2022
+ return new IntentSignerViem(config);
2023
+ }
2024
+
2025
+ // index.ts
2026
+ var import_internal_utils18 = require("@defuse-protocol/internal-utils");
2027
+ var import_internal_utils19 = require("@defuse-protocol/internal-utils");
2028
+ var import_internal_utils20 = require("@defuse-protocol/internal-utils");
2029
+ var import_internal_utils21 = require("@defuse-protocol/internal-utils");
2030
+ var import_internal_utils22 = require("@defuse-protocol/internal-utils");
2031
+ // Annotate the CommonJS export names for ESM import in node:
2032
+ 0 && (module.exports = {
2033
+ AssertionError,
2034
+ BaseError,
2035
+ BridgeNameEnum,
2036
+ Chains,
2037
+ FeeExceedsAmountError,
2038
+ HotWithdrawalCancelledError,
2039
+ HotWithdrawalNotFoundError,
2040
+ HotWithdrawalPendingError,
2041
+ HttpRequestError,
2042
+ IntentSettlementError,
2043
+ IntentsSDK,
2044
+ MinWithdrawalAmountError,
2045
+ PoaWithdrawalInvariantError,
2046
+ PoaWithdrawalNotFoundError,
2047
+ PoaWithdrawalPendingError,
2048
+ QuoteError,
2049
+ RelayPublishError,
2050
+ RouteEnum,
2051
+ RpcRequestError,
2052
+ TimeoutError,
2053
+ TrustlineNotFoundError,
2054
+ UnsupportedDestinationMemoError,
2055
+ createDefaultRoute,
2056
+ createHotBridgeRoute,
2057
+ createIntentSignerNEP413,
2058
+ createIntentSignerNearKeyPair,
2059
+ createIntentSignerViem,
2060
+ createInternalTransferRoute,
2061
+ createNearWithdrawalRoute,
2062
+ createPoaBridgeRoute,
2063
+ createVirtualChainRoute
2064
+ });
2065
+ /*! Bundled license information:
2066
+
2067
+ @noble/hashes/esm/utils.js:
2068
+ (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
2069
+ */