defi-dash-sdk 0.1.3 → 0.1.4

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 (181) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +199 -97
  3. package/dist/__tests__/deleverageEstimate.unit.test.d.ts +10 -0
  4. package/dist/__tests__/deleverageEstimate.unit.test.d.ts.map +1 -0
  5. package/dist/__tests__/deleverageEstimate.unit.test.js +116 -0
  6. package/dist/__tests__/deleverageEstimate.unit.test.js.map +1 -0
  7. package/dist/__tests__/findBestLeverageRoute.test.d.ts +12 -0
  8. package/dist/__tests__/findBestLeverageRoute.test.d.ts.map +1 -0
  9. package/dist/__tests__/findBestLeverageRoute.test.js +177 -0
  10. package/dist/__tests__/findBestLeverageRoute.test.js.map +1 -0
  11. package/dist/__tests__/getAssetApy.test.d.ts +2 -0
  12. package/dist/__tests__/getAssetApy.test.d.ts.map +1 -0
  13. package/dist/__tests__/getAssetApy.test.js +133 -0
  14. package/dist/__tests__/getAssetApy.test.js.map +1 -0
  15. package/dist/__tests__/getAssetRiskParams.test.d.ts +11 -0
  16. package/dist/__tests__/getAssetRiskParams.test.d.ts.map +1 -0
  17. package/dist/__tests__/getAssetRiskParams.test.js +183 -0
  18. package/dist/__tests__/getAssetRiskParams.test.js.map +1 -0
  19. package/dist/__tests__/internal.getAssetApy.test.d.ts +2 -0
  20. package/dist/__tests__/internal.getAssetApy.test.d.ts.map +1 -0
  21. package/dist/__tests__/internal.getAssetApy.test.js +140 -0
  22. package/dist/__tests__/internal.getAssetApy.test.js.map +1 -0
  23. package/dist/__tests__/internal.getAssetRiskParams.test.d.ts +22 -0
  24. package/dist/__tests__/internal.getAssetRiskParams.test.d.ts.map +1 -0
  25. package/dist/__tests__/internal.getAssetRiskParams.test.js +194 -0
  26. package/dist/__tests__/internal.getAssetRiskParams.test.js.map +1 -0
  27. package/dist/__tests__/internal.getPosition.test.d.ts +22 -0
  28. package/dist/__tests__/internal.getPosition.test.d.ts.map +1 -0
  29. package/dist/__tests__/internal.getPosition.test.js +153 -0
  30. package/dist/__tests__/internal.getPosition.test.js.map +1 -0
  31. package/dist/__tests__/leveragePreview.unit.test.d.ts +10 -0
  32. package/dist/__tests__/leveragePreview.unit.test.d.ts.map +1 -0
  33. package/dist/__tests__/leveragePreview.unit.test.js +168 -0
  34. package/dist/__tests__/leveragePreview.unit.test.js.map +1 -0
  35. package/dist/__tests__/leverageRoute.unit.test.d.ts +10 -0
  36. package/dist/__tests__/leverageRoute.unit.test.d.ts.map +1 -0
  37. package/dist/__tests__/leverageRoute.unit.test.js +159 -0
  38. package/dist/__tests__/leverageRoute.unit.test.js.map +1 -0
  39. package/dist/__tests__/previewLeverage.test.d.ts +13 -0
  40. package/dist/__tests__/previewLeverage.test.d.ts.map +1 -0
  41. package/dist/__tests__/previewLeverage.test.js +217 -0
  42. package/dist/__tests__/previewLeverage.test.js.map +1 -0
  43. package/dist/__tests__/sdk.bestRoute.test.d.ts +22 -0
  44. package/dist/__tests__/sdk.bestRoute.test.d.ts.map +1 -0
  45. package/dist/__tests__/sdk.bestRoute.test.js +186 -0
  46. package/dist/__tests__/sdk.bestRoute.test.js.map +1 -0
  47. package/dist/__tests__/sdk.bestRoute.unit.test.d.ts +16 -0
  48. package/dist/__tests__/sdk.bestRoute.unit.test.d.ts.map +1 -0
  49. package/dist/__tests__/sdk.bestRoute.unit.test.js +165 -0
  50. package/dist/__tests__/sdk.bestRoute.unit.test.js.map +1 -0
  51. package/dist/__tests__/sdk.deleverage.test.d.ts +21 -0
  52. package/dist/__tests__/sdk.deleverage.test.d.ts.map +1 -0
  53. package/dist/__tests__/sdk.deleverage.test.js +130 -0
  54. package/dist/__tests__/sdk.deleverage.test.js.map +1 -0
  55. package/dist/__tests__/sdk.deleverage.unit.test.d.ts +21 -0
  56. package/dist/__tests__/sdk.deleverage.unit.test.d.ts.map +1 -0
  57. package/dist/__tests__/sdk.deleverage.unit.test.js +141 -0
  58. package/dist/__tests__/sdk.deleverage.unit.test.js.map +1 -0
  59. package/dist/__tests__/sdk.leverage.test.d.ts +19 -0
  60. package/dist/__tests__/sdk.leverage.test.d.ts.map +1 -0
  61. package/dist/__tests__/sdk.leverage.test.js +188 -0
  62. package/dist/__tests__/sdk.leverage.test.js.map +1 -0
  63. package/dist/__tests__/sdk.portfolio.test.d.ts +17 -0
  64. package/dist/__tests__/sdk.portfolio.test.d.ts.map +1 -0
  65. package/dist/__tests__/sdk.portfolio.test.js +162 -0
  66. package/dist/__tests__/sdk.portfolio.test.js.map +1 -0
  67. package/dist/__tests__/sdk.position.test.d.ts +16 -0
  68. package/dist/__tests__/sdk.position.test.d.ts.map +1 -0
  69. package/dist/__tests__/sdk.position.test.js +193 -0
  70. package/dist/__tests__/sdk.position.test.js.map +1 -0
  71. package/dist/__tests__/sdk.preview.test.d.ts +23 -0
  72. package/dist/__tests__/sdk.preview.test.d.ts.map +1 -0
  73. package/dist/__tests__/sdk.preview.test.js +226 -0
  74. package/dist/__tests__/sdk.preview.test.js.map +1 -0
  75. package/dist/__tests__/sdk.preview.unit.test.d.ts +18 -0
  76. package/dist/__tests__/sdk.preview.unit.test.d.ts.map +1 -0
  77. package/dist/__tests__/sdk.preview.unit.test.js +175 -0
  78. package/dist/__tests__/sdk.preview.unit.test.js.map +1 -0
  79. package/dist/__tests__/utils.normalizeCoinType.test.d.ts +7 -0
  80. package/dist/__tests__/utils.normalizeCoinType.test.d.ts.map +1 -0
  81. package/dist/__tests__/utils.normalizeCoinType.test.js +42 -0
  82. package/dist/__tests__/utils.normalizeCoinType.test.js.map +1 -0
  83. package/dist/index.d.ts +19 -11
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +24 -40
  86. package/dist/index.js.map +1 -1
  87. package/dist/protocols/base-adapter.d.ts +42 -75
  88. package/dist/protocols/base-adapter.d.ts.map +1 -1
  89. package/dist/protocols/base-adapter.js +34 -77
  90. package/dist/protocols/base-adapter.js.map +1 -1
  91. package/dist/protocols/navi/adapter.d.ts +22 -2
  92. package/dist/protocols/navi/adapter.d.ts.map +1 -1
  93. package/dist/protocols/navi/adapter.js +145 -11
  94. package/dist/protocols/navi/adapter.js.map +1 -1
  95. package/dist/protocols/scallop/adapter.d.ts +26 -6
  96. package/dist/protocols/scallop/adapter.d.ts.map +1 -1
  97. package/dist/protocols/scallop/adapter.js +206 -43
  98. package/dist/protocols/scallop/adapter.js.map +1 -1
  99. package/dist/protocols/scallop/flash-loan.d.ts +18 -3
  100. package/dist/protocols/scallop/flash-loan.d.ts.map +1 -1
  101. package/dist/protocols/scallop/flash-loan.js +79 -20
  102. package/dist/protocols/scallop/flash-loan.js.map +1 -1
  103. package/dist/protocols/scallop/types.d.ts.map +1 -1
  104. package/dist/protocols/scallop/types.js +4 -1
  105. package/dist/protocols/scallop/types.js.map +1 -1
  106. package/dist/protocols/suilend/adapter.d.ts +19 -1
  107. package/dist/protocols/suilend/adapter.d.ts.map +1 -1
  108. package/dist/protocols/suilend/adapter.js +196 -51
  109. package/dist/protocols/suilend/adapter.js.map +1 -1
  110. package/dist/sdk.d.ts +107 -148
  111. package/dist/sdk.d.ts.map +1 -1
  112. package/dist/sdk.js +171 -523
  113. package/dist/sdk.js.map +1 -1
  114. package/dist/strategies/common.d.ts +42 -0
  115. package/dist/strategies/common.d.ts.map +1 -0
  116. package/dist/strategies/common.js +81 -0
  117. package/dist/strategies/common.js.map +1 -0
  118. package/dist/strategies/deleverage.d.ts.map +1 -1
  119. package/dist/strategies/deleverage.js +44 -62
  120. package/dist/strategies/deleverage.js.map +1 -1
  121. package/dist/strategies/index.d.ts +9 -1
  122. package/dist/strategies/index.d.ts.map +1 -1
  123. package/dist/strategies/index.js +11 -2
  124. package/dist/strategies/index.js.map +1 -1
  125. package/dist/strategies/leverage-preview.d.ts +27 -0
  126. package/dist/strategies/leverage-preview.d.ts.map +1 -0
  127. package/dist/strategies/leverage-preview.js +120 -0
  128. package/dist/strategies/leverage-preview.js.map +1 -0
  129. package/dist/strategies/leverage-route.d.ts +29 -0
  130. package/dist/strategies/leverage-route.d.ts.map +1 -0
  131. package/dist/strategies/leverage-route.js +112 -0
  132. package/dist/strategies/leverage-route.js.map +1 -0
  133. package/dist/strategies/leverage.d.ts +4 -44
  134. package/dist/strategies/leverage.d.ts.map +1 -1
  135. package/dist/strategies/leverage.js +43 -112
  136. package/dist/strategies/leverage.js.map +1 -1
  137. package/dist/strategies/scallop-leverage.d.ts +34 -0
  138. package/dist/strategies/scallop-leverage.d.ts.map +1 -0
  139. package/dist/strategies/scallop-leverage.js +143 -0
  140. package/dist/strategies/scallop-leverage.js.map +1 -0
  141. package/dist/types/config.d.ts +0 -7
  142. package/dist/types/config.d.ts.map +1 -1
  143. package/dist/types/constants.d.ts +8 -0
  144. package/dist/types/constants.d.ts.map +1 -1
  145. package/dist/types/constants.js +9 -1
  146. package/dist/types/constants.js.map +1 -1
  147. package/dist/types/index.d.ts +3 -3
  148. package/dist/types/index.d.ts.map +1 -1
  149. package/dist/types/index.js +2 -1
  150. package/dist/types/index.js.map +1 -1
  151. package/dist/types/position.d.ts +7 -3
  152. package/dist/types/position.d.ts.map +1 -1
  153. package/dist/types/protocol.d.ts +101 -0
  154. package/dist/types/protocol.d.ts.map +1 -1
  155. package/dist/types/strategy.d.ts +82 -38
  156. package/dist/types/strategy.d.ts.map +1 -1
  157. package/dist/utils/calculations.d.ts +10 -137
  158. package/dist/utils/calculations.d.ts.map +1 -1
  159. package/dist/utils/calculations.js +11 -162
  160. package/dist/utils/calculations.js.map +1 -1
  161. package/dist/utils/coin.d.ts +16 -7
  162. package/dist/utils/coin.d.ts.map +1 -1
  163. package/dist/utils/coin.js +32 -25
  164. package/dist/utils/coin.js.map +1 -1
  165. package/dist/utils/errors.d.ts +0 -18
  166. package/dist/utils/errors.d.ts.map +1 -1
  167. package/dist/utils/errors.js +2 -40
  168. package/dist/utils/errors.js.map +1 -1
  169. package/dist/utils/execution.d.ts +38 -0
  170. package/dist/utils/execution.d.ts.map +1 -0
  171. package/dist/utils/execution.js +110 -0
  172. package/dist/utils/execution.js.map +1 -0
  173. package/dist/utils/gas.d.ts +0 -37
  174. package/dist/utils/gas.d.ts.map +1 -1
  175. package/dist/utils/gas.js +2 -60
  176. package/dist/utils/gas.js.map +1 -1
  177. package/dist/utils/index.d.ts +1 -1
  178. package/dist/utils/index.d.ts.map +1 -1
  179. package/dist/utils/index.js +1 -1
  180. package/dist/utils/index.js.map +1 -1
  181. package/package.json +15 -45
