@gmx-io/sdk 1.2.0 → 1.2.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.
Files changed (88) hide show
  1. package/build/cjs/src/abis/ClaimHandler.json +439 -0
  2. package/build/cjs/src/abis/index.js +4 -0
  3. package/build/cjs/src/configs/__tests__/markets.spec.js +1 -1
  4. package/build/cjs/src/configs/__tests__/tokens.spec.js +1 -1
  5. package/build/cjs/src/configs/batch.js +12 -0
  6. package/build/cjs/src/configs/contracts.js +2 -0
  7. package/build/cjs/src/configs/dataStore.js +13 -3
  8. package/build/cjs/src/configs/express.js +2 -1
  9. package/build/cjs/src/configs/markets.js +35 -0
  10. package/build/cjs/src/configs/oracleKeeper.js +19 -35
  11. package/build/cjs/src/configs/tokens.js +55 -0
  12. package/build/cjs/src/index.js +3 -3
  13. package/build/cjs/src/modules/orders/helpers.js +9 -0
  14. package/build/cjs/src/modules/orders/helpers.spec.js +1 -1
  15. package/build/cjs/src/modules/orders/orders.js +3 -3
  16. package/build/cjs/src/modules/utils/utils.js +3 -3
  17. package/build/cjs/src/prebuilt/hashedKinkModelMarketRatesKeys.json +40 -0
  18. package/build/cjs/src/prebuilt/hashedMarketConfigKeys.json +255 -0
  19. package/build/cjs/src/prebuilt/hashedMarketValuesKeys.json +75 -0
  20. package/build/cjs/src/types/subsquid.js +91 -1
  21. package/build/cjs/src/types/swapStrategy.js +2 -0
  22. package/build/cjs/src/types/trade.js +1 -0
  23. package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +2 -0
  24. package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
  25. package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +10 -0
  26. package/build/cjs/src/utils/errors/transactionsErrors.js +7 -1
  27. package/build/cjs/src/utils/orderTransactions.js +0 -1
  28. package/build/cjs/src/utils/swap/botanixStaking.js +90 -0
  29. package/build/cjs/src/utils/swap/buildSwapStrategy.js +224 -0
  30. package/build/cjs/src/utils/swap/externalSwapPath.js +41 -0
  31. package/build/cjs/src/utils/swap/externalSwapQuoteByPath.js +27 -0
  32. package/build/cjs/src/utils/swap/swapValues.js +250 -11
  33. package/build/cjs/src/utils/trade/amounts.js +89 -41
  34. package/build/cjs/src/utils/trade/trade.js +2 -2
  35. package/build/cjs/src/utils/twap/uiFeeReceiver.js +9 -9
  36. package/build/esm/src/abis/ClaimHandler.json +439 -0
  37. package/build/esm/src/abis/index.js +4 -0
  38. package/build/esm/src/configs/__tests__/markets.spec.js +1 -1
  39. package/build/esm/src/configs/__tests__/tokens.spec.js +1 -1
  40. package/build/esm/src/configs/batch.js +13 -1
  41. package/build/esm/src/configs/contracts.js +2 -0
  42. package/build/esm/src/configs/dataStore.js +8 -0
  43. package/build/esm/src/configs/express.js +1 -0
  44. package/build/esm/src/configs/markets.js +35 -0
  45. package/build/esm/src/configs/oracleKeeper.js +17 -29
  46. package/build/esm/src/configs/tokens.js +55 -0
  47. package/build/esm/src/index.js +3 -3
  48. package/build/esm/src/modules/orders/helpers.js +9 -0
  49. package/build/esm/src/modules/orders/helpers.spec.js +1 -1
  50. package/build/esm/src/modules/orders/orders.js +3 -3
  51. package/build/esm/src/modules/utils/utils.js +3 -3
  52. package/build/esm/src/prebuilt/hashedKinkModelMarketRatesKeys.json +40 -0
  53. package/build/esm/src/prebuilt/hashedMarketConfigKeys.json +255 -0
  54. package/build/esm/src/prebuilt/hashedMarketValuesKeys.json +75 -0
  55. package/build/esm/src/types/subsquid.js +90 -0
  56. package/build/esm/src/types/swapStrategy.js +1 -0
  57. package/build/esm/src/types/trade.js +1 -0
  58. package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +2 -0
  59. package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
  60. package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +11 -1
  61. package/build/esm/src/utils/errors/transactionsErrors.js +7 -1
  62. package/build/esm/src/utils/orderTransactions.js +0 -1
  63. package/build/esm/src/utils/swap/botanixStaking.js +83 -0
  64. package/build/esm/src/utils/swap/buildSwapStrategy.js +219 -0
  65. package/build/esm/src/utils/swap/externalSwapPath.js +37 -0
  66. package/build/esm/src/utils/swap/externalSwapQuoteByPath.js +23 -0
  67. package/build/esm/src/utils/swap/swapValues.js +248 -9
  68. package/build/esm/src/utils/trade/amounts.js +89 -41
  69. package/build/esm/src/utils/trade/trade.js +2 -2
  70. package/build/esm/src/utils/twap/uiFeeReceiver.js +9 -9
  71. package/build/types/src/abis/index.d.ts +1 -1
  72. package/build/types/src/configs/batch.d.ts +2 -1
  73. package/build/types/src/configs/contracts.d.ts +2 -0
  74. package/build/types/src/configs/dataStore.d.ts +4 -0
  75. package/build/types/src/configs/express.d.ts +1 -0
  76. package/build/types/src/configs/oracleKeeper.d.ts +2 -3
  77. package/build/types/src/types/subsquid.d.ts +303 -0
  78. package/build/types/src/types/swapStrategy.d.ts +33 -0
  79. package/build/types/src/types/tokens.d.ts +5 -0
  80. package/build/types/src/types/trade.d.ts +18 -4
  81. package/build/types/src/utils/swap/botanixStaking.d.ts +11 -0
  82. package/build/types/src/utils/swap/buildSwapStrategy.d.ts +22 -0
  83. package/build/types/src/utils/swap/externalSwapPath.d.ts +13 -0
  84. package/build/types/src/utils/swap/externalSwapQuoteByPath.d.ts +6 -0
  85. package/build/types/src/utils/swap/swapValues.d.ts +10 -3
  86. package/build/types/src/utils/trade/amounts.d.ts +5 -2
  87. package/build/types/src/utils/twap/uiFeeReceiver.d.ts +7 -7
  88. package/package.json +1 -1
