@aspan/sdk 0.4.7 → 0.4.9

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/src/router.ts CHANGED
@@ -59,6 +59,7 @@ export class AspanRouterReadClient {
59
59
  protected readonly publicClient: PublicClient;
60
60
  protected readonly routerAddress: Address;
61
61
  protected readonly chain: Chain;
62
+ private readonly _addressCache = new Map<string, Address>();
62
63
 
63
64
  constructor(config: AspanRouterClientConfig) {
64
65
  this.routerAddress = config.routerAddress;
@@ -67,9 +68,18 @@ export class AspanRouterReadClient {
67
68
  this.publicClient = createPublicClient({
68
69
  chain: this.chain,
69
70
  transport: http(config.rpcUrl),
71
+ batch: { multicall: true },
70
72
  });
71
73
  }
72
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
+
73
83
  // ============ View Functions ============
74
84
 
75
85
  /**
@@ -136,11 +146,13 @@ export class AspanRouterReadClient {
136
146
  * Get the Diamond contract address
137
147
  */
138
148
  async getDiamond(): Promise<Address> {
139
- return this.publicClient.readContract({
140
- address: this.routerAddress,
141
- abi: RouterABI,
142
- functionName: "diamond",
143
- });
149
+ return this._getCachedAddress("diamond", async () =>
150
+ this.publicClient.readContract({
151
+ address: this.routerAddress,
152
+ abi: RouterABI,
153
+ functionName: "diamond",
154
+ })
155
+ );
144
156
  }
145
157
 
146
158
  /**
@@ -185,44 +197,47 @@ export class AspanRouterReadClient {
185
197
  ): Promise<{ lstAmount: bigint; mintedAmount: bigint }> {
186
198
  if (inputAmount === 0n) return { lstAmount: 0n, mintedAmount: 0n };
187
199
 
188
- const [diamond, wbnb, usdt, usdc] = await Promise.all([
189
- this.getDiamond(),
190
- this.getWBNB(),
191
- this.getUSDT(),
192
- this.getUSDC(),
193
- ]);
194
-
195
- const [bnbPrice8, lstPrice] = await Promise.all([
196
- this.publicClient.readContract({
197
- address: diamond,
198
- abi: DiamondABI,
199
- functionName: "getBNBPriceUSD",
200
- }),
201
- this.publicClient.readContract({
202
- address: diamond,
203
- abi: DiamondABI,
204
- functionName: "getLSTPriceUSD",
205
- args: [targetLST],
206
- }),
207
- ]);
208
-
209
- const bnbPrice18 = BigInt(bnbPrice8) * 10n ** 10n;
210
- const lstPrice18 = BigInt(lstPrice);
211
- const one = 10n ** 18n;
212
-
213
200
  let lstAmount = 0n;
214
201
  const inNorm = inputToken.toLowerCase();
202
+
215
203
  if (inNorm === targetLST.toLowerCase()) {
216
204
  lstAmount = inputAmount;
217
- } else if (inNorm === zeroAddress.toLowerCase() || inNorm === wbnb.toLowerCase()) {
218
- // BNB/WBNB -> USD -> LST
219
- const usdValue = (inputAmount * bnbPrice18) / one;
220
- lstAmount = lstPrice18 === 0n ? 0n : (usdValue * one) / lstPrice18;
221
- } else if (inNorm === usdt.toLowerCase() || inNorm === usdc.toLowerCase()) {
222
- // stablecoin (18-decimal normalized in this SDK path) -> LST
223
- lstAmount = lstPrice18 === 0n ? 0n : (inputAmount * one) / lstPrice18;
224
205
  } else {
225
- throw new Error("Unsupported input token for SDK preview");
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
+ }
226
241
  }
227
242
 
228
243
  const mintedAmount = await this.previewMint(targetLST, lstAmount, mintXBNB);
@@ -242,42 +257,45 @@ export class AspanRouterReadClient {
242
257
  const lstAmount = await this.previewRedeem(lst, redeemXBNB, amount);
243
258
  if (lstAmount === 0n) return { lstAmount: 0n, outputAmount: 0n };
244
259
 
245
- const [diamond, wbnb, usdt, usdc] = await Promise.all([
246
- this.getDiamond(),
247
- this.getWBNB(),
248
- this.getUSDT(),
249
- this.getUSDC(),
250
- ]);
251
-
252
- const [bnbPrice8, lstPrice] = await Promise.all([
253
- this.publicClient.readContract({
254
- address: diamond,
255
- abi: DiamondABI,
256
- functionName: "getBNBPriceUSD",
257
- }),
258
- this.publicClient.readContract({
259
- address: diamond,
260
- abi: DiamondABI,
261
- functionName: "getLSTPriceUSD",
262
- args: [lst],
263
- }),
264
- ]);
265
-
266
- const bnbPrice18 = BigInt(bnbPrice8) * 10n ** 10n;
267
- const lstPrice18 = BigInt(lstPrice);
268
- const one = 10n ** 18n;
269
- const usdValue = (lstAmount * lstPrice18) / one;
270
-
271
260
  let outputAmount = 0n;
272
261
  const outNorm = outputToken.toLowerCase();
262
+
273
263
  if (outNorm === lst.toLowerCase()) {
274
264
  outputAmount = lstAmount;
275
- } else if (outNorm === zeroAddress.toLowerCase() || outNorm === wbnb.toLowerCase()) {
276
- outputAmount = bnbPrice18 === 0n ? 0n : (usdValue * one) / bnbPrice18;
277
- } else if (outNorm === usdt.toLowerCase() || outNorm === usdc.toLowerCase()) {
278
- outputAmount = usdValue;
279
265
  } else {
280
- throw new Error("Unsupported output token for SDK preview");
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
+ }
281
299
  }
282
300
 
283
301
  return { lstAmount, outputAmount };
@@ -318,67 +336,83 @@ export class AspanRouterReadClient {
318
336
  // ============ Token Address Getters ============
319
337
 
320
338
  async getWBNB(): Promise<Address> {
321
- return this.publicClient.readContract({
322
- address: this.routerAddress,
323
- abi: RouterABI,
324
- functionName: "wbnb",
325
- });
339
+ return this._getCachedAddress("wbnb", async () =>
340
+ this.publicClient.readContract({
341
+ address: this.routerAddress,
342
+ abi: RouterABI,
343
+ functionName: "wbnb",
344
+ })
345
+ );
326
346
  }
327
347
 
328
348
  async getUSDT(): Promise<Address> {
329
- return this.publicClient.readContract({
330
- address: this.routerAddress,
331
- abi: RouterABI,
332
- functionName: "usdt",
333
- });
349
+ return this._getCachedAddress("usdt", async () =>
350
+ this.publicClient.readContract({
351
+ address: this.routerAddress,
352
+ abi: RouterABI,
353
+ functionName: "usdt",
354
+ })
355
+ );
334
356
  }
335
357
 
336
358
  async getUSDC(): Promise<Address> {
337
- return this.publicClient.readContract({
338
- address: this.routerAddress,
339
- abi: RouterABI,
340
- functionName: "usdc",
341
- });
359
+ return this._getCachedAddress("usdc", async () =>
360
+ this.publicClient.readContract({
361
+ address: this.routerAddress,
362
+ abi: RouterABI,
363
+ functionName: "usdc",
364
+ })
365
+ );
342
366
  }
343
367
 
344
368
  async getSlisBNB(): Promise<Address> {
345
- return this.publicClient.readContract({
346
- address: this.routerAddress,
347
- abi: RouterABI,
348
- functionName: "slisBNB",
349
- });
369
+ return this._getCachedAddress("slisBNB", async () =>
370
+ this.publicClient.readContract({
371
+ address: this.routerAddress,
372
+ abi: RouterABI,
373
+ functionName: "slisBNB",
374
+ })
375
+ );
350
376
  }
351
377
 
352
378
  async getAsBNB(): Promise<Address> {
353
- return this.publicClient.readContract({
354
- address: this.routerAddress,
355
- abi: RouterABI,
356
- functionName: "asBNB",
357
- });
379
+ return this._getCachedAddress("asBNB", async () =>
380
+ this.publicClient.readContract({
381
+ address: this.routerAddress,
382
+ abi: RouterABI,
383
+ functionName: "asBNB",
384
+ })
385
+ );
358
386
  }
359
387
 
360
388
  async getWclisBNB(): Promise<Address> {
361
- return this.publicClient.readContract({
362
- address: this.routerAddress,
363
- abi: RouterABI,
364
- functionName: "wclisBNB",
365
- });
389
+ return this._getCachedAddress("wclisBNB", async () =>
390
+ this.publicClient.readContract({
391
+ address: this.routerAddress,
392
+ abi: RouterABI,
393
+ functionName: "wclisBNB",
394
+ })
395
+ );
366
396
  }
367
397
 
368
398
  async getApUSD(): Promise<Address> {
369
- return this.publicClient.readContract({
370
- address: this.routerAddress,
371
- abi: RouterABI,
372
- functionName: "apUSD",
373
- });
399
+ return this._getCachedAddress("apUSD", async () =>
400
+ this.publicClient.readContract({
401
+ address: this.routerAddress,
402
+ abi: RouterABI,
403
+ functionName: "apUSD",
404
+ })
405
+ );
374
406
  }
375
407
 
376
408
  async getXBNB(): Promise<Address> {
377
- return this.publicClient.readContract({
378
- address: this.routerAddress,
379
- abi: RouterABI,
380
- functionName: "xBNB",
381
- });
409
+ return this._getCachedAddress("xBNB", async () =>
410
+ this.publicClient.readContract({
411
+ address: this.routerAddress,
412
+ abi: RouterABI,
413
+ functionName: "xBNB",
414
+ })
415
+ );
382
416
  }
383
417
  }
384
418
 
package/src/types.ts CHANGED
@@ -120,12 +120,15 @@ export interface RedeemXBNBParams {
120
120
 
121
121
  /** Parameters for depositing to stability pool */
122
122
  export interface DepositParams {
123
- apUSDAmount: bigint;
123
+ assets: bigint;
124
+ receiver: Address;
124
125
  }
125
126
 
126
- /** Parameters for withdrawing from stability pool */
127
- export interface WithdrawParams {
127
+ /** Parameters for redeeming from stability pool */
128
+ export interface RedeemParams {
128
129
  shares: bigint;
130
+ receiver: Address;
131
+ owner: Address;
129
132
  }
130
133
 
131
134
  // ============ Protocol Stats ============