@@ -3,113 +3,6 @@
3
3
  *
4
4
  * Common calculations for DeFi strategies (leverage, deleverage, flash loans, etc.)
5
5
  */
6
- /**
7
- * Flash loan parameters
8
- */
9
- export interface FlashLoanParams {
10
- /** Amount to borrow */
11
- borrowAmount: bigint;
12
- /** Flash loan fee */
13
- fee: bigint;
14
- /** Total amount to repay (borrowAmount + fee) */
15
- totalRepayment: bigint;
16
- }
17
- /**
18
- * Deleverage calculation result
19
- */
20
- export interface DeleverageCalculation {
21
- /** Amount of collateral to swap */
22
- swapAmount: bigint;
23
- /** Amount of collateral to keep */
24
- keepAmount: bigint;
25
- /** Expected USDC output from swap */
26
- expectedUsdcOut: bigint;
27
- /** Estimated profit in USDC */
28
- estimatedProfit: bigint;
29
- /** Flash loan parameters */
30
- flashLoan: FlashLoanParams;
31
- }
32
- /**
33
- * Leverage calculation result
34
- */
35
- export interface LeverageCalculation {
36
- /** Flash loan amount in USDC (raw units, 6 decimals) */
37
- flashLoanUsdc: bigint;
38
- /** Amount of deposit asset expected from swap */
39
- expectedAssetOut: bigint;
40
- /** Total deposit amount (user deposit + swapped amount) */
41
- totalDeposit: bigint;
42
- /** Amount to borrow for flash loan repayment */
43
- borrowAmount: bigint;
44
- /** Flash loan parameters */
45
- flashLoan: FlashLoanParams;
46
- }
47
- /**
48
- * Calculate flash loan parameters
49
- *
50
- * @param amount - Amount to borrow
51
- * @param bufferPercent - Buffer percentage to add (e.g., 0.5 for 0.5%)
52
- * @returns Flash loan parameters
53
- */
54
- export declare function calculateFlashLoanParams(amount: bigint, bufferPercent?: number): FlashLoanParams;
55
- /**
56
- * Calculate optimal swap amount for deleverage
57
- *
58
- * Given a collateral amount and debt to repay, calculates how much
59
- * collateral needs to be swapped to cover the debt while keeping
60
- * as much collateral as possible.
61
- *
62
- * @param collateralAmount - Total collateral amount (raw units)
63
- * @param debtAmount - Debt to repay (raw units)
64
- * @param swapRate - Expected output per input (from quote)
65
- * @param bufferPercent - Buffer percentage (e.g., 2 for 2%)
66
- * @returns Swap and keep amounts
67
- */
68
- export declare function calculateDeleverageSwapAmount(collateralAmount: bigint, debtAmount: bigint, swapRateNumerator: bigint, swapRateDenominator: bigint, bufferPercent?: number): {
69
- swapAmount: bigint;
70
- keepAmount: bigint;
71
- };
72
- /**
73
- * Calculate leverage parameters
74
- *
75
- * @param depositValueUsd - Initial deposit value in USD
76
- * @param multiplier - Target leverage multiplier
77
- * @param assetPriceUsd - Asset price in USD
78
- * @param assetDecimals - Asset decimals
79
- * @returns Leverage calculation
80
- */
81
- export declare function calculateLeverageParams(depositValueUsd: number, multiplier: number, assetPriceUsd: number, assetDecimals: number): {
82
- flashLoanUsd: number;
83
- flashLoanUsdc: bigint;
84
- expectedAssetAmount: bigint;
85
- };
86
- /**
87
- * Calculate position metrics
88
- *
89
- * @param collateralValueUsd - Total collateral value in USD
90
- * @param debtValueUsd - Total debt value in USD
91
- * @param collateralPrice - Collateral asset price in USD
92
- * @param liquidationThreshold - Liquidation LTV (e.g., 0.85 for 85%)
93
- * @returns Position metrics
94
- */
95
- export declare function calculatePositionMetrics(collateralValueUsd: number, debtValueUsd: number, collateralPrice: number, liquidationThreshold?: number): {
96
- ltvPercent: number;
97
- healthFactor: number;
98
- liquidationPrice: number;
99
- priceDropBuffer: number;
100
- netValueUsd: number;
101
- effectiveMultiplier: number;
102
- };
103
- /**
104
- * Convert amount from human-readable to raw units
105
- *
106
- * Wrapper around parseUnits that accepts both number and string input.
107
- *
108
- * @param humanAmount - Human-readable amount (e.g., 1.5 or "1.5")
109
- * @param decimals - Token decimals
110
- * @returns Raw amount as bigint
111
- */
112
- export declare function toRawUnits(humanAmount: number | string, decimals: number): bigint;
113
6
  /**
114
7
  * Convert amount from raw units to human-readable number
115
8
  *
@@ -121,36 +14,16 @@ export declare function toRawUnits(humanAmount: number | string, decimals: numbe
121
14
  */
