@gala-chain/launchpad-sdk 3.31.0 → 4.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/API.md +524 -4
- package/CHANGELOG.md +92 -0
- package/EXAMPLES.md +845 -0
- package/README.md +421 -14
- package/dist/LaunchpadSDK.d.ts +435 -18
- package/dist/LaunchpadSDK.d.ts.map +1 -1
- package/dist/api/LaunchpadAPI.d.ts.map +1 -1
- package/dist/api/dto/TransferTokenDto.d.ts.map +1 -1
- package/dist/constants/version.generated.d.ts +1 -1
- package/dist/constants/version.generated.d.ts.map +1 -1
- package/dist/helpers/sdk.d.ts +1 -0
- package/dist/helpers/sdk.d.ts.map +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +10 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.js +1 -1
- package/dist/services/BaseService.d.ts +32 -3
- package/dist/services/BaseService.d.ts.map +1 -1
- package/dist/services/BundleService.d.ts.map +1 -1
- package/dist/services/CommentService.d.ts.map +1 -1
- package/dist/services/DexBackendClient.d.ts +46 -0
- package/dist/services/DexBackendClient.d.ts.map +1 -0
- package/dist/services/DexPoolService.d.ts +54 -12
- package/dist/services/DexPoolService.d.ts.map +1 -1
- package/dist/services/DexQuoteService.d.ts +288 -0
- package/dist/services/DexQuoteService.d.ts.map +1 -0
- package/dist/services/DexService.d.ts.map +1 -1
- package/dist/services/FaucetService.d.ts.map +1 -1
- package/dist/services/GSwapService.d.ts +519 -44
- package/dist/services/GSwapService.d.ts.map +1 -1
- package/dist/services/GalaChainGatewayClient.d.ts +89 -0
- package/dist/services/GalaChainGatewayClient.d.ts.map +1 -0
- package/dist/services/GalaChainService.d.ts +26 -0
- package/dist/services/GalaChainService.d.ts.map +1 -1
- package/dist/services/ImageService.d.ts.map +1 -1
- package/dist/services/PoolService.d.ts.map +1 -1
- package/dist/services/PoolStateManager.d.ts +176 -0
- package/dist/services/PoolStateManager.d.ts.map +1 -0
- package/dist/services/PriceHistoryService.d.ts +4 -5
- package/dist/services/PriceHistoryService.d.ts.map +1 -1
- package/dist/services/SignatureService.d.ts +15 -38
- package/dist/services/SignatureService.d.ts.map +1 -1
- package/dist/services/TokenClassKeyService.d.ts +3 -4
- package/dist/services/TokenClassKeyService.d.ts.map +1 -1
- package/dist/services/TokenMetadataCache.d.ts +2 -2
- package/dist/services/TokenMetadataCache.d.ts.map +1 -1
- package/dist/services/TokenMetadataService.d.ts +3 -3
- package/dist/services/TokenMetadataService.d.ts.map +1 -1
- package/dist/services/TokenResolverService.d.ts +2 -2
- package/dist/services/TokenResolverService.d.ts.map +1 -1
- package/dist/services/TradeService.d.ts.map +1 -1
- package/dist/services/UserService.d.ts.map +1 -1
- package/dist/services/WebSocketService.d.ts +100 -2
- package/dist/services/WebSocketService.d.ts.map +1 -1
- package/dist/services/__mocks__/logger.mock.d.ts +17 -0
- package/dist/services/__mocks__/logger.mock.d.ts.map +1 -0
- package/dist/types/common.d.ts +2 -0
- package/dist/types/common.d.ts.map +1 -1
- package/dist/types/composite-pool.dto.d.ts +103 -0
- package/dist/types/composite-pool.dto.d.ts.map +1 -0
- package/dist/types/dex-pool.dto.d.ts +56 -5
- package/dist/types/dex-pool.dto.d.ts.map +1 -1
- package/dist/types/dto.d.ts +6 -6
- package/dist/types/dto.d.ts.map +1 -1
- package/dist/types/eip712-types.d.ts +140 -0
- package/dist/types/eip712-types.d.ts.map +1 -0
- package/dist/types/galachain-api.types.d.ts +206 -0
- package/dist/types/galachain-api.types.d.ts.map +1 -0
- package/dist/types/gswap-responses.types.d.ts +366 -0
- package/dist/types/gswap-responses.types.d.ts.map +1 -0
- package/dist/types/gswap.dto.d.ts +58 -3
- package/dist/types/gswap.dto.d.ts.map +1 -1
- package/dist/types/launchpad.dto.d.ts +57 -0
- package/dist/types/launchpad.dto.d.ts.map +1 -1
- package/dist/types/pool-state-delta.dto.d.ts +246 -0
- package/dist/types/pool-state-delta.dto.d.ts.map +1 -0
- package/dist/types/pool-state-manager-config.dto.d.ts +103 -0
- package/dist/types/pool-state-manager-config.dto.d.ts.map +1 -0
- package/dist/utils/SignatureHelper.d.ts.map +1 -1
- package/dist/utils/auto-pagination.d.ts +206 -0
- package/dist/utils/auto-pagination.d.ts.map +1 -0
- package/dist/utils/bignumber-helpers.d.ts +336 -0
- package/dist/utils/bignumber-helpers.d.ts.map +1 -0
- package/dist/utils/bignumber-pool-cache.d.ts +169 -0
- package/dist/utils/bignumber-pool-cache.d.ts.map +1 -0
- package/dist/utils/composite-pool-converter.d.ts +121 -0
- package/dist/utils/composite-pool-converter.d.ts.map +1 -0
- package/dist/utils/errors.d.ts +86 -0
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/http.d.ts.map +1 -1
- package/dist/utils/load-env.d.ts +31 -0
- package/dist/utils/load-env.d.ts.map +1 -0
- package/dist/utils/pool-pair-parser.d.ts +55 -0
- package/dist/utils/pool-pair-parser.d.ts.map +1 -0
- package/dist/utils/pool-state-validator.d.ts +207 -0
- package/dist/utils/pool-state-validator.d.ts.map +1 -0
- package/dist/utils/position-filters.d.ts +253 -0
- package/dist/utils/position-filters.d.ts.map +1 -0
- package/dist/utils/swap-delta-calculator.d.ts +231 -0
- package/dist/utils/swap-delta-calculator.d.ts.map +1 -0
- package/dist/utils/tick-crossing-handler.d.ts +250 -0
- package/dist/utils/tick-crossing-handler.d.ts.map +1 -0
- package/dist/utils/token-format-converter.d.ts +85 -6
- package/dist/utils/token-format-converter.d.ts.map +1 -1
- package/dist/utils/token-parser.d.ts +235 -0
- package/dist/utils/token-parser.d.ts.map +1 -0
- package/dist/utils/tokenNormalizer.d.ts +66 -0
- package/dist/utils/tokenNormalizer.d.ts.map +1 -1
- package/dist/utils/transfer-validation.d.ts.map +1 -1
- package/dist/utils/validation-helpers.d.ts +75 -0
- package/dist/utils/validation-helpers.d.ts.map +1 -1
- package/package.json +36 -15
- package/dist/utils/tokenFormatConverter.d.ts +0 -53
- package/dist/utils/tokenFormatConverter.d.ts.map +0 -1
|
@@ -1,31 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
* GSwapService - Wraps @gala-chain/gswap-sdk for DEX trading integration
|
|
3
|
-
*
|
|
4
|
-
* Provides high-level interface to GalaSwap SDK for:
|
|
5
|
-
* - Token swapping with slippage protection
|
|
6
|
-
* - Quote generation (exact input/output)
|
|
7
|
-
* - User asset queries
|
|
8
|
-
* - Pool information
|
|
9
|
-
* - Real-time transaction monitoring
|
|
10
|
-
*/
|
|
1
|
+
import { LoggerBase } from './BaseService';
|
|
11
2
|
import { WebSocketService } from './WebSocketService';
|
|
12
|
-
import
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
import { DexQuoteService } from './DexQuoteService';
|
|
4
|
+
import type { SwapQuoteParams, SwapQuoteResult, ExecuteSwapParams, ExecuteSwapResult, UserAsset, PoolInfo, GSwapServiceConfig, GSwapPosition, PoolSlot0Data, PoolPriceData, GetLiquidityPositionsOptions, GetLiquidityPositionsResult } from '../types/gswap.dto';
|
|
5
|
+
import type { TokenClassKey } from '../types/common';
|
|
6
|
+
export declare class GSwapService extends LoggerBase {
|
|
7
|
+
private gatewayClient;
|
|
8
|
+
private dexBackendClient;
|
|
15
9
|
private tokenConverter;
|
|
16
10
|
private webSocketService;
|
|
17
|
-
private
|
|
18
|
-
|
|
11
|
+
private dexQuoteService;
|
|
12
|
+
private getWalletAddress;
|
|
13
|
+
private galaChainBaseUrl;
|
|
14
|
+
private bundlerBaseUrl;
|
|
15
|
+
private gatewayBaseUrl;
|
|
16
|
+
private privateKey;
|
|
17
|
+
private pricingConcurrency;
|
|
18
|
+
constructor(config: GSwapServiceConfig, webSocketService: WebSocketService, dexQuoteService?: DexQuoteService);
|
|
19
|
+
/**
|
|
20
|
+
* Set the pricing concurrency level for liquidity position pricing operations
|
|
21
|
+
* This is set once at SDK initialization and applies to all pricing operations
|
|
22
|
+
* @param concurrency Number of concurrent pricing requests (default: 5, max recommended: 20)
|
|
23
|
+
*/
|
|
24
|
+
setPricingConcurrency(concurrency: number): void;
|
|
19
25
|
/**
|
|
20
26
|
* Get swap quote for exact input (spending known amount)
|
|
27
|
+
*
|
|
28
|
+
* Uses DexQuoteService with dynamic fee tier discovery ([3000, 500, 10000]).
|
|
29
|
+
* NEVER falls back - if all fee tiers fail, throws loudly to expose the real problem.
|
|
30
|
+
*
|
|
31
|
+
* Guarantees: All quotes are ALWAYS fresh (never cached) to prevent MEV in volatile markets.
|
|
32
|
+
*
|
|
21
33
|
* @param params Quote request with fromToken, toToken, and input amount
|
|
22
|
-
* @returns Quote with estimated output and fees
|
|
34
|
+
* @returns Quote with estimated output, actual price impact, and fees
|
|
35
|
+
* @throws GSwapQuoteError if DexQuoteService not configured or all fee tiers fail
|
|
23
36
|
*/
|
|
24
37
|
getSwapQuoteExactInput(params: SwapQuoteParams): Promise<SwapQuoteResult>;
|
|
25
38
|
/**
|
|
26
39
|
* Get swap quote for exact output (receive known amount)
|
|
40
|
+
*
|
|
41
|
+
* Uses DexQuoteService with dynamic fee tier discovery ([3000, 500, 10000]).
|
|
42
|
+
* NEVER falls back - if all fee tiers fail, throws loudly to expose the real problem.
|
|
43
|
+
*
|
|
44
|
+
* Guarantees: All quotes are ALWAYS fresh (never cached) to prevent MEV in volatile markets.
|
|
45
|
+
*
|
|
27
46
|
* @param params Quote request with fromToken, toToken, and desired output
|
|
28
|
-
* @returns Quote with required input and fees
|
|
47
|
+
* @returns Quote with required input, actual price impact, and fees
|
|
48
|
+
* @throws GSwapQuoteError if DexQuoteService not configured or all fee tiers fail
|
|
29
49
|
*/
|
|
30
50
|
getSwapQuoteExactOutput(params: SwapQuoteParams): Promise<SwapQuoteResult>;
|
|
31
51
|
/**
|
|
@@ -37,17 +57,21 @@ export declare class GSwapService {
|
|
|
37
57
|
/**
|
|
38
58
|
* Get user's token assets/balances
|
|
39
59
|
*
|
|
40
|
-
* Fetches all tokens held by a user from the GSwap API with
|
|
60
|
+
* Fetches all tokens held by a user from the GSwap API with graceful handling
|
|
61
|
+
* of token conversion. Assets that cannot be converted are skipped with debug logging.
|
|
41
62
|
*
|
|
42
63
|
* @param walletAddress User's wallet address (e.g., "eth|0x1234...")
|
|
43
|
-
* @returns Array of user assets with balances
|
|
44
|
-
* @throws {GSwapAssetError} When
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
64
|
+
* @returns Array of user assets with balances (skips unconvertible assets)
|
|
65
|
+
* @throws {GSwapAssetError} When the API call itself fails or wallet address is invalid
|
|
66
|
+
* @since v3.30.0 Now gracefully skips unconvertible assets instead of throwing
|
|
67
|
+
*/
|
|
68
|
+
getUserAssets(walletAddress: string, page?: number, limit?: number): Promise<UserAsset[]>;
|
|
69
|
+
/**
|
|
70
|
+
* Get all user assets across all pages (auto-paginated)
|
|
71
|
+
* @param walletAddress Wallet address to fetch assets for
|
|
72
|
+
* @returns All user assets across all pages
|
|
49
73
|
*/
|
|
50
|
-
|
|
74
|
+
getAllUserAssets(walletAddress: string): Promise<UserAsset[]>;
|
|
51
75
|
/**
|
|
52
76
|
* Get pool information for token pair
|
|
53
77
|
* @param tokenA First token
|
|
@@ -62,7 +86,38 @@ export declare class GSwapService {
|
|
|
62
86
|
* @param bookmark Pagination bookmark
|
|
63
87
|
* @returns Array of liquidity positions with full type safety
|
|
64
88
|
*/
|
|
65
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Chunk array into smaller batches for concurrent processing
|
|
91
|
+
* @private
|
|
92
|
+
*/
|
|
93
|
+
private chunkArray;
|
|
94
|
+
/**
|
|
95
|
+
* Fetch real-time pricing for liquidity positions
|
|
96
|
+
* Deduplicates pools and processes in chunks for optimal performance
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
private fetchPositionPrices;
|
|
100
|
+
/**
|
|
101
|
+
* Normalize GSwap API position response to match LiquidityPosition interface
|
|
102
|
+
* Handles different field naming conventions from the API
|
|
103
|
+
*/
|
|
104
|
+
private normalizePositionResponse;
|
|
105
|
+
/**
|
|
106
|
+
* Parse token string flexibly - supports both delimited and simple formats
|
|
107
|
+
* @param tokenString Token in format "GALA" or "GALA|Unit|none|none"
|
|
108
|
+
* @returns TokenClassKey object
|
|
109
|
+
*/
|
|
110
|
+
private parseTokenFlexible;
|
|
111
|
+
getUserLiquidityPositions(ownerAddress: string, limit?: number, bookmark?: string, options?: GetLiquidityPositionsOptions): Promise<GetLiquidityPositionsResult>;
|
|
112
|
+
/**
|
|
113
|
+
* Get ALL user liquidity positions with automatic pagination
|
|
114
|
+
* Automatically fetches all pages of liquidity positions for a wallet
|
|
115
|
+
* Uses bookmark-based cursor pagination internally
|
|
116
|
+
* @param ownerAddress User's wallet address
|
|
117
|
+
* @param options Optional configuration for pricing enrichment
|
|
118
|
+
* @returns All liquidity positions combined from all pages, optionally with pricing data
|
|
119
|
+
*/
|
|
120
|
+
getAllSwapUserLiquidityPositions(ownerAddress: string, options?: GetLiquidityPositionsOptions): Promise<GSwapPosition[] | GetLiquidityPositionsResult>;
|
|
66
121
|
/**
|
|
67
122
|
* Get a specific liquidity position
|
|
68
123
|
* @param ownerAddress User's wallet address
|
|
@@ -83,6 +138,25 @@ export declare class GSwapService {
|
|
|
83
138
|
* @returns Detailed position information with full type safety
|
|
84
139
|
*/
|
|
85
140
|
getLiquidityPositionById(ownerAddress: string, positionId: string): Promise<GSwapPosition>;
|
|
141
|
+
/**
|
|
142
|
+
* Fetch a single liquidity position using ONLY the compound key (direct HTTP call)
|
|
143
|
+
* Most efficient when you already have all position parameters
|
|
144
|
+
* @param params Position parameters - token0, token1, fee, tickLower, tickUpper, owner (ALL REQUIRED)
|
|
145
|
+
* @returns Detailed position information with full type safety
|
|
146
|
+
* @remarks
|
|
147
|
+
* - Does NOT use positionId (not needed or accepted)
|
|
148
|
+
* - Single direct HTTP call to GalaChain (no GSwap SDK)
|
|
149
|
+
* - Best for cases where caller has full position parameters
|
|
150
|
+
* - Requires: token0, token1, fee (500|3000|10000), tickLower, tickUpper, owner
|
|
151
|
+
*/
|
|
152
|
+
fetchSwapPositionDirect(params: {
|
|
153
|
+
token0: string;
|
|
154
|
+
token1: string;
|
|
155
|
+
fee: number;
|
|
156
|
+
tickLower: number;
|
|
157
|
+
tickUpper: number;
|
|
158
|
+
owner: string;
|
|
159
|
+
}): Promise<GSwapPosition>;
|
|
86
160
|
/**
|
|
87
161
|
* Estimate tokens received from removing liquidity
|
|
88
162
|
* @param args Removal estimation parameters
|
|
@@ -113,13 +187,75 @@ export declare class GSwapService {
|
|
|
113
187
|
amount1Min?: string;
|
|
114
188
|
}): Promise<any>;
|
|
115
189
|
/**
|
|
116
|
-
* Add liquidity using tick ranges (
|
|
117
|
-
*
|
|
118
|
-
*
|
|
190
|
+
* Add liquidity to GSwap pool using precise tick ranges (concentrated liquidity)
|
|
191
|
+
*
|
|
192
|
+
* **CRITICAL: WebSocket-First Pattern**
|
|
193
|
+
* This method ensures WebSocket connection is established BEFORE submitting the
|
|
194
|
+
* transaction to prevent race conditions where the transaction confirms before
|
|
195
|
+
* the listener becomes active. This is essential because:
|
|
196
|
+
*
|
|
197
|
+
* 1. WebSocket listener created IMMEDIATELY after transactionId received
|
|
198
|
+
* 2. Socket.IO buffers events for registered listeners
|
|
199
|
+
* 3. If listener not active when transaction confirms, confirmation is missed
|
|
200
|
+
* 4. Position discovery retry logic handles indexing delays (up to 11 seconds)
|
|
201
|
+
*
|
|
202
|
+
* **Position Discovery Flow**
|
|
203
|
+
* After bundler submits transaction:
|
|
204
|
+
* 1. WebSocket listener monitors for on-chain confirmation (~2-3 seconds)
|
|
205
|
+
* 2. Once confirmed, waits for backend position indexing (5-11 seconds)
|
|
206
|
+
* 3. Queries positions API up to 3 times with exponential backoff
|
|
207
|
+
* 4. Matches position by token pair + fee tier (handles multiple positions)
|
|
208
|
+
* 5. Fetches full position details for complete data
|
|
209
|
+
*
|
|
210
|
+
* **Token Format Support**
|
|
211
|
+
* - String tokens: 'GALA', 'GUSDC' → auto-converted to TokenClassKey
|
|
212
|
+
* - TokenClassKey: { collection, category, type, additionalKey }
|
|
213
|
+
* - Both formats normalized to TokenClassKey for bundler submission
|
|
214
|
+
*
|
|
215
|
+
* **Tick Range Details**
|
|
216
|
+
* - Supports negative ticks for full concentrated liquidity ranges
|
|
217
|
+
* - Example: tickLower: -1080, tickUpper: 900
|
|
218
|
+
* - Must be multiples of tickSpacing (determined by fee tier)
|
|
219
|
+
* - Fee 500bps: tickSpacing=10, Fee 3000bps: tickSpacing=60, Fee 10000bps: tickSpacing=200
|
|
220
|
+
*
|
|
221
|
+
* @param args Liquidity addition parameters
|
|
222
|
+
* @param args.token0 First token (string or TokenClassKey)
|
|
223
|
+
* @param args.token1 Second token (string or TokenClassKey)
|
|
224
|
+
* @param args.fee Fee tier in basis points (500, 3000, or 10000)
|
|
225
|
+
* @param args.tickLower Lower tick boundary (supports negative values)
|
|
226
|
+
* @param args.tickUpper Upper tick boundary
|
|
227
|
+
* @param args.amount0Desired Desired amount of token0
|
|
228
|
+
* @param args.amount1Desired Desired amount of token1
|
|
229
|
+
* @param args.amount0Min Optional minimum token0 (default: '0')
|
|
230
|
+
* @param args.amount1Min Optional minimum token1 (default: '0')
|
|
231
|
+
*
|
|
232
|
+
* @returns Promise<GSwapAddLiquidityResult> Transaction result with:
|
|
233
|
+
* - transactionId: Bundler transaction ID
|
|
234
|
+
* - positionId: GSwap position ID (discovered from on-chain data)
|
|
235
|
+
* - status: Transaction status from WebSocket
|
|
236
|
+
* - liquidity: Position liquidity amount
|
|
237
|
+
* - amount0/amount1: Actual amounts added
|
|
238
|
+
* - wait(): Async method to wait for confirmation
|
|
239
|
+
*
|
|
240
|
+
* @throws GSwapPositionError if private key unavailable or bundler submission fails
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* // Add liquidity with explicit tick range
|
|
244
|
+
* const result = await sdk.addSwapLiquidityByTicks({
|
|
245
|
+
* token0: 'GALA',
|
|
246
|
+
* token1: 'GUSDC',
|
|
247
|
+
* fee: 3000,
|
|
248
|
+
* tickLower: -1080, // Supports negative ticks
|
|
249
|
+
* tickUpper: 900,
|
|
250
|
+
* amount0Desired: '100',
|
|
251
|
+
* amount1Desired: '100',
|
|
252
|
+
* });
|
|
253
|
+
* console.log('Position ID:', result.positionId);
|
|
254
|
+
* await result.wait(); // Wait for on-chain confirmation
|
|
119
255
|
*/
|
|
120
|
-
|
|
121
|
-
token0: string;
|
|
122
|
-
token1: string;
|
|
256
|
+
addSwapLiquidityByTicks(args: {
|
|
257
|
+
token0: string | TokenClassKey;
|
|
258
|
+
token1: string | TokenClassKey;
|
|
123
259
|
fee: number;
|
|
124
260
|
tickLower: number;
|
|
125
261
|
tickUpper: number;
|
|
@@ -129,31 +265,202 @@ export declare class GSwapService {
|
|
|
129
265
|
amount1Min?: string;
|
|
130
266
|
}): Promise<any>;
|
|
131
267
|
/**
|
|
132
|
-
*
|
|
268
|
+
* Monitor a bundler transaction with graceful fallback on timeout
|
|
269
|
+
*
|
|
270
|
+
* **Pattern Overview**
|
|
271
|
+
* This helper consolidates the common error handling pattern used by all bundler-direct
|
|
272
|
+
* operations (removeSwapLiquidity, collectSwapPositionFees, etc.):
|
|
273
|
+
*
|
|
274
|
+
* 1. Set up WebSocket listener BEFORE bundler submission (already done by caller)
|
|
275
|
+
* 2. Try to await transaction confirmation via WebSocket
|
|
276
|
+
* 3. If timeout occurs: Return partial result with status: 'SUBMITTED'
|
|
277
|
+
* 4. If successful: Return confirmed result with on-chain status
|
|
278
|
+
* 5. Both cases include a .wait() method for retry/manual monitoring
|
|
279
|
+
*
|
|
280
|
+
* **Why This Pattern Matters**
|
|
281
|
+
* - Prevents race conditions (listener active before transaction confirmation)
|
|
282
|
+
* - Handles network timeouts gracefully (users still get transaction ID)
|
|
283
|
+
* - Provides flexibility (users can call .wait() again if needed)
|
|
284
|
+
* - Consistent error handling across all bundler operations
|
|
285
|
+
*
|
|
286
|
+
* @param transactionId Transaction ID from bundler submission
|
|
287
|
+
* @param transactionPromise WebSocket monitoring promise (already created before bundler call)
|
|
288
|
+
* @param operationName Operation name for logging context (e.g., 'liquidity removal')
|
|
289
|
+
* @returns Bundler operation result with status and wait() method
|
|
290
|
+
* @private
|
|
291
|
+
*/
|
|
292
|
+
private monitorBundlerTransaction;
|
|
293
|
+
/**
|
|
294
|
+
* Remove liquidity from an existing concentrated liquidity position
|
|
295
|
+
*
|
|
296
|
+
* **CRITICAL: WebSocket-First Pattern**
|
|
297
|
+
* This method ensures WebSocket connection is established BEFORE submitting the
|
|
298
|
+
* transaction to prevent race conditions where the transaction confirms before
|
|
299
|
+
* the listener becomes active. This is essential because:
|
|
300
|
+
*
|
|
301
|
+
* 1. WebSocket listener created IMMEDIATELY after transactionId received
|
|
302
|
+
* 2. Socket.IO buffers events for registered listeners
|
|
303
|
+
* 3. If listener not active when transaction confirms, confirmation is missed
|
|
304
|
+
* 4. Graceful fallback returns partial result with transaction ID on timeout
|
|
305
|
+
*
|
|
306
|
+
* **Error Handling & Recovery**
|
|
307
|
+
* If WebSocket monitoring times out (after 60 seconds):
|
|
308
|
+
* - Returns `{ transactionId, status: 'SUBMITTED' }` indicating transaction was accepted
|
|
309
|
+
* - Transaction may still succeed on-chain even if monitoring fails
|
|
310
|
+
* - Users can call `.wait()` again to retry monitoring
|
|
311
|
+
* - Recommended: retry monitoring after waiting or check on-chain later
|
|
312
|
+
*
|
|
313
|
+
* **Partial vs Complete Removal**
|
|
314
|
+
* - Partial: Set liquidity to a fraction (e.g., 50% of current amount)
|
|
315
|
+
* - Complete: Set liquidity to the full amount to close the position
|
|
316
|
+
* - Retrieved from position details via `getSwapUserLiquidityPositions()`
|
|
317
|
+
*
|
|
318
|
+
* **Token Format Support**
|
|
319
|
+
* - String tokens: 'GALA', 'GUSDC' → auto-converted to TokenClassKey
|
|
320
|
+
* - TokenClassKey: { collection, category, type, additionalKey }
|
|
321
|
+
* - Both formats normalized to TokenClassKey for bundler submission
|
|
322
|
+
*
|
|
133
323
|
* @param args Liquidity removal parameters
|
|
134
|
-
* @
|
|
324
|
+
* @param args.token0 First token (string or TokenClassKey)
|
|
325
|
+
* @param args.token1 Second token (string or TokenClassKey)
|
|
326
|
+
* @param args.fee Fee tier in basis points (500, 3000, or 10000)
|
|
327
|
+
* @param args.tickLower Lower tick boundary of the position
|
|
328
|
+
* @param args.tickUpper Upper tick boundary of the position
|
|
329
|
+
* @param args.liquidity Amount of liquidity to remove (partial or complete)
|
|
330
|
+
* @param args.amount0Min Optional minimum token0 to accept (default: '0')
|
|
331
|
+
* @param args.amount1Min Optional minimum token1 to accept (default: '0')
|
|
332
|
+
* @param args.positionId Optional position ID for tracking/verification
|
|
333
|
+
*
|
|
334
|
+
* @returns Promise<GSwapRemoveLiquidityResult> Transaction result with:
|
|
335
|
+
* - transactionId: Bundler transaction ID
|
|
336
|
+
* - status: Transaction status from WebSocket (or 'SUBMITTED' if timeout)
|
|
337
|
+
* - timestamp: When transaction was submitted
|
|
338
|
+
* - wait(): Async method to monitor or retry confirmation
|
|
339
|
+
*
|
|
340
|
+
* @throws GSwapPositionError if private key unavailable or bundler submission fails
|
|
341
|
+
*
|
|
342
|
+
* @example
|
|
343
|
+
* // Get position first
|
|
344
|
+
* const positions = await sdk.getSwapUserLiquidityPositions(walletAddress);
|
|
345
|
+
* const position = positions[0];
|
|
346
|
+
*
|
|
347
|
+
* // Remove 50% of liquidity
|
|
348
|
+
* const liquidityToRemove = (parseFloat(position.liquidity) * 50) / 100;
|
|
349
|
+
* const result = await sdk.removeSwapLiquidity({
|
|
350
|
+
* token0: 'GALA',
|
|
351
|
+
* token1: 'GUSDC',
|
|
352
|
+
* fee: 3000,
|
|
353
|
+
* tickLower: position.tickLower,
|
|
354
|
+
* tickUpper: position.tickUpper,
|
|
355
|
+
* liquidity: liquidityToRemove.toString(),
|
|
356
|
+
* amount0Min: '0',
|
|
357
|
+
* amount1Min: '0',
|
|
358
|
+
* positionId: position.positionId,
|
|
359
|
+
* });
|
|
360
|
+
*
|
|
361
|
+
* console.log('Removal submitted:', result.transactionId);
|
|
362
|
+
* await result.wait(); // Wait for on-chain confirmation
|
|
363
|
+
* console.log('Final status:', result.status);
|
|
135
364
|
*/
|
|
136
365
|
removeLiquidity(args: {
|
|
137
|
-
token0: string;
|
|
138
|
-
token1: string;
|
|
366
|
+
token0: string | TokenClassKey;
|
|
367
|
+
token1: string | TokenClassKey;
|
|
139
368
|
fee: number;
|
|
140
369
|
tickLower: number;
|
|
141
370
|
tickUpper: number;
|
|
142
371
|
liquidity: string;
|
|
143
372
|
amount0Min?: string;
|
|
144
373
|
amount1Min?: string;
|
|
374
|
+
positionId?: string;
|
|
145
375
|
}): Promise<any>;
|
|
146
376
|
/**
|
|
147
|
-
* Collect fees from a liquidity position
|
|
377
|
+
* Collect accumulated trading fees from a concentrated liquidity position
|
|
378
|
+
*
|
|
379
|
+
* **CRITICAL: WebSocket-First Pattern**
|
|
380
|
+
* This method ensures WebSocket connection is established BEFORE submitting the
|
|
381
|
+
* transaction to prevent race conditions where the transaction confirms before
|
|
382
|
+
* the listener becomes active. This is essential because:
|
|
383
|
+
*
|
|
384
|
+
* 1. WebSocket listener created IMMEDIATELY after transactionId received
|
|
385
|
+
* 2. Socket.IO buffers events for registered listeners
|
|
386
|
+
* 3. If listener not active when transaction confirms, confirmation is missed
|
|
387
|
+
* 4. Graceful fallback returns partial result with transaction ID on timeout
|
|
388
|
+
*
|
|
389
|
+
* **Error Handling & Recovery**
|
|
390
|
+
* If WebSocket monitoring times out (after 60 seconds):
|
|
391
|
+
* - Returns `{ transactionId, status: 'SUBMITTED' }` indicating transaction was accepted
|
|
392
|
+
* - Transaction may still succeed on-chain even if monitoring fails
|
|
393
|
+
* - Users can call `.wait()` again to retry monitoring
|
|
394
|
+
* - Recommended: retry monitoring after waiting or check position fees on-chain later
|
|
395
|
+
*
|
|
396
|
+
* **Fee Accumulation**
|
|
397
|
+
* Fees accumulate when your position is in-range and trades execute within your price range.
|
|
398
|
+
* - In-range fees: Actively accumulating (trading happens in your range)
|
|
399
|
+
* - Out-of-range fees: Not accumulating (current price outside your range)
|
|
400
|
+
* - Collect periodically for optimal capital efficiency (fees don't compound)
|
|
401
|
+
* - Fees are denominated in the two tokens of the pair (token0 and token1)
|
|
402
|
+
*
|
|
403
|
+
* **Partial vs Complete Collection**
|
|
404
|
+
* - Collect all: Set amount0Requested='0' and amount1Requested='0' (default)
|
|
405
|
+
* - Collect specific amount: Set amount0Requested/amount1Requested to desired amounts
|
|
406
|
+
* - Partial collection doesn't affect the position's liquidity
|
|
407
|
+
* - Current fees visible in position details via `getSwapUserLiquidityPositions()`
|
|
408
|
+
*
|
|
409
|
+
* **Token Format Support**
|
|
410
|
+
* - String tokens: 'GALA', 'GUSDC' → auto-converted to TokenClassKey
|
|
411
|
+
* - TokenClassKey: { collection, category, type, additionalKey }
|
|
412
|
+
* - Both formats normalized to TokenClassKey for bundler submission
|
|
413
|
+
*
|
|
148
414
|
* @param args Fee collection parameters
|
|
149
|
-
* @
|
|
415
|
+
* @param args.token0 First token (string or TokenClassKey)
|
|
416
|
+
* @param args.token1 Second token (string or TokenClassKey)
|
|
417
|
+
* @param args.fee Fee tier in basis points (500, 3000, or 10000)
|
|
418
|
+
* @param args.tickLower Lower tick boundary of the position
|
|
419
|
+
* @param args.tickUpper Upper tick boundary of the position
|
|
420
|
+
* @param args.amount0Requested Optional amount of token0 fees to collect (default: '0' = all)
|
|
421
|
+
* @param args.amount1Requested Optional amount of token1 fees to collect (default: '0' = all)
|
|
422
|
+
* @param args.positionId Optional position ID for tracking/verification
|
|
423
|
+
*
|
|
424
|
+
* @returns Promise<GSwapCollectFeesResult> Transaction result with:
|
|
425
|
+
* - transactionId: Bundler transaction ID
|
|
426
|
+
* - status: Transaction status from WebSocket (or 'SUBMITTED' if timeout)
|
|
427
|
+
* - timestamp: When transaction was submitted
|
|
428
|
+
* - wait(): Async method to monitor or retry confirmation
|
|
429
|
+
*
|
|
430
|
+
* @throws GSwapPositionError if private key unavailable or bundler submission fails
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
* // Get position with accumulated fees
|
|
434
|
+
* const positions = await sdk.getSwapUserLiquidityPositions(walletAddress);
|
|
435
|
+
* const position = positions[0];
|
|
436
|
+
*
|
|
437
|
+
* if (parseFloat(position.feeAmount0) > 0 || parseFloat(position.feeAmount1) > 0) {
|
|
438
|
+
* // Collect all accumulated fees
|
|
439
|
+
* const result = await sdk.collectSwapPositionFees({
|
|
440
|
+
* token0: 'GALA',
|
|
441
|
+
* token1: 'GUSDC',
|
|
442
|
+
* fee: 3000,
|
|
443
|
+
* tickLower: position.tickLower,
|
|
444
|
+
* tickUpper: position.tickUpper,
|
|
445
|
+
* amount0Requested: '0', // Collect all token0 fees
|
|
446
|
+
* amount1Requested: '0', // Collect all token1 fees
|
|
447
|
+
* positionId: position.positionId,
|
|
448
|
+
* });
|
|
449
|
+
*
|
|
450
|
+
* console.log('Fee collection submitted:', result.transactionId);
|
|
451
|
+
* await result.wait(); // Wait for on-chain confirmation
|
|
452
|
+
* console.log('Fees collected successfully');
|
|
453
|
+
* }
|
|
150
454
|
*/
|
|
151
455
|
collectPositionFees(args: {
|
|
152
|
-
token0: string;
|
|
153
|
-
token1: string;
|
|
456
|
+
token0: string | TokenClassKey;
|
|
457
|
+
token1: string | TokenClassKey;
|
|
154
458
|
fee: number;
|
|
155
459
|
tickLower: number;
|
|
156
460
|
tickUpper: number;
|
|
461
|
+
amount0Requested?: string;
|
|
462
|
+
amount1Requested?: string;
|
|
463
|
+
positionId?: string;
|
|
157
464
|
}): Promise<any>;
|
|
158
465
|
/**
|
|
159
466
|
* Get real-time pool state with liquidity depth and current price
|
|
@@ -229,30 +536,176 @@ export declare class GSwapService {
|
|
|
229
536
|
* @private
|
|
230
537
|
*/
|
|
231
538
|
private calculatePriceFromSqrtPriceX96;
|
|
539
|
+
/**
|
|
540
|
+
* Calculate price from sqrtPrice in decimal format (GetSlot0 API)
|
|
541
|
+
* Different from sqrtPriceX96 - GetSlot0 returns already-decoded decimal
|
|
542
|
+
* Formula: price = sqrtPrice²
|
|
543
|
+
* @private
|
|
544
|
+
*/
|
|
545
|
+
private calculatePriceFromSqrtPriceDecimal;
|
|
546
|
+
/**
|
|
547
|
+
* Get current pool state (slot0 data) for a token pair
|
|
548
|
+
* Fetches sqrtPrice, tick, and liquidity from GalaChain Gateway
|
|
549
|
+
* @param token0 First token symbol or TokenClassKey
|
|
550
|
+
* Accepts: "GALA" (plain), "GALA|Unit|none|none" (pipe), "GALA$Unit$none$none" (dollar)
|
|
551
|
+
* @param token1 Second token symbol or TokenClassKey
|
|
552
|
+
* Accepts: "GALA" (plain), "GALA|Unit|none|none" (pipe), "GALA$Unit$none$none" (dollar)
|
|
553
|
+
* @param fee Fee tier in basis points (500, 3000, 10000)
|
|
554
|
+
* @returns PoolSlot0Data with current sqrtPrice, tick, liquidity, and feeGrowth
|
|
555
|
+
*/
|
|
556
|
+
getPoolSlot0(token0: string | TokenClassKey, token1: string | TokenClassKey, fee: number): Promise<PoolSlot0Data>;
|
|
557
|
+
/**
|
|
558
|
+
* Get current price for a liquidity position's pool
|
|
559
|
+
* Fetches slot0 data and calculates token0/token1 price ratio
|
|
560
|
+
* @param position Position token pair and fee tier
|
|
561
|
+
*/
|
|
562
|
+
getPositionCurrentPrice(position: {
|
|
563
|
+
token0: string;
|
|
564
|
+
token1: string;
|
|
565
|
+
feeTier: number;
|
|
566
|
+
}): Promise<PoolPriceData>;
|
|
232
567
|
/**
|
|
233
568
|
* Calculate liquidity from amount0 (Uniswap v3 math)
|
|
234
|
-
*
|
|
569
|
+
* Uses @gala-chain/dex liquidity0() function for production-accurate calculation
|
|
235
570
|
* @private
|
|
236
571
|
*/
|
|
237
572
|
private calculateLiquidityFromAmount0;
|
|
238
573
|
/**
|
|
239
574
|
* Calculate liquidity from amount1 (Uniswap v3 math)
|
|
240
|
-
*
|
|
575
|
+
* Uses @gala-chain/dex liquidity1() function for production-accurate calculation
|
|
241
576
|
* @private
|
|
242
577
|
*/
|
|
243
578
|
private calculateLiquidityFromAmount1;
|
|
244
579
|
/**
|
|
245
580
|
* Calculate amount0 from liquidity (Uniswap v3 math)
|
|
246
|
-
*
|
|
581
|
+
* Uses @gala-chain/dex getAmount0Delta() function for production-accurate calculation
|
|
247
582
|
* @private
|
|
248
583
|
*/
|
|
249
584
|
private calculateAmount0FromLiquidity;
|
|
250
585
|
/**
|
|
251
586
|
* Calculate amount1 from liquidity (Uniswap v3 math)
|
|
252
|
-
*
|
|
587
|
+
* Uses @gala-chain/dex getAmount1Delta() function for production-accurate calculation
|
|
253
588
|
* @private
|
|
254
589
|
*/
|
|
255
590
|
private calculateAmount1FromLiquidity;
|
|
591
|
+
/**
|
|
592
|
+
* Convert a token pair from Launchpad format to GSwap format
|
|
593
|
+
*
|
|
594
|
+
* Converts both tokens in a pair simultaneously for cleaner, more readable code
|
|
595
|
+
* when working with token swaps and pool queries.
|
|
596
|
+
*
|
|
597
|
+
* @param token0 - First token in any supported format
|
|
598
|
+
* @param token1 - Second token in any supported format
|
|
599
|
+
* @returns Object with converted tokens as gswapToken0 and gswapToken1
|
|
600
|
+
*
|
|
601
|
+
* @private
|
|
602
|
+
*/
|
|
603
|
+
private convertTokenPair;
|
|
604
|
+
/**
|
|
605
|
+
* Send AddLiquidity transaction directly to bundler (bypassing gswap-sdk)
|
|
606
|
+
*
|
|
607
|
+
* Creates a liquidity position by signing a DTO and posting it directly to the
|
|
608
|
+
* GalaChain bundler. This method replaces the gswap-sdk wrapper with a direct
|
|
609
|
+
* implementation following the same pattern as CreateSale.
|
|
610
|
+
*
|
|
611
|
+
* @param params - AddLiquidity parameters including token pair, fee, tick range, and amounts
|
|
612
|
+
* @returns Transaction ID from bundler response
|
|
613
|
+
* @throws Error if not in full-access mode or if bundler request fails
|
|
614
|
+
*
|
|
615
|
+
* @private
|
|
616
|
+
*/
|
|
617
|
+
private sendAddLiquidityToBundler;
|
|
618
|
+
/**
|
|
619
|
+
* Send RemoveLiquidity transaction directly to bundler
|
|
620
|
+
*
|
|
621
|
+
* Bypasses GSwap SDK and posts signed EIP-712 RemoveLiquidity DTO directly to bundler.
|
|
622
|
+
* Supports full tick ranges including negative values.
|
|
623
|
+
*
|
|
624
|
+
* @param tickLower Lower tick boundary (int256, supports negative)
|
|
625
|
+
* @param tickUpper Upper tick boundary (int256, supports negative)
|
|
626
|
+
* @param amount Liquidity amount to remove
|
|
627
|
+
* @param token0 First token class key
|
|
628
|
+
* @param token1 Second token class key
|
|
629
|
+
* @param fee Fee tier (500, 3000, or 10000)
|
|
630
|
+
* @param amount0Min Minimum amount0 to receive (slippage protection)
|
|
631
|
+
* @param amount1Min Minimum amount1 to receive (slippage protection)
|
|
632
|
+
* @param positionId Unique position identifier
|
|
633
|
+
* @returns Transaction ID from bundler response
|
|
634
|
+
*
|
|
635
|
+
* @private
|
|
636
|
+
* @throws Error if signing or bundler submission fails
|
|
637
|
+
*/
|
|
638
|
+
private sendRemoveLiquidityToBundler;
|
|
639
|
+
/**
|
|
640
|
+
* Send CollectPositionFees transaction directly to bundler
|
|
641
|
+
*
|
|
642
|
+
* Bypasses GSwap SDK and posts signed EIP-712 CollectPositionFees DTO directly to bundler.
|
|
643
|
+
* Collects accumulated trading fees from a liquidity position independently of the position itself.
|
|
644
|
+
*
|
|
645
|
+
* @param token0 First token class key
|
|
646
|
+
* @param token1 Second token class key
|
|
647
|
+
* @param fee Fee tier (500, 3000, or 10000)
|
|
648
|
+
* @param amount0Requested Requested amount0 to collect (0 to collect all available)
|
|
649
|
+
* @param amount1Requested Requested amount1 to collect (0 to collect all available)
|
|
650
|
+
* @param tickLower Lower tick boundary (int256, supports negative)
|
|
651
|
+
* @param tickUpper Upper tick boundary (int256, supports negative)
|
|
652
|
+
* @param positionId Unique position identifier
|
|
653
|
+
* @returns Transaction ID from bundler response
|
|
654
|
+
*
|
|
655
|
+
* @private
|
|
656
|
+
* @throws Error if signing or bundler submission fails
|
|
657
|
+
*/
|
|
658
|
+
private sendCollectPositionFeesToBundler;
|
|
659
|
+
/**
|
|
660
|
+
* Send Swap transaction directly to bundler
|
|
661
|
+
*
|
|
662
|
+
* Bypasses GSwap SDK and posts signed EIP-712 Swap DTO directly to bundler.
|
|
663
|
+
* Implements exact input swapping (sell exact amount of input token, receive output token).
|
|
664
|
+
*
|
|
665
|
+
* @param fromToken Input token (token being sold) in GSwap format
|
|
666
|
+
* @param toToken Output token (token being bought) in GSwap format
|
|
667
|
+
* @param inputAmount Amount of input token to sell
|
|
668
|
+
* @param minOutput Minimum amount of output token to receive (slippage protection)
|
|
669
|
+
* @param feeTier Fee tier (500, 3000, or 10000)
|
|
670
|
+
* @param walletAddress Wallet address executing the swap
|
|
671
|
+
* @returns Transaction ID from bundler response
|
|
672
|
+
*
|
|
673
|
+
* @private
|
|
674
|
+
* @throws Error if signing or bundler submission fails
|
|
675
|
+
*/
|
|
676
|
+
private sendSwapToBundler;
|
|
677
|
+
/**
|
|
678
|
+
* Build strings instructions for AddLiquidity state locking
|
|
679
|
+
*
|
|
680
|
+
* Strings instructions are used by the bundler to lock the necessary state
|
|
681
|
+
* fields during transaction execution. This ensures atomicity of the operation.
|
|
682
|
+
*
|
|
683
|
+
* @param token0 First token class key
|
|
684
|
+
* @param token1 Second token class key
|
|
685
|
+
* @param fee Fee tier (500, 3000, or 10000 basis points)
|
|
686
|
+
* @param ownerAddress Wallet address creating the position
|
|
687
|
+
* @returns Array of strings instructions for state locking
|
|
688
|
+
*
|
|
689
|
+
* @private
|
|
690
|
+
*/
|
|
691
|
+
private buildLiquidityStringsInstructions;
|
|
692
|
+
/**
|
|
693
|
+
* Handle GSwap SDK errors with consistent logging and error transformation
|
|
694
|
+
*
|
|
695
|
+
* Centralizes error handling for GSwap operations by:
|
|
696
|
+
* 1. Logging the error with context
|
|
697
|
+
* 2. Extracting error code from original error
|
|
698
|
+
* 3. Creating typed GSwap error with standardized message
|
|
699
|
+
*
|
|
700
|
+
* @param logMessage - Message to log when error occurs
|
|
701
|
+
* @param ErrorClass - Error constructor to throw (GSwapQuoteError, GSwapSwapError, etc.)
|
|
702
|
+
* @param originalError - Original error from GSwap SDK
|
|
703
|
+
* @param additionalFields - Optional additional context (e.g., { transactionHash: txHash })
|
|
704
|
+
* @throws Throws instance of provided ErrorClass with formatted message
|
|
705
|
+
*
|
|
706
|
+
* @private
|
|
707
|
+
*/
|
|
708
|
+
private handleGSwapError;
|
|
256
709
|
/**
|
|
257
710
|
* Extract error code from GSwap SDK error
|
|
258
711
|
*
|
|
@@ -266,5 +719,27 @@ export declare class GSwapService {
|
|
|
266
719
|
* @private
|
|
267
720
|
*/
|
|
268
721
|
private extractGSwapErrorCode;
|
|
722
|
+
/**
|
|
723
|
+
* Ensure WebSocket connection is established before transaction monitoring
|
|
724
|
+
*
|
|
725
|
+
* Checks if WebSocketService is already connected, and connects if needed.
|
|
726
|
+
* This prevents unnecessary connection attempts while ensuring transactions can be monitored.
|
|
727
|
+
*
|
|
728
|
+
* @private
|
|
729
|
+
* @throws Error if connection fails
|
|
730
|
+
*/
|
|
731
|
+
private ensureWebSocketConnected;
|
|
732
|
+
/**
|
|
733
|
+
* Calculate personal sign prefix for GalaChain compatibility
|
|
734
|
+
*
|
|
735
|
+
* Builds the prefix string required by GalaChain for EIP-712 signing.
|
|
736
|
+
* This matches the format used in production working payloads.
|
|
737
|
+
*
|
|
738
|
+
* @param payload The payload being signed
|
|
739
|
+
* @returns Prefix string for signing
|
|
740
|
+
*
|
|
741
|
+
* @private
|
|
742
|
+
*/
|
|
743
|
+
private calculatePersonalSignPrefix;
|
|
269
744
|
}
|
|
270
745
|
//# sourceMappingURL=GSwapService.d.ts.map
|