@aspan/sdk 0.2.0 → 0.2.2
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/README.md +32 -25
- package/dist/index.d.mts +325 -217
- package/dist/index.d.ts +325 -217
- package/dist/index.js +213 -208
- package/dist/index.mjs +212 -208
- package/package.json +1 -1
- package/src/__tests__/router.test.ts +378 -0
- package/src/abi/router.ts +153 -167
- package/src/index.ts +3 -2
- package/src/router.ts +48 -62
- package/src/types.ts +28 -12
package/src/router.ts
CHANGED
|
@@ -27,9 +27,7 @@ import type {
|
|
|
27
27
|
RouterRedeemApUSDParams,
|
|
28
28
|
RouterRedeemXBNBParams,
|
|
29
29
|
RouterRedeemAndSwapParams,
|
|
30
|
-
RouterRedeemAndUnstakeParams,
|
|
31
30
|
WithdrawalRequestInfo,
|
|
32
|
-
ExpectedOutput,
|
|
33
31
|
} from "./types";
|
|
34
32
|
|
|
35
33
|
// ============ Configuration ============
|
|
@@ -119,25 +117,51 @@ export class AspanRouterReadClient {
|
|
|
119
117
|
}
|
|
120
118
|
|
|
121
119
|
/**
|
|
122
|
-
*
|
|
120
|
+
* Preview apUSD mint output for a given LST amount
|
|
123
121
|
*/
|
|
124
|
-
async
|
|
125
|
-
|
|
126
|
-
inputAmount: bigint,
|
|
127
|
-
targetLST: Address,
|
|
128
|
-
mintXBNB: boolean
|
|
129
|
-
): Promise<ExpectedOutput> {
|
|
130
|
-
const result = await this.publicClient.readContract({
|
|
122
|
+
async previewMintApUSD(lst: Address, lstAmount: bigint): Promise<bigint> {
|
|
123
|
+
return this.publicClient.readContract({
|
|
131
124
|
address: this.routerAddress,
|
|
132
125
|
abi: RouterABI,
|
|
133
|
-
functionName: "
|
|
134
|
-
args: [
|
|
126
|
+
functionName: "previewMintApUSD",
|
|
127
|
+
args: [lst, lstAmount],
|
|
135
128
|
});
|
|
129
|
+
}
|
|
136
130
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
131
|
+
/**
|
|
132
|
+
* Preview xBNB mint output for a given LST amount
|
|
133
|
+
*/
|
|
134
|
+
async previewMintXBNB(lst: Address, lstAmount: bigint): Promise<bigint> {
|
|
135
|
+
return this.publicClient.readContract({
|
|
136
|
+
address: this.routerAddress,
|
|
137
|
+
abi: RouterABI,
|
|
138
|
+
functionName: "previewMintXBNB",
|
|
139
|
+
args: [lst, lstAmount],
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Preview LST output for redeeming apUSD
|
|
145
|
+
*/
|
|
146
|
+
async previewRedeemApUSD(lst: Address, apUSDAmount: bigint): Promise<bigint> {
|
|
147
|
+
return this.publicClient.readContract({
|
|
148
|
+
address: this.routerAddress,
|
|
149
|
+
abi: RouterABI,
|
|
150
|
+
functionName: "previewRedeemApUSD",
|
|
151
|
+
args: [lst, apUSDAmount],
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Preview LST output for redeeming xBNB
|
|
157
|
+
*/
|
|
158
|
+
async previewRedeemXBNB(lst: Address, xBNBAmount: bigint): Promise<bigint> {
|
|
159
|
+
return this.publicClient.readContract({
|
|
160
|
+
address: this.routerAddress,
|
|
161
|
+
abi: RouterABI,
|
|
162
|
+
functionName: "previewRedeemXBNB",
|
|
163
|
+
args: [lst, xBNBAmount],
|
|
164
|
+
});
|
|
141
165
|
}
|
|
142
166
|
|
|
143
167
|
/**
|
|
@@ -245,7 +269,7 @@ export class AspanRouterReadClient {
|
|
|
245
269
|
* Full client with write capabilities for AspanRouter
|
|
246
270
|
*/
|
|
247
271
|
export class AspanRouterClient extends AspanRouterReadClient {
|
|
248
|
-
|
|
272
|
+
public readonly walletClient: WalletClient;
|
|
249
273
|
|
|
250
274
|
constructor(config: AspanRouterWriteClientConfig) {
|
|
251
275
|
super(config);
|
|
@@ -287,10 +311,8 @@ export class AspanRouterClient extends AspanRouterReadClient {
|
|
|
287
311
|
targetLST: params.swapParams.targetLST,
|
|
288
312
|
minLSTOut: params.swapParams.minLSTOut,
|
|
289
313
|
poolFee: params.swapParams.poolFee,
|
|
290
|
-
useV2: params.swapParams.useV2,
|
|
291
314
|
},
|
|
292
315
|
{
|
|
293
|
-
mintXBNB: params.mintParams.mintXBNB,
|
|
294
316
|
minMintOut: params.mintParams.minMintOut,
|
|
295
317
|
recipient: params.mintParams.recipient,
|
|
296
318
|
deadline: params.mintParams.deadline,
|
|
@@ -322,10 +344,8 @@ export class AspanRouterClient extends AspanRouterReadClient {
|
|
|
322
344
|
targetLST: params.swapParams.targetLST,
|
|
323
345
|
minLSTOut: params.swapParams.minLSTOut,
|
|
324
346
|
poolFee: params.swapParams.poolFee,
|
|
325
|
-
useV2: params.swapParams.useV2,
|
|
326
347
|
},
|
|
327
348
|
{
|
|
328
|
-
mintXBNB: params.mintParams.mintXBNB,
|
|
329
349
|
minMintOut: params.mintParams.minMintOut,
|
|
330
350
|
recipient: params.mintParams.recipient,
|
|
331
351
|
deadline: params.mintParams.deadline,
|
|
@@ -347,7 +367,7 @@ export class AspanRouterClient extends AspanRouterReadClient {
|
|
|
347
367
|
functionName: "stakeAndMint",
|
|
348
368
|
args: [
|
|
349
369
|
params.targetLST,
|
|
350
|
-
params.
|
|
370
|
+
params.isXBNB,
|
|
351
371
|
params.minMintOut,
|
|
352
372
|
params.deadline,
|
|
353
373
|
],
|
|
@@ -494,7 +514,8 @@ export class AspanRouterClient extends AspanRouterReadClient {
|
|
|
494
514
|
// ============ Redeem and Swap Functions ============
|
|
495
515
|
|
|
496
516
|
/**
|
|
497
|
-
* Redeem apUSD and swap LST to output token
|
|
517
|
+
* Redeem apUSD and swap LST to output token via V3 path
|
|
518
|
+
* @param params.path - PancakeSwap V3 encoded path (use encodeV3Path helper)
|
|
498
519
|
*/
|
|
499
520
|
async redeemApUSDAndSwap(params: RouterRedeemAndSwapParams): Promise<Hash> {
|
|
500
521
|
return this.walletClient.writeContract({
|
|
@@ -506,17 +527,16 @@ export class AspanRouterClient extends AspanRouterReadClient {
|
|
|
506
527
|
args: [
|
|
507
528
|
params.lst,
|
|
508
529
|
params.amount,
|
|
509
|
-
params.
|
|
530
|
+
params.path,
|
|
510
531
|
params.minOut,
|
|
511
532
|
params.deadline,
|
|
512
|
-
params.useV2,
|
|
513
|
-
params.poolFee,
|
|
514
533
|
],
|
|
515
534
|
});
|
|
516
535
|
}
|
|
517
536
|
|
|
518
537
|
/**
|
|
519
|
-
* Redeem xBNB and swap LST to output token
|
|
538
|
+
* Redeem xBNB and swap LST to output token via V3 path
|
|
539
|
+
* @param params.path - PancakeSwap V3 encoded path (use encodeV3Path helper)
|
|
520
540
|
*/
|
|
521
541
|
async redeemXBNBAndSwap(params: RouterRedeemAndSwapParams): Promise<Hash> {
|
|
522
542
|
return this.walletClient.writeContract({
|
|
@@ -528,47 +548,13 @@ export class AspanRouterClient extends AspanRouterReadClient {
|
|
|
528
548
|
args: [
|
|
529
549
|
params.lst,
|
|
530
550
|
params.amount,
|
|
531
|
-
params.
|
|
551
|
+
params.path,
|
|
532
552
|
params.minOut,
|
|
533
553
|
params.deadline,
|
|
534
|
-
params.useV2,
|
|
535
|
-
params.poolFee,
|
|
536
554
|
],
|
|
537
555
|
});
|
|
538
556
|
}
|
|
539
557
|
|
|
540
|
-
/**
|
|
541
|
-
* Redeem apUSD and instantly unstake LST to native BNB via DEX
|
|
542
|
-
*/
|
|
543
|
-
async redeemApUSDAndUnstake(
|
|
544
|
-
params: RouterRedeemAndUnstakeParams
|
|
545
|
-
): Promise<Hash> {
|
|
546
|
-
return this.walletClient.writeContract({
|
|
547
|
-
chain: this.chain,
|
|
548
|
-
account: this.walletClient.account!,
|
|
549
|
-
address: this.routerAddress,
|
|
550
|
-
abi: RouterABI,
|
|
551
|
-
functionName: "redeemApUSDAndUnstake",
|
|
552
|
-
args: [params.lst, params.amount, params.minBNBOut, params.deadline],
|
|
553
|
-
});
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
/**
|
|
557
|
-
* Redeem xBNB and instantly unstake LST to native BNB via DEX
|
|
558
|
-
*/
|
|
559
|
-
async redeemXBNBAndUnstake(
|
|
560
|
-
params: RouterRedeemAndUnstakeParams
|
|
561
|
-
): Promise<Hash> {
|
|
562
|
-
return this.walletClient.writeContract({
|
|
563
|
-
chain: this.chain,
|
|
564
|
-
account: this.walletClient.account!,
|
|
565
|
-
address: this.routerAddress,
|
|
566
|
-
abi: RouterABI,
|
|
567
|
-
functionName: "redeemXBNBAndUnstake",
|
|
568
|
-
args: [params.lst, params.amount, params.minBNBOut, params.deadline],
|
|
569
|
-
});
|
|
570
|
-
}
|
|
571
|
-
|
|
572
558
|
// ============ Native Unstake Functions ============
|
|
573
559
|
|
|
574
560
|
/**
|
package/src/types.ts
CHANGED
|
@@ -258,14 +258,10 @@ export interface RouterSwapParams {
|
|
|
258
258
|
minLSTOut: bigint;
|
|
259
259
|
/** PancakeSwap V3 pool fee tier (500, 2500, 10000) */
|
|
260
260
|
poolFee: number;
|
|
261
|
-
/** true = PancakeSwap V2, false = V3 */
|
|
262
|
-
useV2: boolean;
|
|
263
261
|
}
|
|
264
262
|
|
|
265
263
|
/** Router mint parameters */
|
|
266
264
|
export interface RouterMintParams {
|
|
267
|
-
/** true = mint xBNB, false = mint apUSD */
|
|
268
|
-
mintXBNB: boolean;
|
|
269
265
|
/** Minimum output to receive (slippage protection) */
|
|
270
266
|
minMintOut: bigint;
|
|
271
267
|
/** Recipient of minted tokens (address(0) = msg.sender) */
|
|
@@ -285,7 +281,7 @@ export interface StakeAndMintParams {
|
|
|
285
281
|
/** Target LST (slisBNB, asBNB, wclisBNB) */
|
|
286
282
|
targetLST: Address;
|
|
287
283
|
/** true = mint xBNB, false = mint apUSD */
|
|
288
|
-
|
|
284
|
+
isXBNB: boolean;
|
|
289
285
|
/** Minimum output to receive */
|
|
290
286
|
minMintOut: bigint;
|
|
291
287
|
/** Transaction deadline timestamp */
|
|
@@ -348,22 +344,42 @@ export interface RouterRedeemXBNBParams {
|
|
|
348
344
|
minOut?: bigint;
|
|
349
345
|
}
|
|
350
346
|
|
|
351
|
-
/** Parameters for redeemAndSwap functions */
|
|
347
|
+
/** Parameters for redeemAndSwap functions (uses PancakeSwap V3 path) */
|
|
352
348
|
export interface RouterRedeemAndSwapParams {
|
|
353
349
|
/** LST to redeem */
|
|
354
350
|
lst: Address;
|
|
355
351
|
/** Amount to redeem (apUSD or xBNB) */
|
|
356
352
|
amount: bigint;
|
|
357
|
-
/**
|
|
358
|
-
|
|
353
|
+
/**
|
|
354
|
+
* PancakeSwap V3 encoded swap path (LST → ... → outputToken)
|
|
355
|
+
* Use encodeV3Path() helper or get from PancakeSwap Quoter
|
|
356
|
+
* Example: slisBNB → WBNB → USDT
|
|
357
|
+
*/
|
|
358
|
+
path: `0x${string}`;
|
|
359
359
|
/** Minimum output to receive */
|
|
360
360
|
minOut: bigint;
|
|
361
361
|
/** Transaction deadline timestamp */
|
|
362
362
|
deadline: bigint;
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Encode a PancakeSwap V3 swap path
|
|
367
|
+
* @param tokens Array of token addresses in order
|
|
368
|
+
* @param fees Array of pool fees between each token pair
|
|
369
|
+
* @returns Encoded path bytes
|
|
370
|
+
*/
|
|
371
|
+
export function encodeV3Path(tokens: Address[], fees: number[]): `0x${string}` {
|
|
372
|
+
if (tokens.length < 2) throw new Error("Path must have at least 2 tokens");
|
|
373
|
+
if (fees.length !== tokens.length - 1) throw new Error("Fees length must be tokens.length - 1");
|
|
374
|
+
|
|
375
|
+
let path = tokens[0].toLowerCase().slice(2); // Remove 0x prefix
|
|
376
|
+
for (let i = 0; i < fees.length; i++) {
|
|
377
|
+
// Fee is 3 bytes (uint24)
|
|
378
|
+
const feeHex = fees[i].toString(16).padStart(6, "0");
|
|
379
|
+
path += feeHex;
|
|
380
|
+
path += tokens[i + 1].toLowerCase().slice(2);
|
|
381
|
+
}
|
|
382
|
+
return `0x${path}`;
|
|
367
383
|
}
|
|
368
384
|
|
|
369
385
|
/** Parameters for redeemAndUnstake functions */
|