122
15
  export declare function fromRawUnits(rawAmount: bigint | string, decimals: number): number;
123
16
  /**
124
- * Check if an amount string is in human-readable format
125
- * (contains decimal point or is a small number)
17
+ * Calculate liquidation metrics for a leveraged position
126
18
  *
127
- * @param amountStr - Amount string to check
128
- * @param threshold - Threshold for considering as human-readable (default: 1000)
129
- * @returns true if human-readable format
19
+ * @param debtUsd - Total debt in USD
20
+ * @param collateralAmount - Total collateral in token units (human-readable)
21
+ * @param liquidationThreshold - Liquidation threshold (0-1, e.g. 0.65)
22
+ * @param currentPrice - Current asset price in USD
23
+ * @returns Liquidation price and price drop buffer percentage
130
24
  */
131
- export declare function isHumanReadableAmount(amountStr: string, threshold?: number): boolean;
132
- /**
133
- * Parse amount from environment variable, handling both human-readable and raw formats
134
- *
135
- * @param amountStr - Amount string (human-readable or raw)
136
- * @param decimals - Token decimals
137
- * @returns Raw amount as bigint
138
- */
139
- export declare function parseEnvAmount(amountStr: string, decimals: number): bigint;
140
- /**
141
- * Calculate minimum amount after slippage
142
- *
143
- * @param amount - Expected amount
144
- * @param slippageBps - Slippage in basis points (100 = 1%)
145
- * @returns Minimum amount after slippage
146
- */
147
- export declare function withSlippage(amount: bigint, slippageBps: number): bigint;
148
- /**
149
- * Add buffer to an amount (for interest, fees, etc.)
150
- *
151
- * @param amount - Base amount
152
- * @param bufferPercent - Buffer percentage (e.g., 2 for 2%)
153
- * @returns Amount with buffer
154
- */
155
- export declare function withBuffer(amount: bigint, bufferPercent: number): bigint;
25
+ export declare function calculateLiquidationMetrics(debtUsd: number, collateralAmount: number, liquidationThreshold: number, currentPrice: number): {
26
+ liquidationPrice: number;
27
+ priceDropBuffer: number;
28
+ };
156
29
  //# sourceMappingURL=calculations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"calculations.d.ts","sourceRoot":"","sources":["../../src/utils/calculations.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,uBAAuB;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB,qBAAqB;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,iDAAiD;IACjD,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,mCAAmC;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,mCAAmC;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,qCAAqC;IACrC,eAAe,EAAE,MAAM,CAAC;IACxB,+BAA+B;IAC/B,eAAe,EAAE,MAAM,CAAC;IACxB,4BAA4B;IAC5B,SAAS,EAAE,eAAe,CAAC;CAC5B;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,wDAAwD;IACxD,aAAa,EAAE,MAAM,CAAC;IACtB,iDAAiD;IACjD,gBAAgB,EAAE,MAAM,CAAC;IACzB,2DAA2D;IAC3D,YAAY,EAAE,MAAM,CAAC;IACrB,gDAAgD;IAChD,YAAY,EAAE,MAAM,CAAC;IACrB,4BAA4B;IAC5B,SAAS,EAAE,eAAe,CAAC;CAC5B;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,MAAM,EACd,aAAa,GAAE,MAAY,GAC1B,eAAe,CAajB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,6BAA6B,CAC3C,gBAAgB,EAAE,MAAM,EACxB,UAAU,EAAE,MAAM,EAClB,iBAAiB,EAAE,MAAM,EACzB,mBAAmB,EAAE,MAAM,EAC3B,aAAa,GAAE,MAAU,GACxB;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAe5C;AAED;;;;;;;;GAQG;AACH,wBAAg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"version":3,"file":"calculations.d.ts","sourceRoot":"","sources":["../../src/utils/calculations.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEjF;AAED;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CACzC,OAAO,EAAE,MAAM,EACf,gBAAgB,EAAE,MAAM,EACxB,oBAAoB,EAAE,MAAM,EAC5B,YAAY,EAAE,MAAM,GACnB;IAAE,gBAAgB,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,CAKvD"}
