@aspan/sdk 0.4.6 → 0.4.8

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.
@@ -254,13 +254,6 @@ export const DiamondABI = [
254
254
  outputs: [{ name: "assets", type: "uint256", internalType: "uint256" }],
255
255
  stateMutability: "nonpayable"
256
256
  },
257
- {
258
- type: "function",
259
- name: "withdrawAssets",
260
- inputs: [{ name: "_assets", type: "uint256", internalType: "uint256" }],
261
- outputs: [{ name: "shares", type: "uint256", internalType: "uint256" }],
262
- stateMutability: "nonpayable"
263
- },
264
257
  {
265
258
  type: "function",
266
259
  name: "getShares",
package/src/abi/router.ts CHANGED
@@ -185,6 +185,20 @@ export const RouterABI = [
185
185
  outputs: [{ type: "bool" }],
186
186
  stateMutability: "view",
187
187
  },
188
+ {
189
+ type: "function",
190
+ name: "lstModes",
191
+ inputs: [{ name: "lst", type: "address" }],
192
+ outputs: [{ type: "uint8" }],
193
+ stateMutability: "view",
194
+ },
195
+ {
196
+ type: "function",
197
+ name: "isLSTRoutable",
198
+ inputs: [{ name: "lst", type: "address" }],
199
+ outputs: [{ type: "bool" }],
200
+ stateMutability: "view",
201
+ },
188
202
 
189
203
  // Preview functions - unified
190
204
  {
package/src/abi/sApUSD.ts CHANGED
@@ -27,13 +27,13 @@ export const SApUSDABI = [
27
27
  },
28
28
  {
29
29
  type: "function",
30
- name: "previewRedeemMulti",
30
+ name: "previewRedeem",
31
31
  inputs: [
32
32
  { name: "shares", type: "uint256", internalType: "uint256" }
33
33
  ],
34
34
  outputs: [
35
- { name: "assets", type: "address[]", internalType: "address[]" },
36
- { name: "amounts", type: "uint256[]", internalType: "uint256[]" }
35
+ { name: "apUSDOut", type: "uint256", internalType: "uint256" },
36
+ { name: "xBNBOut", type: "uint256", internalType: "uint256" }
37
37
  ],
38
38
  stateMutability: "view"
39
39
  },
@@ -80,57 +80,71 @@ export const SApUSDABI = [
80
80
  },
81
81
  {
82
82
  type: "function",
83
- name: "previewCleanXBNB",
83
+ name: "hasRole",
84
84
  inputs: [
85
- { name: "_xBNBAmount", type: "uint256", internalType: "uint256" },
86
- { name: "_router", type: "address", internalType: "address" },
87
- { name: "_path", type: "address[]", internalType: "address[]" }
85
+ { name: "role", type: "bytes32", internalType: "bytes32" },
86
+ { name: "account", type: "address", internalType: "address" }
88
87
  ],
89
- outputs: [{ name: "expectedApUSD", type: "uint256", internalType: "uint256" }],
88
+ outputs: [{ name: "", type: "bool", internalType: "bool" }],
90
89
  stateMutability: "view"
91
90
  },
91
+
92
+ // ============ Write Functions ============
92
93
  {
93
94
  type: "function",
94
- name: "KEEPER_ROLE",
95
- inputs: [],
96
- outputs: [{ name: "", type: "bytes32", internalType: "bytes32" }],
97
- stateMutability: "view"
95
+ name: "redeem",
96
+ inputs: [
97
+ { name: "shares", type: "uint256", internalType: "uint256" },
98
+ { name: "receiver", type: "address", internalType: "address" },
99
+ { name: "owner", type: "address", internalType: "address" }
100
+ ],
101
+ outputs: [
102
+ { name: "apUSDOut", type: "uint256", internalType: "uint256" },
103
+ { name: "xBNBOut", type: "uint256", internalType: "uint256" }
104
+ ],
105
+ stateMutability: "nonpayable"
98
106
  },
99
107
  {
100
108
  type: "function",
101
- name: "hasRole",
109
+ name: "deposit",
102
110
  inputs: [
103
- { name: "role", type: "bytes32", internalType: "bytes32" },
104
- { name: "account", type: "address", internalType: "address" }
111
+ { name: "assets", type: "uint256", internalType: "uint256" },
112
+ { name: "receiver", type: "address", internalType: "address" }
105
113
  ],
106
- outputs: [{ name: "", type: "bool", internalType: "bool" }],
107
- stateMutability: "view"
114
+ outputs: [{ name: "shares", type: "uint256", internalType: "uint256" }],
115
+ stateMutability: "nonpayable"
108
116
  },
109
117
 
110
- // ============ Keeper Functions ============
118
+ // ============ Accounting Functions ============
111
119
  {
112
120
  type: "function",
113
- name: "cleanXBNB",
121
+ name: "addAccounting",
114
122
  inputs: [
115
- { name: "_xBNBAmount", type: "uint256", internalType: "uint256" },
116
- { name: "_minApUSDOut", type: "uint256", internalType: "uint256" },
117
- { name: "_router", type: "address", internalType: "address" },
118
- { name: "_path", type: "address[]", internalType: "address[]" },
119
- { name: "_deadline", type: "uint256", internalType: "uint256" }
123
+ { name: "apUSD", type: "uint256", internalType: "uint256" },
124
+ { name: "xBNB", type: "uint256", internalType: "uint256" }
120
125
  ],
121
- outputs: [{ name: "apUSDReceived", type: "uint256", internalType: "uint256" }],
126
+ outputs: [],
127
+ stateMutability: "nonpayable"
128
+ },
129
+ {
130
+ type: "function",
131
+ name: "subAccounting",
132
+ inputs: [
133
+ { name: "apUSD", type: "uint256", internalType: "uint256" },
134
+ { name: "xBNB", type: "uint256", internalType: "uint256" }
135
+ ],
136
+ outputs: [],
122
137
  stateMutability: "nonpayable"
123
138
  },
124
139
 
125
- // ============ Events ============
140
+ // ============ Admin Functions ============
126
141
  {
127
- type: "event",
128
- name: "VaultCleaned",
142
+ type: "function",
143
+ name: "sweepExcess",
129
144
  inputs: [
130
- { name: "xBNBSold", type: "uint256", indexed: false, internalType: "uint256" },
131
- { name: "apUSDReceived", type: "uint256", indexed: false, internalType: "uint256" },
132
- { name: "keeper", type: "address", indexed: true, internalType: "address" }
145
+ { name: "recipient", type: "address", internalType: "address" }
133
146
  ],
134
- anonymous: false
135
- }
147
+ outputs: [],
148
+ stateMutability: "nonpayable"
149
+ },
136
150
  ] as const;
