@defuse-protocol/intents-sdk 0.43.3 → 0.45.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/README.md +143 -21
- package/dist/index.cjs +19 -18
- package/dist/index.d.cts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +7 -5
- package/dist/src/bridges/aurora-engine-bridge/aurora-engine-bridge-utils.cjs +5 -5
- package/dist/src/bridges/aurora-engine-bridge/aurora-engine-bridge.cjs +24 -12
- package/dist/src/bridges/aurora-engine-bridge/aurora-engine-bridge.js +19 -7
- package/dist/src/bridges/direct-bridge/direct-bridge-utils.cjs +5 -5
- package/dist/src/bridges/direct-bridge/direct-bridge.cjs +22 -10
- package/dist/src/bridges/direct-bridge/direct-bridge.js +16 -4
- package/dist/src/bridges/direct-bridge/error.cjs +2 -2
- package/dist/src/bridges/hot-bridge/error.cjs +4 -26
- package/dist/src/bridges/hot-bridge/error.d.cts +1 -17
- package/dist/src/bridges/hot-bridge/error.d.ts +1 -17
- package/dist/src/bridges/hot-bridge/error.js +1 -21
- package/dist/src/bridges/hot-bridge/hot-bridge-chains.cjs +3 -1
- package/dist/src/bridges/hot-bridge/hot-bridge-chains.d.cts +1 -1
- package/dist/src/bridges/hot-bridge/hot-bridge-chains.d.ts +1 -1
- package/dist/src/bridges/hot-bridge/hot-bridge-chains.js +3 -1
- package/dist/src/bridges/hot-bridge/hot-bridge-utils.cjs +17 -12
- package/dist/src/bridges/hot-bridge/hot-bridge-utils.js +7 -2
- package/dist/src/bridges/hot-bridge/hot-bridge.cjs +46 -32
- package/dist/src/bridges/hot-bridge/hot-bridge.js +38 -24
- package/dist/src/bridges/intents-bridge/intents-bridge.cjs +18 -4
- package/dist/src/bridges/intents-bridge/intents-bridge.js +18 -4
- package/dist/src/bridges/omni-bridge/error.cjs +6 -27
- package/dist/src/bridges/omni-bridge/error.d.cts +1 -16
- package/dist/src/bridges/omni-bridge/error.d.ts +1 -16
- package/dist/src/bridges/omni-bridge/error.js +1 -20
- package/dist/src/bridges/omni-bridge/omni-bridge-utils.cjs +12 -7
- package/dist/src/bridges/omni-bridge/omni-bridge-utils.js +5 -1
- package/dist/src/bridges/omni-bridge/omni-bridge.cjs +72 -54
- package/dist/src/bridges/omni-bridge/omni-bridge.js +47 -29
- package/dist/src/bridges/poa-bridge/poa-bridge-utils.cjs +2 -2
- package/dist/src/bridges/poa-bridge/poa-bridge.cjs +79 -26
- package/dist/src/bridges/poa-bridge/poa-bridge.js +66 -13
- package/dist/src/classes/errors.cjs +7 -7
- package/dist/src/constants/poa-tokens-routable-through-omni-bridge.cjs +17 -0
- package/dist/src/constants/poa-tokens-routable-through-omni-bridge.d.cts +12 -0
- package/dist/src/constants/poa-tokens-routable-through-omni-bridge.d.ts +12 -0
- package/dist/src/constants/poa-tokens-routable-through-omni-bridge.js +16 -0
- package/dist/src/constants/public-rpc-urls.cjs +3 -1
- package/dist/src/constants/public-rpc-urls.js +3 -1
- package/dist/src/constants/withdrawal-timing.cjs +57 -0
- package/dist/src/constants/withdrawal-timing.js +56 -0
- package/dist/src/core/withdrawal-watcher.cjs +83 -0
- package/dist/src/core/withdrawal-watcher.d.cts +18 -0
- package/dist/src/core/withdrawal-watcher.d.ts +18 -0
- package/dist/src/core/withdrawal-watcher.js +79 -0
- package/dist/src/intents/expirable-nonce.cjs +3 -3
- package/dist/src/intents/intent-executer-impl/intent-executer.cjs +7 -4
- package/dist/src/intents/intent-executer-impl/intent-executer.js +5 -2
- package/dist/src/intents/intent-hash.cjs +2 -2
- package/dist/src/intents/intent-hashes/erc191.cjs +2 -2
- package/dist/src/intents/intent-hashes/nep413.cjs +2 -2
- package/dist/src/intents/intent-hashes/raw-ed25519.cjs +2 -2
- package/dist/src/intents/intent-hashes/sep53.cjs +4 -4
- package/dist/src/intents/intent-hashes/tip191.cjs +2 -2
- package/dist/src/intents/intent-hashes/ton-connect.cjs +2 -2
- package/dist/src/intents/intent-hashes/webauthn.cjs +2 -2
- package/dist/src/intents/intent-payload-builder.cjs +4 -4
- package/dist/src/intents/intent-relayer-impl/intent-relayer-public.cjs +7 -7
- package/dist/src/intents/intent-relayer-impl/intent-relayer-public.js +1 -1
- package/dist/src/intents/intent-signer-impl/intent-signer-near-keypair.cjs +2 -2
- package/dist/src/intents/intent-signer-impl/intent-signer-nep413.cjs +3 -3
- package/dist/src/intents/intent-signer-impl/intent-signer-viem.cjs +3 -3
- package/dist/src/intents/interfaces/intent-relayer.d.cts +2 -1
- package/dist/src/intents/interfaces/intent-relayer.d.ts +2 -1
- package/dist/src/intents/salt-manager.cjs +5 -5
- package/dist/src/lib/array.cjs +2 -2
- package/dist/src/lib/caip2.cjs +7 -5
- package/dist/src/lib/caip2.d.cts +2 -0
- package/dist/src/lib/caip2.d.ts +2 -0
- package/dist/src/lib/caip2.js +3 -1
- package/dist/src/lib/configure-rpc-config.cjs +3 -3
- package/dist/src/lib/estimate-fee.cjs +6 -6
- package/dist/src/lib/parse-defuse-asset-id.cjs +2 -2
- package/dist/src/lib/route-config-factory.cjs +2 -5
- package/dist/src/lib/route-config-factory.d.cts +1 -1
- package/dist/src/lib/route-config-factory.d.ts +1 -1
- package/dist/src/lib/route-config-factory.js +2 -5
- package/dist/src/lib/tokensUsdPricesHttpClient/apis.cjs +3 -3
- package/dist/src/lib/validateAddress.cjs +21 -19
- package/dist/src/lib/validateAddress.js +3 -1
- package/dist/src/sdk.cjs +96 -47
- package/dist/src/sdk.d.cts +72 -11
- package/dist/src/sdk.d.ts +72 -11
- package/dist/src/sdk.js +85 -36
- package/dist/src/shared-types.d.cts +49 -15
- package/dist/src/shared-types.d.ts +49 -15
- package/package.json +3 -5
- package/dist/src/lib/route-config.cjs +0 -19
- package/dist/src/lib/route-config.js +0 -19
|
@@ -1,33 +1,35 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_errors = require('../../classes/errors.cjs');
|
|
2
3
|
const require_route_enum = require('../../constants/route-enum.cjs');
|
|
3
4
|
const require_estimate_fee = require('../../lib/estimate-fee.cjs');
|
|
4
|
-
const require_errors = require('../../classes/errors.cjs');
|
|
5
5
|
const require_parse_defuse_asset_id = require('../../lib/parse-defuse-asset-id.cjs');
|
|
6
6
|
const require_validateAddress = require('../../lib/validateAddress.cjs');
|
|
7
7
|
const require_bridge_name_enum = require('../../constants/bridge-name-enum.cjs');
|
|
8
8
|
const require_error = require('./error.cjs');
|
|
9
9
|
const require_omni_bridge_constants = require('./omni-bridge-constants.cjs');
|
|
10
|
+
const require_poa_tokens_routable_through_omni_bridge = require('../../constants/poa-tokens-routable-through-omni-bridge.cjs');
|
|
10
11
|
const require_omni_bridge_utils = require('./omni-bridge-utils.cjs');
|
|
11
|
-
let
|
|
12
|
+
let _defuse_protocol_internal_utils = require("@defuse-protocol/internal-utils");
|
|
12
13
|
let lru_cache = require("lru-cache");
|
|
13
|
-
let
|
|
14
|
-
|
|
15
|
-
__isaacs_ttlcache = require_rolldown_runtime.__toESM(__isaacs_ttlcache);
|
|
14
|
+
let _isaacs_ttlcache = require("@isaacs/ttlcache");
|
|
15
|
+
_isaacs_ttlcache = require_rolldown_runtime.__toESM(_isaacs_ttlcache);
|
|
16
16
|
let omni_bridge_sdk = require("omni-bridge-sdk");
|
|
17
17
|
|
|
18
18
|
//#region src/bridges/omni-bridge/omni-bridge.ts
|
|
19
19
|
var OmniBridge = class {
|
|
20
|
-
constructor({ env, nearProvider, solverRelayApiKey }) {
|
|
20
|
+
constructor({ env, nearProvider, solverRelayApiKey, routeMigratedPoaTokensThroughOmniBridge }) {
|
|
21
|
+
this.route = require_route_enum.RouteEnum.OmniBridge;
|
|
21
22
|
this.storageDepositCache = new lru_cache.LRUCache({
|
|
22
23
|
max: 100,
|
|
23
24
|
ttl: 36e5
|
|
24
25
|
});
|
|
25
|
-
this.destinationChainAddressCache = new
|
|
26
|
-
this.tokenDecimalsCache = new
|
|
26
|
+
this.destinationChainAddressCache = new _isaacs_ttlcache.default({ ttl: 36e5 });
|
|
27
|
+
this.tokenDecimalsCache = new _isaacs_ttlcache.default({ ttl: 36e5 });
|
|
27
28
|
this.env = env;
|
|
28
29
|
this.nearProvider = nearProvider;
|
|
29
30
|
this.omniBridgeAPI = new omni_bridge_sdk.OmniBridgeAPI();
|
|
30
31
|
this.solverRelayApiKey = solverRelayApiKey;
|
|
32
|
+
this.routeMigratedPoaTokensThroughOmniBridge = routeMigratedPoaTokensThroughOmniBridge ?? false;
|
|
31
33
|
}
|
|
32
34
|
is(routeConfig) {
|
|
33
35
|
return routeConfig.route === require_route_enum.RouteEnum.OmniBridge;
|
|
@@ -40,7 +42,8 @@ var OmniBridge = class {
|
|
|
40
42
|
const nonValidStandard = parsed.standard !== "nep141";
|
|
41
43
|
if (nonValidStandard && (omniBridgeSetWithNoChain || targetChainSpecified)) throw new require_errors.UnsupportedAssetIdError(params.assetId, `Only NEP-141 tokens are supported by Omni Bridge.`);
|
|
42
44
|
if (nonValidStandard) return false;
|
|
43
|
-
const
|
|
45
|
+
const poaTokenRoutedThroughOmniBridge = this.isPoaTokenRoutedThroughOmniBridge(parsed.contractId);
|
|
46
|
+
const nonValidToken = !poaTokenRoutedThroughOmniBridge && require_omni_bridge_utils.validateOmniToken(parsed.contractId) === false;
|
|
44
47
|
if (nonValidToken && omniBridgeSetWithNoChain) throw new require_errors.UnsupportedAssetIdError(params.assetId, `Non valid omni contract id ${parsed.contractId}`);
|
|
45
48
|
if (!targetChainSpecified && nonValidToken) return false;
|
|
46
49
|
let omniChainKind = null;
|
|
@@ -50,7 +53,7 @@ var OmniBridge = class {
|
|
|
50
53
|
if (omniChainKind === null) throw new require_errors.UnsupportedAssetIdError(params.assetId, `Chain ${params.routeConfig.chain} is not supported in Omni Bridge.`);
|
|
51
54
|
caip2Chain = params.routeConfig.chain;
|
|
52
55
|
} else {
|
|
53
|
-
omniChainKind = (0, omni_bridge_sdk.parseOriginChain)(parsed.contractId);
|
|
56
|
+
omniChainKind = poaTokenRoutedThroughOmniBridge ? require_omni_bridge_utils.poaContractIdToChainKind(parsed.contractId) : (0, omni_bridge_sdk.parseOriginChain)(parsed.contractId);
|
|
54
57
|
if (omniChainKind === null) throw new require_errors.UnsupportedAssetIdError(params.assetId, `Withdrawal of ${parsed.contractId} to its origin chain is not supported in Omni Bridge.`);
|
|
55
58
|
caip2Chain = require_omni_bridge_utils.chainKindToCaip2(omniChainKind);
|
|
56
59
|
if (caip2Chain === null) throw new require_errors.UnsupportedAssetIdError(params.assetId, `Withdrawal of ${parsed.contractId} to its origin chain is not supported in Omni Bridge.`);
|
|
@@ -64,7 +67,7 @@ var OmniBridge = class {
|
|
|
64
67
|
parseAssetId(assetId) {
|
|
65
68
|
const parsed = require_parse_defuse_asset_id.parseDefuseAssetId(assetId);
|
|
66
69
|
if (parsed.standard !== "nep141") return null;
|
|
67
|
-
const omniChainKind = (0, omni_bridge_sdk.parseOriginChain)(parsed.contractId);
|
|
70
|
+
const omniChainKind = this.isPoaTokenRoutedThroughOmniBridge(parsed.contractId) ? require_omni_bridge_utils.poaContractIdToChainKind(parsed.contractId) : (0, omni_bridge_sdk.parseOriginChain)(parsed.contractId);
|
|
68
71
|
if (omniChainKind === null) return null;
|
|
69
72
|
const blockchain = require_omni_bridge_utils.chainKindToCaip2(omniChainKind);
|
|
70
73
|
if (blockchain === null) return null;
|
|
@@ -83,7 +86,7 @@ var OmniBridge = class {
|
|
|
83
86
|
omniChainKind = require_omni_bridge_utils.caip2ToChainKind(routeConfig.chain);
|
|
84
87
|
blockchain = routeConfig.chain;
|
|
85
88
|
} else {
|
|
86
|
-
omniChainKind = (0, omni_bridge_sdk.parseOriginChain)(parsed.contractId);
|
|
89
|
+
omniChainKind = this.isPoaTokenRoutedThroughOmniBridge(parsed.contractId) ? require_omni_bridge_utils.poaContractIdToChainKind(parsed.contractId) : (0, omni_bridge_sdk.parseOriginChain)(parsed.contractId);
|
|
87
90
|
if (omniChainKind === null) return null;
|
|
88
91
|
blockchain = require_omni_bridge_utils.chainKindToCaip2(omniChainKind);
|
|
89
92
|
}
|
|
@@ -96,9 +99,9 @@ var OmniBridge = class {
|
|
|
96
99
|
}
|
|
97
100
|
async createWithdrawalIntents(args) {
|
|
98
101
|
const assetInfo = this.makeAssetInfo(args.withdrawalParams.assetId, args.withdrawalParams.routeConfig);
|
|
99
|
-
(0,
|
|
102
|
+
(0, _defuse_protocol_internal_utils.assert)(assetInfo !== null, `Asset ${args.withdrawalParams.assetId} is not supported by Omni Bridge`);
|
|
100
103
|
const omniChainKind = require_omni_bridge_utils.caip2ToChainKind(assetInfo.blockchain);
|
|
101
|
-
(0,
|
|
104
|
+
(0, _defuse_protocol_internal_utils.assert)(omniChainKind !== null, `Chain ${assetInfo.blockchain} is not supported by Omni Bridge`);
|
|
102
105
|
const intents = [];
|
|
103
106
|
if (args.feeEstimation.quote !== null) intents.push({
|
|
104
107
|
intent: "token_diff",
|
|
@@ -109,7 +112,7 @@ var OmniBridge = class {
|
|
|
109
112
|
referral: args.referral
|
|
110
113
|
});
|
|
111
114
|
const relayerFee = require_estimate_fee.getUnderlyingFee(args.feeEstimation, require_route_enum.RouteEnum.OmniBridge, "relayerFee");
|
|
112
|
-
(0,
|
|
115
|
+
(0, _defuse_protocol_internal_utils.assert)(relayerFee >= 0n, `Invalid Omni bridge relayer fee: expected >= 0, got ${relayerFee}`);
|
|
113
116
|
let amount = args.withdrawalParams.amount;
|
|
114
117
|
let utxoMaxGasFee = null;
|
|
115
118
|
/**
|
|
@@ -128,8 +131,8 @@ var OmniBridge = class {
|
|
|
128
131
|
if (require_omni_bridge_utils.isUtxoChain(omniChainKind)) {
|
|
129
132
|
utxoMaxGasFee = require_estimate_fee.getUnderlyingFee(args.feeEstimation, require_route_enum.RouteEnum.OmniBridge, "utxoMaxGasFee");
|
|
130
133
|
const utxoProtocolFee = require_estimate_fee.getUnderlyingFee(args.feeEstimation, require_route_enum.RouteEnum.OmniBridge, "utxoProtocolFee");
|
|
131
|
-
(0,
|
|
132
|
-
(0,
|
|
134
|
+
(0, _defuse_protocol_internal_utils.assert)(utxoMaxGasFee !== void 0 && utxoMaxGasFee > 0n, `Invalid Omni Bridge utxo max gas fee: expected > 0, got ${utxoMaxGasFee}`);
|
|
135
|
+
(0, _defuse_protocol_internal_utils.assert)(utxoProtocolFee !== void 0 && utxoProtocolFee > 0n, `Invalid Omni Bridge utxo protocol fee: expected > 0, got ${utxoProtocolFee}`);
|
|
133
136
|
amount += utxoMaxGasFee + utxoProtocolFee;
|
|
134
137
|
}
|
|
135
138
|
intents.push(...require_omni_bridge_utils.createWithdrawIntentsPrimitive({
|
|
@@ -137,7 +140,7 @@ var OmniBridge = class {
|
|
|
137
140
|
destinationAddress: args.withdrawalParams.destinationAddress,
|
|
138
141
|
amount,
|
|
139
142
|
omniChainKind,
|
|
140
|
-
intentsContract:
|
|
143
|
+
intentsContract: _defuse_protocol_internal_utils.configsByEnvironment[this.env].contractID,
|
|
141
144
|
nativeFee: relayerFee,
|
|
142
145
|
storageDepositAmount: require_estimate_fee.getUnderlyingFee(args.feeEstimation, require_route_enum.RouteEnum.OmniBridge, "storageDepositFee"),
|
|
143
146
|
utxoMaxGasFee
|
|
@@ -145,48 +148,48 @@ var OmniBridge = class {
|
|
|
145
148
|
return Promise.resolve(intents);
|
|
146
149
|
}
|
|
147
150
|
async validateWithdrawal(args) {
|
|
148
|
-
(0,
|
|
151
|
+
(0, _defuse_protocol_internal_utils.assert)(args.feeEstimation.amount > 0n, `Invalid Omni Bridge fee: expected > 0, got ${args.feeEstimation.amount}`);
|
|
149
152
|
const assetInfo = this.makeAssetInfo(args.assetId, args.routeConfig);
|
|
150
|
-
(0,
|
|
153
|
+
(0, _defuse_protocol_internal_utils.assert)(assetInfo !== null, `Asset ${args.assetId} is not supported by Omni Bridge`);
|
|
151
154
|
if (require_validateAddress.validateAddress(args.destinationAddress, assetInfo.blockchain) === false) throw new require_errors.InvalidDestinationAddressForWithdrawalError(args.destinationAddress, assetInfo.blockchain);
|
|
152
155
|
const omniChainKind = require_omni_bridge_utils.caip2ToChainKind(assetInfo.blockchain);
|
|
153
|
-
(0,
|
|
156
|
+
(0, _defuse_protocol_internal_utils.assert)(omniChainKind !== null, `Chain ${assetInfo.blockchain} is not supported by Omni Bridge`);
|
|
154
157
|
const destTokenAddress = await this.getCachedDestinationTokenAddress(assetInfo.contractId, omniChainKind);
|
|
155
158
|
if (destTokenAddress === null) throw new require_error.TokenNotFoundInDestinationChainError(args.assetId, assetInfo.blockchain);
|
|
156
159
|
const decimals = await this.getCachedTokenDecimals(destTokenAddress);
|
|
157
|
-
(0,
|
|
160
|
+
(0, _defuse_protocol_internal_utils.assert)(decimals !== null, `Failed to retrieve token decimals for address ${destTokenAddress} via OmniBridge contract.
|
|
158
161
|
Ensure the token is supported and the address is correct.`);
|
|
159
162
|
if ((0, omni_bridge_sdk.verifyTransferAmount)(args.amount, 0n, decimals.origin_decimals, decimals.decimals) === false) throw new require_errors.MinWithdrawalAmountError((0, omni_bridge_sdk.getMinimumTransferableAmount)(decimals.origin_decimals, decimals.decimals), args.amount, args.assetId);
|
|
160
|
-
const storageBalance = await require_omni_bridge_utils.getAccountOmniStorageBalance(this.nearProvider,
|
|
163
|
+
const storageBalance = await require_omni_bridge_utils.getAccountOmniStorageBalance(this.nearProvider, _defuse_protocol_internal_utils.configsByEnvironment[this.env].contractID);
|
|
161
164
|
const intentsNearStorageBalance = storageBalance === null ? 0n : BigInt(storageBalance.available);
|
|
162
165
|
if (intentsNearStorageBalance <= require_omni_bridge_constants.MIN_ALLOWED_STORAGE_BALANCE_FOR_INTENTS_NEAR) throw new require_error.IntentsNearOmniAvailableBalanceTooLowError(intentsNearStorageBalance.toString());
|
|
163
166
|
const utxoChainWithdrawal = require_omni_bridge_utils.isUtxoChain(omniChainKind);
|
|
164
167
|
if (utxoChainWithdrawal === false) {
|
|
165
168
|
const relayerFee = require_estimate_fee.getUnderlyingFee(args.feeEstimation, require_route_enum.RouteEnum.OmniBridge, "relayerFee");
|
|
166
|
-
(0,
|
|
169
|
+
(0, _defuse_protocol_internal_utils.assert)(require_estimate_fee.getUnderlyingFee(args.feeEstimation, require_route_enum.RouteEnum.OmniBridge, "relayerFee") > 0n, `Invalid Omni Bridge relayer fee for non UTXO chain withdrawal: expected > 0, got ${relayerFee}`);
|
|
167
170
|
}
|
|
168
171
|
if (utxoChainWithdrawal) {
|
|
169
|
-
const fee = await (0,
|
|
172
|
+
const fee = await (0, _defuse_protocol_internal_utils.withTimeout)(() => this.omniBridgeAPI.getFee((0, omni_bridge_sdk.omniAddress)(omni_bridge_sdk.ChainKind.Near, _defuse_protocol_internal_utils.configsByEnvironment[this.env].contractID), (0, omni_bridge_sdk.omniAddress)(omniChainKind, args.destinationAddress), (0, omni_bridge_sdk.omniAddress)(omni_bridge_sdk.ChainKind.Near, assetInfo.contractId), args.amount), {
|
|
170
173
|
timeout: typeof window !== "undefined" ? 1e4 : 3e3,
|
|
171
174
|
errorInstance: new require_error.OmniWithdrawalApiFeeRequestTimeoutError()
|
|
172
175
|
});
|
|
173
176
|
if (fee.insufficient_utxo) throw new require_error.InsufficientUtxoForOmniBridgeWithdrawalError(assetInfo.blockchain);
|
|
174
|
-
(0,
|
|
177
|
+
(0, _defuse_protocol_internal_utils.assert)(fee.min_amount !== null && fee.min_amount !== void 0 && BigInt(fee.min_amount) > 0n, `Invalid min amount value for a UTXO chain withdrawal: expected > 0, got ${fee.min_amount}`);
|
|
175
178
|
const minAmount = BigInt(fee.min_amount);
|
|
176
179
|
const utxoMaxGasFee = require_estimate_fee.getUnderlyingFee(args.feeEstimation, require_route_enum.RouteEnum.OmniBridge, "utxoMaxGasFee");
|
|
177
180
|
const utxoProtocolFee = require_estimate_fee.getUnderlyingFee(args.feeEstimation, require_route_enum.RouteEnum.OmniBridge, "utxoProtocolFee");
|
|
178
|
-
(0,
|
|
179
|
-
(0,
|
|
181
|
+
(0, _defuse_protocol_internal_utils.assert)(utxoMaxGasFee !== void 0 && utxoMaxGasFee > 0n, `Invalid Omni Bridge utxo max gas fee: expected > 0, got ${utxoMaxGasFee}`);
|
|
182
|
+
(0, _defuse_protocol_internal_utils.assert)(utxoProtocolFee !== void 0 && utxoProtocolFee > 0n, `Invalid Omni Bridge utxo protocol fee: expected > 0, got ${utxoProtocolFee}`);
|
|
180
183
|
const actualAmountWithFee = args.amount + utxoMaxGasFee + utxoProtocolFee;
|
|
181
184
|
if (actualAmountWithFee < minAmount) throw new require_errors.MinWithdrawalAmountError(minAmount, actualAmountWithFee, args.assetId);
|
|
182
185
|
}
|
|
183
186
|
}
|
|
184
187
|
async estimateWithdrawalFee(args) {
|
|
185
188
|
const assetInfo = this.makeAssetInfo(args.withdrawalParams.assetId, args.withdrawalParams.routeConfig);
|
|
186
|
-
(0,
|
|
189
|
+
(0, _defuse_protocol_internal_utils.assert)(assetInfo !== null, `Asset ${args.withdrawalParams.assetId} is not supported by Omni Bridge`);
|
|
187
190
|
const omniChainKind = require_omni_bridge_utils.caip2ToChainKind(assetInfo.blockchain);
|
|
188
|
-
(0,
|
|
189
|
-
const fee = await (0,
|
|
191
|
+
(0, _defuse_protocol_internal_utils.assert)(omniChainKind !== null, `Chain ${assetInfo.blockchain} is not supported by Omni Bridge`);
|
|
192
|
+
const fee = await (0, _defuse_protocol_internal_utils.withTimeout)(() => this.omniBridgeAPI.getFee((0, omni_bridge_sdk.omniAddress)(omni_bridge_sdk.ChainKind.Near, _defuse_protocol_internal_utils.configsByEnvironment[this.env].contractID), (0, omni_bridge_sdk.omniAddress)(omniChainKind, args.withdrawalParams.destinationAddress), (0, omni_bridge_sdk.omniAddress)(omni_bridge_sdk.ChainKind.Near, assetInfo.contractId), args.withdrawalParams.amount), {
|
|
190
193
|
timeout: typeof window !== "undefined" ? 1e4 : 3e3,
|
|
191
194
|
errorInstance: new require_error.OmniWithdrawalApiFeeRequestTimeoutError()
|
|
192
195
|
});
|
|
@@ -222,8 +225,8 @@ var OmniBridge = class {
|
|
|
222
225
|
amount += BigInt(quote.amount_in);
|
|
223
226
|
}
|
|
224
227
|
if (require_omni_bridge_utils.isUtxoChain(omniChainKind)) {
|
|
225
|
-
(0,
|
|
226
|
-
(0,
|
|
228
|
+
(0, _defuse_protocol_internal_utils.assert)(fee.gas_fee !== null && fee.gas_fee !== void 0 && fee.gas_fee > 0n, `Invalid Omni Bridge utxo gas fee: expected > 0, got ${fee.gas_fee}`);
|
|
229
|
+
(0, _defuse_protocol_internal_utils.assert)(fee.protocol_fee !== null && fee.protocol_fee !== void 0 && fee.protocol_fee > 0n, `Invalid Omni Bridge utxo protocol fee: expected > 0, got ${fee.protocol_fee}`);
|
|
227
230
|
amount += fee.gas_fee + fee.protocol_fee;
|
|
228
231
|
underlyingFees.utxoMaxGasFee = fee.gas_fee;
|
|
229
232
|
underlyingFees.utxoProtocolFee = fee.protocol_fee;
|
|
@@ -234,25 +237,33 @@ var OmniBridge = class {
|
|
|
234
237
|
underlyingFees: { [require_route_enum.RouteEnum.OmniBridge]: underlyingFees }
|
|
235
238
|
};
|
|
236
239
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
240
|
+
createWithdrawalIdentifier(args) {
|
|
241
|
+
const assetInfo = this.makeAssetInfo(args.withdrawalParams.assetId, args.withdrawalParams.routeConfig);
|
|
242
|
+
(0, _defuse_protocol_internal_utils.assert)(assetInfo !== null, `Asset ${args.withdrawalParams.assetId} is not supported by Omni Bridge`);
|
|
243
|
+
return {
|
|
244
|
+
landingChain: args.withdrawalParams.routeConfig != null && "chain" in args.withdrawalParams.routeConfig && args.withdrawalParams.routeConfig.chain !== void 0 ? args.withdrawalParams.routeConfig.chain : assetInfo.blockchain,
|
|
245
|
+
index: args.index,
|
|
246
|
+
withdrawalParams: args.withdrawalParams,
|
|
247
|
+
tx: args.tx
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
async describeWithdrawal(args) {
|
|
251
|
+
const transfer = (await this.omniBridgeAPI.getTransfer({ transactionHash: args.tx.hash }))[args.index];
|
|
252
|
+
if (transfer == null || transfer.transfer_message == null) return { status: "pending" };
|
|
253
|
+
const destinationChain = (0, omni_bridge_sdk.getChain)(transfer.transfer_message.recipient);
|
|
254
|
+
let txHash = null;
|
|
255
|
+
if ((0, omni_bridge_sdk.isEvmChain)(destinationChain)) txHash = transfer.finalised?.EVMLog?.transaction_hash;
|
|
256
|
+
else if (destinationChain === omni_bridge_sdk.ChainKind.Sol) txHash = transfer.finalised?.Solana?.signature;
|
|
257
|
+
else if (destinationChain === omni_bridge_sdk.ChainKind.Btc) txHash = typeof window !== "undefined" ? transfer.utxo_transfer?.btc_pending_id : transfer.finalised?.UtxoLog?.transaction_hash;
|
|
258
|
+
else return {
|
|
259
|
+
status: "completed",
|
|
260
|
+
txHash: null
|
|
261
|
+
};
|
|
262
|
+
if (!txHash) return { status: "pending" };
|
|
263
|
+
return {
|
|
264
|
+
status: "completed",
|
|
265
|
+
txHash
|
|
266
|
+
};
|
|
256
267
|
}
|
|
257
268
|
/**
|
|
258
269
|
* Gets storage deposit for a token to avoid frequent RPC calls.
|
|
@@ -260,10 +271,10 @@ var OmniBridge = class {
|
|
|
260
271
|
async getCachedStorageDepositValue(contractId) {
|
|
261
272
|
const cached = this.storageDepositCache.get(contractId);
|
|
262
273
|
if (cached !== void 0) return cached;
|
|
263
|
-
const result = await Promise.all([(0,
|
|
274
|
+
const result = await Promise.all([(0, _defuse_protocol_internal_utils.getNearNep141MinStorageBalance)({
|
|
264
275
|
contractId,
|
|
265
276
|
nearProvider: this.nearProvider
|
|
266
|
-
}), (0,
|
|
277
|
+
}), (0, _defuse_protocol_internal_utils.getNearNep141StorageBalance)({
|
|
267
278
|
contractId,
|
|
268
279
|
accountId: require_omni_bridge_constants.OMNI_BRIDGE_CONTRACT,
|
|
269
280
|
nearProvider: this.nearProvider
|
|
@@ -292,6 +303,13 @@ var OmniBridge = class {
|
|
|
292
303
|
if (tokenDecimals !== null) this.tokenDecimalsCache.set(omniAddress$1, tokenDecimals);
|
|
293
304
|
return tokenDecimals;
|
|
294
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* Checks if passed token contract id is an allowlisted PoA token that should be routed via OmniBridge.
|
|
308
|
+
* Always return false when feature flag routeMigratedPoaTokensThroughOmniBridge = false.
|
|
309
|
+
*/
|
|
310
|
+
isPoaTokenRoutedThroughOmniBridge(nearAddress) {
|
|
311
|
+
return this.routeMigratedPoaTokensThroughOmniBridge ? require_poa_tokens_routable_through_omni_bridge.POA_TOKENS_ROUTABLE_THROUGH_OMNI_BRIDGE[nearAddress] !== void 0 : false;
|
|
312
|
+
}
|
|
295
313
|
};
|
|
296
314
|
|
|
297
315
|
//#endregion
|
|
@@ -1,21 +1,22 @@
|
|
|
1
|
+
import { InvalidDestinationAddressForWithdrawalError, MinWithdrawalAmountError, UnsupportedAssetIdError } from "../../classes/errors.js";
|
|
1
2
|
import { RouteEnum } from "../../constants/route-enum.js";
|
|
2
3
|
import { getFeeQuote, getUnderlyingFee } from "../../lib/estimate-fee.js";
|
|
3
|
-
import { InvalidDestinationAddressForWithdrawalError, MinWithdrawalAmountError, UnsupportedAssetIdError } from "../../classes/errors.js";
|
|
4
4
|
import { parseDefuseAssetId } from "../../lib/parse-defuse-asset-id.js";
|
|
5
5
|
import { validateAddress } from "../../lib/validateAddress.js";
|
|
6
6
|
import { BridgeNameEnum } from "../../constants/bridge-name-enum.js";
|
|
7
|
-
import { InsufficientUtxoForOmniBridgeWithdrawalError, IntentsNearOmniAvailableBalanceTooLowError, InvalidFeeValueError,
|
|
7
|
+
import { InsufficientUtxoForOmniBridgeWithdrawalError, IntentsNearOmniAvailableBalanceTooLowError, InvalidFeeValueError, OmniWithdrawalApiFeeRequestTimeoutError, TokenNotFoundInDestinationChainError } from "./error.js";
|
|
8
8
|
import { MIN_ALLOWED_STORAGE_BALANCE_FOR_INTENTS_NEAR, NEAR_NATIVE_ASSET_ID, OMNI_BRIDGE_CONTRACT } from "./omni-bridge-constants.js";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
9
|
+
import { POA_TOKENS_ROUTABLE_THROUGH_OMNI_BRIDGE } from "../../constants/poa-tokens-routable-through-omni-bridge.js";
|
|
10
|
+
import { caip2ToChainKind, chainKindToCaip2, createWithdrawIntentsPrimitive, getAccountOmniStorageBalance, getBridgedToken, getTokenDecimals, isUtxoChain, poaContractIdToChainKind, validateOmniToken } from "./omni-bridge-utils.js";
|
|
11
|
+
import { assert, configsByEnvironment, getNearNep141MinStorageBalance, getNearNep141StorageBalance, withTimeout } from "@defuse-protocol/internal-utils";
|
|
11
12
|
import { LRUCache } from "lru-cache";
|
|
12
|
-
import { retry } from "@lifeomic/attempt";
|
|
13
13
|
import TTLCache from "@isaacs/ttlcache";
|
|
14
14
|
import { ChainKind, OmniBridgeAPI, getChain, getMinimumTransferableAmount, isEvmChain, omniAddress, parseOriginChain, verifyTransferAmount } from "omni-bridge-sdk";
|
|
15
15
|
|
|
16
16
|
//#region src/bridges/omni-bridge/omni-bridge.ts
|
|
17
17
|
var OmniBridge = class {
|
|
18
|
-
constructor({ env, nearProvider, solverRelayApiKey }) {
|
|
18
|
+
constructor({ env, nearProvider, solverRelayApiKey, routeMigratedPoaTokensThroughOmniBridge }) {
|
|
19
|
+
this.route = RouteEnum.OmniBridge;
|
|
19
20
|
this.storageDepositCache = new LRUCache({
|
|
20
21
|
max: 100,
|
|
21
22
|
ttl: 36e5
|
|
@@ -26,6 +27,7 @@ var OmniBridge = class {
|
|
|
26
27
|
this.nearProvider = nearProvider;
|
|
27
28
|
this.omniBridgeAPI = new OmniBridgeAPI();
|
|
28
29
|
this.solverRelayApiKey = solverRelayApiKey;
|
|
30
|
+
this.routeMigratedPoaTokensThroughOmniBridge = routeMigratedPoaTokensThroughOmniBridge ?? false;
|
|
29
31
|
}
|
|
30
32
|
is(routeConfig) {
|
|
31
33
|
return routeConfig.route === RouteEnum.OmniBridge;
|
|
@@ -38,7 +40,8 @@ var OmniBridge = class {
|
|
|
38
40
|
const nonValidStandard = parsed.standard !== "nep141";
|
|
39
41
|
if (nonValidStandard && (omniBridgeSetWithNoChain || targetChainSpecified)) throw new UnsupportedAssetIdError(params.assetId, `Only NEP-141 tokens are supported by Omni Bridge.`);
|
|
40
42
|
if (nonValidStandard) return false;
|
|
41
|
-
const
|
|
43
|
+
const poaTokenRoutedThroughOmniBridge = this.isPoaTokenRoutedThroughOmniBridge(parsed.contractId);
|
|
44
|
+
const nonValidToken = !poaTokenRoutedThroughOmniBridge && validateOmniToken(parsed.contractId) === false;
|
|
42
45
|
if (nonValidToken && omniBridgeSetWithNoChain) throw new UnsupportedAssetIdError(params.assetId, `Non valid omni contract id ${parsed.contractId}`);
|
|
43
46
|
if (!targetChainSpecified && nonValidToken) return false;
|
|
44
47
|
let omniChainKind = null;
|
|
@@ -48,7 +51,7 @@ var OmniBridge = class {
|
|
|
48
51
|
if (omniChainKind === null) throw new UnsupportedAssetIdError(params.assetId, `Chain ${params.routeConfig.chain} is not supported in Omni Bridge.`);
|
|
49
52
|
caip2Chain = params.routeConfig.chain;
|
|
50
53
|
} else {
|
|
51
|
-
omniChainKind = parseOriginChain(parsed.contractId);
|
|
54
|
+
omniChainKind = poaTokenRoutedThroughOmniBridge ? poaContractIdToChainKind(parsed.contractId) : parseOriginChain(parsed.contractId);
|
|
52
55
|
if (omniChainKind === null) throw new UnsupportedAssetIdError(params.assetId, `Withdrawal of ${parsed.contractId} to its origin chain is not supported in Omni Bridge.`);
|
|
53
56
|
caip2Chain = chainKindToCaip2(omniChainKind);
|
|
54
57
|
if (caip2Chain === null) throw new UnsupportedAssetIdError(params.assetId, `Withdrawal of ${parsed.contractId} to its origin chain is not supported in Omni Bridge.`);
|
|
@@ -62,7 +65,7 @@ var OmniBridge = class {
|
|
|
62
65
|
parseAssetId(assetId) {
|
|
63
66
|
const parsed = parseDefuseAssetId(assetId);
|
|
64
67
|
if (parsed.standard !== "nep141") return null;
|
|
65
|
-
const omniChainKind = parseOriginChain(parsed.contractId);
|
|
68
|
+
const omniChainKind = this.isPoaTokenRoutedThroughOmniBridge(parsed.contractId) ? poaContractIdToChainKind(parsed.contractId) : parseOriginChain(parsed.contractId);
|
|
66
69
|
if (omniChainKind === null) return null;
|
|
67
70
|
const blockchain = chainKindToCaip2(omniChainKind);
|
|
68
71
|
if (blockchain === null) return null;
|
|
@@ -81,7 +84,7 @@ var OmniBridge = class {
|
|
|
81
84
|
omniChainKind = caip2ToChainKind(routeConfig.chain);
|
|
82
85
|
blockchain = routeConfig.chain;
|
|
83
86
|
} else {
|
|
84
|
-
omniChainKind = parseOriginChain(parsed.contractId);
|
|
87
|
+
omniChainKind = this.isPoaTokenRoutedThroughOmniBridge(parsed.contractId) ? poaContractIdToChainKind(parsed.contractId) : parseOriginChain(parsed.contractId);
|
|
85
88
|
if (omniChainKind === null) return null;
|
|
86
89
|
blockchain = chainKindToCaip2(omniChainKind);
|
|
87
90
|
}
|
|
@@ -232,25 +235,33 @@ var OmniBridge = class {
|
|
|
232
235
|
underlyingFees: { [RouteEnum.OmniBridge]: underlyingFees }
|
|
233
236
|
};
|
|
234
237
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
238
|
+
createWithdrawalIdentifier(args) {
|
|
239
|
+
const assetInfo = this.makeAssetInfo(args.withdrawalParams.assetId, args.withdrawalParams.routeConfig);
|
|
240
|
+
assert(assetInfo !== null, `Asset ${args.withdrawalParams.assetId} is not supported by Omni Bridge`);
|
|
241
|
+
return {
|
|
242
|
+
landingChain: args.withdrawalParams.routeConfig != null && "chain" in args.withdrawalParams.routeConfig && args.withdrawalParams.routeConfig.chain !== void 0 ? args.withdrawalParams.routeConfig.chain : assetInfo.blockchain,
|
|
243
|
+
index: args.index,
|
|
244
|
+
withdrawalParams: args.withdrawalParams,
|
|
245
|
+
tx: args.tx
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
async describeWithdrawal(args) {
|
|
249
|
+
const transfer = (await this.omniBridgeAPI.getTransfer({ transactionHash: args.tx.hash }))[args.index];
|
|
250
|
+
if (transfer == null || transfer.transfer_message == null) return { status: "pending" };
|
|
251
|
+
const destinationChain = getChain(transfer.transfer_message.recipient);
|
|
252
|
+
let txHash = null;
|
|
253
|
+
if (isEvmChain(destinationChain)) txHash = transfer.finalised?.EVMLog?.transaction_hash;
|
|
254
|
+
else if (destinationChain === ChainKind.Sol) txHash = transfer.finalised?.Solana?.signature;
|
|
255
|
+
else if (destinationChain === ChainKind.Btc) txHash = typeof window !== "undefined" ? transfer.utxo_transfer?.btc_pending_id : transfer.finalised?.UtxoLog?.transaction_hash;
|
|
256
|
+
else return {
|
|
257
|
+
status: "completed",
|
|
258
|
+
txHash: null
|
|
259
|
+
};
|
|
260
|
+
if (!txHash) return { status: "pending" };
|
|
261
|
+
return {
|
|
262
|
+
status: "completed",
|
|
263
|
+
txHash
|
|
264
|
+
};
|
|
254
265
|
}
|
|
255
266
|
/**
|
|
256
267
|
* Gets storage deposit for a token to avoid frequent RPC calls.
|
|
@@ -290,6 +301,13 @@ var OmniBridge = class {
|
|
|
290
301
|
if (tokenDecimals !== null) this.tokenDecimalsCache.set(omniAddress$1, tokenDecimals);
|
|
291
302
|
return tokenDecimals;
|
|
292
303
|
}
|
|
304
|
+
/**
|
|
305
|
+
* Checks if passed token contract id is an allowlisted PoA token that should be routed via OmniBridge.
|
|
306
|
+
* Always return false when feature flag routeMigratedPoaTokensThroughOmniBridge = false.
|
|
307
|
+
*/
|
|
308
|
+
isPoaTokenRoutedThroughOmniBridge(nearAddress) {
|
|
309
|
+
return this.routeMigratedPoaTokensThroughOmniBridge ? POA_TOKENS_ROUTABLE_THROUGH_OMNI_BRIDGE[nearAddress] !== void 0 : false;
|
|
310
|
+
}
|
|
293
311
|
};
|
|
294
312
|
|
|
295
313
|
//#endregion
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
2
2
|
const require_caip2 = require('../../lib/caip2.cjs');
|
|
3
3
|
const require_poa_constants = require('./poa-constants.cjs');
|
|
4
|
-
let
|
|
4
|
+
let _defuse_protocol_internal_utils = require("@defuse-protocol/internal-utils");
|
|
5
5
|
|
|
6
6
|
//#region src/bridges/poa-bridge/poa-bridge-utils.ts
|
|
7
7
|
function createWithdrawIntentPrimitive(params) {
|
|
8
|
-
const { contractId: tokenAccountId } =
|
|
8
|
+
const { contractId: tokenAccountId } = _defuse_protocol_internal_utils.utils.parseDefuseAssetId(params.assetId);
|
|
9
9
|
return {
|
|
10
10
|
intent: "ft_withdraw",
|
|
11
11
|
token: tokenAccountId,
|
|
@@ -1,33 +1,38 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_errors = require('../../classes/errors.cjs');
|
|
2
3
|
const require_route_enum = require('../../constants/route-enum.cjs');
|
|
3
4
|
const require_estimate_fee = require('../../lib/estimate-fee.cjs');
|
|
4
|
-
const require_errors = require('../../classes/errors.cjs');
|
|
5
5
|
const require_parse_defuse_asset_id = require('../../lib/parse-defuse-asset-id.cjs');
|
|
6
6
|
const require_validateAddress = require('../../lib/validateAddress.cjs');
|
|
7
7
|
const require_bridge_name_enum = require('../../constants/bridge-name-enum.cjs');
|
|
8
|
+
const require_poa_tokens_routable_through_omni_bridge = require('../../constants/poa-tokens-routable-through-omni-bridge.cjs');
|
|
8
9
|
const require_poa_bridge_utils = require('./poa-bridge-utils.cjs');
|
|
9
|
-
let
|
|
10
|
-
let
|
|
11
|
-
|
|
10
|
+
let _defuse_protocol_internal_utils = require("@defuse-protocol/internal-utils");
|
|
11
|
+
let _isaacs_ttlcache = require("@isaacs/ttlcache");
|
|
12
|
+
_isaacs_ttlcache = require_rolldown_runtime.__toESM(_isaacs_ttlcache);
|
|
12
13
|
|
|
13
14
|
//#region src/bridges/poa-bridge/poa-bridge.ts
|
|
14
15
|
var PoaBridge = class {
|
|
15
|
-
constructor({ env }) {
|
|
16
|
-
this.
|
|
16
|
+
constructor({ env, routeMigratedPoaTokensThroughOmniBridge }) {
|
|
17
|
+
this.route = require_route_enum.RouteEnum.PoaBridge;
|
|
18
|
+
this.supportedTokensCache = new _isaacs_ttlcache.default({ ttl: 30 * 1e3 });
|
|
17
19
|
this.env = env;
|
|
20
|
+
this.routeMigratedPoaTokensThroughOmniBridge = routeMigratedPoaTokensThroughOmniBridge ?? false;
|
|
18
21
|
}
|
|
19
22
|
is(routeConfig) {
|
|
20
23
|
return routeConfig.route === require_route_enum.RouteEnum.PoaBridge;
|
|
21
24
|
}
|
|
22
25
|
async supports(params) {
|
|
23
26
|
if (params.routeConfig != null && !this.is(params.routeConfig)) return false;
|
|
24
|
-
const
|
|
27
|
+
const assetInfo = this.parseAssetId(params.assetId);
|
|
28
|
+
const isValid = assetInfo != null;
|
|
25
29
|
if (!isValid && params.routeConfig != null) throw new require_errors.UnsupportedAssetIdError(params.assetId, "`assetId` does not match `routeConfig`.");
|
|
30
|
+
if (this.routeMigratedPoaTokensThroughOmniBridge && assetInfo != null && require_poa_tokens_routable_through_omni_bridge.POA_TOKENS_ROUTABLE_THROUGH_OMNI_BRIDGE[assetInfo.contractId] !== void 0 && params.routeConfig === void 0) return false;
|
|
26
31
|
return isValid;
|
|
27
32
|
}
|
|
28
33
|
parseAssetId(assetId) {
|
|
29
34
|
const parsed = require_parse_defuse_asset_id.parseDefuseAssetId(assetId);
|
|
30
|
-
if (!parsed.contractId.endsWith(`.${
|
|
35
|
+
if (!parsed.contractId.endsWith(`.${_defuse_protocol_internal_utils.configsByEnvironment[this.env].poaTokenFactoryContractID}`)) return null;
|
|
31
36
|
let blockchain;
|
|
32
37
|
try {
|
|
33
38
|
blockchain = require_poa_bridge_utils.contractIdToCaip2(parsed.contractId);
|
|
@@ -42,7 +47,7 @@ var PoaBridge = class {
|
|
|
42
47
|
}
|
|
43
48
|
createWithdrawalIntents(args) {
|
|
44
49
|
const relayerFee = require_estimate_fee.getUnderlyingFee(args.feeEstimation, require_route_enum.RouteEnum.PoaBridge, "relayerFee");
|
|
45
|
-
(0,
|
|
50
|
+
(0, _defuse_protocol_internal_utils.assert)(relayerFee > 0n, `Invalid POA bridge relayer fee: expected > 0, got ${relayerFee}`);
|
|
46
51
|
const intent = require_poa_bridge_utils.createWithdrawIntentPrimitive({
|
|
47
52
|
...args.withdrawalParams,
|
|
48
53
|
amount: args.withdrawalParams.amount + relayerFee,
|
|
@@ -58,7 +63,7 @@ var PoaBridge = class {
|
|
|
58
63
|
*/
|
|
59
64
|
async validateWithdrawal(args) {
|
|
60
65
|
const assetInfo = this.parseAssetId(args.assetId);
|
|
61
|
-
(0,
|
|
66
|
+
(0, _defuse_protocol_internal_utils.assert)(assetInfo != null, "Asset is not supported");
|
|
62
67
|
if (require_validateAddress.validateAddress(args.destinationAddress, assetInfo.blockchain) === false) throw new require_errors.InvalidDestinationAddressForWithdrawalError(args.destinationAddress, assetInfo.blockchain);
|
|
63
68
|
const { tokens } = await this.getCachedSupportedTokens([require_poa_bridge_utils.toPoaNetwork(assetInfo.blockchain)], args.logger);
|
|
64
69
|
const tokenInfo = tokens.find((token) => token.intents_token_id === args.assetId);
|
|
@@ -69,32 +74,59 @@ var PoaBridge = class {
|
|
|
69
74
|
}
|
|
70
75
|
async estimateWithdrawalFee(args) {
|
|
71
76
|
const assetInfo = this.parseAssetId(args.withdrawalParams.assetId);
|
|
72
|
-
(0,
|
|
73
|
-
const estimation = await
|
|
74
|
-
token:
|
|
77
|
+
(0, _defuse_protocol_internal_utils.assert)(assetInfo != null, "Asset is not supported");
|
|
78
|
+
const estimation = await _defuse_protocol_internal_utils.poaBridge.httpClient.getWithdrawalEstimate({
|
|
79
|
+
token: _defuse_protocol_internal_utils.utils.getTokenAccountId(args.withdrawalParams.assetId),
|
|
75
80
|
address: args.withdrawalParams.destinationAddress,
|
|
76
81
|
chain: require_poa_bridge_utils.toPoaNetwork(assetInfo.blockchain)
|
|
77
82
|
}, {
|
|
78
|
-
baseURL:
|
|
83
|
+
baseURL: _defuse_protocol_internal_utils.configsByEnvironment[this.env].poaBridgeBaseURL,
|
|
79
84
|
logger: args.logger
|
|
80
85
|
});
|
|
81
86
|
const relayerFee = BigInt(estimation.withdrawalFee);
|
|
82
|
-
(0,
|
|
87
|
+
(0, _defuse_protocol_internal_utils.assert)(relayerFee > 0n, `Invalid POA bridge relayer fee: expected > 0, got ${relayerFee}`);
|
|
83
88
|
return {
|
|
84
89
|
amount: relayerFee,
|
|
85
90
|
quote: null,
|
|
86
91
|
underlyingFees: { [require_route_enum.RouteEnum.PoaBridge]: { relayerFee } }
|
|
87
92
|
};
|
|
88
93
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
94
|
+
createWithdrawalIdentifier(args) {
|
|
95
|
+
const assetInfo = this.parseAssetId(args.withdrawalParams.assetId);
|
|
96
|
+
(0, _defuse_protocol_internal_utils.assert)(assetInfo != null, "Asset is not supported");
|
|
97
|
+
return {
|
|
98
|
+
landingChain: assetInfo.blockchain,
|
|
99
|
+
index: args.index,
|
|
100
|
+
withdrawalParams: args.withdrawalParams,
|
|
101
|
+
tx: args.tx
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
async describeWithdrawal(args) {
|
|
105
|
+
const withdrawal = findMatchingWithdrawal((await this.getWithdrawalStatusWithRetry(args)).withdrawals, args.withdrawalParams.assetId);
|
|
106
|
+
if (withdrawal == null) return { status: "pending" };
|
|
107
|
+
if (withdrawal.status === "PENDING") return { status: "pending" };
|
|
108
|
+
if (withdrawal.status === "COMPLETED") return {
|
|
109
|
+
status: "completed",
|
|
110
|
+
txHash: withdrawal.data.transfer_tx_hash
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
status: "failed",
|
|
114
|
+
reason: withdrawal.status
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
async getWithdrawalStatusWithRetry(args) {
|
|
118
|
+
const startTime = Date.now();
|
|
119
|
+
while (true) try {
|
|
120
|
+
return await _defuse_protocol_internal_utils.poaBridge.httpClient.getWithdrawalStatus({ withdrawal_hash: args.tx.hash }, {
|
|
121
|
+
baseURL: _defuse_protocol_internal_utils.configsByEnvironment[this.env].poaBridgeBaseURL,
|
|
122
|
+
logger: args.logger
|
|
123
|
+
});
|
|
124
|
+
} catch (err) {
|
|
125
|
+
if (!isWithdrawalNotFoundError(err)) throw err;
|
|
126
|
+
if (Date.now() - startTime >= NOT_FOUND_RETRY_TIMEOUT_MS) return { withdrawals: [] };
|
|
127
|
+
args.logger?.warn("Withdrawal not indexed yet, retrying...");
|
|
128
|
+
await sleep(NOT_FOUND_RETRY_INTERVAL_MS);
|
|
129
|
+
}
|
|
98
130
|
}
|
|
99
131
|
/**
|
|
100
132
|
* Gets supported tokens with caching to avoid frequent API calls.
|
|
@@ -104,14 +136,35 @@ var PoaBridge = class {
|
|
|
104
136
|
const cacheKey = chains.sort().join(",");
|
|
105
137
|
const cached = this.supportedTokensCache.get(cacheKey);
|
|
106
138
|
if (cached != null) return cached;
|
|
107
|
-
const data = await
|
|
108
|
-
baseURL:
|
|
139
|
+
const data = await _defuse_protocol_internal_utils.poaBridge.httpClient.getSupportedTokens({ chains }, {
|
|
140
|
+
baseURL: _defuse_protocol_internal_utils.configsByEnvironment[this.env].poaBridgeBaseURL,
|
|
109
141
|
logger
|
|
110
142
|
});
|
|
111
143
|
this.supportedTokensCache.set(cacheKey, data);
|
|
112
144
|
return data;
|
|
113
145
|
}
|
|
114
146
|
};
|
|
147
|
+
/**
|
|
148
|
+
* Finds a withdrawal matching the given assetId.
|
|
149
|
+
*
|
|
150
|
+
* NOTE: Currently only matches by assetId. This means multiple withdrawals
|
|
151
|
+
* of the same token in a single transaction are not supported.
|
|
152
|
+
* POA API doesn't currently support this case either. When support is added,
|
|
153
|
+
* matching could be done by sorting both API results and withdrawal params by
|
|
154
|
+
* amount (fees are equal for same token, so relative ordering is preserved).
|
|
155
|
+
*/
|
|
156
|
+
function findMatchingWithdrawal(withdrawals, assetId) {
|
|
157
|
+
return withdrawals.find((w) => `nep141:${w.data.near_token_id}` === assetId);
|
|
158
|
+
}
|
|
159
|
+
const NOT_FOUND_RETRY_TIMEOUT_MS = 3 * 1e3;
|
|
160
|
+
const NOT_FOUND_RETRY_INTERVAL_MS = 1e3;
|
|
161
|
+
const RPC_ERR_MSG_WITHDRAWALS_NOT_FOUND = "Withdrawals not found";
|
|
162
|
+
function isWithdrawalNotFoundError(err) {
|
|
163
|
+
return err instanceof _defuse_protocol_internal_utils.RpcRequestError && err.details === RPC_ERR_MSG_WITHDRAWALS_NOT_FOUND;
|
|
164
|
+
}
|
|
165
|
+
function sleep(ms) {
|
|
166
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
167
|
+
}
|
|
115
168
|
|
|
116
169
|
//#endregion
|
|
117
170
|
exports.PoaBridge = PoaBridge;
|