@@ -5,129 +5,9 @@
5
5
  * Common calculations for DeFi strategies (leverage, deleverage, flash loans, etc.)
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.calculateFlashLoanParams = calculateFlashLoanParams;
9
- exports.calculateDeleverageSwapAmount = calculateDeleverageSwapAmount;
10
- exports.calculateLeverageParams = calculateLeverageParams;
11
- exports.calculatePositionMetrics = calculatePositionMetrics;
12
- exports.toRawUnits = toRawUnits;
13
8
  exports.fromRawUnits = fromRawUnits;
14
- exports.isHumanReadableAmount = isHumanReadableAmount;
15
- exports.parseEnvAmount = parseEnvAmount;
16
- exports.withSlippage = withSlippage;
17
- exports.withBuffer = withBuffer;
18
- const flash_loan_1 = require("../protocols/scallop/flash-loan");
9
+ exports.calculateLiquidationMetrics = calculateLiquidationMetrics;
19
10
  const format_1 = require("./format");
20
- /**
21
- * Calculate flash loan parameters
22
- *
23
- * @param amount - Amount to borrow
24
- * @param bufferPercent - Buffer percentage to add (e.g., 0.5 for 0.5%)
25
- * @returns Flash loan parameters
26
- */
27
- function calculateFlashLoanParams(amount, bufferPercent = 0.5) {
28
- // Add buffer for interest accrual
29
- const bufferMultiplier = BigInt(Math.floor((100 + bufferPercent) * 10));
30
- const borrowAmount = (amount * bufferMultiplier) / 1000n;
31
- const fee = flash_loan_1.ScallopFlashLoanClient.calculateFee(borrowAmount);
32
- const totalRepayment = borrowAmount + fee;
33
- return {
34
- borrowAmount,
35
- fee,
36
- totalRepayment,
37
- };
38
- }
39
- /**
40
- * Calculate optimal swap amount for deleverage
41
- *
42
- * Given a collateral amount and debt to repay, calculates how much
43
- * collateral needs to be swapped to cover the debt while keeping
44
- * as much collateral as possible.
45
- *
46
- * @param collateralAmount - Total collateral amount (raw units)
47
- * @param debtAmount - Debt to repay (raw units)
48
- * @param swapRate - Expected output per input (from quote)
49
- * @param bufferPercent - Buffer percentage (e.g., 2 for 2%)
50
- * @returns Swap and keep amounts
51
- */
52
- function calculateDeleverageSwapAmount(collateralAmount, debtAmount, swapRateNumerator, swapRateDenominator, bufferPercent = 2) {
53
- // Target output = debt + buffer
54
- const bufferMultiplier = BigInt(Math.floor((100 + bufferPercent) * 100));
55
- const targetOutput = (debtAmount * bufferMultiplier) / 10000n;
56
- // Calculate required input: input = target / rate
57
- // rate = numerator / denominator
58
- // input = target * denominator / numerator
59
- const requiredInput = (targetOutput * swapRateDenominator) / swapRateNumerator;
60
- // Cap at total collateral
61
- const swapAmount = requiredInput > collateralAmount ? collateralAmount : requiredInput;
62
- const keepAmount = collateralAmount - swapAmount;
63
- return { swapAmount, keepAmount };
64
- }
65
- /**
66
- * Calculate leverage parameters
67
- *
68
- * @param depositValueUsd - Initial deposit value in USD
69
- * @param multiplier - Target leverage multiplier
70
- * @param assetPriceUsd - Asset price in USD
71
- * @param assetDecimals - Asset decimals
72
- * @returns Leverage calculation
73
- */
74
- function calculateLeverageParams(depositValueUsd, multiplier, assetPriceUsd, assetDecimals) {
75
- // Flash loan = deposit * (multiplier - 1)
76
- const flashLoanUsd = depositValueUsd * (multiplier - 1);
77
- // Convert to USDC raw units (6 decimals) with 2% buffer
78
- const flashLoanUsdc = BigInt(Math.ceil(flashLoanUsd * 1e6 * 1.02));
79
- // Expected asset amount = flash loan USD / asset price
80
- const expectedAssetHuman = flashLoanUsd / assetPriceUsd;
81
- const expectedAssetAmount = BigInt(Math.floor(expectedAssetHuman * Math.pow(10, assetDecimals)));
82
- return {
83
- flashLoanUsd,
84
- flashLoanUsdc,
85
- expectedAssetAmount,
86
- };
87
- }
88
- /**
89
- * Calculate position metrics
90
- *
91
- * @param collateralValueUsd - Total collateral value in USD
92
- * @param debtValueUsd - Total debt value in USD
93
- * @param collateralPrice - Collateral asset price in USD
94
- * @param liquidationThreshold - Liquidation LTV (e.g., 0.85 for 85%)
95
- * @returns Position metrics
96
- */
97
- function calculatePositionMetrics(collateralValueUsd, debtValueUsd, collateralPrice, liquidationThreshold = 0.85) {
98
- const netValueUsd = collateralValueUsd - debtValueUsd;
99
- const ltvPercent = debtValueUsd > 0 ? (debtValueUsd / collateralValueUsd) * 100 : 0;
100
- const healthFactor = debtValueUsd > 0 ? collateralValueUsd / debtValueUsd : Infinity;
101
- // Liquidation price = current price * (debt / (collateral * threshold))
102
- const liquidationPrice = debtValueUsd > 0
103
- ? collateralPrice * (debtValueUsd / (collateralValueUsd * liquidationThreshold))
104
- : 0;
105
- // Price drop buffer = (current price - liquidation price) / current price
106
- const priceDropBuffer = liquidationPrice > 0 ? (collateralPrice - liquidationPrice) / collateralPrice : 1;
107
- // Effective multiplier = collateral / (collateral - debt)
108
- const effectiveMultiplier = netValueUsd > 0 ? collateralValueUsd / netValueUsd : 1;
109
- return {
110
- ltvPercent,
111
- healthFactor,
112
- liquidationPrice,
113
- priceDropBuffer,
114
- netValueUsd,
115
- effectiveMultiplier,
116
- };
117
- }
118
- /**
119
- * Convert amount from human-readable to raw units
120
- *
121
- * Wrapper around parseUnits that accepts both number and string input.
122
- *
123
- * @param humanAmount - Human-readable amount (e.g., 1.5 or "1.5")
124
- * @param decimals - Token decimals
125
- * @returns Raw amount as bigint
126
- */
127
- function toRawUnits(humanAmount, decimals) {
128
- const amountStr = typeof humanAmount === "number" ? humanAmount.toString() : humanAmount;
129
- return (0, format_1.parseUnits)(amountStr, decimals);
130
- }
131
11
  /**
132
12
  * Convert amount from raw units to human-readable number
133
13
  *
@@ -141,48 +21,17 @@ function fromRawUnits(rawAmount, decimals) {
141
21
  return parseFloat((0, format_1.formatUnits)(rawAmount, decimals));
142
22
  }
143
23
  /**
144
- * Check if an amount string is in human-readable format
145
- * (contains decimal point or is a small number)
146
- *
147
- * @param amountStr - Amount string to check
148
- * @param threshold - Threshold for considering as human-readable (default: 1000)
149
- * @returns true if human-readable format
150
- */
151
- function isHumanReadableAmount(amountStr, threshold = 1000) {
152
- return amountStr.includes(".") || parseFloat(amountStr) < threshold;
153
- }
154
- /**
155
- * Parse amount from environment variable, handling both human-readable and raw formats
156
- *
157
- * @param amountStr - Amount string (human-readable or raw)
158
- * @param decimals - Token decimals
159
- * @returns Raw amount as bigint
160
- */
161
- function parseEnvAmount(amountStr, decimals) {
162
- if (isHumanReadableAmount(amountStr)) {
163
- return toRawUnits(amountStr, decimals);
164
- }
165
- return BigInt(amountStr);
166
- }
167
- /**
168
- * Calculate minimum amount after slippage
169
- *
170
- * @param amount - Expected amount
171
- * @param slippageBps - Slippage in basis points (100 = 1%)
172
- * @returns Minimum amount after slippage
173
- */
174
- function withSlippage(amount, slippageBps) {
175
- return (amount * BigInt(10000 - slippageBps)) / 10000n;
176
- }
177
- /**
178
- * Add buffer to an amount (for interest, fees, etc.)
24
+ * Calculate liquidation metrics for a leveraged position
179
25
  *
180
- * @param amount - Base amount
181
- * @param bufferPercent - Buffer percentage (e.g., 2 for 2%)
182
- * @returns Amount with buffer
26
+ * @param debtUsd - Total debt in USD
27
+ * @param collateralAmount - Total collateral in token units (human-readable)
28
+ * @param liquidationThreshold - Liquidation threshold (0-1, e.g. 0.65)
29
+ * @param currentPrice - Current asset price in USD
30
+ * @returns Liquidation price and price drop buffer percentage
183
31
  */