@@ -465,6 +465,69 @@ export var CumulativePoolValueOrderByInput;
465
465
  CumulativePoolValueOrderByInput["snapshotTimestamp_DESC_NULLS_FIRST"] = "snapshotTimestamp_DESC_NULLS_FIRST";
466
466
  CumulativePoolValueOrderByInput["snapshotTimestamp_DESC_NULLS_LAST"] = "snapshotTimestamp_DESC_NULLS_LAST";
467
467
  })(CumulativePoolValueOrderByInput || (CumulativePoolValueOrderByInput = {}));
468
+ export var DistributionOrderByInput;
469
+ (function (DistributionOrderByInput) {
470
+ DistributionOrderByInput["id_ASC"] = "id_ASC";
471
+ DistributionOrderByInput["id_ASC_NULLS_FIRST"] = "id_ASC_NULLS_FIRST";
472
+ DistributionOrderByInput["id_ASC_NULLS_LAST"] = "id_ASC_NULLS_LAST";
473
+ DistributionOrderByInput["id_DESC"] = "id_DESC";
474
+ DistributionOrderByInput["id_DESC_NULLS_FIRST"] = "id_DESC_NULLS_FIRST";
475
+ DistributionOrderByInput["id_DESC_NULLS_LAST"] = "id_DESC_NULLS_LAST";
476
+ DistributionOrderByInput["receiver_ASC"] = "receiver_ASC";
477
+ DistributionOrderByInput["receiver_ASC_NULLS_FIRST"] = "receiver_ASC_NULLS_FIRST";
478
+ DistributionOrderByInput["receiver_ASC_NULLS_LAST"] = "receiver_ASC_NULLS_LAST";
479
+ DistributionOrderByInput["receiver_DESC"] = "receiver_DESC";
480
+ DistributionOrderByInput["receiver_DESC_NULLS_FIRST"] = "receiver_DESC_NULLS_FIRST";
481
+ DistributionOrderByInput["receiver_DESC_NULLS_LAST"] = "receiver_DESC_NULLS_LAST";
482
+ DistributionOrderByInput["transaction_blockNumber_ASC"] = "transaction_blockNumber_ASC";
483
+ DistributionOrderByInput["transaction_blockNumber_ASC_NULLS_FIRST"] = "transaction_blockNumber_ASC_NULLS_FIRST";
484
+ DistributionOrderByInput["transaction_blockNumber_ASC_NULLS_LAST"] = "transaction_blockNumber_ASC_NULLS_LAST";
485
+ DistributionOrderByInput["transaction_blockNumber_DESC"] = "transaction_blockNumber_DESC";
486
+ DistributionOrderByInput["transaction_blockNumber_DESC_NULLS_FIRST"] = "transaction_blockNumber_DESC_NULLS_FIRST";
487
+ DistributionOrderByInput["transaction_blockNumber_DESC_NULLS_LAST"] = "transaction_blockNumber_DESC_NULLS_LAST";
488
+ DistributionOrderByInput["transaction_from_ASC"] = "transaction_from_ASC";
489
+ DistributionOrderByInput["transaction_from_ASC_NULLS_FIRST"] = "transaction_from_ASC_NULLS_FIRST";
490
+ DistributionOrderByInput["transaction_from_ASC_NULLS_LAST"] = "transaction_from_ASC_NULLS_LAST";
491
+ DistributionOrderByInput["transaction_from_DESC"] = "transaction_from_DESC";
492
+ DistributionOrderByInput["transaction_from_DESC_NULLS_FIRST"] = "transaction_from_DESC_NULLS_FIRST";
493
+ DistributionOrderByInput["transaction_from_DESC_NULLS_LAST"] = "transaction_from_DESC_NULLS_LAST";
494
+ DistributionOrderByInput["transaction_hash_ASC"] = "transaction_hash_ASC";
495
+ DistributionOrderByInput["transaction_hash_ASC_NULLS_FIRST"] = "transaction_hash_ASC_NULLS_FIRST";
496
+ DistributionOrderByInput["transaction_hash_ASC_NULLS_LAST"] = "transaction_hash_ASC_NULLS_LAST";
497
+ DistributionOrderByInput["transaction_hash_DESC"] = "transaction_hash_DESC";
498
+ DistributionOrderByInput["transaction_hash_DESC_NULLS_FIRST"] = "transaction_hash_DESC_NULLS_FIRST";
499
+ DistributionOrderByInput["transaction_hash_DESC_NULLS_LAST"] = "transaction_hash_DESC_NULLS_LAST";
500
+ DistributionOrderByInput["transaction_id_ASC"] = "transaction_id_ASC";
501
+ DistributionOrderByInput["transaction_id_ASC_NULLS_FIRST"] = "transaction_id_ASC_NULLS_FIRST";
502
+ DistributionOrderByInput["transaction_id_ASC_NULLS_LAST"] = "transaction_id_ASC_NULLS_LAST";
503
+ DistributionOrderByInput["transaction_id_DESC"] = "transaction_id_DESC";
504
+ DistributionOrderByInput["transaction_id_DESC_NULLS_FIRST"] = "transaction_id_DESC_NULLS_FIRST";
505
+ DistributionOrderByInput["transaction_id_DESC_NULLS_LAST"] = "transaction_id_DESC_NULLS_LAST";
506
+ DistributionOrderByInput["transaction_timestamp_ASC"] = "transaction_timestamp_ASC";
507
+ DistributionOrderByInput["transaction_timestamp_ASC_NULLS_FIRST"] = "transaction_timestamp_ASC_NULLS_FIRST";
508
+ DistributionOrderByInput["transaction_timestamp_ASC_NULLS_LAST"] = "transaction_timestamp_ASC_NULLS_LAST";
509
+ DistributionOrderByInput["transaction_timestamp_DESC"] = "transaction_timestamp_DESC";
510
+ DistributionOrderByInput["transaction_timestamp_DESC_NULLS_FIRST"] = "transaction_timestamp_DESC_NULLS_FIRST";
511
+ DistributionOrderByInput["transaction_timestamp_DESC_NULLS_LAST"] = "transaction_timestamp_DESC_NULLS_LAST";
512
+ DistributionOrderByInput["transaction_to_ASC"] = "transaction_to_ASC";
513
+ DistributionOrderByInput["transaction_to_ASC_NULLS_FIRST"] = "transaction_to_ASC_NULLS_FIRST";
514
+ DistributionOrderByInput["transaction_to_ASC_NULLS_LAST"] = "transaction_to_ASC_NULLS_LAST";
515
+ DistributionOrderByInput["transaction_to_DESC"] = "transaction_to_DESC";
516
+ DistributionOrderByInput["transaction_to_DESC_NULLS_FIRST"] = "transaction_to_DESC_NULLS_FIRST";
517
+ DistributionOrderByInput["transaction_to_DESC_NULLS_LAST"] = "transaction_to_DESC_NULLS_LAST";
518
+ DistributionOrderByInput["transaction_transactionIndex_ASC"] = "transaction_transactionIndex_ASC";
519
+ DistributionOrderByInput["transaction_transactionIndex_ASC_NULLS_FIRST"] = "transaction_transactionIndex_ASC_NULLS_FIRST";
520
+ DistributionOrderByInput["transaction_transactionIndex_ASC_NULLS_LAST"] = "transaction_transactionIndex_ASC_NULLS_LAST";
521
+ DistributionOrderByInput["transaction_transactionIndex_DESC"] = "transaction_transactionIndex_DESC";
522
+ DistributionOrderByInput["transaction_transactionIndex_DESC_NULLS_FIRST"] = "transaction_transactionIndex_DESC_NULLS_FIRST";
523
+ DistributionOrderByInput["transaction_transactionIndex_DESC_NULLS_LAST"] = "transaction_transactionIndex_DESC_NULLS_LAST";
524
+ DistributionOrderByInput["typeId_ASC"] = "typeId_ASC";
525
+ DistributionOrderByInput["typeId_ASC_NULLS_FIRST"] = "typeId_ASC_NULLS_FIRST";
526
+ DistributionOrderByInput["typeId_ASC_NULLS_LAST"] = "typeId_ASC_NULLS_LAST";
527
+ DistributionOrderByInput["typeId_DESC"] = "typeId_DESC";
528
+ DistributionOrderByInput["typeId_DESC_NULLS_FIRST"] = "typeId_DESC_NULLS_FIRST";
529
+ DistributionOrderByInput["typeId_DESC_NULLS_LAST"] = "typeId_DESC_NULLS_LAST";
530
+ })(DistributionOrderByInput || (DistributionOrderByInput = {}));
468
531
  export var EntityType;
