@gibs/bridge-sdk 1.12.0 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +234 -432
  3. package/dist/abis.d.ts +1 -0
  4. package/dist/bridge-limits.d.ts +103 -0
  5. package/dist/bridge-limits.js +84 -0
  6. package/dist/chain-info.d.ts +169 -0
  7. package/dist/chain-info.js +410 -0
  8. package/dist/chain-names.d.ts +160 -0
  9. package/dist/chain-names.js +200 -0
  10. package/dist/chains.d.ts +65 -0
  11. package/dist/chains.js +284 -0
  12. package/dist/config.d.ts +216 -0
  13. package/dist/config.js +493 -0
  14. package/dist/consolidated-quote.d.ts +554 -0
  15. package/dist/consolidated-quote.js +773 -0
  16. package/dist/destination-calldata.d.ts +118 -0
  17. package/dist/destination-calldata.js +152 -0
  18. package/dist/ecosystems.d.ts +217 -0
  19. package/dist/ecosystems.js +288 -0
  20. package/dist/fee-type.d.ts +10 -0
  21. package/dist/fee-type.js +9 -0
  22. package/dist/hyperliquid-execution.d.ts +152 -0
  23. package/dist/hyperliquid-execution.js +168 -0
  24. package/dist/image-links.d.ts +117 -0
  25. package/dist/image-links.js +142 -0
  26. package/dist/index.d.ts +20 -0
  27. package/dist/index.js +20 -0
  28. package/dist/lifi-execution-guard.d.ts +199 -0
  29. package/dist/lifi-execution-guard.js +400 -0
  30. package/dist/near-intents-conformance.d.ts +85 -0
  31. package/dist/near-intents-conformance.js +329 -0
  32. package/dist/near-intents-route.d.ts +47 -0
  33. package/dist/near-intents-route.js +265 -0
  34. package/dist/near-intents.d.ts +524 -0
  35. package/dist/near-intents.js +1055 -0
  36. package/dist/origin-deposit.d.ts +326 -0
  37. package/dist/origin-deposit.js +68 -0
  38. package/dist/pre-quote.d.ts +198 -0
  39. package/dist/pre-quote.js +175 -0
  40. package/dist/prices.d.ts +107 -0
  41. package/dist/prices.js +162 -0
  42. package/dist/providers.d.ts +221 -0
  43. package/dist/providers.js +157 -0
  44. package/dist/quote-fees.d.ts +102 -0
  45. package/dist/quote-fees.js +195 -0
  46. package/dist/referral.d.ts +77 -0
  47. package/dist/referral.js +72 -0
  48. package/dist/relay-currencies.d.ts +47 -0
  49. package/dist/relay-currencies.js +89 -0
  50. package/dist/relay-quote.d.ts +301 -0
  51. package/dist/relay-quote.js +983 -0
  52. package/dist/route-steps.d.ts +138 -0
  53. package/dist/route-steps.js +305 -0
  54. package/dist/routing/amounts.d.ts +80 -0
  55. package/dist/routing/amounts.js +120 -0
  56. package/dist/routing/candidate-route.d.ts +43 -0
  57. package/dist/routing/candidate-route.js +45 -0
  58. package/dist/routing/candidate.d.ts +237 -0
  59. package/dist/routing/candidate.js +96 -0
  60. package/dist/routing/carriers/ask-answer.d.ts +58 -0
  61. package/dist/routing/carriers/ask-answer.js +1 -0
  62. package/dist/routing/carriers/omnibridge.d.ts +137 -0
  63. package/dist/routing/carriers/omnibridge.js +419 -0
  64. package/dist/routing/carriers/pulsex.d.ts +111 -0
  65. package/dist/routing/carriers/pulsex.js +404 -0
  66. package/dist/routing/carriers/wrap.d.ts +25 -0
  67. package/dist/routing/carriers/wrap.js +115 -0
  68. package/dist/routing/composed-route-quality.d.ts +103 -0
  69. package/dist/routing/composed-route-quality.js +108 -0
  70. package/dist/routing/entry-chain-anchor.d.ts +88 -0
  71. package/dist/routing/entry-chain-anchor.js +55 -0
  72. package/dist/routing/format.d.ts +31 -0
  73. package/dist/routing/format.js +58 -0
  74. package/dist/routing/index.d.ts +34 -0
  75. package/dist/routing/index.js +42 -0
  76. package/dist/routing/producers.d.ts +770 -0
  77. package/dist/routing/producers.js +1564 -0
  78. package/dist/routing/providers.d.ts +26 -0
  79. package/dist/routing/providers.js +38 -0
  80. package/dist/routing/route-data.d.ts +355 -0
  81. package/dist/routing/route-data.js +1 -0
  82. package/dist/routing/search/compose.d.ts +168 -0
  83. package/dist/routing/search/compose.js +98 -0
  84. package/dist/routing/search/graph.d.ts +639 -0
  85. package/dist/routing/search/graph.js +445 -0
  86. package/dist/routing/search/plan-route-data.d.ts +244 -0
  87. package/dist/routing/search/plan-route-data.js +767 -0
  88. package/dist/routing/search/score.d.ts +324 -0
  89. package/dist/routing/search/score.js +400 -0
  90. package/dist/routing/usd-value.d.ts +17 -0
  91. package/dist/routing/usd-value.js +25 -0
  92. package/dist/settings.d.ts +692 -0
  93. package/dist/settings.js +909 -0
  94. package/dist/ton-execution.d.ts +117 -0
  95. package/dist/ton-execution.js +247 -0
  96. package/dist/tron-execution.d.ts +146 -0
  97. package/dist/tron-execution.js +239 -0
  98. package/dist/types.d.ts +51 -0
  99. package/dist/types.js +1 -0
  100. package/package.json +203 -23
  101. package/src/chain-info.test.ts +0 -968
  102. package/src/chain-info.ts +0 -498
  103. package/src/chains.test.ts +0 -123
  104. package/src/chains.ts +0 -88
  105. package/src/config.test.ts +0 -679
  106. package/src/config.ts +0 -500
  107. package/src/fee-type.test.ts +0 -45
  108. package/src/fee-type.ts +0 -14
  109. package/src/image-links.test.ts +0 -160
  110. package/src/image-links.ts +0 -65
  111. package/src/index.ts +0 -8
  112. package/src/settings.test.ts +0 -2754
  113. package/src/settings.ts +0 -875
  114. package/src/types.ts +0 -48
  115. package/tsconfig.json +0 -17
  116. package/vitest.config.ts +0 -13
  117. /package/{src/abis.ts → dist/abis.js} +0 -0