package/src/client.ts CHANGED
@@ -75,7 +75,6 @@ import type {
75
75
  RedeemXBNBParams,
76
76
  DepositParams,
77
77
  WithdrawParams,
78
- WithdrawAssetsParams,
79
78
  } from "./types";
80
79
 
81
80
  // ============ Configuration ============
@@ -118,6 +117,7 @@ export class AspanReadClient {
118
117
  this.publicClient = createPublicClient({
119
118
  chain: this.chain,
120
119
  transport: http(config.rpcUrl),
120
+ batch: { multicall: true },
121
121
  });
122
122
  }
123
123
 
@@ -1210,22 +1210,6 @@ export class AspanClient extends AspanReadClient {
1210
1210
  });
1211
1211
  }
1212
1212
 
1213
- /**
1214
- * Withdraw from stability pool by asset amount
1215
- * @param params Withdraw parameters
1216
- * @returns Transaction hash
1217
- */
1218
- async withdrawAssets(params: WithdrawAssetsParams): Promise<Hash> {
1219
- return this.walletClient.writeContract({
1220
- chain: this.chain,
1221
- account: this.walletClient.account!,
1222
- address: this.diamondAddress,
1223
- abi: DiamondABI,
1224
- functionName: "withdrawAssets",
1225
- args: [params.assets],
1226
- });
1227
- }
1228
-
1229
1213
  /**
1230
1214
  * Harvest yield from LSTs
1231
1215
  * @returns Transaction hash
package/src/index.ts CHANGED
@@ -68,7 +68,6 @@ export type {
68
68
  RedeemXBNBParams,
69
69
  DepositParams,
70
70
  WithdrawParams,
71
- WithdrawAssetsParams,
72
71
  // Events
73
72
  ApUSDMintedEvent,
74
73
  ApUSDRedeemedEvent,
@@ -117,7 +116,7 @@ export const PRICE_PRECISION = 10n ** 8n; // Chainlink price precision
117
116
  // ============ Contract Addresses (BSC Mainnet) ============
118
117
  export const BSC_ADDRESSES = {
119
118
  diamond: "0x6a11B30d3a70727d5477D6d8090e144443fA1c78" as const,
120
- router: "0x29dd49b2e98674ee7531f17e4d40a7725918c3f6" as const,
119
+ router: "0x34a64c4EbDe830773083BA8c9469456616F6723b" as const,
121
120
  apUSD: "0x4570047eeB5aDb4081c5d470494EB5134e34A287" as const,
122
121
  xBNB: "0x0A0c9CD826e747D99F90D63e780B3727Da4D0d43" as const,
123
122
  sApUSD: "0x896770Dba7c0481539E25aaB56bE285ECF6D65eB" as const,
package/src/router.ts CHANGED
@@ -20,6 +20,7 @@ import {
20
20
  import { bsc, bscTestnet } from "viem/chains";
21
21
  import { pharosTestnet, CHAIN_IDS, getChainById } from "./client";
22
22
  import { RouterABI } from "./abi/router";
23
+ import { DiamondABI } from "./abi/diamond";
23
24
  import type {
24
25
  SwapAndMintParams,
25
26
  StakeAndMintParams,
@@ -58,6 +59,7 @@ export class AspanRouterReadClient {
58
59
  protected readonly publicClient: PublicClient;
59
60
  protected readonly routerAddress: Address;
60
61
  protected readonly chain: Chain;
62
+ private readonly _addressCache = new Map<string, Address>();
61
63
 
62
64
  constructor(config: AspanRouterClientConfig) {
63
65
  this.routerAddress = config.routerAddress;
@@ -66,9 +68,18 @@ export class AspanRouterReadClient {
66
68
  this.publicClient = createPublicClient({
67
69
  chain: this.chain,
68
70
  transport: http(config.rpcUrl),
71
+ batch: { multicall: true },
69
72
  });
70
73
  }
71
74
 
75
+ protected async _getCachedAddress(key: string, fetcher: () => Promise<Address>): Promise<Address> {
76
+ const cached = this._addressCache.get(key);
77
+ if (cached) return cached;
78
+ const addr = await fetcher();
79
+ this._addressCache.set(key, addr);
80
+ return addr;
81
+ }
82
+
72
83
  // ============ View Functions ============
73
84
 
74
85
  /**
@@ -107,16 +118,43 @@ export class AspanRouterReadClient {
107
118
  }
108
119
 
109
120
  /**
110
- * Get the Diamond contract address
121
+ * Get LST mode (0 = SYNC, 1 = ASYNC_DIRECT_ONLY)
111
122
  */