469
532
  (function (EntityType) {
470
533
  EntityType["Glv"] = "Glv";
@@ -1977,6 +2040,33 @@ export var PriceType;
1977
2040
  PriceType["onchainFeed"] = "onchainFeed";
1978
2041
  PriceType["v2"] = "v2";
1979
2042
  })(PriceType || (PriceType = {}));
2043
+ export var ProcessorStatusOrderByInput;
2044
+ (function (ProcessorStatusOrderByInput) {
2045
+ ProcessorStatusOrderByInput["id_ASC"] = "id_ASC";
2046
+ ProcessorStatusOrderByInput["id_ASC_NULLS_FIRST"] = "id_ASC_NULLS_FIRST";
2047
+ ProcessorStatusOrderByInput["id_ASC_NULLS_LAST"] = "id_ASC_NULLS_LAST";
2048
+ ProcessorStatusOrderByInput["id_DESC"] = "id_DESC";
2049
+ ProcessorStatusOrderByInput["id_DESC_NULLS_FIRST"] = "id_DESC_NULLS_FIRST";
2050
+ ProcessorStatusOrderByInput["id_DESC_NULLS_LAST"] = "id_DESC_NULLS_LAST";
2051
+ ProcessorStatusOrderByInput["lastParsedBlockNumber_ASC"] = "lastParsedBlockNumber_ASC";
2052
+ ProcessorStatusOrderByInput["lastParsedBlockNumber_ASC_NULLS_FIRST"] = "lastParsedBlockNumber_ASC_NULLS_FIRST";
2053
+ ProcessorStatusOrderByInput["lastParsedBlockNumber_ASC_NULLS_LAST"] = "lastParsedBlockNumber_ASC_NULLS_LAST";
2054
+ ProcessorStatusOrderByInput["lastParsedBlockNumber_DESC"] = "lastParsedBlockNumber_DESC";
2055
+ ProcessorStatusOrderByInput["lastParsedBlockNumber_DESC_NULLS_FIRST"] = "lastParsedBlockNumber_DESC_NULLS_FIRST";
2056
+ ProcessorStatusOrderByInput["lastParsedBlockNumber_DESC_NULLS_LAST"] = "lastParsedBlockNumber_DESC_NULLS_LAST";
2057
+ ProcessorStatusOrderByInput["lastParsedBlockTimestamp_ASC"] = "lastParsedBlockTimestamp_ASC";
2058
+ ProcessorStatusOrderByInput["lastParsedBlockTimestamp_ASC_NULLS_FIRST"] = "lastParsedBlockTimestamp_ASC_NULLS_FIRST";
2059
+ ProcessorStatusOrderByInput["lastParsedBlockTimestamp_ASC_NULLS_LAST"] = "lastParsedBlockTimestamp_ASC_NULLS_LAST";
2060
+ ProcessorStatusOrderByInput["lastParsedBlockTimestamp_DESC"] = "lastParsedBlockTimestamp_DESC";
2061
+ ProcessorStatusOrderByInput["lastParsedBlockTimestamp_DESC_NULLS_FIRST"] = "lastParsedBlockTimestamp_DESC_NULLS_FIRST";
2062
+ ProcessorStatusOrderByInput["lastParsedBlockTimestamp_DESC_NULLS_LAST"] = "lastParsedBlockTimestamp_DESC_NULLS_LAST";
2063
+ ProcessorStatusOrderByInput["lastProcessorCallTimestamp_ASC"] = "lastProcessorCallTimestamp_ASC";
2064
+ ProcessorStatusOrderByInput["lastProcessorCallTimestamp_ASC_NULLS_FIRST"] = "lastProcessorCallTimestamp_ASC_NULLS_FIRST";
2065
+ ProcessorStatusOrderByInput["lastProcessorCallTimestamp_ASC_NULLS_LAST"] = "lastProcessorCallTimestamp_ASC_NULLS_LAST";
2066
+ ProcessorStatusOrderByInput["lastProcessorCallTimestamp_DESC"] = "lastProcessorCallTimestamp_DESC";
2067
+ ProcessorStatusOrderByInput["lastProcessorCallTimestamp_DESC_NULLS_FIRST"] = "lastProcessorCallTimestamp_DESC_NULLS_FIRST";
2068
+ ProcessorStatusOrderByInput["lastProcessorCallTimestamp_DESC_NULLS_LAST"] = "lastProcessorCallTimestamp_DESC_NULLS_LAST";
2069
+ })(ProcessorStatusOrderByInput || (ProcessorStatusOrderByInput = {}));
1980
2070
  export var SwapInfoOrderByInput;