package/src/settings.ts DELETED
@@ -1,875 +0,0 @@
1
- import {
2
- concatHex,
3
- encodeAbiParameters,
4
- encodeFunctionData,
5
- getAddress,
6
- type Hex,
7
- isAddress,
8
- zeroAddress,
9
- } from 'viem';
10
- import * as abis from './abis.js';
11
- import type { TokenBridgeInfo } from './chain-info.js';
12
- import { type Chains, isNative, nativeAssetOut, type Pathway, pathway } from './config.js';
13
- import { FeeType } from './fee-type.js';
14
- import type { BridgeKey, Token } from './types.js';
15
-
16
- export const oneEther = 10n ** 18n;
17
-
18
- export const basisPoints = 10_000n;
19
-
20
- /**
21
- * Returns the bridge pathway (configuration) for a given bridge key, asset in, and whether to use the production pathway
22
- * @param bridgeKey - the bridge key to use
23
- * @param isProd - whether to use the production pathway
24
- * @param assetIn - the asset in to bridge
25
- * @returns the bridge pathway
26
- */
27
- export const bridgePathway = ({
28
- bridgeKey,
29
- isProd,
30
- assetIn,
31
- }: {
32
- bridgeKey: BridgeKey;
33
- isProd: boolean;
34
- assetIn: Token | null;
35
- }) => pathway(bridgeKey, isProd, assetIn?.address) ?? null;
36
-
37
- /**
38
- * Retrieves the amount to bridge from the amount and asset in. If either of them is nil, returns 0n
39
- * @param amountIn - the amount to bridge
40
- * @param assetIn - the asset in to bridge
41
- * @returns the amount to bridge
42
- */
43
- export const amountToBridge = ({
44
- amountIn,
45
- assetIn,
46
- }: {
47
- amountIn: bigint | null;
48
- assetIn: Token | null;
49
- }) => {
50
- if (!amountIn || amountIn === 0n || !assetIn) {
51
- return 0n;
52
- }
53
- return amountIn;
54
- };
55
-
56
- /**
57
- * Calculates the bridge cost from the amount to bridge and bridge fee
58
- * @param amountToBridge - the amount to bridge
59
- * @param bridgeFee - the bridge fee
60
- * @returns the bridge cost
61
- */
62
- export const bridgeCost = ({
63
- amountToBridge,
64
- bridgeFee,
65
- }: {
66
- amountToBridge: bigint;
67
- bridgeFee: bigint | null;
68
- }) => {
69
- if (bridgeFee === null) return null;
70
- return (amountToBridge * bridgeFee) / oneEther;
71
- };
72
-
73
- /**
74
- * Calculates the amount after bridge fee from the amount to bridge and bridge cost
75
- * @param amountToBridge - the amount to bridge
76
- * @param bridgeCost - the bridge cost
77
- * @returns the amount after bridge fee
78
- */
79
- export const amountAfterBridgeFee = ({
80
- amountToBridge,
81
- bridgeCost,
82
- }: {
83
- amountToBridge: bigint;
84
- bridgeCost: bigint | null;
85
- }) => {
86
- if (bridgeCost === null) return null;
87
- const afterFee = amountToBridge - bridgeCost;
88
- if (afterFee < 0n) return 0n;
89
- return afterFee;
90
- };
91
-
92
- /**
93
- * Calculates the estimated amount out from the amount after bridge fee and fee
94
- * @param amountAfterBridgeFee - the amount after bridge fee
95
- * @param fee - the fee
96
- * @returns the estimated amount out
97
- */
98
- export const estimatedAmountOut = ({
99
- amountAfterBridgeFee,
100
- fee,
101
- }: {
102
- amountAfterBridgeFee: bigint | null;
103
- fee: bigint | null;
104
- }) => {
105
- if (typeof fee !== 'bigint') return null;
106
- if (amountAfterBridgeFee === null) return null;
107
- if (fee > amountAfterBridgeFee) return 0n;
108
- return amountAfterBridgeFee - fee;
109
- };
110
-
111
- /**
112
- * Calculates the estimated fee from the amount after bridge fee, fee, percent fee, fee type, limit, and whether to deliver the costs
113
- * @param amountAfterBridgeFee - the amount after bridge fee
114
- * @param fee - the fee
115
- * @param percentFee - the percent fee
116
- * @param feeType - the fee type
117
- * @param limit - the limit
118
- * @param costsToDeliver - whether to deliver the costs
119
- * @returns the estimated fee
120
- */
121
- export const estimatedFee = ({
122
- amountAfterBridgeFee,
123
- fee,
124
- percentFee,
125
- feeType,
126
- limit,
127
- costsToDeliver,
128
- }: {
129
- amountAfterBridgeFee: bigint | null;
130
- fee: bigint;
131
- percentFee: bigint;
132
- feeType: FeeType;
133
- limit: bigint | null;
134
- costsToDeliver: boolean;
135
- }) => {
136
- const fixedFee = amountAfterBridgeFee ? (amountAfterBridgeFee * percentFee) / oneEther : null;
137
- if (!costsToDeliver) {
138
- return 0n;
139
- }
140
- if (feeType === FeeType.FIXED) {
141
- return fee;
142
- } else if (feeType === FeeType.PERCENT) {
143
- if (amountAfterBridgeFee === null) return null;
144
- return fixedFee;
145
- } else if (feeType === FeeType.GAS_TIP) {
146
- return limit;
147
- }
148
- return null;
149
- };
150
-
151
- /**
152
- * Calculates the estimated native network cost from the estimated gas and latest base fee per gas
153
- * @param estimatedGas - the estimated gas
154
- * @param latestBaseFeePerGas - the latest base fee per gas
155
- * @param requiresDelivery - whether to deliver the costs
156
- * @returns the estimated native network cost
157
- */
158
- export const estimatedNativeNetworkCost = ({
159
- estimatedGas,
160
- latestBaseFeePerGas,
161
- requiresDelivery,
162
- }: {
163
- estimatedGas: bigint | null;
164
- latestBaseFeePerGas: bigint | null;
165
- requiresDelivery: boolean;
166
- }) => {
167
- if (!estimatedGas || !latestBaseFeePerGas || !requiresDelivery) {
168
- return null;
169
- }
170
- return BigInt(estimatedGas * latestBaseFeePerGas);
171
- };
172
-
173
- /**
174
- * Calculates the estimated token network cost from the estimated native
175
- * network cost, price corrective, decimals, and one token int
176
- * @param estimatedNativeNetworkCost - the estimated native network cost
177
- * @param priceCorrective - the price corrective
178
- * @param decimals - the decimals
179
- * @param oneTokenInt - the one token int
180
- * @returns the estimated token network cost
181
- */
182
- export const estimatedTokenNetworkCost = ({
183
- estimatedNativeNetworkCost,
184
- priceCorrective,
185
- decimals,
186
- oneTokenInt,
187
- }: {
188
- estimatedNativeNetworkCost: bigint | null;
189
- priceCorrective: bigint | null;
190
- decimals: number | null;
191
- oneTokenInt: bigint | null;
192
- }) => {
193
- if (!priceCorrective || !oneTokenInt || !estimatedNativeNetworkCost || !decimals) {
194
- return null;
195
- }
196
- const tokenCost = (estimatedNativeNetworkCost * oneTokenInt) / priceCorrective;
197
- return tokenCost;
198
- };
199
-
200
- /**
201
- * Calculates one token of the asset in as an int
202
- * @param assetIn - the asset in to bridge
203
- * @returns the one token int
204
- */
205
- export const oneTokenInt = ({ assetIn }: { assetIn: Token | null }) => {
206
- if (!assetIn) return 1n;
207
- return 10n ** BigInt(assetIn.decimals);
208
- };
209
-
210
- /**
211
- * Returns the limit as a bigint. Nil uses 0n
212
- * @param lim - the limit
213
- * @returns the limit
214
- */
215
- export const limit = (lim: bigint | null) => {
216
- if (!lim) {
217
- return 0n;
218
- }
219
- return lim;
220
- };
221
-
222
- /**
223
- * Returns the network switch asset out address from the asset out, unwrap, and to chain
224
- * @param assetOut - the asset out
225
- * @param unwrap - whether to unwrap
226
- * @param toChain - the chain to switch to
227
- * @returns the network switch asset out address
228
- */
229
- export const networkSwitchAssetOutAddress = ({
230
- assetOut,
231
- unwrap,
232
- toChain,
233
- }: {
234
- assetOut: Token | null;
235
- unwrap: boolean;
236
- toChain: Chains;
237
- }) => {
238
- if (!assetOut) return null;
239
- if (!unwrap) return assetOut.address;
240
- return nativeAssetOut[toChain] === assetOut.address ? zeroAddress : assetOut.address;
241
- };
242
-
243
- /**
244
- * Calculates the desired excess compensation basis points from the path, asset in, asset out, bridge key, fee type, list, and asset out address
245
- * @param path - the path
246
- * @param assetIn - the asset in to bridge
247
- * @param assetOut - the asset out
248
- * @param bridgeKey - the bridge key
249
- * @param feeType - the fee type
250
- * @param list - the list
251
- * @param assetOutAddress - the asset out address
252
- * @returns the desired excess compensation basis points
253
- */
254
- export const desiredExcessCompensationBasisPoints = ({
255
- path,
256
- assetIn,
257
- assetOut,
258
- bridgeKey,
259
- feeType,
260
- list,
261
- assetOutAddress,
262
- }: {
263
- path: Pathway | null;
264
- assetIn: Token | null;
265
- assetOut: Token | null;
266
- bridgeKey: BridgeKey;
267
- feeType: FeeType;
268
- list: Set<string>;
269
- assetOutAddress: string | null;
270
- }) => {
271
- if (!path?.requiresDelivery) {
272
- return 0n;
273
- }
274
- return !assetIn
275
- ? 0n
276
- : feeType === FeeType.PERCENT
277
- ? 1_000n
278
- : isNative(assetIn, bridgeKey) || isNative(assetOut, bridgeKey)
279
- ? 1_000n
280
- : list.has(getAddress(assetIn?.address ?? '')) ||
281
- list.has(assetOutAddress ? getAddress(assetOutAddress) : '0x')
282
- ? 5_000n
283
- : 10_000n;
284
- };
285
-
286
- /**
287
- * Calculates the estimated cost from the should deliver, amount after bridge fee, percent fee,
288
- * estimated token network cost, reasonable percent on top of gas fee, and fee type
289
- * @param shouldDeliver - whether to deliver the costs
290
- * @param amountAfterBridgeFee - the amount after bridge fee
291
- * @param percentFee - the percent fee
292
- * @param estimatedTokenNetworkCost - the estimated token network cost
293
- * @param reasonablePercentOnTopOfGasFee - the reasonable percent on top of gas fee
294
- * @param feeType - the fee type
295
- * @returns the estimated cost
296
- */
297
- export const estimatedCost = ({
298
- shouldDeliver,
299
- amountAfterBridgeFee,
300
- percentFee,
301
- estimatedTokenNetworkCost,
302
- reasonablePercentOnTopOfGasFee,
303
- feeType,
304
- }: {
305
- shouldDeliver: boolean;
306
- amountAfterBridgeFee: bigint | null;
307
- percentFee: bigint | null;
308
- estimatedTokenNetworkCost: bigint | null;
309
- reasonablePercentOnTopOfGasFee: bigint | null;
310
- feeType: FeeType;
311
- }) => {
312
- if (!shouldDeliver) {
313
- return null;
314
- }
315
- if (feeType === FeeType.PERCENT) {
316
- if (amountAfterBridgeFee === null) return null;
317
- return (amountAfterBridgeFee * (percentFee ?? 0n)) / oneEther;
318
- }
319
- // gas tip
320
- if (!estimatedTokenNetworkCost) {
321
- return null;
322
- }
323
- if (!reasonablePercentOnTopOfGasFee) {
324
- return null;
325
- }
326
- return estimatedTokenNetworkCost * (reasonablePercentOnTopOfGasFee + oneEther);
327
- };
328
-
329
- /**
330
- * Calculates the reasonable fixed fee from the estimated token network cost and desired excess compensation basis points
331
- * @param estimatedTokenNetworkCost - the estimated token network cost
332
- * @param desiredExcessCompensationBasisPoints - the desired excess compensation basis points
333
- * @returns the reasonable fixed fee
334
- */
335
- export const reasonableFixedFee = ({
336
- estimatedTokenNetworkCost,
337
- desiredExcessCompensationBasisPoints,
338
- }: {
339
- estimatedTokenNetworkCost: bigint | null;
340
- desiredExcessCompensationBasisPoints: bigint;
341
- }) => {
342
- if (!estimatedTokenNetworkCost) {
343
- return null;
344
- }
345
- return (
346
- (estimatedTokenNetworkCost * (25_000n + desiredExcessCompensationBasisPoints)) / basisPoints
347
- );
348
- };
349
-
350
- /**
351
- * Calculates the reasonable percent on gas limit from the gas tip fee and estimated token network cost
352
- * @param gasTipFee - the gas tip fee
353
- * @param estimatedTokenNetworkCost - the estimated token network cost
354
- * @returns the reasonable percent on gas limit
355
- */
356
- export const reasonablePercentOnGasLimit = ({
357
- gasTipFee,
358
- estimatedTokenNetworkCost,
359
- }: {
360
- gasTipFee: bigint | null;
361
- estimatedTokenNetworkCost: bigint | null;
362
- }) => {
363
- return (
364
- (25_000n * ((oneEther + (gasTipFee ?? 0n)) * (estimatedTokenNetworkCost ?? 0n))) /
365
- (oneEther * basisPoints)
366
- );
367
- };
368
-
369
- /**
370
- * Calculates the reasonable percent fee from the reasonable fixed fee and amount after bridge fee
371
- * @param reasonableFixedFee - the reasonable fixed fee
372
- * @param amountAfterBridgeFee - the amount after bridge fee
373
- * @returns the reasonable percent fee
374
- */
375
- export const reasonablePercentFee = ({
376
- reasonableFixedFee,
377
- amountAfterBridgeFee,
378
- }: {
379
- reasonableFixedFee: bigint | null;
380
- amountAfterBridgeFee: bigint | null;
381
- }) => {
382
- if (!reasonableFixedFee) {
383
- return null;
384
- }
385
- if (!amountAfterBridgeFee) {
386
- return null;
387
- }
388
- const highFee = amountAfterBridgeFee / 10n;
389
- const basisFeeTruncator = 10n ** 14n;
390
- const max = 1_000n * basisFeeTruncator;
391
- const min = 5n * basisFeeTruncator;
392
- if (highFee < reasonableFixedFee) {
393
- return max; // 10%
394
- }
395
- const highResPercent = (reasonableFixedFee * oneEther) / amountAfterBridgeFee;
396
- if (highResPercent < min) {
397
- return min;
398
- } else if (highResPercent > max) {
399
- return max;
400
- }
401
- const basisPoint = highResPercent / basisFeeTruncator;
402
- return basisPoint * basisFeeTruncator;
403
- };
404
-
405
- /**
406
- * Returns whether the asset in address is interacting with a bridge token
407
- * @param assetLink - the asset link
408
- * @param assetInAddress - the asset in address
409
- * @returns whether the asset in address is interacting with a bridge token
410
- */
411
- export const interactingWithBridgeToken = ({
412
- assetLink,
413
- assetInAddress,
414
- }: {
415
- assetLink: TokenBridgeInfo | null;
416
- assetInAddress: string | null;
417
- }) => {
418
- const { toForeign, toHome } = assetLink || {};
419
- const { foreign } = toForeign || {};
420
- const { home } = toHome || {};
421
- if (!assetInAddress) return false;
422
- return !!(
423
- (foreign && getAddress(foreign) === getAddress(assetInAddress)) ||
424
- (home && getAddress(home) === getAddress(assetInAddress))
425
- );
426
- };
427
-
428
- /**
429
- * Calculates the fee director struct encoded from the recipient, bridge pathway, fee type, price corrective, gas tip fee, percent fee, fee type settings, limit, and asset out
430
- * @param recipient - the recipient
431
- * @param bridgePathway - the bridge pathway
432
- * @param feeType - the fee type
433
- * @param priceCorrective - the price corrective
434
- * @param gasTipFee - the gas tip fee
435
- * @param percentFee - the percent fee
436
- * @param feeTypeSettings - the fee type settings
437
- * @param limit - the limit
438
- * @param assetOut - the asset out
439
- * @returns the fee director struct encoded
440
- */
441
- export const feeDirectorStructEncoded = ({
442
- recipient,
443
- bridgePathway,
444
- feeType,
445
- priceCorrective,
446
- gasTipFee,
447
- percentFee,
448
- feeTypeSettings,
449
- limit,
450
- assetOut,
451
- }: {
452
- recipient: Hex | null;
453
- feeType: FeeType;
454
- priceCorrective: bigint | null;
455
- gasTipFee: bigint | null;
456
- percentFee: bigint | null;
457
- feeTypeSettings: bigint;
458
- limit: bigint;
459
- bridgePathway: Pathway | null;
460
- assetOut: Token | null;
461
- }) => {
462
- if (!assetOut || !bridgePathway) {
463
- return null;
464
- }
465
- if (!bridgePathway.requiresDelivery) {
466
- return null;
467
- }
468
- if (!priceCorrective) {
469
- return null;
470
- }
471
- let multiplier = 0n;
472
- if (feeType === FeeType.GAS_TIP && priceCorrective > 0n) {
473
- multiplier =
474
- ((oneEther + (gasTipFee ?? 0n)) * 10n ** BigInt(assetOut.decimals)) / priceCorrective;
475
- } else if (feeType === FeeType.PERCENT) {
476
- multiplier = percentFee ?? 0n;
477
- }
478
- if (!recipient || !isAddress(recipient)) {
479
- return null;
480
- }
481
- return encodeAbiParameters(abis.feeDeliveryStruct, [
482
- [recipient, feeTypeSettings, limit, multiplier],
483
- ]);
484
- };
485
-
486
- /**
487
- * Calculates the destination data param from the recipient, bridge pathway, asset in, destination router, fee director struct encoded, asset link, should deliver, and unwrap
488
- * @param recipient - the recipient
489
- * @param bridgePathway - the bridge pathway
490
- * @param assetIn - the asset in to bridge
491
- * @param destinationRouter - the destination router
492
- * @param feeDirectorStructEncoded - the fee director struct encoded
493
- * @param assetLink - the asset link
494
- * @param shouldDeliver - whether to deliver the costs
495
- * @param unwrap - whether to unwrap
496
- * @returns the destination data param
497
- */
498
- export const destinationDataParam = ({
499
- recipient,
500
- bridgePathway,
501
- assetIn,
502
- destinationRouter,
503
- feeDirectorStructEncoded,
504
- assetLink,
505
- shouldDeliver,
506
- unwrap,
507
- }: {
508
- recipient: Hex | null;
509
- bridgePathway: Pathway | null;
510
- assetIn: Token | null;
511
- destinationRouter: Hex | null;
512
- feeDirectorStructEncoded: Hex | null;
513
- assetLink: TokenBridgeInfo | null;
514
- shouldDeliver: boolean;
515
- unwrap: boolean;
516
- }) => {
517
- // const recipient = input.recipient.value
518
- if (!recipient) {
519
- return null;
520
- }
521
- if (!bridgePathway || !assetIn) {
522
- return null;
523
- }
524
- if (!destinationRouter) {
525
- console.log('no destination router');
526
- return null;
527
- }
528
- if (bridgePathway.requiresDelivery && !feeDirectorStructEncoded) {
529
- console.log('no fee director struct encoded');
530
- return null;
531
- }
532
- if (!assetLink) {
533
- console.log('no asset link');
534
- return null;
535
- }
536
- if (bridgePathway.requiresDelivery) {
537
- // going from home to foreign
538
- if (!shouldDeliver) {
539
- // act like a regular bridge
540
- return recipient;
541
- }
542
- // going from foreign to home
543
- if (feeDirectorStructEncoded) {
544
- return concatHex([destinationRouter, feeDirectorStructEncoded]);
545
- }
546
- return recipient;
547
- }
548
- if (!unwrap) {
549
- return recipient;
550
- }
551
- // unwrap wpls->pls
552
- return concatHex([destinationRouter, recipient]);
553
- };
554
-
555
- /**
556
- * Converts a desired excess compensation basis point value into a percentage as an integer, with the denominator assumed to be 1e18
557
- * @param desiredExcessCompensationBasisPoints - the desired excess compensation basis points
558
- * @returns the reasonable percent on top of gas fee
559
- */
560
- export const reasonablePercentOnTopOfGasFee = ({
561
- desiredExcessCompensationBasisPoints,
562
- }: {
563
- desiredExcessCompensationBasisPoints: bigint;
564
- }) => {
565
- return desiredExcessCompensationBasisPoints * (oneEther / basisPoints);
566
- };
567
-
568
- /**
569
- * Calculates the desired compensation ratio from the desired excess compensation basis points
570
- * @param desiredExcessCompensationBasisPoints - the desired excess compensation basis points
571
- * @returns the desired compensation ratio
572
- */
573
- export const desiredCompensationRatio = ({
574
- desiredExcessCompensationBasisPoints,
575
- }: {
576
- desiredExcessCompensationBasisPoints: bigint;
577
- }) => {
578
- return oneEther + (desiredExcessCompensationBasisPoints * oneEther) / basisPoints;
579
- };
580
-
581
- /**
582
- * Calculates the available compensation maximum from the amount after bridge fee, percent fee, fee type, fixed fee, and limit
583
- * @param amountAfterBridgeFee - the amount after bridge fee
584
- * @param percentFee - the percent fee
585
- * @param feeType - the fee type
586
- * @param fixedFee - the fixed fee
587
- * @param limit - the limit
588
- * @returns the available compensation maximum
589
- */
590
- export const availableCompensationMaximum = ({
591
- amountAfterBridgeFee,
592
- percentFee,
593
- feeType,
594
- fixedFee,
595
- limit,
596
- }: {
597
- amountAfterBridgeFee: bigint | null;
598
- percentFee: bigint | null;
599
- feeType: FeeType;
600
- fixedFee: bigint | null;
601
- limit: bigint;
602
- }) => {
603
- if (feeType === FeeType.PERCENT) {
604
- if (amountAfterBridgeFee === null) return null;
605
- return (amountAfterBridgeFee * (percentFee ?? 0n)) / oneEther;
606
- } else if (feeType === FeeType.GAS_TIP) {
607
- return limit;
608
- } else if (feeType === FeeType.FIXED) {
609
- return fixedFee ?? 0n;
610
- }
611
- return 0n;
612
- };
613
-
614
- /**
615
- * Determines if the bridge is undercompensated based on network costs vs compensation limit using desired compensation rate
616
- * @param estimatedTokenNetworkCost - the estimated token network cost
617
- * @param feeType - the fee type
618
- * @param limit - the maximum compensation limit
619
- * @param amountAfterBridgeFee - the amount after bridge fee
620
- * @param desiredExcessCompensationBasisPoints - the desired excess compensation basis points
621
- * @returns whether the bridge is undercompensated
622
- */
623
- export const isUndercompensated = ({
624
- estimatedTokenNetworkCost,
625
- feeType,
626
- limit,
627
- amountAfterBridgeFee,
628
- desiredExcessCompensationBasisPoints,
629
- }: {
630
- estimatedTokenNetworkCost: bigint | null;
631
- feeType: FeeType;
632
- limit: bigint;
633
- amountAfterBridgeFee: bigint | null;
634
- desiredExcessCompensationBasisPoints: bigint;
635
- }) => {
636
- // No undercompensation if no amount to bridge
637
- if (!amountAfterBridgeFee || amountAfterBridgeFee <= 0n) {
638
- return false;
639
- }
640
-
641
- // GAS_TIP fee type doesn't have undercompensation issues
642
- if (feeType === FeeType.GAS_TIP) {
643
- return false;
644
- }
645
-
646
- // No network cost estimate means we can't determine undercompensation
647
- if (!estimatedTokenNetworkCost) {
648
- return false;
649
- }
650
-
651
- // Calculate desired compensation using the actual desired compensation ratio
652
- const desiredCompensationRatio =
653
- oneEther + (desiredExcessCompensationBasisPoints * oneEther) / basisPoints;
654
- const desiredCompensationForNetworkCost =
655
- (estimatedTokenNetworkCost * desiredCompensationRatio) / oneEther;
656
-
657
- // Undercompensated if desired compensation exceeds the limit
658
- return desiredCompensationForNetworkCost > limit;
659
- };
660
-
661
- /**
662
- * Calculates the transaction inputs from the bridge pathway, asset in, recipient, account, asset link, destination data param, amount to bridge, fee director struct encoded, and should deliver
663
- * @param bridgePathway - the bridge pathway to use
664
- * @param assetIn - the asset in to bridge
665
- * @param recipient - the recipient of the bridge
666
- * @param account - the account to bridge from
667
- * @param assetLink - the asset link to use
668
- * @param destinationDataParam - the destination data param to use
669
- * @param amountToBridge - the amount to bridge
670
- * @param feeDirectorStructEncoded - the fee director struct encoded to use
671
- * @param shouldDeliver - whether to deliver the tokens to the recipient
672
- * @returns the transaction inputs
673
- */
674
- export const transactionInputs = ({
675
- bridgePathway,
676
- assetIn,
677
- recipient,
678
- account,
679
- assetLink,
680
- destinationDataParam,
681
- amountToBridge,
682
- feeDirectorStructEncoded,
683
- shouldDeliver,
684
- }: {
685
- bridgePathway: Pathway | null;
686
- assetIn: Token | null;
687
- recipient: Hex | null;
688
- account: Hex | null;
689
- assetLink: TokenBridgeInfo | null;
690
- destinationDataParam: Hex | null;
691
- amountToBridge: bigint;
692
- feeDirectorStructEncoded: Hex | null;
693
- shouldDeliver: boolean;
694
- }) => {
695
- if (!bridgePathway) {
696
- console.log('no bridge pathway');
697
- return null;
698
- }
699
- // check that recipient is valid
700
- if (!recipient || !isAddress(recipient) || recipient === zeroAddress) {
701
- console.log('no recipient');
702
- return null;
703
- }
704
- // check that wallet account is valid
705
- if (!account || !isAddress(account) || account === zeroAddress) {
706
- console.log('no account');
707
- return null;
708
- }
709
- // check that asset in is valid
710
- if (!assetIn) {
711
- console.log('no asset in');
712
- return null;
713
- }
714
- // check that asset link is valid
715
- if (!assetLink) {
716
- console.log('no asset link');
717
- return null;
718
- }
719
- let value = 0n;
720
- // we are moving "from" this side, so we need to call a function on the "from" address
721
- // only relevant for the relayTokens(AndCall) pathway outside of native tokens
722
- let toAddress = bridgePathway.from;
723
- let data = '0x' as Hex;
724
- const inputIsBridgeToken = interactingWithBridgeToken({
725
- assetLink,
726
- assetInAddress: assetIn.address,
727
- });
728
- if (inputIsBridgeToken) {
729
- // when interacting with a bridged token, we need to call the token address directly
730
- toAddress = assetIn.address as Hex;
731
- value = 0n;
732
- // if we want delivery of the tokens (going from home to foreign) then we need to have the foreign data param
733
- // and it needs to start with the destination router
734
- if (!destinationDataParam) {
735
- return null;
736
- }
737
- data = bridgePathway.usesExtraParam
738
- ? encodeFunctionData({
739
- abi: abis.erc677ExtraInput,
740
- functionName: 'transferAndCall',
741
- args: [bridgePathway.from, amountToBridge, destinationDataParam, account],
742
- })
743
- : encodeFunctionData({
744
- abi: abis.erc677,
745
- functionName: 'transferAndCall',
746
- args: [bridgePathway.from, amountToBridge, destinationDataParam],
747
- });
748
- } else if (assetIn.address === zeroAddress) {
749
- value = amountToBridge;
750
- toAddress = bridgePathway.nativeRouter;
751
- if (bridgePathway.feeManager === 'from' && shouldDeliver) {
752
- // transferring native to foreign
753
- if (!feeDirectorStructEncoded) return null;
754
- if (!bridgePathway.destinationRouter) return null;
755
- data = bridgePathway.usesExtraParam
756
- ? encodeFunctionData({
757
- abi: abis.nativeRouterExtraInput,
758
- functionName: 'relayTokensAndCall',
759
- args: [bridgePathway.destinationRouter, feeDirectorStructEncoded, account],
760
- })
761
- : encodeFunctionData({
762
- abi: abis.nativeRouter,
763
- functionName: 'relayTokensAndCall',
764
- args: [bridgePathway.destinationRouter, feeDirectorStructEncoded],
765
- });
766
- } else {
767
- // delivery always occurs when moving from foreign to home
768
- data = bridgePathway.usesExtraParam
769
- ? encodeFunctionData({
770
- abi: abis.nativeRouterExtraInput,
771
- functionName: 'wrapAndRelayTokens',
772
- args: [recipient, account],
773
- })
774
- : encodeFunctionData({
775
- abi: abis.nativeRouter,
776
- functionName: 'wrapAndRelayTokens',
777
- args: [recipient],
778
- });
779
- }
780
- } else {
781
- // tokens native to this side, entering the bridge
782
- if (shouldDeliver && bridgePathway.requiresDelivery) {
783
- if (!feeDirectorStructEncoded) return null;
784
- if (!bridgePathway.destinationRouter) return null;
785
- data = bridgePathway.usesExtraParam
786
- ? encodeFunctionData({
787
- abi: abis.inputBridgeExtraInput,
788
- functionName: 'relayTokensAndCall',
789
- args: [
790
- assetIn.address as Hex,
791
- bridgePathway.destinationRouter,
792
- amountToBridge,
793
- feeDirectorStructEncoded,
794
- account,
795
- ],
796
- })
797
- : encodeFunctionData({
798
- abi: abis.inputBridge,
799
- functionName: 'relayTokensAndCall',
800
- args: [
801
- assetIn.address as Hex,
802
- bridgePathway.destinationRouter,
803
- amountToBridge,
804
- feeDirectorStructEncoded,
805
- ],
806
- });
807
- } else {
808
- data = bridgePathway.usesExtraParam
809
- ? encodeFunctionData({
810
- abi: abis.inputBridgeExtraInput,
811
- functionName: 'relayTokens',
812
- args: [assetIn.address as Hex, recipient, amountToBridge, account],
813
- })
814
- : encodeFunctionData({
815
- abi: abis.inputBridge,
816
- functionName: 'relayTokens',
817
- args: [assetIn.address as Hex, recipient, amountToBridge],
818
- });
819
- }
820
- }
821
- return {
822
- to: toAddress,
823
- value,
824
- data,
825
- };
826
- };
827
-
828
- /**
829
- * Calculates the asset out key from the bridge key path, asset in address, and unwrap
830
- * @param bridgeKeyPath - the bridge key path
831
- * @param assetInAddress - the asset in address
832
- * @param unwrap - whether to unwrap
833
- * @returns the asset out key
834
- */
835
- export const assetOutKey = ({
836
- bridgeKeyPath,
837
- assetInAddress,
838
- unwrap,
839
- }: {
840
- bridgeKeyPath: string;
841
- assetInAddress?: Hex | null;
842
- unwrap: boolean;
843
- }) => {
844
- if (!bridgeKeyPath || !assetInAddress) return null;
845
- return `${bridgeKeyPath}/${unwrap ? 'native' : 'erc20'}/${assetInAddress.toLowerCase()}`;
846
- };
847
-
848
- /**
849
- * Returns the asset out from the asset in address, assets out, bridge key path, and unwrap
850
- * @param assetInAddress - the asset in address
851
- * @param assetsOut - the assets out
852
- * @param bridgeKeyPath - the bridge key path
853
- * @param unwrap - whether to unwrap
854
- * @returns the asset out
855
- */
856
- export const assetOut = ({
857
- assetInAddress,
858
- assetsOut,
859
- bridgeKeyPath,
860
- unwrap,
861
- }: {
862
- assetInAddress: Hex | null;
863
- assetsOut: Map<string, Token>;
864
- bridgeKeyPath: string;
865
- unwrap: boolean;
866
- }) => {
867
- if (!assetInAddress) return null;
868
- const key = assetOutKey({
869
- bridgeKeyPath,
870
- assetInAddress,
871
- unwrap,
872
- });
873
- if (!key) return null;
874
- return assetsOut.get(key) ?? null;
875
- };