@mento-protocol/mento-sdk 3.0.0-rc.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/types/pool.d.ts +3 -1
- package/dist/esm/index.js +8 -6
- package/dist/esm/services/borrow/index.js +2 -0
- package/dist/esm/services/index.js +2 -0
- package/dist/esm/services/liquidity/liquidityHelpers.js +4 -2
- package/dist/esm/services/pools/poolDetails.js +10 -4
- package/dist/esm/services/pools/poolDiscovery.js +6 -2
- package/dist/esm/services/swap/SwapService.js +13 -0
- package/dist/esm/services/trading/TradingLimitsService.js +21 -6
- package/dist/index.d.ts +8 -6
- package/dist/index.js +8 -6
- package/dist/services/borrow/index.d.ts +2 -0
- package/dist/services/borrow/index.js +2 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.js +2 -0
- package/dist/services/liquidity/liquidityHelpers.js +4 -2
- package/dist/services/pools/poolDetails.js +10 -4
- package/dist/services/pools/poolDiscovery.js +6 -2
- package/dist/services/swap/SwapService.d.ts +7 -0
- package/dist/services/swap/SwapService.js +13 -0
- package/dist/services/trading/TradingLimitsService.js +21 -6
- package/package.json +1 -1
- package/dist/core/types/provider.d.ts +0 -45
- package/dist/core/types/provider.js +0 -3
- package/dist/esm/core/types/provider.js +0 -1
|
@@ -110,8 +110,10 @@ export interface FPMMPoolDetails extends Pool {
|
|
|
110
110
|
reserve1: bigint;
|
|
111
111
|
/** Block timestamp of last reserve update */
|
|
112
112
|
blockTimestampLast: bigint;
|
|
113
|
-
/** Pricing and oracle data (null when
|
|
113
|
+
/** Pricing and oracle data (null when unavailable — see pricingUnavailableReason) */
|
|
114
114
|
pricing: FPMMPricing | null;
|
|
115
|
+
/** Reason pricing data is unavailable, or null when pricing is present */
|
|
116
|
+
pricingUnavailableReason: string | null;
|
|
115
117
|
/** Fee configuration */
|
|
116
118
|
fees: FPMMFees;
|
|
117
119
|
/** Rebalancing state */
|
package/dist/esm/index.js
CHANGED
|
@@ -49,14 +49,16 @@ import { BorrowService } from './services/borrow';
|
|
|
49
49
|
* const status = await mento.trading.getPoolTradabilityStatus(pool);
|
|
50
50
|
*
|
|
51
51
|
* // Add liquidity to a pool
|
|
52
|
-
* const {
|
|
53
|
-
* poolAddress,
|
|
54
|
-
*
|
|
52
|
+
* const { approvalA, approvalB, addLiquidity } = await mento.liquidity.buildAddLiquidityTransaction({
|
|
53
|
+
* poolAddress, tokenA, amountA, tokenB, amountB, recipient, owner,
|
|
54
|
+
* options: { slippageTolerance: 0.5, deadline: deadlineFromMinutes(5) },
|
|
55
|
+
* });
|
|
55
56
|
*
|
|
56
57
|
* // Add liquidity using a single token (zap in)
|
|
57
|
-
* const { approval, zapIn } = await mento.liquidity.buildZapInTransaction(
|
|
58
|
-
* poolAddress, tokenIn, amountIn, 0.5, recipient, owner,
|
|
59
|
-
*
|
|
58
|
+
* const { approval, zapIn } = await mento.liquidity.buildZapInTransaction({
|
|
59
|
+
* poolAddress, tokenIn, amountIn, amountInSplit: 0.5, recipient, owner,
|
|
60
|
+
* options: { slippageTolerance: 0.5, deadline: deadlineFromMinutes(5) },
|
|
61
|
+
* });
|
|
60
62
|
*/
|
|
61
63
|
export class Mento {
|
|
62
64
|
constructor(chainId, tokens, pools, routes, quotes, swap, trading, liquidity, borrow) {
|
|
@@ -22,11 +22,13 @@ export async function getAllowance(publicClient, token, owner, chainId) {
|
|
|
22
22
|
}));
|
|
23
23
|
}
|
|
24
24
|
export function calculateMinAmount(amount, slippageTolerance) {
|
|
25
|
+
const MAX_SLIPPAGE_TOLERANCE = 20;
|
|
25
26
|
if (slippageTolerance < 0) {
|
|
26
27
|
throw new Error('Slippage tolerance cannot be negative');
|
|
27
28
|
}
|
|
28
|
-
if (slippageTolerance >
|
|
29
|
-
throw new Error(
|
|
29
|
+
if (slippageTolerance > MAX_SLIPPAGE_TOLERANCE) {
|
|
30
|
+
throw new Error(`Slippage tolerance ${slippageTolerance}% exceeds maximum of ${MAX_SLIPPAGE_TOLERANCE}%. ` +
|
|
31
|
+
'High slippage makes transactions vulnerable to sandwich attacks.');
|
|
30
32
|
}
|
|
31
33
|
const basisPoints = BigInt(Math.floor(slippageTolerance * 100));
|
|
32
34
|
const slippageMultiplier = 10000n - basisPoints;
|
|
@@ -37,6 +37,7 @@ export async function fetchFPMMPoolDetails(publicClient, chainId, pool) {
|
|
|
37
37
|
const liquidityStrategy = activeIndex >= 0 ? knownStrategies[activeIndex] : null;
|
|
38
38
|
// Fetch pricing separately — graceful degradation when FX market is closed
|
|
39
39
|
let pricing = null;
|
|
40
|
+
let pricingUnavailableReason = null;
|
|
40
41
|
let inBand = null;
|
|
41
42
|
try {
|
|
42
43
|
const rebalancingStateResult = await publicClient.readContract({
|
|
@@ -58,8 +59,8 @@ export async function fetchFPMMPoolDetails(publicClient, chainId, pool) {
|
|
|
58
59
|
};
|
|
59
60
|
inBand = priceDifference < BigInt(rebalanceThreshold);
|
|
60
61
|
}
|
|
61
|
-
catch {
|
|
62
|
-
|
|
62
|
+
catch (error) {
|
|
63
|
+
pricingUnavailableReason = `getRebalancingState() reverted: ${error.message ?? 'unknown error'}`;
|
|
63
64
|
}
|
|
64
65
|
return {
|
|
65
66
|
...pool,
|
|
@@ -70,6 +71,7 @@ export async function fetchFPMMPoolDetails(publicClient, chainId, pool) {
|
|
|
70
71
|
reserve1,
|
|
71
72
|
blockTimestampLast,
|
|
72
73
|
pricing,
|
|
74
|
+
pricingUnavailableReason,
|
|
73
75
|
fees: {
|
|
74
76
|
lpFeeBps,
|
|
75
77
|
lpFeePercent: Number(lpFeeBps) / 100,
|
|
@@ -90,7 +92,9 @@ export async function fetchFPMMPoolDetails(publicClient, chainId, pool) {
|
|
|
90
92
|
};
|
|
91
93
|
}
|
|
92
94
|
catch (error) {
|
|
93
|
-
|
|
95
|
+
const wrapped = new Error(`Failed to fetch FPMM pool details for ${pool.poolAddr}: ${error.message}`);
|
|
96
|
+
wrapped.cause = error;
|
|
97
|
+
throw wrapped;
|
|
94
98
|
}
|
|
95
99
|
}
|
|
96
100
|
/**
|
|
@@ -120,7 +124,9 @@ export async function fetchVirtualPoolDetails(publicClient, pool) {
|
|
|
120
124
|
};
|
|
121
125
|
}
|
|
122
126
|
catch (error) {
|
|
123
|
-
|
|
127
|
+
const wrapped = new Error(`Failed to fetch Virtual pool details for ${pool.poolAddr}: ${error.message}`);
|
|
128
|
+
wrapped.cause = error;
|
|
129
|
+
throw wrapped;
|
|
124
130
|
}
|
|
125
131
|
}
|
|
126
132
|
/**
|
|
@@ -44,7 +44,9 @@ export async function fetchFPMMPools(publicClient, chainId) {
|
|
|
44
44
|
return await Promise.all(poolDataPromises);
|
|
45
45
|
}
|
|
46
46
|
catch (error) {
|
|
47
|
-
|
|
47
|
+
const wrapped = new Error(`Failed to fetch FPMM pools: ${error.message}`);
|
|
48
|
+
wrapped.cause = error;
|
|
49
|
+
throw wrapped;
|
|
48
50
|
}
|
|
49
51
|
}
|
|
50
52
|
/**
|
|
@@ -104,6 +106,8 @@ export async function fetchVirtualPools(publicClient, chainId) {
|
|
|
104
106
|
return await Promise.all(poolPromises);
|
|
105
107
|
}
|
|
106
108
|
catch (error) {
|
|
107
|
-
|
|
109
|
+
const wrapped = new Error(`Failed to fetch Virtual pools: ${error.message}`);
|
|
110
|
+
wrapped.cause = error;
|
|
111
|
+
throw wrapped;
|
|
108
112
|
}
|
|
109
113
|
}
|
|
@@ -27,6 +27,7 @@ export class SwapService {
|
|
|
27
27
|
* @param options - Swap configuration options (slippage, deadline)
|
|
28
28
|
* @param route - Optional pre-fetched route for better performance
|
|
29
29
|
* @returns Combined transaction with approval (if needed) and swap params
|
|
30
|
+
* @throws {Error} 'amountIn must be greater than zero' - if amountIn <= 0
|
|
30
31
|
* @throws {Error} 'Slippage tolerance cannot be negative' - if slippageTolerance < 0
|
|
31
32
|
* @throws {Error} 'Slippage tolerance exceeds maximum' - if slippageTolerance > 20%
|
|
32
33
|
* @throws {Error} 'Deadline must be in the future' - if deadline is not a future timestamp
|
|
@@ -54,6 +55,7 @@ export class SwapService {
|
|
|
54
55
|
* ```
|
|
55
56
|
*/
|
|
56
57
|
async buildSwapTransaction(tokenIn, tokenOut, amountIn, recipient, owner, options, route) {
|
|
58
|
+
this.validateAmountIn(amountIn);
|
|
57
59
|
// Validate all address inputs
|
|
58
60
|
validateAddress(tokenIn, 'tokenIn');
|
|
59
61
|
validateAddress(tokenOut, 'tokenOut');
|
|
@@ -77,6 +79,7 @@ export class SwapService {
|
|
|
77
79
|
* @param options - Swap configuration options (slippage, deadline)
|
|
78
80
|
* @param route - Optional pre-fetched route for better performance
|
|
79
81
|
* @returns Detailed swap parameters including transaction data
|
|
82
|
+
* @throws {Error} 'amountIn must be greater than zero' - if amountIn <= 0
|
|
80
83
|
* @throws {Error} 'Slippage tolerance cannot be negative' - if slippageTolerance < 0
|
|
81
84
|
* @throws {Error} 'Slippage tolerance exceeds maximum' - if slippageTolerance > 20%
|
|
82
85
|
* @throws {Error} 'Deadline must be in the future' - if deadline is not a future timestamp
|
|
@@ -98,6 +101,7 @@ export class SwapService {
|
|
|
98
101
|
* ```
|
|
99
102
|
*/
|
|
100
103
|
async buildSwapParams(tokenIn, tokenOut, amountIn, recipient, options, route) {
|
|
104
|
+
this.validateAmountIn(amountIn);
|
|
101
105
|
const deadline = options.deadline;
|
|
102
106
|
if (deadline <= BigInt(Date.now()) / 1000n) {
|
|
103
107
|
throw new Error('Deadline must be in the future');
|
|
@@ -155,6 +159,15 @@ export class SwapService {
|
|
|
155
159
|
args: [owner, routerAddress],
|
|
156
160
|
}));
|
|
157
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Validates that the input amount is strictly positive.
|
|
164
|
+
* @private
|
|
165
|
+
*/
|
|
166
|
+
validateAmountIn(amountIn) {
|
|
167
|
+
if (amountIn <= 0n) {
|
|
168
|
+
throw new Error('amountIn must be greater than zero');
|
|
169
|
+
}
|
|
170
|
+
}
|
|
158
171
|
/**
|
|
159
172
|
* Calculates minimum output amount after applying slippage tolerance
|
|
160
173
|
* @param amountOut - Expected output amount
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BaseError, ContractFunctionRevertedError } from 'viem';
|
|
1
2
|
import { PoolType } from '../../core/types';
|
|
2
3
|
import { FPMM_ABI, BROKER_ABI } from '../../core/abis';
|
|
3
4
|
import { getContractAddress } from '../../core/constants';
|
|
@@ -70,9 +71,16 @@ export class TradingLimitsService {
|
|
|
70
71
|
}
|
|
71
72
|
return calculateTradingLimitsV2(config, state, token);
|
|
72
73
|
}
|
|
73
|
-
catch {
|
|
74
|
-
//
|
|
75
|
-
|
|
74
|
+
catch (error) {
|
|
75
|
+
// Contract reverts indicate limits aren't configured for this token — return empty
|
|
76
|
+
if (error instanceof BaseError) {
|
|
77
|
+
const revertError = error.walk((e) => e instanceof ContractFunctionRevertedError);
|
|
78
|
+
if (revertError instanceof ContractFunctionRevertedError) {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Re-throw non-revert errors (network failures, RPC timeouts, etc.)
|
|
83
|
+
throw error;
|
|
76
84
|
}
|
|
77
85
|
}
|
|
78
86
|
/**
|
|
@@ -143,9 +151,16 @@ export class TradingLimitsService {
|
|
|
143
151
|
const tokenDecimals = 0; // V1 stores values with 0 decimal precision
|
|
144
152
|
return calculateTradingLimitsV1(config, state, token, tokenDecimals);
|
|
145
153
|
}
|
|
146
|
-
catch {
|
|
147
|
-
//
|
|
148
|
-
|
|
154
|
+
catch (error) {
|
|
155
|
+
// Contract reverts indicate limits aren't configured for this token — return empty
|
|
156
|
+
if (error instanceof BaseError) {
|
|
157
|
+
const revertError = error.walk((e) => e instanceof ContractFunctionRevertedError);
|
|
158
|
+
if (revertError instanceof ContractFunctionRevertedError) {
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// Re-throw non-revert errors (network failures, RPC timeouts, etc.)
|
|
163
|
+
throw error;
|
|
149
164
|
}
|
|
150
165
|
}
|
|
151
166
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -47,14 +47,16 @@ import { BorrowService } from './services/borrow';
|
|
|
47
47
|
* const status = await mento.trading.getPoolTradabilityStatus(pool);
|
|
48
48
|
*
|
|
49
49
|
* // Add liquidity to a pool
|
|
50
|
-
* const {
|
|
51
|
-
* poolAddress,
|
|
52
|
-
*
|
|
50
|
+
* const { approvalA, approvalB, addLiquidity } = await mento.liquidity.buildAddLiquidityTransaction({
|
|
51
|
+
* poolAddress, tokenA, amountA, tokenB, amountB, recipient, owner,
|
|
52
|
+
* options: { slippageTolerance: 0.5, deadline: deadlineFromMinutes(5) },
|
|
53
|
+
* });
|
|
53
54
|
*
|
|
54
55
|
* // Add liquidity using a single token (zap in)
|
|
55
|
-
* const { approval, zapIn } = await mento.liquidity.buildZapInTransaction(
|
|
56
|
-
* poolAddress, tokenIn, amountIn, 0.5, recipient, owner,
|
|
57
|
-
*
|
|
56
|
+
* const { approval, zapIn } = await mento.liquidity.buildZapInTransaction({
|
|
57
|
+
* poolAddress, tokenIn, amountIn, amountInSplit: 0.5, recipient, owner,
|
|
58
|
+
* options: { slippageTolerance: 0.5, deadline: deadlineFromMinutes(5) },
|
|
59
|
+
* });
|
|
58
60
|
*/
|
|
59
61
|
export declare class Mento {
|
|
60
62
|
private chainId;
|
package/dist/index.js
CHANGED
|
@@ -66,14 +66,16 @@ const borrow_1 = require("./services/borrow");
|
|
|
66
66
|
* const status = await mento.trading.getPoolTradabilityStatus(pool);
|
|
67
67
|
*
|
|
68
68
|
* // Add liquidity to a pool
|
|
69
|
-
* const {
|
|
70
|
-
* poolAddress,
|
|
71
|
-
*
|
|
69
|
+
* const { approvalA, approvalB, addLiquidity } = await mento.liquidity.buildAddLiquidityTransaction({
|
|
70
|
+
* poolAddress, tokenA, amountA, tokenB, amountB, recipient, owner,
|
|
71
|
+
* options: { slippageTolerance: 0.5, deadline: deadlineFromMinutes(5) },
|
|
72
|
+
* });
|
|
72
73
|
*
|
|
73
74
|
* // Add liquidity using a single token (zap in)
|
|
74
|
-
* const { approval, zapIn } = await mento.liquidity.buildZapInTransaction(
|
|
75
|
-
* poolAddress, tokenIn, amountIn, 0.5, recipient, owner,
|
|
76
|
-
*
|
|
75
|
+
* const { approval, zapIn } = await mento.liquidity.buildZapInTransaction({
|
|
76
|
+
* poolAddress, tokenIn, amountIn, amountInSplit: 0.5, recipient, owner,
|
|
77
|
+
* options: { slippageTolerance: 0.5, deadline: deadlineFromMinutes(5) },
|
|
78
|
+
* });
|
|
77
79
|
*/
|
|
78
80
|
class Mento {
|
|
79
81
|
constructor(chainId, tokens, pools, routes, quotes, swap, trading, liquidity, borrow) {
|
|
@@ -15,4 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./BorrowService"), exports);
|
|
18
|
+
__exportStar(require("./borrowHelpers"), exports);
|
|
19
|
+
__exportStar(require("./borrowMath"), exports);
|
|
18
20
|
//# sourceMappingURL=index.js.map
|
package/dist/services/index.d.ts
CHANGED
package/dist/services/index.js
CHANGED
|
@@ -20,4 +20,6 @@ __exportStar(require("./quotes"), exports);
|
|
|
20
20
|
__exportStar(require("./swap"), exports);
|
|
21
21
|
__exportStar(require("./trading"), exports);
|
|
22
22
|
__exportStar(require("./borrow"), exports);
|
|
23
|
+
__exportStar(require("./tokens"), exports);
|
|
24
|
+
__exportStar(require("./liquidity"), exports);
|
|
23
25
|
//# sourceMappingURL=index.js.map
|
|
@@ -29,11 +29,13 @@ async function getAllowance(publicClient, token, owner, chainId) {
|
|
|
29
29
|
}));
|
|
30
30
|
}
|
|
31
31
|
function calculateMinAmount(amount, slippageTolerance) {
|
|
32
|
+
const MAX_SLIPPAGE_TOLERANCE = 20;
|
|
32
33
|
if (slippageTolerance < 0) {
|
|
33
34
|
throw new Error('Slippage tolerance cannot be negative');
|
|
34
35
|
}
|
|
35
|
-
if (slippageTolerance >
|
|
36
|
-
throw new Error(
|
|
36
|
+
if (slippageTolerance > MAX_SLIPPAGE_TOLERANCE) {
|
|
37
|
+
throw new Error(`Slippage tolerance ${slippageTolerance}% exceeds maximum of ${MAX_SLIPPAGE_TOLERANCE}%. ` +
|
|
38
|
+
'High slippage makes transactions vulnerable to sandwich attacks.');
|
|
37
39
|
}
|
|
38
40
|
const basisPoints = BigInt(Math.floor(slippageTolerance * 100));
|
|
39
41
|
const slippageMultiplier = 10000n - basisPoints;
|
|
@@ -41,6 +41,7 @@ async function fetchFPMMPoolDetails(publicClient, chainId, pool) {
|
|
|
41
41
|
const liquidityStrategy = activeIndex >= 0 ? knownStrategies[activeIndex] : null;
|
|
42
42
|
// Fetch pricing separately — graceful degradation when FX market is closed
|
|
43
43
|
let pricing = null;
|
|
44
|
+
let pricingUnavailableReason = null;
|
|
44
45
|
let inBand = null;
|
|
45
46
|
try {
|
|
46
47
|
const rebalancingStateResult = await publicClient.readContract({
|
|
@@ -62,8 +63,8 @@ async function fetchFPMMPoolDetails(publicClient, chainId, pool) {
|
|
|
62
63
|
};
|
|
63
64
|
inBand = priceDifference < BigInt(rebalanceThreshold);
|
|
64
65
|
}
|
|
65
|
-
catch {
|
|
66
|
-
|
|
66
|
+
catch (error) {
|
|
67
|
+
pricingUnavailableReason = `getRebalancingState() reverted: ${error.message ?? 'unknown error'}`;
|
|
67
68
|
}
|
|
68
69
|
return {
|
|
69
70
|
...pool,
|
|
@@ -74,6 +75,7 @@ async function fetchFPMMPoolDetails(publicClient, chainId, pool) {
|
|
|
74
75
|
reserve1,
|
|
75
76
|
blockTimestampLast,
|
|
76
77
|
pricing,
|
|
78
|
+
pricingUnavailableReason,
|
|
77
79
|
fees: {
|
|
78
80
|
lpFeeBps,
|
|
79
81
|
lpFeePercent: Number(lpFeeBps) / 100,
|
|
@@ -94,7 +96,9 @@ async function fetchFPMMPoolDetails(publicClient, chainId, pool) {
|
|
|
94
96
|
};
|
|
95
97
|
}
|
|
96
98
|
catch (error) {
|
|
97
|
-
|
|
99
|
+
const wrapped = new Error(`Failed to fetch FPMM pool details for ${pool.poolAddr}: ${error.message}`);
|
|
100
|
+
wrapped.cause = error;
|
|
101
|
+
throw wrapped;
|
|
98
102
|
}
|
|
99
103
|
}
|
|
100
104
|
/**
|
|
@@ -124,7 +128,9 @@ async function fetchVirtualPoolDetails(publicClient, pool) {
|
|
|
124
128
|
};
|
|
125
129
|
}
|
|
126
130
|
catch (error) {
|
|
127
|
-
|
|
131
|
+
const wrapped = new Error(`Failed to fetch Virtual pool details for ${pool.poolAddr}: ${error.message}`);
|
|
132
|
+
wrapped.cause = error;
|
|
133
|
+
throw wrapped;
|
|
128
134
|
}
|
|
129
135
|
}
|
|
130
136
|
/**
|
|
@@ -48,7 +48,9 @@ async function fetchFPMMPools(publicClient, chainId) {
|
|
|
48
48
|
return await Promise.all(poolDataPromises);
|
|
49
49
|
}
|
|
50
50
|
catch (error) {
|
|
51
|
-
|
|
51
|
+
const wrapped = new Error(`Failed to fetch FPMM pools: ${error.message}`);
|
|
52
|
+
wrapped.cause = error;
|
|
53
|
+
throw wrapped;
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
/**
|
|
@@ -108,7 +110,9 @@ async function fetchVirtualPools(publicClient, chainId) {
|
|
|
108
110
|
return await Promise.all(poolPromises);
|
|
109
111
|
}
|
|
110
112
|
catch (error) {
|
|
111
|
-
|
|
113
|
+
const wrapped = new Error(`Failed to fetch Virtual pools: ${error.message}`);
|
|
114
|
+
wrapped.cause = error;
|
|
115
|
+
throw wrapped;
|
|
112
116
|
}
|
|
113
117
|
}
|
|
114
118
|
//# sourceMappingURL=poolDiscovery.js.map
|
|
@@ -85,6 +85,7 @@ export declare class SwapService {
|
|
|
85
85
|
* @param options - Swap configuration options (slippage, deadline)
|
|
86
86
|
* @param route - Optional pre-fetched route for better performance
|
|
87
87
|
* @returns Combined transaction with approval (if needed) and swap params
|
|
88
|
+
* @throws {Error} 'amountIn must be greater than zero' - if amountIn <= 0
|
|
88
89
|
* @throws {Error} 'Slippage tolerance cannot be negative' - if slippageTolerance < 0
|
|
89
90
|
* @throws {Error} 'Slippage tolerance exceeds maximum' - if slippageTolerance > 20%
|
|
90
91
|
* @throws {Error} 'Deadline must be in the future' - if deadline is not a future timestamp
|
|
@@ -123,6 +124,7 @@ export declare class SwapService {
|
|
|
123
124
|
* @param options - Swap configuration options (slippage, deadline)
|
|
124
125
|
* @param route - Optional pre-fetched route for better performance
|
|
125
126
|
* @returns Detailed swap parameters including transaction data
|
|
127
|
+
* @throws {Error} 'amountIn must be greater than zero' - if amountIn <= 0
|
|
126
128
|
* @throws {Error} 'Slippage tolerance cannot be negative' - if slippageTolerance < 0
|
|
127
129
|
* @throws {Error} 'Slippage tolerance exceeds maximum' - if slippageTolerance > 20%
|
|
128
130
|
* @throws {Error} 'Deadline must be in the future' - if deadline is not a future timestamp
|
|
@@ -154,6 +156,11 @@ export declare class SwapService {
|
|
|
154
156
|
* @private
|
|
155
157
|
*/
|
|
156
158
|
private getAllowance;
|
|
159
|
+
/**
|
|
160
|
+
* Validates that the input amount is strictly positive.
|
|
161
|
+
* @private
|
|
162
|
+
*/
|
|
163
|
+
private validateAmountIn;
|
|
157
164
|
/**
|
|
158
165
|
* Calculates minimum output amount after applying slippage tolerance
|
|
159
166
|
* @param amountOut - Expected output amount
|
|
@@ -30,6 +30,7 @@ class SwapService {
|
|
|
30
30
|
* @param options - Swap configuration options (slippage, deadline)
|
|
31
31
|
* @param route - Optional pre-fetched route for better performance
|
|
32
32
|
* @returns Combined transaction with approval (if needed) and swap params
|
|
33
|
+
* @throws {Error} 'amountIn must be greater than zero' - if amountIn <= 0
|
|
33
34
|
* @throws {Error} 'Slippage tolerance cannot be negative' - if slippageTolerance < 0
|
|
34
35
|
* @throws {Error} 'Slippage tolerance exceeds maximum' - if slippageTolerance > 20%
|
|
35
36
|
* @throws {Error} 'Deadline must be in the future' - if deadline is not a future timestamp
|
|
@@ -57,6 +58,7 @@ class SwapService {
|
|
|
57
58
|
* ```
|
|
58
59
|
*/
|
|
59
60
|
async buildSwapTransaction(tokenIn, tokenOut, amountIn, recipient, owner, options, route) {
|
|
61
|
+
this.validateAmountIn(amountIn);
|
|
60
62
|
// Validate all address inputs
|
|
61
63
|
(0, validation_1.validateAddress)(tokenIn, 'tokenIn');
|
|
62
64
|
(0, validation_1.validateAddress)(tokenOut, 'tokenOut');
|
|
@@ -80,6 +82,7 @@ class SwapService {
|
|
|
80
82
|
* @param options - Swap configuration options (slippage, deadline)
|
|
81
83
|
* @param route - Optional pre-fetched route for better performance
|
|
82
84
|
* @returns Detailed swap parameters including transaction data
|
|
85
|
+
* @throws {Error} 'amountIn must be greater than zero' - if amountIn <= 0
|
|
83
86
|
* @throws {Error} 'Slippage tolerance cannot be negative' - if slippageTolerance < 0
|
|
84
87
|
* @throws {Error} 'Slippage tolerance exceeds maximum' - if slippageTolerance > 20%
|
|
85
88
|
* @throws {Error} 'Deadline must be in the future' - if deadline is not a future timestamp
|
|
@@ -101,6 +104,7 @@ class SwapService {
|
|
|
101
104
|
* ```
|
|
102
105
|
*/
|
|
103
106
|
async buildSwapParams(tokenIn, tokenOut, amountIn, recipient, options, route) {
|
|
107
|
+
this.validateAmountIn(amountIn);
|
|
104
108
|
const deadline = options.deadline;
|
|
105
109
|
if (deadline <= BigInt(Date.now()) / 1000n) {
|
|
106
110
|
throw new Error('Deadline must be in the future');
|
|
@@ -158,6 +162,15 @@ class SwapService {
|
|
|
158
162
|
args: [owner, routerAddress],
|
|
159
163
|
}));
|
|
160
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Validates that the input amount is strictly positive.
|
|
167
|
+
* @private
|
|
168
|
+
*/
|
|
169
|
+
validateAmountIn(amountIn) {
|
|
170
|
+
if (amountIn <= 0n) {
|
|
171
|
+
throw new Error('amountIn must be greater than zero');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
161
174
|
/**
|
|
162
175
|
* Calculates minimum output amount after applying slippage tolerance
|
|
163
176
|
* @param amountOut - Expected output amount
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TradingLimitsService = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
4
5
|
const types_1 = require("../../core/types");
|
|
5
6
|
const abis_1 = require("../../core/abis");
|
|
6
7
|
const constants_1 = require("../../core/constants");
|
|
@@ -73,9 +74,16 @@ class TradingLimitsService {
|
|
|
73
74
|
}
|
|
74
75
|
return (0, tradingLimits_1.calculateTradingLimitsV2)(config, state, token);
|
|
75
76
|
}
|
|
76
|
-
catch {
|
|
77
|
-
//
|
|
78
|
-
|
|
77
|
+
catch (error) {
|
|
78
|
+
// Contract reverts indicate limits aren't configured for this token — return empty
|
|
79
|
+
if (error instanceof viem_1.BaseError) {
|
|
80
|
+
const revertError = error.walk((e) => e instanceof viem_1.ContractFunctionRevertedError);
|
|
81
|
+
if (revertError instanceof viem_1.ContractFunctionRevertedError) {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Re-throw non-revert errors (network failures, RPC timeouts, etc.)
|
|
86
|
+
throw error;
|
|
79
87
|
}
|
|
80
88
|
}
|
|
81
89
|
/**
|
|
@@ -146,9 +154,16 @@ class TradingLimitsService {
|
|
|
146
154
|
const tokenDecimals = 0; // V1 stores values with 0 decimal precision
|
|
147
155
|
return (0, tradingLimits_1.calculateTradingLimitsV1)(config, state, token, tokenDecimals);
|
|
148
156
|
}
|
|
149
|
-
catch {
|
|
150
|
-
//
|
|
151
|
-
|
|
157
|
+
catch (error) {
|
|
158
|
+
// Contract reverts indicate limits aren't configured for this token — return empty
|
|
159
|
+
if (error instanceof viem_1.BaseError) {
|
|
160
|
+
const revertError = error.walk((e) => e instanceof viem_1.ContractFunctionRevertedError);
|
|
161
|
+
if (revertError instanceof viem_1.ContractFunctionRevertedError) {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// Re-throw non-revert errors (network failures, RPC timeouts, etc.)
|
|
166
|
+
throw error;
|
|
152
167
|
}
|
|
153
168
|
}
|
|
154
169
|
}
|
package/package.json
CHANGED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
export interface ContractCallOptions {
|
|
2
|
-
address: string;
|
|
3
|
-
abi: string[] | unknown[];
|
|
4
|
-
functionName: string;
|
|
5
|
-
args?: unknown[];
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Contract write options for building call parameters
|
|
9
|
-
*
|
|
10
|
-
* Configuration for building transaction call parameters for write operations.
|
|
11
|
-
* These parameters can be used by consumers to execute transactions themselves.
|
|
12
|
-
*/
|
|
13
|
-
export interface ContractWriteOptions extends ContractCallOptions {
|
|
14
|
-
/**
|
|
15
|
-
* Optional gas limit override
|
|
16
|
-
* If not provided, will be estimated automatically
|
|
17
|
-
*/
|
|
18
|
-
gasLimit?: bigint;
|
|
19
|
-
/**
|
|
20
|
-
* Transaction value in wei
|
|
21
|
-
* For payable functions only
|
|
22
|
-
*/
|
|
23
|
-
value?: bigint;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Call parameters for executing a contract transaction
|
|
27
|
-
*
|
|
28
|
-
* Contains all the data needed to execute a contract call.
|
|
29
|
-
* Consumers can use these parameters with their own wallet/signer to execute transactions.
|
|
30
|
-
*/
|
|
31
|
-
export interface CallParams {
|
|
32
|
-
/**
|
|
33
|
-
* Contract address to call
|
|
34
|
-
*/
|
|
35
|
-
to: string;
|
|
36
|
-
/**
|
|
37
|
-
* Encoded function call data (hex string)
|
|
38
|
-
*/
|
|
39
|
-
data: string;
|
|
40
|
-
/**
|
|
41
|
-
* Transaction value in wei (hex string with 0x prefix)
|
|
42
|
-
*/
|
|
43
|
-
value: string;
|
|
44
|
-
}
|
|
45
|
-
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|