1981
2071
  (function (SwapInfoOrderByInput) {
1982
2072
  SwapInfoOrderByInput["amountInAfterFees_ASC"] = "amountInAfterFees_ASC";
@@ -0,0 +1 @@
1
+ export {};
@@ -20,4 +20,5 @@ export var TriggerThresholdType;
20
20
  export var ExternalSwapAggregator;
21
21
  (function (ExternalSwapAggregator) {
22
22
  ExternalSwapAggregator["OpenOcean"] = "openOcean";
23
+ ExternalSwapAggregator["BotanixStaking"] = "botanixStaking";
23
24
  })(ExternalSwapAggregator || (ExternalSwapAggregator = {}));
@@ -162,6 +162,7 @@ describe("Exchange Router Multicall", () => {
162
162
  it("Token transfers with multiple orders, external swaps, update and cancel", () => {
163
163
  const params1 = {
164
164
  ...commonParams,
165
+ swapPath: [],
165
166
  payTokenAddress: NATIVE_TOKEN_ADDRESS,
166
167
  payTokenAmount: parseValue("1", WETH.decimals), // 1 ETH
167
168
  externalSwapQuote: mockExternalSwap({
@@ -175,6 +176,7 @@ describe("Exchange Router Multicall", () => {
175
176
  };
176
177
  const params2 = {
177
178
  ...commonParams,
179
+ swapPath: [],
178
180
  payTokenAddress: USDC.address,
179
181
  payTokenAmount: parseValue("1000", USDC.decimals), // 1000 USDC
180
182
  externalSwapQuote: mockExternalSwap({
@@ -294,6 +294,7 @@ describe("Increase Order Payloads", () => {
294
294
  it("Market Increase with External Swap, Pay With Native Token", () => {
295
295
  const params = {
296
296
  ...commonParams,
297
+ swapPath: [],
297
298
  payTokenAddress: NATIVE_TOKEN_ADDRESS,
298
299
  payTokenAmount: parseValue("1", WETH.decimals), // 1 ETH
299
300
  collateralTokenAddress: USDC.address,
@@ -373,6 +374,7 @@ describe("Increase Order Payloads", () => {
373
374
  it("Market Increase with External Swap, Pay With ERC20", () => {
374
375
  const params = {
375
376
  ...commonParams,
377
+ swapPath: [],
376
378
  payTokenAddress: USDC.address,
377
379
  payTokenAmount: parseValue("1000", USDC.decimals), // 1000 USDC
378
380
  collateralTokenAddress: WETH.address,
@@ -1,6 +1,6 @@
1
1
  import { isAddress } from "viem";
2
2
  import { describe, it, expect } from "vitest";
3
- import { createTwapUiFeeReceiver, decodeTwapUiFeeReceiver } from "../twap/uiFeeReceiver";
3
+ import { createTwapUiFeeReceiver, decodeTwapUiFeeReceiver, setUiFeeReceiverIsExpress } from "../twap/uiFeeReceiver";
4
4
  describe("uiFeeReceiver", () => {
5
5
  describe("decodeTwapUiFeeReceiver", () => {
6
6
  it("should return undefined if the address is not a valid address", () => {
@@ -50,3 +50,13 @@ describe("uiFeeReceiver", () => {
50
50
  });
51
51
  });
52
52
  });
53
+ describe("setUiFeeReceiverIsExpress", () => {
54
+ it("should correctly set isExpress for simple uiFeeReceiver", () => {
55
+ expect(setUiFeeReceiverIsExpress("0xff00000000000000000000000000000000000001", true)).toEqual("0xff00000000000000000000000000000100000001");
56
+ expect(setUiFeeReceiverIsExpress("0xff00000000000000000000000000000000000001", false)).toEqual("0xff00000000000000000000000000000000000001");
57
+ });
58
+ it("should correctly set isExpress for twap uiFeeReceiver", () => {
59
+ expect(setUiFeeReceiverIsExpress("0xff0000000000000000000000000000000a123401", true)).toEqual("0xff0000000000000000000000000000010a123401");
60
+ expect(setUiFeeReceiverIsExpress("0xff0000000000000000000000000000000a123401", false)).toEqual("0xff0000000000000000000000000000000a123401");
61
+ });
62
+ });
@@ -17,6 +17,8 @@ const TX_ERROR_PATTERNS = {
17
17
  { msg: "User denied transaction signature" },
18
18
  { msg: "User rejected" },
19
19
  { msg: "User cancelled" },
20
+ { msg: "Cancelled" },
21
+ { msg: "Cancelled by user" },
20
22
  { msg: "user rejected action" },
21
23
  { msg: "ethers-user-denied" },
22
24
  { msg: "Action cancelled by user" },
@@ -26,7 +28,11 @@ const TX_ERROR_PATTERNS = {
26
28
  { msg: "Router: mark price lower than limit" },
27
29
  { msg: "Router: mark price higher than limit" },
28
30
  ],
29
- [TxErrorType.NetworkChanged]: [{ msg: "network changed" }, { msg: "Invalid network" }],
31
+ [TxErrorType.NetworkChanged]: [
32
+ { msg: "network changed" },
33
+ { msg: "Invalid network" },
34
+ { msg: "chainId should be same" },
35
+ ],
30
36
  [TxErrorType.Expired]: [{ msg: "Request expired" }],
31
37
  [TxErrorType.RpcError]: [
32
38
  // @see https://eips.ethereum.org/EIPS/eip-1474#error-codes
@@ -364,7 +364,6 @@ export function buildTokenTransfersParamsForIncreaseOrSwap({ chainId, receiver,
364
364
  * */
365
365
  initialCollateralTokenAddress = convertTokenAddress(chainId, externalSwapQuote.outTokenAddress, "wrapped");
366
366
  initialCollateralDeltaAmount = 0n;
367
- swapPath = [];
368
367
  externalCalls = getExternalCallsPayload({
369
368
  chainId,
370
369
  account: receiver,
@@ -0,0 +1,83 @@
1
+ import { encodeFunctionData } from "viem";
2
+ import StBTCABI from "../../abis/StBTC.json";
3
+ import { BOTANIX } from "../../configs/chains";
4
+ import { getContract } from "../../configs/contracts";
5
+ import { ExternalSwapAggregator } from "../../types/trade";
6
+ import { bigMath } from "../bigmath";
7
+ import { BASIS_POINTS_DIVISOR_BIGINT, getBasisPoints } from "../numbers";
8
+ import { AVAILABLE_BOTANIX_DEPOSIT_PAIRS, AVAILABLE_BOTANIX_WITHDRAW_PAIRS } from "./externalSwapPath";
9
+ import { convertToUsd, getMidPrice, getTokenData } from "../tokens";
10
+ const COEF_REDUCER = getBasisPoints(1n, 10000n);
11
+ export const getBotanixStakingExternalSwapQuote = ({ tokenInAddress, tokenOutAddress, amountIn, gasPrice, receiverAddress, tokensData, assetsPerShare, }) => {
12
+ const inTokenData = getTokenData(tokensData, tokenInAddress);
13
+ const outTokenData = getTokenData(tokensData, tokenOutAddress);
14
+ const assetsPerShareRate = getBasisPoints(assetsPerShare, 10n ** 18n) - COEF_REDUCER;
15
+ const sharesPerAssetRate = getBasisPoints(10n ** 18n, assetsPerShare) - COEF_REDUCER;
16
+ if (!inTokenData || !outTokenData) {
17
+ return undefined;
18
+ }
19
+ if (AVAILABLE_BOTANIX_DEPOSIT_PAIRS.some((pair) => pair.from === tokenInAddress && pair.to === tokenOutAddress)) {
20
+ const priceIn = getMidPrice(inTokenData.prices);
21
+ const priceOut = bigMath.mulDiv(priceIn, sharesPerAssetRate, BASIS_POINTS_DIVISOR_BIGINT);
22
+ const usdIn = convertToUsd(amountIn, inTokenData.decimals, priceIn);
23
+ const amountOut = amountIn > 0n ? bigMath.mulDiv(amountIn, sharesPerAssetRate, BASIS_POINTS_DIVISOR_BIGINT) - gasPrice : 0n;
24
+ const usdOut = amountOut > 0n ? convertToUsd(amountOut, outTokenData.decimals, priceOut) : 0n;
25
+ return {
26
+ aggregator: ExternalSwapAggregator.BotanixStaking,
27
+ inTokenAddress: tokenInAddress,
28
+ outTokenAddress: tokenOutAddress,
29
+ receiver: receiverAddress,
30
+ amountIn,
31
+ amountOut,
32
+ usdIn: usdIn,
33
+ usdOut: usdOut,
34
+ priceIn,
35
+ priceOut,
36
+ feesUsd: gasPrice,
37
+ needSpenderApproval: true,
38
+ txnData: {
39
+ to: getContract(BOTANIX, "StBTC"),
40
+ data: encodeFunctionData({
41
+ abi: StBTCABI.abi,
42
+ functionName: "deposit",
43
+ args: [amountIn, receiverAddress],
44
+ }),
45
+ value: 0n,
46
+ estimatedGas: gasPrice,
47
+ estimatedExecutionFee: gasPrice,
48
+ },
49
+ };
50
+ }
51
+ if (AVAILABLE_BOTANIX_WITHDRAW_PAIRS.some((pair) => pair.from === tokenInAddress && pair.to === tokenOutAddress)) {
52
+ const priceIn = getMidPrice(inTokenData.prices);
53
+ const priceOut = bigMath.mulDiv(priceIn, assetsPerShareRate, BASIS_POINTS_DIVISOR_BIGINT);
54
+ const usdIn = convertToUsd(amountIn, inTokenData.decimals, priceIn);
55
+ const amountOut = amountIn > 0n ? bigMath.mulDiv(amountIn, assetsPerShareRate, BASIS_POINTS_DIVISOR_BIGINT) - gasPrice : 0n;
56
+ const usdOut = amountOut > 0n ? convertToUsd(amountOut, outTokenData.decimals, priceOut) : 0n;
57
+ return {
58
+ aggregator: ExternalSwapAggregator.BotanixStaking,
59
+ inTokenAddress: tokenInAddress,
60
+ outTokenAddress: tokenOutAddress,
61
+ receiver: receiverAddress,
62
+ amountIn,
63
+ amountOut,
64
+ usdIn: usdIn,
65
+ usdOut: usdOut,
66
+ priceIn,
67
+ priceOut,
68
+ feesUsd: gasPrice,
69
+ needSpenderApproval: true,
70
+ txnData: {
71
+ to: getContract(BOTANIX, "StBTC"),
72
+ data: encodeFunctionData({
73
+ abi: StBTCABI.abi,
74
+ functionName: "withdraw",
75
+ args: [amountIn, receiverAddress, getContract(BOTANIX, "ExternalHandler")],
76
+ }),
77
+ value: 0n,
78
+ estimatedGas: gasPrice,
79
+ estimatedExecutionFee: gasPrice,
80
+ },
81
+ };
82
+ }
83
+ };
@@ -0,0 +1,219 @@
1
+ import { bigMath } from "../bigmath";
2
+ import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens, getIsStake, getIsUnstake, getMidPrice, } from "../tokens";
3
+ import { getAvailableExternalSwapPaths } from "./externalSwapPath";
4
+ import { getExternalSwapQuoteByPath } from "./externalSwapQuoteByPath";
5
+ import { createFindSwapPath } from "./swapPath";
6
+ /*
7
+ Order/Priority of getting swap strategy:
8
+ 1. Check if it needs a swap and return noSwap if tokens are equivalent, stake or unstake [noSwap]
9
+ 2. Check if there is a swap path stats for the internal swap quote and return internalSwap if there is [internalSwap]
10
+ 3. Check if there is a combined swap strategy and return combinedSwap if there is [combinedSwap]
11
+ 4. Return defaultSwapStrategy (noSwap) if there is no other swap strategy [noSwap]
12
+ */
13
+ export function buildSwapStrategy({ amountIn, tokenIn, tokenOut, marketsInfoData, chainId, swapOptimizationOrder, externalSwapQuoteParams, }) {
14
+ const priceIn = tokenIn.prices.minPrice;
15
+ const usdIn = convertToUsd(amountIn, tokenIn.decimals, priceIn);
16
+ if (amountIn < 0n) {
17
+ amountIn = 0n;
18
+ }
19
+ const defaultSwapStrategy = {
20
+ type: "noSwap",
21
+ externalSwapQuote: undefined,
22
+ swapPathStats: undefined,
23
+ amountIn,
24
+ amountOut: convertToTokenAmount(usdIn, tokenOut.decimals, tokenOut.prices.maxPrice),
25
+ usdIn,
26
+ usdOut: usdIn,
27
+ priceIn,
28
+ priceOut: tokenOut.prices.maxPrice,
29
+ feesUsd: 0n,
30
+ };
31
+ if (getIsEquivalentTokens(tokenIn, tokenOut) || getIsStake(tokenIn, tokenOut) || getIsUnstake(tokenIn, tokenOut)) {
32
+ return defaultSwapStrategy;
33
+ }
34
+ const findSwapPath = createFindSwapPath({
35
+ chainId,
36
+ fromTokenAddress: tokenIn.address,
37
+ toTokenAddress: tokenOut.address,
38
+ marketsInfoData,
39
+ isExpressFeeSwap: false,
40
+ });
41
+ const swapPathStats = findSwapPath(usdIn, { order: swapOptimizationOrder });
42
+ if (swapPathStats) {
43
+ return {
44
+ type: "internalSwap",
45
+ swapPathStats,
46
+ externalSwapQuote: undefined,
47
+ amountIn,
48
+ amountOut: swapPathStats.amountOut,
49
+ usdIn: usdIn,
50
+ usdOut: swapPathStats.usdOut,
51
+ priceIn: priceIn,
52
+ priceOut: tokenOut.prices.maxPrice,
53
+ feesUsd: usdIn - swapPathStats.usdOut,
54
+ };
55
+ }
56
+ const availableExternalSwapPaths = getAvailableExternalSwapPaths({ chainId, fromTokenAddress: tokenIn.address });
57
+ const suitableSwapPath = availableExternalSwapPaths.find((path) => {
58
+ const findSwapPath = createFindSwapPath({
59
+ chainId,
60
+ fromTokenAddress: path.outTokenAddress,
61
+ toTokenAddress: tokenOut.address,
62
+ marketsInfoData,
63
+ isExpressFeeSwap: false,
64
+ });
65
+ const swapPathStats = findSwapPath(usdIn);
66
+ return Boolean(swapPathStats);
67
+ });
68
+ if (suitableSwapPath && suitableSwapPath.outTokenAddress !== tokenOut.address) {
69
+ const externalSwapQuoteForCombinedSwap = getExternalSwapQuoteByPath({
70
+ amountIn,
71
+ externalSwapPath: suitableSwapPath,
72
+ externalSwapQuoteParams,
73
+ });
74
+ const findSwapPathForSuitableSwapPath = createFindSwapPath({
75
+ chainId,
76
+ fromTokenAddress: suitableSwapPath.outTokenAddress,
77
+ toTokenAddress: tokenOut.address,
78
+ marketsInfoData,
79
+ isExpressFeeSwap: false,
80
+ });
81
+ const swapPathStatsForCombinedSwap = externalSwapQuoteForCombinedSwap
82
+ ? findSwapPathForSuitableSwapPath(externalSwapQuoteForCombinedSwap.usdOut)
83
+ : undefined;
84
+ return externalSwapQuoteForCombinedSwap && swapPathStatsForCombinedSwap
85
+ ? {
86
+ type: "combinedSwap",
87
+ externalSwapQuote: externalSwapQuoteForCombinedSwap,
88
+ swapPathStats: swapPathStatsForCombinedSwap,
89
+ amountIn,
90
+ amountOut: swapPathStatsForCombinedSwap.amountOut,
91
+ usdIn: externalSwapQuoteForCombinedSwap.usdIn,
92
+ usdOut: swapPathStatsForCombinedSwap.usdOut,
93
+ priceIn: externalSwapQuoteForCombinedSwap.priceIn,
94
+ priceOut: tokenOut.prices.maxPrice,
95
+ feesUsd: externalSwapQuoteForCombinedSwap.usdIn - swapPathStatsForCombinedSwap.usdOut,
96
+ }
97
+ : defaultSwapStrategy;
98
+ }
99
+ return defaultSwapStrategy;
100
+ }
101
+ // Used for getting swap amounts by to value
102
+ export function buildReverseSwapStrategy({ amountOut, tokenIn, tokenOut, marketsInfoData, chainId, externalSwapQuoteParams, swapOptimizationOrder, }) {
103
+ const priceIn = getMidPrice(tokenIn.prices);
104
+ const priceOut = getMidPrice(tokenOut.prices);
105
+ const preferredUsdOut = convertToUsd(amountOut, tokenOut.decimals, getMidPrice(tokenOut.prices));
106
+ const approximateAmountIn = convertToTokenAmount(preferredUsdOut, tokenIn.decimals, getMidPrice(tokenIn.prices));
107
+ const approximateUsdIn = preferredUsdOut;
108
+ const defaultSwapStrategy = {
109
+ type: "noSwap",
110
+ externalSwapQuote: undefined,
111
+ swapPathStats: undefined,
112
+ amountIn: approximateAmountIn,
113
+ amountOut: amountOut,
114
+ usdIn: approximateUsdIn,
115
+ usdOut: preferredUsdOut,
116
+ priceIn,
117
+ priceOut,
118
+ feesUsd: 0n,
119
+ };
120
+ if (getIsEquivalentTokens(tokenIn, tokenOut) || getIsStake(tokenIn, tokenOut) || getIsUnstake(tokenIn, tokenOut)) {
121
+ return defaultSwapStrategy;
122
+ }
123
+ const findSwapPath = createFindSwapPath({
124
+ chainId,
125
+ fromTokenAddress: tokenIn.address,
126
+ toTokenAddress: tokenOut.address,
127
+ marketsInfoData,
128
+ isExpressFeeSwap: false,
129
+ });
130
+ const approximateSwapPathStats = findSwapPath(approximateUsdIn, { order: swapOptimizationOrder });
131
+ if (approximateSwapPathStats) {
132
+ // Increase or decrease usdIn the same way preferred usdOut is different from swapStrategy.usdOut
133
+ // preferred_in / approximate_in = preferred_out / approximate_out
134
+ // preferred_in = approximate_in * preferred_out / approximate_out
135
+ const adjustedUsdIn = approximateSwapPathStats.usdOut > 0
136
+ ? bigMath.mulDiv(approximateUsdIn, preferredUsdOut, approximateSwapPathStats.usdOut)
137
+ : 0n;
138
+ const adjustedAmountIn = convertToTokenAmount(adjustedUsdIn, tokenIn.decimals, getMidPrice(tokenIn.prices));
139
+ const adjustedSwapPathStats = findSwapPath(adjustedUsdIn, { order: swapOptimizationOrder });
140
+ if (adjustedSwapPathStats) {
141
+ return {
142
+ type: "internalSwap",
143
+ swapPathStats: adjustedSwapPathStats,
144
+ externalSwapQuote: undefined,
145
+ amountIn: adjustedAmountIn,
146
+ amountOut: adjustedSwapPathStats.amountOut,
147
+ usdIn: adjustedUsdIn,
148
+ usdOut: adjustedSwapPathStats.usdOut,
149
+ priceIn: priceIn,
150
+ priceOut: priceOut,
151
+ feesUsd: adjustedUsdIn - adjustedSwapPathStats.usdOut,
152
+ };
153
+ }
154
+ }
155
+ const availableExternalSwapPaths = getAvailableExternalSwapPaths({ chainId, fromTokenAddress: tokenIn.address });
156
+ const suitableSwapPath = availableExternalSwapPaths.find((path) => {
157
+ if (path.outTokenAddress !== tokenOut.address)
158
+ return false;
159
+ const findSwapPath = createFindSwapPath({
160
+ chainId,
161
+ fromTokenAddress: tokenIn.address,
162
+ toTokenAddress: path.inTokenAddress,
163
+ marketsInfoData,
164
+ isExpressFeeSwap: false,
165
+ });
166
+ const swapPathStats = findSwapPath(approximateUsdIn);
167
+ return Boolean(swapPathStats);
168
+ });
169
+ if (suitableSwapPath) {
170
+ const approximateExternalSwapQuoteForCombinedSwap = getExternalSwapQuoteByPath({
171
+ amountIn: approximateAmountIn,
172
+ externalSwapPath: suitableSwapPath,
173
+ externalSwapQuoteParams,
174
+ });
175
+ if (!approximateExternalSwapQuoteForCombinedSwap) {
176
+ return defaultSwapStrategy;
177
+ }
178
+ const findSwapPathForSuitableSwapPath = createFindSwapPath({
179
+ chainId,
180
+ fromTokenAddress: tokenIn.address,
181
+ toTokenAddress: suitableSwapPath.inTokenAddress,
182
+ marketsInfoData,
183
+ isExpressFeeSwap: false,
184
+ });
185
+ const approximateSwapPathStatsForCombinedSwap = findSwapPathForSuitableSwapPath(approximateExternalSwapQuoteForCombinedSwap.usdOut);
186
+ if (!approximateSwapPathStatsForCombinedSwap) {
187
+ return defaultSwapStrategy;
188
+ }
189
+ const adjustedUsdIn = approximateSwapPathStatsForCombinedSwap.usdOut > 0
190
+ ? bigMath.mulDiv(approximateUsdIn, preferredUsdOut, approximateSwapPathStatsForCombinedSwap.usdOut)
191
+ : 0n;
192
+ const adjustedAmountIn = convertToTokenAmount(adjustedUsdIn, tokenIn.decimals, getMidPrice(tokenIn.prices));
193
+ const adjustedExternalSwapQuoteForCombinedSwap = getExternalSwapQuoteByPath({
194
+ amountIn: adjustedAmountIn,
195
+ externalSwapPath: suitableSwapPath,
196
+ externalSwapQuoteParams,
197
+ });
198
+ if (!adjustedExternalSwapQuoteForCombinedSwap) {
199
+ return defaultSwapStrategy;
200
+ }
201
+ const adjustedSwapPathStatsForCombinedSwap = findSwapPathForSuitableSwapPath(adjustedExternalSwapQuoteForCombinedSwap.usdOut);
202
+ if (!adjustedSwapPathStatsForCombinedSwap) {
203
+ return defaultSwapStrategy;
204
+ }
205
+ return {
206
+ type: "combinedSwap",
207
+ externalSwapQuote: adjustedExternalSwapQuoteForCombinedSwap,
208
+ swapPathStats: adjustedSwapPathStatsForCombinedSwap,
209
+ amountIn: adjustedAmountIn,
210
+ amountOut: adjustedSwapPathStatsForCombinedSwap.amountOut,
211
+ usdIn: adjustedExternalSwapQuoteForCombinedSwap.usdIn,
212
+ usdOut: adjustedSwapPathStatsForCombinedSwap.usdOut,
213
+ priceIn: adjustedExternalSwapQuoteForCombinedSwap.priceIn,
214
+ priceOut: priceOut,
215
+ feesUsd: adjustedExternalSwapQuoteForCombinedSwap.usdIn - adjustedSwapPathStatsForCombinedSwap.usdOut,
216
+ };
217
+ }
218
+ return defaultSwapStrategy;
219
+ }
@@ -0,0 +1,37 @@
1
+ import { BOTANIX } from "../../configs/chains";
2
+ import { NATIVE_TOKEN_ADDRESS, getTokenBySymbol } from "../../configs/tokens";
3
+ import { ExternalSwapAggregator } from "../../types/trade";
4
+ const BBTC_ADDRESS = NATIVE_TOKEN_ADDRESS;
5
+ const PBTC_ADDRESS = getTokenBySymbol(BOTANIX, "PBTC").address;
6
+ const STBTC_ADDRESS = getTokenBySymbol(BOTANIX, "STBTC").address;
7
+ export const AVAILABLE_BOTANIX_DEPOSIT_PAIRS = [
8
+ {
9
+ from: BBTC_ADDRESS,
10
+ to: STBTC_ADDRESS,
11
+ },
12
+ {
13
+ from: PBTC_ADDRESS,
14
+ to: STBTC_ADDRESS,
15
+ },
16
+ ];
17
+ export const AVAILABLE_BOTANIX_WITHDRAW_PAIRS = [
18
+ {
19
+ from: STBTC_ADDRESS,
20
+ to: PBTC_ADDRESS,
21
+ },
22
+ ];
23
+ const getBotanixStakingExternalSwapPaths = ({ fromTokenAddress }) => {
24
+ return [...AVAILABLE_BOTANIX_DEPOSIT_PAIRS, ...AVAILABLE_BOTANIX_WITHDRAW_PAIRS]
25
+ .filter((pair) => pair.from === fromTokenAddress)
26
+ .map((pair) => ({
27
+ aggregator: ExternalSwapAggregator.BotanixStaking,
28
+ inTokenAddress: pair.from,
29
+ outTokenAddress: pair.to,
30
+ }));
31
+ };
32
+ export const getAvailableExternalSwapPaths = ({ chainId, fromTokenAddress, }) => {
33
+ if (chainId === BOTANIX) {
34
+ return getBotanixStakingExternalSwapPaths({ fromTokenAddress });
35
+ }
36
+ return [];
37
+ };
@@ -0,0 +1,23 @@
1
+ import { ExternalSwapAggregator } from "../../types/trade";
2
+ import { getBotanixStakingExternalSwapQuote } from "./botanixStaking";
3
+ export const getExternalSwapQuoteByPath = ({ amountIn, externalSwapPath, externalSwapQuoteParams, }) => {
4
+ if (amountIn === undefined ||
5
+ externalSwapQuoteParams.gasPrice === undefined ||
6
+ externalSwapQuoteParams.tokensData === undefined ||
7
+ externalSwapQuoteParams.botanixStakingAssetsPerShare === undefined ||
8
+ externalSwapQuoteParams.receiverAddress === undefined) {
9
+ return undefined;
10
+ }
11
+ if (externalSwapPath.aggregator === ExternalSwapAggregator.BotanixStaking) {
12
+ return getBotanixStakingExternalSwapQuote({
13
+ tokenInAddress: externalSwapPath.inTokenAddress,
14
+ tokenOutAddress: externalSwapPath.outTokenAddress,
15
+ amountIn,
16
+ gasPrice: externalSwapQuoteParams.gasPrice,
17
+ receiverAddress: externalSwapQuoteParams.receiverAddress,
18
+ tokensData: externalSwapQuoteParams.tokensData,
19
+ assetsPerShare: externalSwapQuoteParams.botanixStakingAssetsPerShare,
20
+ });
21
+ }
22
+ return undefined;
23
+ };