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.
- package/LICENSE +7 -0
- package/README.md +199 -97
- package/dist/__tests__/deleverageEstimate.unit.test.d.ts +10 -0
- package/dist/__tests__/deleverageEstimate.unit.test.d.ts.map +1 -0
- package/dist/__tests__/deleverageEstimate.unit.test.js +116 -0
- package/dist/__tests__/deleverageEstimate.unit.test.js.map +1 -0
- package/dist/__tests__/findBestLeverageRoute.test.d.ts +12 -0
- package/dist/__tests__/findBestLeverageRoute.test.d.ts.map +1 -0
- package/dist/__tests__/findBestLeverageRoute.test.js +177 -0
- package/dist/__tests__/findBestLeverageRoute.test.js.map +1 -0
- package/dist/__tests__/getAssetApy.test.d.ts +2 -0
- package/dist/__tests__/getAssetApy.test.d.ts.map +1 -0
- package/dist/__tests__/getAssetApy.test.js +133 -0
- package/dist/__tests__/getAssetApy.test.js.map +1 -0
- package/dist/__tests__/getAssetRiskParams.test.d.ts +11 -0
- package/dist/__tests__/getAssetRiskParams.test.d.ts.map +1 -0
- package/dist/__tests__/getAssetRiskParams.test.js +183 -0
- package/dist/__tests__/getAssetRiskParams.test.js.map +1 -0
- package/dist/__tests__/internal.getAssetApy.test.d.ts +2 -0
- package/dist/__tests__/internal.getAssetApy.test.d.ts.map +1 -0
- package/dist/__tests__/internal.getAssetApy.test.js +140 -0
- package/dist/__tests__/internal.getAssetApy.test.js.map +1 -0
- package/dist/__tests__/internal.getAssetRiskParams.test.d.ts +22 -0
- package/dist/__tests__/internal.getAssetRiskParams.test.d.ts.map +1 -0
- package/dist/__tests__/internal.getAssetRiskParams.test.js +194 -0
- package/dist/__tests__/internal.getAssetRiskParams.test.js.map +1 -0
- package/dist/__tests__/internal.getPosition.test.d.ts +22 -0
- package/dist/__tests__/internal.getPosition.test.d.ts.map +1 -0
- package/dist/__tests__/internal.getPosition.test.js +153 -0
- package/dist/__tests__/internal.getPosition.test.js.map +1 -0
- package/dist/__tests__/leveragePreview.unit.test.d.ts +10 -0
- package/dist/__tests__/leveragePreview.unit.test.d.ts.map +1 -0
- package/dist/__tests__/leveragePreview.unit.test.js +168 -0
- package/dist/__tests__/leveragePreview.unit.test.js.map +1 -0
- package/dist/__tests__/leverageRoute.unit.test.d.ts +10 -0
- package/dist/__tests__/leverageRoute.unit.test.d.ts.map +1 -0
- package/dist/__tests__/leverageRoute.unit.test.js +159 -0
- package/dist/__tests__/leverageRoute.unit.test.js.map +1 -0
- package/dist/__tests__/previewLeverage.test.d.ts +13 -0
- package/dist/__tests__/previewLeverage.test.d.ts.map +1 -0
- package/dist/__tests__/previewLeverage.test.js +217 -0
- package/dist/__tests__/previewLeverage.test.js.map +1 -0
- package/dist/__tests__/sdk.bestRoute.test.d.ts +22 -0
- package/dist/__tests__/sdk.bestRoute.test.d.ts.map +1 -0
- package/dist/__tests__/sdk.bestRoute.test.js +186 -0
- package/dist/__tests__/sdk.bestRoute.test.js.map +1 -0
- package/dist/__tests__/sdk.bestRoute.unit.test.d.ts +16 -0
- package/dist/__tests__/sdk.bestRoute.unit.test.d.ts.map +1 -0
- package/dist/__tests__/sdk.bestRoute.unit.test.js +165 -0
- package/dist/__tests__/sdk.bestRoute.unit.test.js.map +1 -0
- package/dist/__tests__/sdk.deleverage.test.d.ts +21 -0
- package/dist/__tests__/sdk.deleverage.test.d.ts.map +1 -0
- package/dist/__tests__/sdk.deleverage.test.js +130 -0
- package/dist/__tests__/sdk.deleverage.test.js.map +1 -0
- package/dist/__tests__/sdk.deleverage.unit.test.d.ts +21 -0
- package/dist/__tests__/sdk.deleverage.unit.test.d.ts.map +1 -0
- package/dist/__tests__/sdk.deleverage.unit.test.js +141 -0
- package/dist/__tests__/sdk.deleverage.unit.test.js.map +1 -0
- package/dist/__tests__/sdk.leverage.test.d.ts +19 -0
- package/dist/__tests__/sdk.leverage.test.d.ts.map +1 -0
- package/dist/__tests__/sdk.leverage.test.js +188 -0
- package/dist/__tests__/sdk.leverage.test.js.map +1 -0
- package/dist/__tests__/sdk.portfolio.test.d.ts +17 -0
- package/dist/__tests__/sdk.portfolio.test.d.ts.map +1 -0
- package/dist/__tests__/sdk.portfolio.test.js +162 -0
- package/dist/__tests__/sdk.portfolio.test.js.map +1 -0
- package/dist/__tests__/sdk.position.test.d.ts +16 -0
- package/dist/__tests__/sdk.position.test.d.ts.map +1 -0
- package/dist/__tests__/sdk.position.test.js +193 -0
- package/dist/__tests__/sdk.position.test.js.map +1 -0
- package/dist/__tests__/sdk.preview.test.d.ts +23 -0
- package/dist/__tests__/sdk.preview.test.d.ts.map +1 -0
- package/dist/__tests__/sdk.preview.test.js +226 -0
- package/dist/__tests__/sdk.preview.test.js.map +1 -0
- package/dist/__tests__/sdk.preview.unit.test.d.ts +18 -0
- package/dist/__tests__/sdk.preview.unit.test.d.ts.map +1 -0
- package/dist/__tests__/sdk.preview.unit.test.js +175 -0
- package/dist/__tests__/sdk.preview.unit.test.js.map +1 -0
- package/dist/__tests__/utils.normalizeCoinType.test.d.ts +7 -0
- package/dist/__tests__/utils.normalizeCoinType.test.d.ts.map +1 -0
- package/dist/__tests__/utils.normalizeCoinType.test.js +42 -0
- package/dist/__tests__/utils.normalizeCoinType.test.js.map +1 -0
- package/dist/index.d.ts +19 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -40
- package/dist/index.js.map +1 -1
- package/dist/protocols/base-adapter.d.ts +42 -75
- package/dist/protocols/base-adapter.d.ts.map +1 -1
- package/dist/protocols/base-adapter.js +34 -77
- package/dist/protocols/base-adapter.js.map +1 -1
- package/dist/protocols/navi/adapter.d.ts +22 -2
- package/dist/protocols/navi/adapter.d.ts.map +1 -1
- package/dist/protocols/navi/adapter.js +145 -11
- package/dist/protocols/navi/adapter.js.map +1 -1
- package/dist/protocols/scallop/adapter.d.ts +26 -6
- package/dist/protocols/scallop/adapter.d.ts.map +1 -1
- package/dist/protocols/scallop/adapter.js +206 -43
- package/dist/protocols/scallop/adapter.js.map +1 -1
- package/dist/protocols/scallop/flash-loan.d.ts +18 -3
- package/dist/protocols/scallop/flash-loan.d.ts.map +1 -1
- package/dist/protocols/scallop/flash-loan.js +79 -20
- package/dist/protocols/scallop/flash-loan.js.map +1 -1
- package/dist/protocols/scallop/types.d.ts.map +1 -1
- package/dist/protocols/scallop/types.js +4 -1
- package/dist/protocols/scallop/types.js.map +1 -1
- package/dist/protocols/suilend/adapter.d.ts +19 -1
- package/dist/protocols/suilend/adapter.d.ts.map +1 -1
- package/dist/protocols/suilend/adapter.js +196 -51
- package/dist/protocols/suilend/adapter.js.map +1 -1
- package/dist/sdk.d.ts +107 -148
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +171 -523
- package/dist/sdk.js.map +1 -1
- package/dist/strategies/common.d.ts +42 -0
- package/dist/strategies/common.d.ts.map +1 -0
- package/dist/strategies/common.js +81 -0
- package/dist/strategies/common.js.map +1 -0
- package/dist/strategies/deleverage.d.ts.map +1 -1
- package/dist/strategies/deleverage.js +44 -62
- package/dist/strategies/deleverage.js.map +1 -1
- package/dist/strategies/index.d.ts +9 -1
- package/dist/strategies/index.d.ts.map +1 -1
- package/dist/strategies/index.js +11 -2
- package/dist/strategies/index.js.map +1 -1
- package/dist/strategies/leverage-preview.d.ts +27 -0
- package/dist/strategies/leverage-preview.d.ts.map +1 -0
- package/dist/strategies/leverage-preview.js +120 -0
- package/dist/strategies/leverage-preview.js.map +1 -0
- package/dist/strategies/leverage-route.d.ts +29 -0
- package/dist/strategies/leverage-route.d.ts.map +1 -0
- package/dist/strategies/leverage-route.js +112 -0
- package/dist/strategies/leverage-route.js.map +1 -0
- package/dist/strategies/leverage.d.ts +4 -44
- package/dist/strategies/leverage.d.ts.map +1 -1
- package/dist/strategies/leverage.js +43 -112
- package/dist/strategies/leverage.js.map +1 -1
- package/dist/strategies/scallop-leverage.d.ts +34 -0
- package/dist/strategies/scallop-leverage.d.ts.map +1 -0
- package/dist/strategies/scallop-leverage.js +143 -0
- package/dist/strategies/scallop-leverage.js.map +1 -0
- package/dist/types/config.d.ts +0 -7
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/constants.d.ts +8 -0
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/constants.js +9 -1
- package/dist/types/constants.js.map +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +2 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/position.d.ts +7 -3
- package/dist/types/position.d.ts.map +1 -1
- package/dist/types/protocol.d.ts +101 -0
- package/dist/types/protocol.d.ts.map +1 -1
- package/dist/types/strategy.d.ts +82 -38
- package/dist/types/strategy.d.ts.map +1 -1
- package/dist/utils/calculations.d.ts +10 -137
- package/dist/utils/calculations.d.ts.map +1 -1
- package/dist/utils/calculations.js +11 -162
- package/dist/utils/calculations.js.map +1 -1
- package/dist/utils/coin.d.ts +16 -7
- package/dist/utils/coin.d.ts.map +1 -1
- package/dist/utils/coin.js +32 -25
- package/dist/utils/coin.js.map +1 -1
- package/dist/utils/errors.d.ts +0 -18
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/errors.js +2 -40
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/execution.d.ts +38 -0
- package/dist/utils/execution.d.ts.map +1 -0
- package/dist/utils/execution.js +110 -0
- package/dist/utils/execution.js.map +1 -0
- package/dist/utils/gas.d.ts +0 -37
- package/dist/utils/gas.d.ts.map +1 -1
- package/dist/utils/gas.js +2 -60
- package/dist/utils/gas.js.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/index.js.map +1 -1
- 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
|
-
*
|
|
125
|
-
* (contains decimal point or is a small number)
|
|
17
|
+
* Calculate liquidation metrics for a leveraged position
|
|
126
18
|
*
|
|
127
|
-
* @param
|
|
128
|
-
* @param
|
|
129
|
-
* @
|
|
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
|
|
132
|
-
|
|
133
|
-
|
|
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;
|
|
1
|
+
{"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.
|
|
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
|
-
*
|
|
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
|
|
181
|
-
* @param
|
|
182
|
-
* @
|
|
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
|
|
185
|
-
const
|
|
186
|
-
|
|
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;;
|
|
1
|
+
{"version":3,"file":"calculations.js","sourceRoot":"","sources":["../../src/utils/calculations.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;AAaH,oCAEC;AAWD,kEAUC;AAlCD,qCAAuC;AAEvC;;;;;;;;GAQG;AACH,SAAgB,YAAY,CAAC,SAA0B,EAAE,QAAgB;IACvE,OAAO,UAAU,CAAC,IAAA,oBAAW,EAAC,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC;AACtD,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,2BAA2B,CACzC,OAAe,EACf,gBAAwB,EACxB,oBAA4B,EAC5B,YAAoB;IAEpB,MAAM,gBAAgB,GACpB,OAAO,GAAG,CAAC,gBAAgB,GAAG,oBAAoB,CAAC,CAAC;IACtD,MAAM,eAAe,GAAG,CAAC,CAAC,GAAG,gBAAgB,GAAG,YAAY,CAAC,GAAG,GAAG,CAAC;IACpE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,CAAC;AAC/C,CAAC"}
|
package/dist/utils/coin.d.ts
CHANGED
|
@@ -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
|
-
*
|
|
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
|
|
20
|
-
* @returns
|
|
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
|
|
31
|
+
export declare function resolveCoinType(asset: string): string;
|
|
23
32
|
//# sourceMappingURL=coin.d.ts.map
|
package/dist/utils/coin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coin.d.ts","sourceRoot":"","sources":["../../src/utils/coin.ts"],"names":[],"mappings":"
|
|
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"}
|
package/dist/utils/coin.js
CHANGED
|
@@ -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.
|
|
5
|
-
const
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
|
32
|
-
* @returns
|
|
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
|
|
35
|
-
if (
|
|
36
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
package/dist/utils/coin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coin.js","sourceRoot":"","sources":["../../src/utils/coin.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"coin.js","sourceRoot":"","sources":["../../src/utils/coin.ts"],"names":[],"mappings":";;AAYA,kCAEC;AAcD,8CAQC;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"}
|
package/dist/utils/errors.d.ts
CHANGED
|
@@ -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;;
|
|
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"}
|
package/dist/utils/errors.js
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
-
exports.InvalidCoinTypeError = exports.KeypairRequiredError = exports.
|
|
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 =
|
|
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;
|