112
- async getDiamond(): Promise<Address> {
123
+ async getLSTMode(lst: Address): Promise<number> {
124
+ const mode = await this.publicClient.readContract({
125
+ address: this.routerAddress,
126
+ abi: RouterABI,
127
+ functionName: "lstModes",
128
+ args: [lst],
129
+ });
130
+ return Number(mode);
131
+ }
132
+
133
+ /**
134
+ * Check whether an LST supports routed flows (swap/stake/redeemAndSwap)
135
+ */
136
+ async isLSTRoutable(lst: Address): Promise<boolean> {
113
137
  return this.publicClient.readContract({
114
138
  address: this.routerAddress,
115
139
  abi: RouterABI,
116
- functionName: "diamond",
140
+ functionName: "isLSTRoutable",
141
+ args: [lst],
117
142
  });
118
143
  }
119
144
 
145
+ /**
146
+ * Get the Diamond contract address
147
+ */
148
+ async getDiamond(): Promise<Address> {
149
+ return this._getCachedAddress("diamond", async () =>
150
+ this.publicClient.readContract({
151
+ address: this.routerAddress,
152
+ abi: RouterABI,
153
+ functionName: "diamond",
154
+ })
155
+ );
156
+ }
157
+
120
158
  /**
121
159
  * Preview mint output for a given LST amount
122
160
  * @param lst LST token address
@@ -147,6 +185,122 @@ export class AspanRouterReadClient {
147
185
  });
148
186
  }
149
187
 
188
+ /**
189
+ * SDK-only preview: input token -> estimated LST -> previewMint
190
+ * Note: oracle-based estimation (does not include live DEX slippage/price impact)
191
+ */
192
+ async previewMintByInput(
193
+ inputToken: Address,
194
+ inputAmount: bigint,
195
+ targetLST: Address,
196
+ mintXBNB: boolean
197
+ ): Promise<{ lstAmount: bigint; mintedAmount: bigint }> {
198
+ if (inputAmount === 0n) return { lstAmount: 0n, mintedAmount: 0n };
199
+
200
+ let lstAmount = 0n;
201
+ const inNorm = inputToken.toLowerCase();
202
+
203
+ if (inNorm === targetLST.toLowerCase()) {
204
+ lstAmount = inputAmount;
205
+ } else {
206
+ const [diamond, wbnb, usdt, usdc] = await Promise.all([
207
+ this.getDiamond(),
208
+ this.getWBNB(),
209
+ this.getUSDT(),
210
+ this.getUSDC(),
211
+ ]);
212
+
213
+ const [bnbPrice8, lstPrice] = await Promise.all([
214
+ this.publicClient.readContract({
215
+ address: diamond,
216
+ abi: DiamondABI,
217
+ functionName: "getBNBPriceUSD",
218
+ }),
219
+ this.publicClient.readContract({
220
+ address: diamond,
221
+ abi: DiamondABI,
222
+ functionName: "getLSTPriceUSD",
223
+ args: [targetLST],
224
+ }),
225
+ ]);
226
+
227
+ const bnbPrice18 = BigInt(bnbPrice8) * 10n ** 10n;
228
+ const lstPrice18 = BigInt(lstPrice);
229
+ const one = 10n ** 18n;
230
+
231
+ if (inNorm === zeroAddress.toLowerCase() || inNorm === wbnb.toLowerCase()) {
232
+ // BNB/WBNB -> USD -> LST
233
+ const usdValue = (inputAmount * bnbPrice18) / one;
234
+ lstAmount = lstPrice18 === 0n ? 0n : (usdValue * one) / lstPrice18;
235
+ } else if (inNorm === usdt.toLowerCase() || inNorm === usdc.toLowerCase()) {
236
+ // stablecoin (18-decimal normalized in this SDK path) -> LST
237
+ lstAmount = lstPrice18 === 0n ? 0n : (inputAmount * one) / lstPrice18;
238
+ } else {
239
+ throw new Error("Unsupported input token for SDK preview");
240
+ }
241
+ }
242
+
243
+ const mintedAmount = await this.previewMint(targetLST, lstAmount, mintXBNB);
244
+ return { lstAmount, mintedAmount };
245
+ }
246
+
247
+ /**
248
+ * SDK-only preview: previewRedeem -> estimated output token
249
+ * Note: oracle-based estimation (does not include live DEX slippage/price impact)
250
+ */
251
+ async previewRedeemToOutput(
252
+ lst: Address,
253
+ redeemXBNB: boolean,
254
+ amount: bigint,
255
+ outputToken: Address
256
+ ): Promise<{ lstAmount: bigint; outputAmount: bigint }> {
257
+ const lstAmount = await this.previewRedeem(lst, redeemXBNB, amount);
258
+ if (lstAmount === 0n) return { lstAmount: 0n, outputAmount: 0n };
259
+
260
+ let outputAmount = 0n;
261
+ const outNorm = outputToken.toLowerCase();
262
+
263
+ if (outNorm === lst.toLowerCase()) {
264
+ outputAmount = lstAmount;
265
+ } else {
266
+ const [diamond, wbnb, usdt, usdc] = await Promise.all([
267
+ this.getDiamond(),
268
+ this.getWBNB(),
269
+ this.getUSDT(),
270
+ this.getUSDC(),
271
+ ]);
272
+
273
+ const [bnbPrice8, lstPrice] = await Promise.all([
274
+ this.publicClient.readContract({
275
+ address: diamond,
276
+ abi: DiamondABI,
277
+ functionName: "getBNBPriceUSD",
278
+ }),
279
+ this.publicClient.readContract({
280
+ address: diamond,
281
+ abi: DiamondABI,
282
+ functionName: "getLSTPriceUSD",
283
+ args: [lst],
284
+ }),
285
+ ]);
286
+
287
+ const bnbPrice18 = BigInt(bnbPrice8) * 10n ** 10n;
288
+ const lstPrice18 = BigInt(lstPrice);
289
+ const one = 10n ** 18n;
290
+ const usdValue = (lstAmount * lstPrice18) / one;
291
+
292
+ if (outNorm === zeroAddress.toLowerCase() || outNorm === wbnb.toLowerCase()) {
293
+ outputAmount = bnbPrice18 === 0n ? 0n : (usdValue * one) / bnbPrice18;
294
+ } else if (outNorm === usdt.toLowerCase() || outNorm === usdc.toLowerCase()) {
295
+ outputAmount = usdValue;
296
+ } else {
297
+ throw new Error("Unsupported output token for SDK preview");
298
+ }
299
+ }
300
+
301
+ return { lstAmount, outputAmount };
302
+ }
303
+
150
304
  /**
151
305
  * Get user's withdrawal request indices
152
306
  */
@@ -182,67 +336,83 @@ export class AspanRouterReadClient {
182
336
  // ============ Token Address Getters ============
183
337
 
184
338
  async getWBNB(): Promise<Address> {
185
- return this.publicClient.readContract({
186
- address: this.routerAddress,
187
- abi: RouterABI,
188
- functionName: "wbnb",
189
- });
339
+ return this._getCachedAddress("wbnb", async () =>
340
+ this.publicClient.readContract({
341
+ address: this.routerAddress,
342
+ abi: RouterABI,
343
+ functionName: "wbnb",
344
+ })
345
+ );
190
346
  }
191
347
 
192
348
  async getUSDT(): Promise<Address> {
193
- return this.publicClient.readContract({
194
- address: this.routerAddress,
195
- abi: RouterABI,
196
- functionName: "usdt",
197
- });
349
+ return this._getCachedAddress("usdt", async () =>
350
+ this.publicClient.readContract({
351
+ address: this.routerAddress,
352
+ abi: RouterABI,
353
+ functionName: "usdt",
354
+ })
355
+ );
198
356
  }
199
357
 
200
358
  async getUSDC(): Promise<Address> {
201
- return this.publicClient.readContract({
202
- address: this.routerAddress,
203
- abi: RouterABI,
204
- functionName: "usdc",
205
- });
359
+ return this._getCachedAddress("usdc", async () =>
360
+ this.publicClient.readContract({
361
+ address: this.routerAddress,
362
+ abi: RouterABI,
363
+ functionName: "usdc",
364
+ })
365
+ );
206
366
  }
207
367
 
208
368
  async getSlisBNB(): Promise<Address> {
209
- return this.publicClient.readContract({
210
- address: this.routerAddress,
211
- abi: RouterABI,
212
- functionName: "slisBNB",
213
- });
369
+ return this._getCachedAddress("slisBNB", async () =>
370
+ this.publicClient.readContract({
371
+ address: this.routerAddress,
372
+ abi: RouterABI,
373
+ functionName: "slisBNB",
374
+ })
375
+ );
214
376
  }
215
377
 
216
378
  async getAsBNB(): Promise<Address> {
217
- return this.publicClient.readContract({
218
- address: this.routerAddress,
219
- abi: RouterABI,
220
- functionName: "asBNB",
221
- });
379
+ return this._getCachedAddress("asBNB", async () =>
380
+ this.publicClient.readContract({
381
+ address: this.routerAddress,
382
+ abi: RouterABI,
383
+ functionName: "asBNB",
384
+ })
385
+ );
222
386
  }
223
387
 
224
388
  async getWclisBNB(): Promise<Address> {
225
- return this.publicClient.readContract({
226
- address: this.routerAddress,
227
- abi: RouterABI,
228
- functionName: "wclisBNB",
229
- });
389
+ return this._getCachedAddress("wclisBNB", async () =>
390
+ this.publicClient.readContract({
391
+ address: this.routerAddress,
392
+ abi: RouterABI,
393
+ functionName: "wclisBNB",
394
+ })
395
+ );
230
396
  }
231
397
 
232
398
  async getApUSD(): Promise<Address> {
233
- return this.publicClient.readContract({
234
- address: this.routerAddress,
235
- abi: RouterABI,
236
- functionName: "apUSD",
237
- });
399
+ return this._getCachedAddress("apUSD", async () =>
400
+ this.publicClient.readContract({
401
+ address: this.routerAddress,
402
+ abi: RouterABI,
403
+ functionName: "apUSD",
404
+ })
405
+ );
238
406
  }
239
407
 
240
408
  async getXBNB(): Promise<Address> {
241
- return this.publicClient.readContract({
242
- address: this.routerAddress,
243
- abi: RouterABI,
244
- functionName: "xBNB",
245
- });
409
+ return this._getCachedAddress("xBNB", async () =>
410
+ this.publicClient.readContract({
411
+ address: this.routerAddress,
412
+ abi: RouterABI,
413
+ functionName: "xBNB",
414
+ })
415
+ );
246
416
  }
247
417
  }
248
418
 
package/src/types.ts CHANGED
@@ -128,11 +128,6 @@ export interface WithdrawParams {
128
128
  shares: bigint;
129
129
  }
130
130
 
131
- /** Parameters for withdrawing assets from stability pool */
132
- export interface WithdrawAssetsParams {
133
- assets: bigint;
134
- }
135
-
136
131
  // ============ Protocol Stats ============
137
132
 
138
133
  /** Overall protocol statistics */