@defuse-protocol/intents-sdk 0.18.0 → 0.19.1

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 CHANGED
@@ -30,28 +30,28 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- AssertionError: () => import_internal_utils25.AssertionError,
34
- BaseError: () => import_internal_utils22.BaseError,
33
+ AssertionError: () => import_internal_utils27.AssertionError,
34
+ BaseError: () => import_internal_utils24.BaseError,
35
35
  BridgeNameEnum: () => BridgeNameEnum,
36
36
  Chains: () => Chains,
37
37
  FeeExceedsAmountError: () => FeeExceedsAmountError,
38
38
  HotWithdrawalCancelledError: () => HotWithdrawalCancelledError,
39
39
  HotWithdrawalNotFoundError: () => HotWithdrawalNotFoundError,
40
40
  HotWithdrawalPendingError: () => HotWithdrawalPendingError,
41
- HttpRequestError: () => import_internal_utils24.HttpRequestError,
42
- IntentSettlementError: () => import_internal_utils26.IntentSettlementError,
41
+ HttpRequestError: () => import_internal_utils26.HttpRequestError,
42
+ IntentSettlementError: () => import_internal_utils28.IntentSettlementError,
43
43
  IntentsSDK: () => IntentsSDK,
44
44
  MinWithdrawalAmountError: () => MinWithdrawalAmountError,
45
45
  OmniTransferDestinationChainHashNotFoundError: () => OmniTransferDestinationChainHashNotFoundError,
46
46
  OmniTransferNotFoundError: () => OmniTransferNotFoundError,
47
- PoaWithdrawalInvariantError: () => import_internal_utils23.PoaWithdrawalInvariantError,
48
- PoaWithdrawalNotFoundError: () => import_internal_utils23.PoaWithdrawalNotFoundError,
49
- PoaWithdrawalPendingError: () => import_internal_utils23.PoaWithdrawalPendingError,
50
- QuoteError: () => import_internal_utils26.QuoteError,
51
- RelayPublishError: () => import_internal_utils26.RelayPublishError,
47
+ PoaWithdrawalInvariantError: () => import_internal_utils25.PoaWithdrawalInvariantError,
48
+ PoaWithdrawalNotFoundError: () => import_internal_utils25.PoaWithdrawalNotFoundError,
49
+ PoaWithdrawalPendingError: () => import_internal_utils25.PoaWithdrawalPendingError,
50
+ QuoteError: () => import_internal_utils28.QuoteError,
51
+ RelayPublishError: () => import_internal_utils28.RelayPublishError,
52
52
  RouteEnum: () => RouteEnum,
53
- RpcRequestError: () => import_internal_utils24.RpcRequestError,
54
- TimeoutError: () => import_internal_utils24.TimeoutError,
53
+ RpcRequestError: () => import_internal_utils26.RpcRequestError,
54
+ TimeoutError: () => import_internal_utils26.TimeoutError,
55
55
  TokenNotFoundInDestinationChainError: () => TokenNotFoundInDestinationChainError,
56
56
  TokenNotSupportedByOmniRelayerError: () => TokenNotSupportedByOmniRelayerError,
57
57
  TrustlineNotFoundError: () => TrustlineNotFoundError,