184
- function withBuffer(amount, bufferPercent) {
185
- const multiplier = BigInt(Math.floor((100 + bufferPercent) * 100));
186
- return (amount * multiplier) / 10000n;
32
+ function calculateLiquidationMetrics(debtUsd, collateralAmount, liquidationThreshold, currentPrice) {
33
+ const liquidationPrice = debtUsd / (collateralAmount * liquidationThreshold);
34
+ const priceDropBuffer = (1 - liquidationPrice / currentPrice) * 100;
35
+ return { liquidationPrice, priceDropBuffer };
187
36
  }
188
37
  //# sourceMappingURL=calculations.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"calculations.js","sourceRoot":"","sources":["../../src/utils/calculations.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;AAwDH,4DAgBC;AAeD,sEAqBC;AAWD,0DAuBC;AAWD,4DAsCC;AAWD,gCAGC;AAWD,oCAEC;AAUD,sDAEC;AASD,wCAKC;AASD,oCAEC;AASD,gCAGC;AAzQD,gEAAyE;AACzE,qCAAmD;AA8CnD;;;;;;GAMG;AACH,SAAgB,wBAAwB,CACtC,MAAc,EACd,gBAAwB,GAAG;IAE3B,kCAAkC;IAClC,MAAM,gBAAgB,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,aAAa,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;IACxE,MAAM,YAAY,GAAG,CAAC,MAAM,GAAG,gBAAgB,CAAC,GAAG,KAAK,CAAC;IAEzD,MAAM,GAAG,GAAG,mCAAsB,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;IAC9D,MAAM,cAAc,GAAG,YAAY,GAAG,GAAG,CAAC;IAE1C,OAAO;QACL,YAAY;QACZ,GAAG;QACH,cAAc;KACf,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,6BAA6B,CAC3C,gBAAwB,EACxB,UAAkB,EAClB,iBAAyB,EACzB,mBAA2B,EAC3B,gBAAwB,CAAC;IAEzB,gCAAgC;IAChC,MAAM,gBAAgB,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,aAAa,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;IACzE,MAAM,YAAY,GAAG,CAAC,UAAU,GAAG,gBAAgB,CAA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G,YAAY,CAAC,GAAG,GAAG,CAAC;IACpE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,CAAC;AAC/C,CAAC"}
@@ -1,23 +1,32 @@
1
+ /**
2
+ * Look up the decimal places for a coin type.
3
+ *
4
+ * Uses the SDK-level COIN_DECIMALS map with a configurable fallback
5
+ * (default 8, which covers most Sui assets).
6
+ *
7
+ * @param coinType - Normalized full coin type string
8
+ * @param fallback - Decimal places to return if coin type is unknown (default: 8)
9
+ */
10
+ export declare function getDecimals(coinType: string, fallback?: number): number;
1
11
  /**
2
12
  * Normalizes a Sui coin type address to ensure consistent formatting.
3
13
  * Pads the package address to 64 characters and ensures 0x prefix.
4
14
  *
5
15
  * @param coinType - The coin type string (e.g., "0x2::sui::SUI")
6
16
  * @returns The normalized coin type with padded address
17
+ * @throws {InvalidCoinTypeError} If coinType is not in `package::module::Type` format
7
18
  *
8
19
  * @example
9
20
  * normalizeCoinType("0x2::sui::SUI")
10
21
  * // "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI"
11
- *
12
- * @see scripts/tests/test_normalize_coin_type.ts for test cases
13
22
  */
14
23
  export declare function normalizeCoinType(coinType: string): string;
15
24
  /**
16
- * Formats a coin type using normalizeStructTag with fallback.
17
- * Ensures the result always has 0x prefix.
25
+ * Resolve an asset symbol (e.g. "SUI", "LBTC") or full coin type to a normalized coin type.
18
26
  *
19
- * @param type - The coin type string
20
- * @returns The normalized coin type with 0x prefix
27
+ * @param asset - Symbol string or full coin type (containing "::")
28
+ * @returns Normalized coin type
29
+ * @throws {UnknownAssetError} If symbol is not recognized
21
30
  */
22
- export declare function formatCoinType(type: string): string;
31
+ export declare function resolveCoinType(asset: string): string;
23
32
  //# sourceMappingURL=coin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"coin.d.ts","sourceRoot":"","sources":["../../src/utils/coin.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAM1D;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAgBnD"}
1
+ {"version":3,"file":"coin.d.ts","sourceRoot":"","sources":["../../src/utils/coin.ts"],"names":[],"mappings":"AAGA;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,SAAI,GAAG,MAAM,CAElE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAQ1D;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAYrD"}
@@ -1,52 +1,59 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDecimals = getDecimals;
3
4
  exports.normalizeCoinType = normalizeCoinType;
4
- exports.formatCoinType = formatCoinType;
5
- const utils_1 = require("@mysten/sui/utils");
5
+ exports.resolveCoinType = resolveCoinType;
6
+ const errors_1 = require("./errors");
7
+ const constants_1 = require("../types/constants");
8
+ /**
9
+ * Look up the decimal places for a coin type.
10
+ *
11
+ * Uses the SDK-level COIN_DECIMALS map with a configurable fallback
12
+ * (default 8, which covers most Sui assets).
13
+ *
14
+ * @param coinType - Normalized full coin type string
15
+ * @param fallback - Decimal places to return if coin type is unknown (default: 8)
16
+ */
17
+ function getDecimals(coinType, fallback = 8) {
18
+ return constants_1.COIN_DECIMALS[coinType] ?? fallback;
19
+ }
6
20
  /**
7
21
  * Normalizes a Sui coin type address to ensure consistent formatting.
8
22
  * Pads the package address to 64 characters and ensures 0x prefix.
9
23
  *
10
24
  * @param coinType - The coin type string (e.g., "0x2::sui::SUI")
11
25
  * @returns The normalized coin type with padded address
26
+ * @throws {InvalidCoinTypeError} If coinType is not in `package::module::Type` format
12
27
  *
13
28
  * @example
14
29
  * normalizeCoinType("0x2::sui::SUI")
15
30
  * // "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI"
16
- *
17
- * @see scripts/tests/test_normalize_coin_type.ts for test cases
18
31
  */
19
32
  function normalizeCoinType(coinType) {
20
33
  const parts = coinType.split('::');
21
- if (parts.length !== 3)
22
- return coinType;
34
+ if (parts.length !== 3) {
35
+ throw new errors_1.InvalidCoinTypeError(coinType);
36
+ }
23
37
  let pkg = parts[0].replace('0x', '');
24
38
  pkg = pkg.padStart(64, '0');
25
39
  return `0x${pkg}::${parts[1]}::${parts[2]}`;
26
40
  }
27
41
  /**
28
- * Formats a coin type using normalizeStructTag with fallback.
29
- * Ensures the result always has 0x prefix.
42
+ * Resolve an asset symbol (e.g. "SUI", "LBTC") or full coin type to a normalized coin type.
30
43
  *
31
- * @param type - The coin type string
32
- * @returns The normalized coin type with 0x prefix
44
+ * @param asset - Symbol string or full coin type (containing "::")
45
+ * @returns Normalized coin type
46
+ * @throws {UnknownAssetError} If symbol is not recognized
33
47
  */
34
- function formatCoinType(type) {
35
- if (!type.startsWith('0x') && !type.includes('::')) {
36
- // Likely invalid, but let normalizeStructTag handle
37
- }
38
- try {
39
- const normalized = (0, utils_1.normalizeStructTag)(type);
40
- if (!normalized.startsWith('0x')) {
41
- return `0x${normalized}`;
42
- }
43
- return normalized;
48
+ function resolveCoinType(asset) {
49
+ if (asset.includes('::')) {
50
+ return normalizeCoinType(asset);
44
51
  }
45
- catch (e) {
46
- if (type.includes('::') && !type.startsWith('0x')) {
47
- return `0x${type}`;
48
- }
49
- return type;
52
+ const upperSymbol = asset.toUpperCase();
53
+ const coinType = constants_1.COIN_TYPES[upperSymbol];
54
+ if (coinType) {
55
+ return normalizeCoinType(coinType);
50
56
  }
57
+ throw new errors_1.UnknownAssetError(asset);
51
58
  }
52
59
  //# sourceMappingURL=coin.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"coin.js","sourceRoot":"","sources":["../../src/utils/coin.ts"],"names":[],"mappings":";;AAeA,8CAMC;AASD,wCAgBC;AA9CD,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AASD,0CAYC;AAzDD,qCAAmE;AACnE,kDAA+D;AAE/D;;;;;;;;GAQG;AACH,SAAgB,WAAW,CAAC,QAAgB,EAAE,QAAQ,GAAG,CAAC;IACxD,OAAO,yBAAa,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC;AAC7C,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,iBAAiB,CAAC,QAAgB;IAChD,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACnC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,6BAAoB,CAAC,QAAQ,CAAC,CAAC;IAC3C,CAAC;IACD,IAAI,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACrC,GAAG,GAAG,GAAG,CAAC,QAAQ,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;IAC5B,OAAO,KAAK,GAAG,KAAK,KAAK,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AAC9C,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,eAAe,CAAC,KAAa;IAC3C,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACzB,OAAO,iBAAiB,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC;IAED,MAAM,WAAW,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC;IACxC,MAAM,QAAQ,GAAG,sBAAU,CAAC,WAAsC,CAAC,CAAC;IACpE,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IACrC,CAAC;IAED,MAAM,IAAI,0BAAiB,CAAC,KAAK,CAAC,CAAC;AACrC,CAAC"}
@@ -75,24 +75,6 @@ export declare class NoDebtError extends DefiDashError {
75
75
  export declare class InvalidParameterError extends DefiDashError {
76
76
  constructor(message: string);
77
77
  }
78
- /**
79
- * Error thrown when insufficient balance for operation
80
- */
81
- export declare class InsufficientBalanceError extends DefiDashError {
82
- constructor(required: string, available: string, asset?: string);
83
- }
84
- /**
85
- * Error thrown when a transaction dry run fails
86
- */
87
- export declare class DryRunFailedError extends DefiDashError {
88
- constructor(reason?: string);
89
- }
90
- /**
91
- * Error thrown when a transaction execution fails
92
- */
93
- export declare class TransactionFailedError extends DefiDashError {
94
- constructor(reason?: string);
95
- }
96
78
  /**
97
79
  * Error thrown when keypair is required but not provided
98
80
  */
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAc,SAAQ,KAAK;gBAC1B,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,sBAAuB,SAAQ,aAAa;;CAKxD;AAED;;GAEG;AACH,qBAAa,wBAAyB,SAAQ,aAAa;gBAC7C,QAAQ,EAAE,MAAM;CAI7B;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,aAAa;gBACtC,KAAK,EAAE,MAAM;CAI1B;AAED;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,aAAa;gBAC1C,QAAQ,CAAC,EAAE,MAAM;CAQ9B;AAED;;GAEG;AACH,qBAAa,WAAY,SAAQ,aAAa;;CAK7C;AAED;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,aAAa;gBAC1C,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,wBAAyB,SAAQ,aAAa;gBAC7C,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,GAAE,MAAc;CAMvE;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,aAAa;gBACtC,MAAM,CAAC,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,sBAAuB,SAAQ,aAAa;gBAC3C,MAAM,CAAC,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,aAAa;;CAKtD;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,aAAa;gBACzC,QAAQ,EAAE,MAAM;CAI7B"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAc,SAAQ,KAAK;gBAC1B,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,sBAAuB,SAAQ,aAAa;;CAIxD;AAED;;GAEG;AACH,qBAAa,wBAAyB,SAAQ,aAAa;gBAC7C,QAAQ,EAAE,MAAM;CAG7B;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,aAAa;gBACtC,KAAK,EAAE,MAAM;CAG1B;AAED;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,aAAa;gBAC1C,QAAQ,CAAC,EAAE,MAAM;CAO9B;AAED;;GAEG;AACH,qBAAa,WAAY,SAAQ,aAAa;;CAI7C;AAED;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,aAAa;gBAC1C,OAAO,EAAE,MAAM;CAG5B;AAGD;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,aAAa;;CAItD;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,aAAa;gBACzC,QAAQ,EAAE,MAAM;CAG7B"}
@@ -25,7 +25,7 @@
25
25
  * ```
26
26
  */
27
27
  Object.defineProperty(exports, "__esModule", { value: true });
28
- exports.InvalidCoinTypeError = exports.KeypairRequiredError = exports.TransactionFailedError = exports.DryRunFailedError = exports.InsufficientBalanceError = exports.InvalidParameterError = exports.NoDebtError = exports.PositionNotFoundError = exports.UnknownAssetError = exports.UnsupportedProtocolError = exports.SDKNotInitializedError = exports.DefiDashError = void 0;
28
+ exports.InvalidCoinTypeError = exports.KeypairRequiredError = exports.InvalidParameterError = exports.NoDebtError = exports.PositionNotFoundError = exports.UnknownAssetError = exports.UnsupportedProtocolError = exports.SDKNotInitializedError = exports.DefiDashError = void 0;
29
29
  /**
30
30
  * Base error class for all SDK errors
31
31
  *
@@ -42,7 +42,7 @@ exports.InvalidCoinTypeError = exports.KeypairRequiredError = exports.Transactio
42
42
  class DefiDashError extends Error {
43
43
  constructor(message) {
44
44
  super(message);
45
- this.name = "DefiDashError";
45
+ this.name = this.constructor.name;
46
46
  }
47
47
  }
48
48
  exports.DefiDashError = DefiDashError;
@@ -52,7 +52,6 @@ exports.DefiDashError = DefiDashError;
52
52
  class SDKNotInitializedError extends DefiDashError {
53
53
  constructor() {
54
54
  super("SDK not initialized. Call initialize() first.");
55
- this.name = "SDKNotInitializedError";
56
55
  }
57
56
  }
58
57
  exports.SDKNotInitializedError = SDKNotInitializedError;
@@ -62,7 +61,6 @@ exports.SDKNotInitializedError = SDKNotInitializedError;
62
61
  class UnsupportedProtocolError extends DefiDashError {
63
62
  constructor(protocol) {
64
63
  super(`Protocol "${protocol}" is not supported`);
65
- this.name = "UnsupportedProtocolError";
66
64
  }
67
65
  }
68
66
  exports.UnsupportedProtocolError = UnsupportedProtocolError;
@@ -72,7 +70,6 @@ exports.UnsupportedProtocolError = UnsupportedProtocolError;
72
70
  class UnknownAssetError extends DefiDashError {
73
71
  constructor(asset) {
74
72
  super(`Unknown asset: "${asset}"`);
75
- this.name = "UnknownAssetError";
76
73
  }
77
74
  }
78
75
  exports.UnknownAssetError = UnknownAssetError;
@@ -84,7 +81,6 @@ class PositionNotFoundError extends DefiDashError {
84
81
  super(protocol
85
82
  ? `No position found on ${protocol}`
86
83
  : "No position found");
87
- this.name = "PositionNotFoundError";
88
84
  }
89
85
  }
90
86
  exports.PositionNotFoundError = PositionNotFoundError;
@@ -94,7 +90,6 @@ exports.PositionNotFoundError = PositionNotFoundError;
94
90
  class NoDebtError extends DefiDashError {
95
91
  constructor() {
96
92
  super("No debt to repay. Use withdraw instead.");
97
- this.name = "NoDebtError";
98
93
  }
99
94
  }
100
95
  exports.NoDebtError = NoDebtError;
@@ -104,47 +99,15 @@ exports.NoDebtError = NoDebtError;
104
99
  class InvalidParameterError extends DefiDashError {
105
100
  constructor(message) {
106
101
  super(`Invalid parameter: ${message}`);
107
- this.name = "InvalidParameterError";
108
102
  }
109
103
  }
110
104
  exports.InvalidParameterError = InvalidParameterError;
111
- /**
112
- * Error thrown when insufficient balance for operation
113
- */
114
- class InsufficientBalanceError extends DefiDashError {
115
- constructor(required, available, asset = "SUI") {
116
- super(`Insufficient ${asset} balance. Required: ${required}, Available: ${available}`);
117
- this.name = "InsufficientBalanceError";
118
- }
119
- }
120
- exports.InsufficientBalanceError = InsufficientBalanceError;
121
- /**
122
- * Error thrown when a transaction dry run fails
123
- */
124
- class DryRunFailedError extends DefiDashError {
125
- constructor(reason) {
126
- super(reason ? `Dry run failed: ${reason}` : "Dry run failed");
127
- this.name = "DryRunFailedError";
128
- }
129
- }
130
- exports.DryRunFailedError = DryRunFailedError;
131
- /**
132
- * Error thrown when a transaction execution fails
133
- */
134
- class TransactionFailedError extends DefiDashError {
135
- constructor(reason) {
136
- super(reason ? `Transaction failed: ${reason}` : "Transaction failed");
137
- this.name = "TransactionFailedError";
138
- }
139
- }
140
- exports.TransactionFailedError = TransactionFailedError;
141
105
  /**
142
106
  * Error thrown when keypair is required but not provided
143
107
  */
144
108
  class KeypairRequiredError extends DefiDashError {
145
109
  constructor() {
146
110
  super("Keypair required for this operation");
147
- this.name = "KeypairRequiredError";
148
111
  }
149
112
  }
150
113
  exports.KeypairRequiredError = KeypairRequiredError;
@@ -154,7 +117,6 @@ exports.KeypairRequiredError = KeypairRequiredError;
154
117
  class InvalidCoinTypeError extends DefiDashError {
155
118
  constructor(coinType) {
156
119
  super(`Invalid coin type: "${coinType}"`);
157
- this.name = "InvalidCoinTypeError";
158
120
  }
159
121
  }
160
122
  exports.InvalidCoinTypeError = InvalidCoinTypeError;