@@ -71,7 +71,7 @@ __export(index_exports, {
71
71
  module.exports = __toCommonJS(index_exports);
72
72
 
73
73
  // src/sdk.ts
74
- var import_internal_utils19 = require("@defuse-protocol/internal-utils");
74
+ var import_internal_utils22 = require("@defuse-protocol/internal-utils");
75
75
  var import_omni_sdk4 = __toESM(require("@hot-labs/omni-sdk"), 1);
76
76
  var import_viem2 = require("viem");
77
77
 
@@ -958,14 +958,432 @@ var IntentsBridge = class {
958
958
  }
959
959
  };
960
960
 
961
- // src/bridges/poa-bridge/poa-bridge.ts
962
- var import_internal_utils14 = require("@defuse-protocol/internal-utils");
961
+ // src/bridges/omni-bridge/omni-bridge.ts
962
+ var import_internal_utils15 = require("@defuse-protocol/internal-utils");
963
963
  var import_ttlcache = __toESM(require("@isaacs/ttlcache"), 1);
964
+ var import_attempt2 = require("@lifeomic/attempt");
965
+ var import_omni_bridge_sdk2 = require("omni-bridge-sdk");
964
966
 
965
- // src/bridges/poa-bridge/poa-bridge-utils.ts
967
+ // src/bridges/omni-bridge/error.ts
966
968
  var import_internal_utils13 = require("@defuse-protocol/internal-utils");
969
+ var OmniTransferNotFoundError = class extends import_internal_utils13.BaseError {
970
+ constructor(txHash) {
971
+ super("Omni transfer with given hash is not found in the relayer.", {
972
+ metaMessages: [`OriginTxHash: ${txHash}`],
973
+ name: "OmniTransferNotFoundError"
974
+ });
975
+ this.txHash = txHash;
976
+ }
977
+ };
978
+ var OmniTransferDestinationChainHashNotFoundError = class extends import_internal_utils13.BaseError {
979
+ constructor(txHash, destinationChain) {
980
+ super("Relayer did not return destination chain hash for a transfer.", {
981
+ metaMessages: [
982
+ `OriginTxHash: ${txHash}`,
983
+ `DestinationChain: ${destinationChain}`
984
+ ],
985
+ name: "OmniTransferDestinationChainHashNotFoundError"
986
+ });
987
+ this.txHash = txHash;
988
+ this.destinationChain = destinationChain;
989
+ }
990
+ };
991
+ var TokenNotFoundInDestinationChainError = class extends import_internal_utils13.BaseError {
992
+ constructor(token, destinationChain) {
993
+ super(
994
+ `The token ${token} doesn't exist in destination chain ${destinationChain}`,
995
+ {
996
+ metaMessages: [
997
+ `Token: ${token}`,
998
+ `Destination Chain: ${destinationChain}`
999
+ ],
1000
+ name: "TokenNotFoundInDestinationChainError"
1001
+ }
1002
+ );
1003
+ this.token = token;
1004
+ this.destinationChain = destinationChain;
1005
+ }
1006
+ };
1007
+ var TokenNotSupportedByOmniRelayerError = class extends import_internal_utils13.BaseError {
1008
+ constructor(token) {
1009
+ super(`Omni Relayer doesn't accept fee in the transferred token ${token}`, {
1010
+ metaMessages: [`Token: ${token}`],
1011
+ name: "TokenNotSupportedByOmniRelayerError"
1012
+ });
1013
+ this.token = token;
1014
+ }
1015
+ };
1016
+
1017
+ // src/bridges/omni-bridge/omni-bridge-constants.ts
1018
+ var NEAR_NATIVE_ASSET_ID3 = "nep141:wrap.near";
1019
+ var OMNI_BRIDGE_CONTRACT = "omni.bridge.near";
1020
+
1021
+ // src/bridges/omni-bridge/omni-bridge-utils.ts
1022
+ var import_internal_utils14 = require("@defuse-protocol/internal-utils");
1023
+ var import_omni_bridge_sdk = require("omni-bridge-sdk");
967
1024
  function createWithdrawIntentPrimitive3(params) {
968
- const { contractId: tokenAccountId } = import_internal_utils13.utils.parseDefuseAssetId(
1025
+ const { contractId: tokenAccountId, standard } = import_internal_utils14.utils.parseDefuseAssetId(
1026
+ params.assetId
1027
+ );
1028
+ (0, import_internal_utils14.assert)(standard === "nep141", "Only NEP-141 is supported");
1029
+ return {
1030
+ intent: "ft_withdraw",
1031
+ token: tokenAccountId,
1032
+ receiver_id: OMNI_BRIDGE_CONTRACT,
1033
+ amount: params.amount.toString(),
1034
+ storage_deposit: params.storageDeposit > 0n ? params.storageDeposit.toString() : null,
1035
+ msg: JSON.stringify({
1036
+ recipient: (0, import_omni_bridge_sdk.omniAddress)(params.omniChainKind, params.destinationAddress),
1037
+ fee: params.transferredTokenFee.toString(),
1038
+ native_token_fee: "0"
1039
+ })
1040
+ };
1041
+ }
1042
+ function caip2ToChainKind(network) {
1043
+ switch (network) {
1044
+ case Chains.Ethereum:
1045
+ return import_omni_bridge_sdk.ChainKind.Eth;
1046
+ case Chains.Base:
1047
+ return import_omni_bridge_sdk.ChainKind.Base;
1048
+ case Chains.Arbitrum:
1049
+ return import_omni_bridge_sdk.ChainKind.Arb;
1050
+ case Chains.Solana:
1051
+ return import_omni_bridge_sdk.ChainKind.Sol;
1052
+ // case Chains.Bitcoin:
1053
+ // return ChainKind.Btc;
1054
+ default:
1055
+ return null;
1056
+ }
1057
+ }
1058
+ function chainKindToCaip2(network) {
1059
+ switch (network) {
1060
+ case import_omni_bridge_sdk.ChainKind.Eth:
1061
+ return Chains.Ethereum;
1062
+ case import_omni_bridge_sdk.ChainKind.Base:
1063
+ return Chains.Base;
1064
+ case import_omni_bridge_sdk.ChainKind.Arb:
1065
+ return Chains.Arbitrum;
1066
+ case import_omni_bridge_sdk.ChainKind.Sol:
1067
+ return Chains.Solana;
1068
+ // case ChainKind.Btc:
1069
+ // return Chains.Bitcoin;
1070
+ default:
1071
+ return null;
1072
+ }
1073
+ }
1074
+ var CHAIN_PATTERNS = {
1075
+ "nbtc.bridge.near": import_omni_bridge_sdk.ChainKind.Btc,
1076
+ "eth.bridge.near": import_omni_bridge_sdk.ChainKind.Eth,
1077
+ "sol.omdep.near": import_omni_bridge_sdk.ChainKind.Sol,
1078
+ "base.omdep.near": import_omni_bridge_sdk.ChainKind.Base,
1079
+ "arb.omdep.near": import_omni_bridge_sdk.ChainKind.Arb
1080
+ };
1081
+ function validateOmniToken(nearAddress) {
1082
+ return nearAddress in CHAIN_PATTERNS || /\.(omdep\.near|factory\.bridge\.near)$/.test(nearAddress);
1083
+ }
1084
+
1085
+ // src/bridges/omni-bridge/omni-bridge.ts
1086
+ var OmniBridge = class {
1087
+ // 10800000 - 3 hours
1088
+ constructor({
1089
+ env,
1090
+ nearProvider
1091
+ }) {
1092
+ this.storageDepositCache = new import_ttlcache.default({ ttl: 108e5 });
1093
+ // 10800000 - 3 hours
1094
+ this.destinationChainAddressCache = new import_ttlcache.default({ ttl: 108e5 });
1095
+ this.env = env;
1096
+ this.nearProvider = nearProvider;
1097
+ this.omniBridgeAPI = new import_omni_bridge_sdk2.OmniBridgeAPI();
1098
+ }
1099
+ is(routeConfig) {
1100
+ return routeConfig.route === RouteEnum.OmniBridge;
1101
+ }
1102
+ async supports(params) {
1103
+ if (params.routeConfig && !this.is(params.routeConfig)) {
1104
+ return false;
1105
+ }
1106
+ const parsed = parseDefuseAssetId(params.assetId);
1107
+ const omniBridgeSetWithNoChain = Boolean(
1108
+ params.routeConfig && params.routeConfig.route === RouteEnum.OmniBridge && params.routeConfig.chain === void 0
1109
+ );
1110
+ const targetChainSpecified = this.targetChainSpecified(params.routeConfig);
1111
+ const nonValidStandard = parsed.standard !== "nep141";
1112
+ if (nonValidStandard && (omniBridgeSetWithNoChain || targetChainSpecified)) {
1113
+ throw new UnsupportedAssetIdError(
1114
+ params.assetId,
1115
+ `Only NEP-141 tokens are supported by Omni Bridge.`
1116
+ );
1117
+ }
1118
+ if (nonValidStandard) return false;
1119
+ const nonValidToken = validateOmniToken(parsed.contractId) === false;
1120
+ if (nonValidToken && omniBridgeSetWithNoChain) {
1121
+ throw new UnsupportedAssetIdError(
1122
+ params.assetId,
1123
+ `Non valid omni contract id ${parsed.contractId}`
1124
+ );
1125
+ }
1126
+ if (!targetChainSpecified && nonValidToken) return false;
1127
+ let omniChainKind = null;
1128
+ let caip2Chain = null;
1129
+ if (this.targetChainSpecified(params.routeConfig)) {
1130
+ omniChainKind = caip2ToChainKind(params.routeConfig.chain);
1131
+ if (omniChainKind === null) {
1132
+ throw new UnsupportedAssetIdError(
1133
+ params.assetId,
1134
+ `Chain ${params.routeConfig.chain} is not supported in Omni Bridge.`
1135
+ );
1136
+ }
1137
+ caip2Chain = params.routeConfig.chain;
1138
+ } else {
1139
+ omniChainKind = (0, import_omni_bridge_sdk2.parseOriginChain)(parsed.contractId);
1140
+ if (omniChainKind === null) {
1141
+ throw new UnsupportedAssetIdError(
1142
+ params.assetId,
1143
+ `Withdrawal of ${parsed.contractId} to its origin chain is not supported in Omni Bridge.`
1144
+ );
1145
+ }
1146
+ caip2Chain = chainKindToCaip2(omniChainKind);
1147
+ if (caip2Chain === null) {
1148
+ throw new UnsupportedAssetIdError(
1149
+ params.assetId,
1150
+ `Withdrawal of ${parsed.contractId} to its origin chain is not supported in Omni Bridge.`
1151
+ );
1152
+ }
1153
+ }
1154
+ const tokenOnDestinationNetwork = await this.getCachedDestinationTokenAddress(
1155
+ parsed.contractId,
1156
+ omniChainKind
1157
+ );
1158
+ if (tokenOnDestinationNetwork === null) {
1159
+ throw new TokenNotFoundInDestinationChainError(
1160
+ params.assetId,
1161
+ caip2Chain
1162
+ );
1163
+ }
1164
+ return true;
1165
+ }
1166
+ targetChainSpecified(routeConfig) {
1167
+ return Boolean(
1168
+ routeConfig?.route && routeConfig.route === RouteEnum.OmniBridge && routeConfig.chain
1169
+ );
1170
+ }
1171
+ parseAssetId(assetId) {
1172
+ const parsed = parseDefuseAssetId(assetId);
1173
+ if (parsed.standard !== "nep141") return null;
1174
+ const omniChainKind = (0, import_omni_bridge_sdk2.parseOriginChain)(parsed.contractId);
1175
+ if (omniChainKind === null) return null;
1176
+ const blockchain = chainKindToCaip2(omniChainKind);
1177
+ if (blockchain === null) return null;
1178
+ return Object.assign(parsed, {
1179
+ blockchain,
1180
+ bridgeName: BridgeNameEnum.Omni,
1181
+ address: parsed.contractId
1182
+ });
1183
+ }
1184
+ makeAssetInfo(assetId, routeConfig) {
1185
+ const parsed = parseDefuseAssetId(assetId);
1186
+ if (parsed.standard !== "nep141") return null;
1187
+ let omniChainKind = null;
1188
+ let blockchain = null;
1189
+ if (this.targetChainSpecified(routeConfig)) {
1190
+ omniChainKind = caip2ToChainKind(routeConfig.chain);
1191
+ blockchain = routeConfig.chain;
1192
+ } else {
1193
+ omniChainKind = (0, import_omni_bridge_sdk2.parseOriginChain)(parsed.contractId);
1194
+ if (omniChainKind === null) return null;
1195
+ blockchain = chainKindToCaip2(omniChainKind);
1196
+ }
1197
+ if (omniChainKind === null || blockchain === null) return null;
1198
+ return Object.assign(parsed, {
1199
+ blockchain,
1200
+ bridgeName: BridgeNameEnum.Omni,
1201
+ address: parsed.contractId
1202
+ });
1203
+ }
1204
+ async createWithdrawalIntents(args) {
1205
+ const assetInfo = this.makeAssetInfo(
1206
+ args.withdrawalParams.assetId,
1207
+ args.withdrawalParams.routeConfig
1208
+ );
1209
+ (0, import_internal_utils15.assert)(
1210
+ assetInfo !== null,
1211
+ `Asset ${args.withdrawalParams.assetId} is not supported by Omni Bridge`
1212
+ );
1213
+ const intents = [];
1214
+ if (args.feeEstimation.quote !== null) {
1215
+ intents.push({
1216
+ intent: "token_diff",
1217
+ diff: {
1218
+ [args.feeEstimation.quote.defuse_asset_identifier_in]: `-${args.feeEstimation.quote.amount_in}`,
1219
+ [args.feeEstimation.quote.defuse_asset_identifier_out]: args.feeEstimation.quote.amount_out
1220
+ },
1221
+ referral: args.referral
1222
+ });
1223
+ }
1224
+ const omniChainKind = caip2ToChainKind(assetInfo.blockchain);
1225
+ (0, import_internal_utils15.assert)(
1226
+ omniChainKind !== null,
1227
+ `Chain ${assetInfo.blockchain} is not supported by Omni Bridge`
1228
+ );
1229
+ const intent = createWithdrawIntentPrimitive3({
1230
+ assetId: args.withdrawalParams.assetId,
1231
+ destinationAddress: args.withdrawalParams.destinationAddress,
1232
+ amount: args.withdrawalParams.amount + args.feeEstimation.amount,
1233
+ omniChainKind,
1234
+ storageDeposit: args.feeEstimation.quote ? BigInt(args.feeEstimation.quote.amount_out) : 0n,
1235
+ transferredTokenFee: args.feeEstimation.amount
1236
+ });
1237
+ intents.push(intent);
1238
+ return Promise.resolve(intents);
1239
+ }
1240
+ async validateWithdrawal(args) {
1241
+ (0, import_internal_utils15.assert)(
1242
+ args.feeEstimation.amount > 0n,
1243
+ `Fee must be greater than zero. Current fee is ${args.feeEstimation.amount}.`
1244
+ );
1245
+ return;
1246
+ }
1247
+ async estimateWithdrawalFee(args) {
1248
+ const assetInfo = this.makeAssetInfo(
1249
+ args.withdrawalParams.assetId,
1250
+ args.withdrawalParams.routeConfig
1251
+ );
1252
+ (0, import_internal_utils15.assert)(
1253
+ assetInfo !== null,
1254
+ `Asset ${args.withdrawalParams.assetId} is not supported by Omni Bridge`
1255
+ );
1256
+ const omniChainKind = caip2ToChainKind(assetInfo.blockchain);
1257
+ (0, import_internal_utils15.assert)(
1258
+ omniChainKind !== null,
1259
+ `Chain ${assetInfo.blockchain} is not supported by Omni Bridge`
1260
+ );
1261
+ const fee = await this.omniBridgeAPI.getFee(
1262
+ (0, import_omni_bridge_sdk2.omniAddress)(import_omni_bridge_sdk2.ChainKind.Near, import_internal_utils15.configsByEnvironment[this.env].contractID),
1263
+ (0, import_omni_bridge_sdk2.omniAddress)(omniChainKind, args.withdrawalParams.destinationAddress),
1264
+ (0, import_omni_bridge_sdk2.omniAddress)(import_omni_bridge_sdk2.ChainKind.Near, assetInfo.contractId)
1265
+ );
1266
+ if (fee.transferred_token_fee === null) {
1267
+ throw new TokenNotSupportedByOmniRelayerError(
1268
+ args.withdrawalParams.assetId
1269
+ );
1270
+ }
1271
+ const [minStorageBalance, userStorageBalance] = await this.getCachedStorageDepositValue(assetInfo.contractId);
1272
+ if (minStorageBalance <= userStorageBalance) {
1273
+ return {
1274
+ amount: BigInt(fee.transferred_token_fee),
1275
+ quote: null
1276
+ };
1277
+ }
1278
+ const feeAmount = minStorageBalance - userStorageBalance;
1279
+ const feeQuote = await import_internal_utils15.solverRelay.getQuote({
1280
+ quoteParams: {
1281
+ defuse_asset_identifier_in: args.withdrawalParams.assetId,
1282
+ defuse_asset_identifier_out: NEAR_NATIVE_ASSET_ID3,
1283
+ exact_amount_out: feeAmount.toString(),
1284
+ wait_ms: args.quoteOptions?.waitMs
1285
+ },
1286
+ config: {
1287
+ baseURL: import_internal_utils15.configsByEnvironment[this.env].solverRelayBaseURL,
1288
+ logBalanceSufficient: false,
1289
+ logger: args.logger
1290
+ }
1291
+ });
1292
+ return {
1293
+ amount: BigInt(fee.transferred_token_fee) + BigInt(feeQuote.amount_in),
1294
+ quote: feeQuote
1295
+ };
1296
+ }
1297
+ async waitForWithdrawalCompletion(args) {
1298
+ return (0, import_attempt2.retry)(
1299
+ async () => {
1300
+ if (args.signal?.aborted) {
1301
+ throw args.signal.reason;
1302
+ }
1303
+ const transfer = (await this.omniBridgeAPI.findOmniTransfers({
1304
+ transaction_id: args.tx.hash,
1305
+ offset: args.index,
1306
+ limit: 1
1307
+ }))[0];
1308
+ if (!transfer) throw new OmniTransferNotFoundError(args.tx.hash);
1309
+ const destinationChain = (0, import_omni_bridge_sdk2.getChain)(
1310
+ transfer.transfer_message.recipient
1311
+ );
1312
+ let txHash = null;
1313
+ if ((0, import_omni_bridge_sdk2.isEvmChain)(destinationChain)) {
1314
+ txHash = transfer.finalised?.EVMLog?.transaction_hash;
1315
+ } else if (destinationChain === import_omni_bridge_sdk2.ChainKind.Sol) {
1316
+ txHash = transfer.finalised?.Solana?.signature;
1317
+ } else {
1318
+ return { hash: null };
1319
+ }
1320
+ if (!txHash)
1321
+ throw new OmniTransferDestinationChainHashNotFoundError(
1322
+ args.tx.hash,
1323
+ import_omni_bridge_sdk2.ChainKind[destinationChain].toLowerCase()
1324
+ );
1325
+ return { hash: txHash };
1326
+ },
1327
+ {
1328
+ ...args.retryOptions ?? import_internal_utils15.RETRY_CONFIGS.FIVE_MINS_STEADY,
1329
+ handleError: (err, ctx) => {
1330
+ if (err instanceof OmniTransferNotFoundError || err === args.signal?.reason) {
1331
+ ctx.abort();
1332
+ }
1333
+ }
1334
+ }
1335
+ );
1336
+ }
1337
+ /**
1338
+ * Gets storage deposit for a token to avoid frequent RPC calls.
1339
+ * Cache expires after one day using TTL cache.
1340
+ */
1341
+ async getCachedStorageDepositValue(contractId) {
1342
+ const cached = this.storageDepositCache.get(contractId);
1343
+ if (cached !== void 0) {
1344
+ return cached;
1345
+ }
1346
+ const data = await Promise.all([
1347
+ (0, import_internal_utils15.getNearNep141MinStorageBalance)({
1348
+ contractId,
1349
+ nearProvider: this.nearProvider
1350
+ }),
1351
+ (0, import_internal_utils15.getNearNep141StorageBalance)({
1352
+ contractId,
1353
+ accountId: OMNI_BRIDGE_CONTRACT,
1354
+ nearProvider: this.nearProvider
1355
+ })
1356
+ ]);
1357
+ this.storageDepositCache.set(contractId, data);
1358
+ return data;
1359
+ }
1360
+ /**
1361
+ * Gets cached token address on destination chain.
1362
+ * Cache expires after one day using TTL cache.
1363
+ */
1364
+ async getCachedDestinationTokenAddress(contractId, omniChainKind) {
1365
+ const key = `${omniChainKind}:${contractId}`;
1366
+ const cached = this.destinationChainAddressCache.get(key);
1367
+ if (cached !== void 0) {
1368
+ return cached;
1369
+ }
1370
+ const tokenOnDestinationNetwork = await (0, import_omni_bridge_sdk2.getBridgedToken)(
1371
+ (0, import_omni_bridge_sdk2.omniAddress)(import_omni_bridge_sdk2.ChainKind.Near, contractId),
1372
+ omniChainKind
1373
+ );
1374
+ this.destinationChainAddressCache.set(key, tokenOnDestinationNetwork);
1375
+ return tokenOnDestinationNetwork;
1376
+ }
1377
+ };
1378
+
1379
+ // src/bridges/poa-bridge/poa-bridge.ts
1380
+ var import_internal_utils17 = require("@defuse-protocol/internal-utils");
1381
+ var import_ttlcache2 = __toESM(require("@isaacs/ttlcache"), 1);
1382
+
1383
+ // src/bridges/poa-bridge/poa-bridge-utils.ts
1384
+ var import_internal_utils16 = require("@defuse-protocol/internal-utils");
1385
+ function createWithdrawIntentPrimitive4(params) {
1386
+ const { contractId: tokenAccountId } = import_internal_utils16.utils.parseDefuseAssetId(
969
1387
  params.assetId
970
1388
  );
971
1389
  return {
@@ -1040,7 +1458,7 @@ function contractIdToCaip2(contractId) {
1040
1458
  var PoaBridge = class {
1041
1459
  constructor({ env }) {
1042
1460
  // TTL cache for supported tokens with 30-second TTL
1043
- this.supportedTokensCache = new import_ttlcache.default({ ttl: 30 * 1e3 });
1461
+ this.supportedTokensCache = new import_ttlcache2.default({ ttl: 30 * 1e3 });
1044
1462
  this.env = env;
1045
1463
  }
1046
1464
  is(routeConfig) {
@@ -1063,7 +1481,7 @@ var PoaBridge = class {
1063
1481
  parseAssetId(assetId) {
1064
1482
  const parsed = parseDefuseAssetId(assetId);
1065
1483
  const contractIdSatisfies = parsed.contractId.endsWith(
1066
- `.${import_internal_utils14.configsByEnvironment[this.env].poaTokenFactoryContractID}`
1484
+ `.${import_internal_utils17.configsByEnvironment[this.env].poaTokenFactoryContractID}`
1067
1485
  );
1068
1486
  if (!contractIdSatisfies) {
1069
1487
  return null;
@@ -1085,7 +1503,7 @@ var PoaBridge = class {
1085
1503
  });
1086
1504
  }
1087
1505
  createWithdrawalIntents(args) {
1088
- const intent = createWithdrawIntentPrimitive3({
1506
+ const intent = createWithdrawIntentPrimitive4({
1089
1507
  ...args.withdrawalParams,
1090
1508
  amount: args.withdrawalParams.amount + args.feeEstimation.amount,
1091
1509
  destinationMemo: args.withdrawalParams.destinationMemo
@@ -1100,7 +1518,7 @@ var PoaBridge = class {
1100
1518
  */
1101
1519
  async validateWithdrawal(args) {
1102
1520
  const assetInfo = this.parseAssetId(args.assetId);
1103
- (0, import_internal_utils14.assert)(assetInfo != null, "Asset is not supported");
1521
+ (0, import_internal_utils17.assert)(assetInfo != null, "Asset is not supported");
1104
1522
  const { tokens: tokens2 } = await this.getCachedSupportedTokens(
1105
1523
  [toPoaNetwork(assetInfo.blockchain)],
1106
1524
  args.logger
@@ -1121,16 +1539,16 @@ var PoaBridge = class {
1121
1539
  }
1122
1540
  async estimateWithdrawalFee(args) {
1123
1541
  const assetInfo = this.parseAssetId(args.withdrawalParams.assetId);
1124
- (0, import_internal_utils14.assert)(assetInfo != null, "Asset is not supported");
1125
- const estimation = await import_internal_utils14.poaBridge.httpClient.getWithdrawalEstimate(
1542
+ (0, import_internal_utils17.assert)(assetInfo != null, "Asset is not supported");
1543
+ const estimation = await import_internal_utils17.poaBridge.httpClient.getWithdrawalEstimate(
1126
1544
  {
1127
- token: import_internal_utils14.utils.getTokenAccountId(args.withdrawalParams.assetId),
1545
+ token: import_internal_utils17.utils.getTokenAccountId(args.withdrawalParams.assetId),
1128
1546
  address: args.withdrawalParams.destinationAddress,
1129
1547
  // biome-ignore lint/suspicious/noExplicitAny: <explanation>
1130
1548
  chain: toPoaNetwork(assetInfo.blockchain)
1131
1549
  },
1132
1550
  {
1133
- baseURL: import_internal_utils14.configsByEnvironment[this.env].poaBridgeBaseURL,
1551
+ baseURL: import_internal_utils17.configsByEnvironment[this.env].poaBridgeBaseURL,
1134
1552
  logger: args.logger
1135
1553
  }
1136
1554
  );
@@ -1140,12 +1558,12 @@ var PoaBridge = class {
1140
1558
  };
1141
1559
  }
1142
1560
  async waitForWithdrawalCompletion(args) {
1143
- const withdrawalStatus = await import_internal_utils14.poaBridge.waitForWithdrawalCompletion({
1561
+ const withdrawalStatus = await import_internal_utils17.poaBridge.waitForWithdrawalCompletion({
1144
1562
  txHash: args.tx.hash,
1145
1563
  index: args.index,
1146
1564
  signal: args.signal ?? new AbortController().signal,
1147
1565
  retryOptions: args.retryOptions,
1148
- baseURL: import_internal_utils14.configsByEnvironment[this.env].poaBridgeBaseURL,
1566
+ baseURL: import_internal_utils17.configsByEnvironment[this.env].poaBridgeBaseURL,
1149
1567
  logger: args.logger
1150
1568
  });
1151
1569
  return { hash: withdrawalStatus.destinationTxHash };
@@ -1160,10 +1578,10 @@ var PoaBridge = class {
1160
1578
  if (cached != null) {
1161
1579
  return cached;
1162
1580
  }
1163
- const data = await import_internal_utils14.poaBridge.httpClient.getSupportedTokens(
1581
+ const data = await import_internal_utils17.poaBridge.httpClient.getSupportedTokens(
1164
1582
  { chains },
1165
1583
  {
1166
- baseURL: import_internal_utils14.configsByEnvironment[this.env].poaBridgeBaseURL,
1584
+ baseURL: import_internal_utils17.configsByEnvironment[this.env].poaBridgeBaseURL,
1167
1585
  logger
1168
1586
  }
1169
1587
  );
@@ -1185,7 +1603,7 @@ var PUBLIC_STELLAR_RPC_URLS = {
1185
1603
  };
1186
1604
 
1187
1605
  // src/intents/intent-executer-impl/intent-executer.ts
1188
- var import_internal_utils15 = require("@defuse-protocol/internal-utils");
1606
+ var import_internal_utils18 = require("@defuse-protocol/internal-utils");
1189
1607
 
1190
1608
  // ../../node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/_u64.js
1191
1609
  var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
@@ -1567,7 +1985,7 @@ var IntentExecuter = class {
1567
1985
  relayParams: relayParamsFactory,
1568
1986
  ...intentParams
1569
1987
  }) {
1570
- const verifyingContract = import_internal_utils15.configsByEnvironment[this.env].contractID;
1988
+ const verifyingContract = import_internal_utils18.configsByEnvironment[this.env].contractID;
1571
1989
  let intentPayload = defaultIntentPayloadFactory({
1572
1990
  verifying_contract: verifyingContract,
1573
1991
  ...intentParams
@@ -1617,7 +2035,7 @@ async function mergeIntentPayloads(basePayload, intentPayloadFactory) {
1617
2035
  }
1618
2036
 
1619
2037
  // src/intents/intent-relayer-impl/intent-relayer-public.ts
1620
- var import_internal_utils16 = require("@defuse-protocol/internal-utils");
2038
+ var import_internal_utils19 = require("@defuse-protocol/internal-utils");
1621
2039
  var IntentRelayerPublic = class {
1622
2040
  constructor({ env }) {
1623
2041
  this.env = env;
@@ -1639,13 +2057,13 @@ var IntentRelayerPublic = class {
1639
2057
  multiPayloads,
1640
2058
  quoteHashes
1641
2059
  }, ctx = {}) {
1642
- const a = await import_internal_utils16.solverRelay.publishIntents(
2060
+ const a = await import_internal_utils19.solverRelay.publishIntents(
1643
2061
  {
1644
2062
  quote_hashes: quoteHashes,
1645
2063
  signed_datas: multiPayloads
1646
2064
  },
1647
2065
  {
1648
- baseURL: import_internal_utils16.configsByEnvironment[this.env].solverRelayBaseURL,
2066
+ baseURL: import_internal_utils19.configsByEnvironment[this.env].solverRelayBaseURL,
1649
2067
  logger: ctx.logger
1650
2068
  }
1651
2069
  );
@@ -1655,10 +2073,10 @@ var IntentRelayerPublic = class {
1655
2073
  throw a.unwrapErr();
1656
2074
  }
1657
2075
  async waitForSettlement(ticket, ctx = {}) {
1658
- const result = await import_internal_utils16.solverRelay.waitForIntentSettlement({
2076
+ const result = await import_internal_utils19.solverRelay.waitForIntentSettlement({
1659
2077
  intentHash: ticket,
1660
2078
  signal: new AbortController().signal,
1661
- baseURL: import_internal_utils16.configsByEnvironment[this.env].solverRelayBaseURL,
2079
+ baseURL: import_internal_utils19.configsByEnvironment[this.env].solverRelayBaseURL,
1662
2080
  logger: ctx.logger
1663
2081
  });
1664
2082
  return {
@@ -1666,7 +2084,7 @@ var IntentRelayerPublic = class {
1666
2084
  hash: result.txHash,
1667
2085
  // Usually relayer's account id is the verifying contract (`intents.near`),
1668
2086
  // but it is not set in stone and may change in the future.
1669
- accountId: import_internal_utils16.configsByEnvironment[this.env].contractID
2087
+ accountId: import_internal_utils19.configsByEnvironment[this.env].contractID
1670
2088
  }
1671
2089
  };
1672
2090
  }
@@ -1680,14 +2098,14 @@ var noopIntentSigner = {
1680
2098
  };
1681
2099
 
1682
2100
  // src/lib/array.ts
1683
- var import_internal_utils17 = require("@defuse-protocol/internal-utils");
2101
+ var import_internal_utils20 = require("@defuse-protocol/internal-utils");
1684
2102
  function zip(arr1, arr2) {
1685
- (0, import_internal_utils17.assert)(arr1.length === arr2.length, "Arrays must have the same length");
2103
+ (0, import_internal_utils20.assert)(arr1.length === arr2.length, "Arrays must have the same length");
1686
2104
  return arr1.map((v, i) => [v, arr2[i]]);
1687
2105
  }
1688
2106
 
1689
2107
  // src/lib/configure-rpc-config.ts
1690
- var import_internal_utils18 = require("@defuse-protocol/internal-utils");
2108
+ var import_internal_utils21 = require("@defuse-protocol/internal-utils");
1691
2109
 
1692
2110
  // src/lib/object.ts
1693
2111
  function pick(obj, keys) {
@@ -1711,7 +2129,7 @@ function configureEvmRpcUrls(defaultRpcUrls, userRpcUrls, supportedChains) {
1711
2129
  ).map(([caip2, urls]) => [getEIP155ChainId(caip2), urls])
1712
2130
  );
1713
2131
  for (const [chainId, urls] of Object.entries(evmRpcUrls)) {
1714
- (0, import_internal_utils18.assert)(
2132
+ (0, import_internal_utils21.assert)(
1715
2133
  urls.length > 0,
1716
2134
  `EVM RPC URLs for chain ${chainId} are not provided`
1717
2135
  );
@@ -1725,7 +2143,7 @@ function configureStellarRpcUrls(defaultRpcUrls, userRpcUrls) {
1725
2143
  userRpcUrls?.[Chains.Stellar] ?? {}
1726
2144
  );
1727
2145
  for (const [key, value] of Object.entries(stellarRpcUrls)) {
1728
- (0, import_internal_utils18.assert)(value.length > 0, `Stellar RPC URL for ${key} is not provided`);
2146
+ (0, import_internal_utils21.assert)(value.length > 0, `Stellar RPC URL for ${key} is not provided`);
1729
2147
  }
1730
2148
  return stellarRpcUrls;
1731
2149
  }
@@ -1737,8 +2155,10 @@ function createInternalTransferRoute() {
1737
2155
  function createNearWithdrawalRoute(msg) {
1738
2156
  return { route: RouteEnum.NearWithdrawal, msg };
1739
2157
  }
1740
- function createOmniBridgeRoute() {
1741
- return { route: RouteEnum.OmniBridge };
2158
+ function createOmniBridgeRoute(chain) {
2159
+ const routeConfig = { route: RouteEnum.OmniBridge };
2160
+ if (chain) routeConfig.chain = chain;
2161
+ return routeConfig;
1742
2162
  }
1743
2163
  function createVirtualChainRoute(auroraEngineContractId, proxyTokenContractId) {
1744
2164
  return {
@@ -1775,7 +2195,7 @@ function determineRouteConfig(sdk, withdrawalParams) {
1775
2195
  case BridgeNameEnum.Poa:
1776
2196
  return createPoaBridgeRoute(parseAssetId.blockchain);
1777
2197
  case BridgeNameEnum.Omni:
1778
- return createOmniBridgeRoute();
2198
+ return createOmniBridgeRoute(parseAssetId.blockchain);
1779
2199
  case BridgeNameEnum.None:
1780
2200
  return createNearWithdrawalRoute();
1781
2201
  default:
@@ -1789,9 +2209,9 @@ var IntentsSDK = class {
1789
2209
  constructor(args) {
1790
2210
  this.env = args.env ?? "production";
1791
2211
  this.referral = args.referral;
1792
- const nearRpcUrls = args.rpc?.[Chains.Near] ?? import_internal_utils19.PUBLIC_NEAR_RPC_URLS;
1793
- (0, import_internal_utils19.assert)(nearRpcUrls.length > 0, "NEAR RPC URLs are not provided");
1794
- const nearProvider = (0, import_internal_utils19.nearFailoverRpcProvider)({ urls: nearRpcUrls });
2212
+ const nearRpcUrls = args.rpc?.[Chains.Near] ?? import_internal_utils22.PUBLIC_NEAR_RPC_URLS;
2213
+ (0, import_internal_utils22.assert)(nearRpcUrls.length > 0, "NEAR RPC URLs are not provided");
2214
+ const nearProvider = (0, import_internal_utils22.nearFailoverRpcProvider)({ urls: nearRpcUrls });
1795
2215
  const stellarRpcUrls = configureStellarRpcUrls(
1796
2216
  PUBLIC_STELLAR_RPC_URLS,
1797
2217
  args.rpc
@@ -1823,10 +2243,10 @@ var IntentsSDK = class {
1823
2243
  }
1824
2244
  })
1825
2245
  }),
1826
- // new OmniBridge({
1827
- // env: this.env,
1828
- // nearProvider,
1829
- // }),
2246
+ new OmniBridge({
2247
+ env: this.env,
2248
+ nearProvider
2249
+ }),
1830
2250
  new DirectBridge({
1831
2251
  env: this.env,
1832
2252
  nearProvider
@@ -1917,7 +2337,7 @@ var IntentsSDK = class {
1917
2337
  const routeConfig = determineRouteConfig(this, w);
1918
2338
  const route = routeConfig.route;
1919
2339
  const index = indexes.get(route);
1920
- (0, import_internal_utils19.assert)(index != null, "Index is not found for route");
2340
+ (0, import_internal_utils22.assert)(index != null, "Index is not found for route");
1921
2341
  indexes.set(route, index + 1);
1922
2342
  return {
1923
2343
  routeConfig,
@@ -1951,7 +2371,7 @@ var IntentsSDK = class {
1951
2371
  if (Array.isArray(args.withdrawalParams)) {
1952
2372
  return result;
1953
2373
  }
1954
- (0, import_internal_utils19.assert)(result.length === 1, "Unexpected result length");
2374
+ (0, import_internal_utils22.assert)(result.length === 1, "Unexpected result length");
1955
2375
  return result[0];
1956
2376
  }
1957
2377
  parseAssetId(assetId) {
@@ -1965,7 +2385,7 @@ var IntentsSDK = class {
1965
2385
  }
1966
2386
  async signAndSendIntent(args) {
1967
2387
  const intentSigner = args.signer ?? this.intentSigner;
1968
- (0, import_internal_utils19.assert)(intentSigner != null, "Intent signer is not provided");
2388
+ (0, import_internal_utils22.assert)(intentSigner != null, "Intent signer is not provided");
1969
2389
  const intentExecuter = new IntentExecuter({
1970
2390
  env: this.env,
1971
2391
  logger: args.logger,
@@ -2034,12 +2454,12 @@ var IntentsSDK = class {
2034
2454
  intentHash,
2035
2455
  logger
2036
2456
  }) {
2037
- return import_internal_utils19.solverRelay.getStatus(
2457
+ return import_internal_utils22.solverRelay.getStatus(
2038
2458
  {
2039
2459
  intent_hash: intentHash
2040
2460
  },
2041
2461
  {
2042
- baseURL: import_internal_utils19.configsByEnvironment[this.env].solverRelayBaseURL,
2462
+ baseURL: import_internal_utils22.configsByEnvironment[this.env].solverRelayBaseURL,
2043
2463
  logger
2044
2464
  }
2045
2465
  );
@@ -2070,7 +2490,7 @@ var IntentsSDK = class {
2070
2490
  withdrawalParams,
2071
2491
  intentTx,
2072
2492
  logger: args.logger,
2073
- retryOptions: import_internal_utils19.RETRY_CONFIGS.FIVE_MINS_STEADY
2493
+ retryOptions: import_internal_utils22.RETRY_CONFIGS.FIVE_MINS_STEADY
2074
2494
  });
2075
2495
  if (!Array.isArray(args.withdrawalParams)) {
2076
2496
  return {
@@ -2149,14 +2569,14 @@ var IntentSignerNearKeypair = class extends IntentSignerNEP413 {
2149
2569
  };
2150
2570
 
2151
2571
  // src/intents/intent-signer-impl/intent-signer-viem.ts
2152
- var import_internal_utils20 = require("@defuse-protocol/internal-utils");
2572
+ var import_internal_utils23 = require("@defuse-protocol/internal-utils");
2153
2573
  var IntentSignerViem = class {
2154
2574
  constructor(account) {
2155
2575
  this.account = account;
2156
2576
  }
2157
2577
  async signIntent(intent) {
2158
2578
  const payload = JSON.stringify({
2159
- signer_id: intent.signer_id ?? import_internal_utils20.utils.authHandleToIntentsUserId({
2579
+ signer_id: intent.signer_id ?? import_internal_utils23.utils.authHandleToIntentsUserId({
2160
2580
  identifier: this.account.address,
2161
2581
  method: "evm"
2162
2582
  }),
@@ -2174,7 +2594,7 @@ var IntentSignerViem = class {
2174
2594
  return {
2175
2595
  standard: "erc191",
2176
2596
  payload,
2177
- signature: import_internal_utils20.utils.transformERC191Signature(signature)
2597
+ signature: import_internal_utils23.utils.transformERC191Signature(signature)
2178
2598
  };
2179
2599
  }
2180
2600
  };
@@ -2191,59 +2611,11 @@ function createIntentSignerViem(config) {
2191
2611
  }
2192
2612
 
2193
2613
  // index.ts
2194
- var import_internal_utils22 = require("@defuse-protocol/internal-utils");
2195
-
2196
- // src/bridges/omni-bridge/error.ts
2197
- var import_internal_utils21 = require("@defuse-protocol/internal-utils");
2198
- var OmniTransferNotFoundError = class extends import_internal_utils21.BaseError {
2199
- constructor(txHash) {
2200
- super("Omni transfer with given hash is not found in the relayer.", {
2201
- metaMessages: [`OriginTxHash: ${txHash}`],
2202
- name: "OmniTransferNotFoundError"
2203
- });
2204
- this.txHash = txHash;
2205
- }
2206
- };
2207
- var OmniTransferDestinationChainHashNotFoundError = class extends import_internal_utils21.BaseError {
2208
- constructor(txHash, destinationChain) {
2209
- super("Relayer did not return destination chain hash for a transfer.", {
2210
- metaMessages: [
2211
- `OriginTxHash: ${txHash}`,
2212
- `DestinationChain: ${destinationChain}`
2213
- ],
2214
- name: "OmniTransferDestinationChainHashNotFoundError"
2215
- });
2216
- this.txHash = txHash;
2217
- this.destinationChain = destinationChain;
2218
- }
2219
- };
2220
- var TokenNotSupportedByOmniRelayerError = class extends import_internal_utils21.BaseError {
2221
- constructor(token) {
2222
- super(`Omni Relayer doesn't accept fee in the transferred token ${token}`, {
2223
- metaMessages: [`Token: ${token}`],
2224
- name: "TokenNotSupportedByOmniRelayerError"
2225
- });
2226
- this.token = token;
2227
- }
2228
- };
2229
- var TokenNotFoundInDestinationChainError = class extends import_internal_utils21.BaseError {
2230
- constructor(token, chainKind) {
2231
- super(
2232
- `The token ${token} doesn't exist in destination network ${chainKind}`,
2233
- {
2234
- metaMessages: [`Token: ${token}`, `Destination Chain: ${chainKind}`],
2235
- name: "TokenNotFoundInDestinationChainError"
2236
- }
2237
- );
2238
- this.token = token;
2239
- }
2240
- };
2241
-
2242
- // index.ts
2243
- var import_internal_utils23 = require("@defuse-protocol/internal-utils");
2244
2614
  var import_internal_utils24 = require("@defuse-protocol/internal-utils");
2245
2615
  var import_internal_utils25 = require("@defuse-protocol/internal-utils");
2246
2616
  var import_internal_utils26 = require("@defuse-protocol/internal-utils");
2617
+ var import_internal_utils27 = require("@defuse-protocol/internal-utils");
2618
+ var import_internal_utils28 = require("@defuse-protocol/internal-utils");
2247
2619
  // Annotate the CommonJS export names for ESM import in node:
2248
2620
  0 && (module.exports = {
2249
2621
  AssertionError,