@aspan/sdk 0.1.4

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/index.ts ADDED
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Aspan SDK
3
+ * TypeScript SDK for interacting with the Aspan Protocol on BNB Chain
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+
8
+ // ============ Client Classes ============
9
+ export {
10
+ AspanReadClient,
11
+ AspanClient,
12
+ createAspanReadClient,
13
+ createAspanClient,
14
+ createAspanTestnetReadClient,
15
+ createAspanTestnetClient,
16
+ type AspanClientConfig,
17
+ type AspanWriteClientConfig,
18
+ } from "./client";
19
+
20
+ // ============ Types ============
21
+ export type {
22
+ // Core types
23
+ BigIntString,
24
+ TokenAddresses,
25
+ LSTInfo,
26
+ LSTYieldInfo,
27
+ FeeTier,
28
+ CurrentFeeTier,
29
+ CurrentFees,
30
+ OracleBounds,
31
+ StabilityModeInfo,
32
+ StabilityMode2Info,
33
+ // Stats
34
+ ProtocolStats,
35
+ StabilityPoolStats,
36
+ YieldStats,
37
+ UserStabilityPoolPosition,
38
+ // Transaction params
39
+ MintApUSDParams,
40
+ RedeemApUSDParams,
41
+ MintXBNBParams,
42
+ RedeemXBNBParams,
43
+ DepositParams,
44
+ WithdrawParams,
45
+ WithdrawAssetsParams,
46
+ // Events
47
+ ApUSDMintedEvent,
48
+ ApUSDRedeemedEvent,
49
+ XBNBMintedEvent,
50
+ XBNBRedeemedEvent,
51
+ DepositedEvent,
52
+ WithdrawnEvent,
53
+ YieldHarvestedEvent,
54
+ // Transaction result
55
+ TransactionResult,
56
+ TransactionReceipt,
57
+ } from "./types";
58
+
59
+ // ============ ABI ============
60
+ export { DiamondABI } from "./abi/diamond";
61
+
62
+ // ============ Constants ============
63
+ export const PRECISION = 10n ** 18n;
64
+ export const BPS_PRECISION = 10000n;
65
+ export const PRICE_PRECISION = 10n ** 8n; // Chainlink price precision
66
+
67
+ // ============ Utility Functions ============
68
+
69
+ /**
70
+ * Format a bigint amount to human-readable string with decimals
71
+ * @param amount Amount in wei (18 decimals)
72
+ * @param decimals Number of decimal places to show
73
+ * @returns Formatted string
74
+ */
75
+ export function formatAmount(amount: bigint, decimals: number = 4): string {
76
+ const divisor = 10n ** BigInt(18 - decimals);
77
+ const scaled = amount / divisor;
78
+ const intPart = scaled / 10n ** BigInt(decimals);
79
+ const fracPart = scaled % 10n ** BigInt(decimals);
80
+ return `${intPart}.${fracPart.toString().padStart(decimals, "0")}`;
81
+ }
82
+
83
+ /**
84
+ * Parse a human-readable amount to bigint (18 decimals)
85
+ * @param amount Human-readable amount string
86
+ * @returns Amount in wei
87
+ */
88
+ export function parseAmount(amount: string): bigint {
89
+ const [intPart, fracPart = ""] = amount.split(".");
90
+ const paddedFrac = fracPart.padEnd(18, "0").slice(0, 18);
91
+ return BigInt(intPart) * PRECISION + BigInt(paddedFrac);
92
+ }
93
+
94
+ /**
95
+ * Format a fee in basis points to percentage string
96
+ * @param bps Fee in basis points
97
+ * @returns Percentage string (e.g., "0.25%")
98
+ */
99
+ export function formatFeeBPS(bps: number): string {
100
+ return `${(bps / 100).toFixed(2)}%`;
101
+ }
102
+
103
+ /**
104
+ * Format collateral ratio to percentage string
105
+ * @param cr Collateral ratio (18 decimals, 1e18 = 100%)
106
+ * @returns Percentage string (e.g., "150%")
107
+ */
108
+ export function formatCR(cr: bigint): string {
109
+ const percentage = (cr * 100n) / PRECISION;
110
+ return `${percentage}%`;
111
+ }
112
+
113
+ /**
114
+ * Format price (8 decimals) to USD string
115
+ * @param price Price in 8 decimals
116
+ * @returns USD string (e.g., "$583.25")
117
+ */
118
+ export function formatPriceUSD(price: bigint): string {
119
+ const dollars = price / PRICE_PRECISION;
120
+ const cents = (price % PRICE_PRECISION) / 10n ** 6n;
121
+ return `$${dollars}.${cents.toString().padStart(2, "0")}`;
122
+ }
123
+
124
+ /**
125
+ * Calculate effective APY from exchange rate change
126
+ * @param previousRate Previous exchange rate
127
+ * @param currentRate Current exchange rate
128
+ * @param periodDays Number of days between rates
129
+ * @returns Annualized percentage yield
130
+ */
131
+ export function calculateAPY(
132
+ previousRate: bigint,
133
+ currentRate: bigint,
134
+ periodDays: number
135
+ ): number {
136
+ if (previousRate === 0n || periodDays === 0) return 0;
137
+
138
+ const rateChange =
139
+ Number((currentRate - previousRate) * 10000n) / Number(previousRate);
140
+ const dailyRate = rateChange / periodDays;
141
+ const annualizedRate = dailyRate * 365;
142
+
143
+ return annualizedRate / 100; // Convert from percentage basis points
144
+ }
package/src/types.ts ADDED
@@ -0,0 +1,237 @@
1
+ /**
2
+ * Aspan SDK Type Definitions
3
+ */
4
+
5
+ import type { Address, Hash } from "viem";
6
+
7
+ // ============ Core Types ============
8
+
9
+ /** BigInt formatted as string for serialization */
10
+ export type BigIntString = string;
11
+
12
+ /** Token addresses */
13
+ export interface TokenAddresses {
14
+ apUSD: Address;
15
+ xBNB: Address;
16
+ sApUSD: Address;
17
+ }
18
+
19
+ /** LST (Liquid Staking Token) info */
20
+ export interface LSTInfo {
21
+ isAccepted: boolean;
22
+ priceFeed: Address;
23
+ manualPriceUSD: bigint;
24
+ collateralAmount: bigint;
25
+ decimals: number;
26
+ exchangeRateProvider: Address;
27
+ useIntrinsicValue: boolean;
28
+ }
29
+
30
+ /** LST yield tracking info */
31
+ export interface LSTYieldInfo {
32
+ lastExchangeRate: bigint;
33
+ lastUpdateTimestamp: bigint;
34
+ }
35
+
36
+ /** Fee tier configuration */
37
+ export interface FeeTier {
38
+ minCR: bigint;
39
+ apUSDMintFee: number;
40
+ apUSDRedeemFee: number;
41
+ xBNBMintFee: number;
42
+ xBNBRedeemFee: number;
43
+ apUSDMintDisabled: boolean;
44
+ }
45
+
46
+ /** Current fee tier with CR */
47
+ export interface CurrentFeeTier extends FeeTier {
48
+ currentCR: bigint;
49
+ }
50
+
51
+ /** Current fees with tier info */
52
+ export interface CurrentFees {
53
+ currentCR: bigint;
54
+ tierMinCR: bigint;
55
+ apUSDMintFee: number;
56
+ apUSDRedeemFee: number;
57
+ xBNBMintFee: number;
58
+ xBNBRedeemFee: number;
59
+ apUSDMintDisabled: boolean;
60
+ }
61
+
62
+ /** Oracle bounds */
63
+ export interface OracleBounds {
64
+ minPrice: bigint;
65
+ maxPrice: bigint;
66
+ }
67
+
68
+ /** Stability mode info */
69
+ export interface StabilityModeInfo {
70
+ mode: number;
71
+ currentCR: bigint;
72
+ }
73
+
74
+ /** Stability mode 2 trigger info */
75
+ export interface StabilityMode2Info {
76
+ canTrigger: boolean;
77
+ currentCR: bigint;
78
+ potentialConversion: bigint;
79
+ }
80
+
81
+ /** Stability mode 2 trigger result */
82
+ export interface StabilityMode2Result {
83
+ apUSDBurned: bigint;
84
+ xBNBMinted: bigint;
85
+ }
86
+
87
+ // ============ Transaction Parameters ============
88
+
89
+ /** Parameters for minting apUSD */
90
+ export interface MintApUSDParams {
91
+ lstToken: Address;
92
+ lstAmount: bigint;
93
+ }
94
+
95
+ /** Parameters for redeeming apUSD */
96
+ export interface RedeemApUSDParams {
97
+ lstToken: Address;
98
+ apUSDAmount: bigint;
99
+ }
100
+
101
+ /** Parameters for minting xBNB */
102
+ export interface MintXBNBParams {
103
+ lstToken: Address;
104
+ lstAmount: bigint;
105
+ }
106
+
107
+ /** Parameters for redeeming xBNB */
108
+ export interface RedeemXBNBParams {
109
+ lstToken: Address;
110
+ xBNBAmount: bigint;
111
+ }
112
+
113
+ /** Parameters for depositing to stability pool */
114
+ export interface DepositParams {
115
+ apUSDAmount: bigint;
116
+ }
117
+
118
+ /** Parameters for withdrawing from stability pool */
119
+ export interface WithdrawParams {
120
+ shares: bigint;
121
+ }
122
+
123
+ /** Parameters for withdrawing assets from stability pool */
124
+ export interface WithdrawAssetsParams {
125
+ assets: bigint;
126
+ }
127
+
128
+ // ============ Protocol Stats ============
129
+
130
+ /** Overall protocol statistics */
131
+ export interface ProtocolStats {
132
+ tvlInBNB: bigint;
133
+ tvlInUSD: bigint;
134
+ collateralRatio: bigint;
135
+ apUSDSupply: bigint;
136
+ xBNBSupply: bigint;
137
+ xBNBPriceBNB: bigint;
138
+ xBNBPriceUSD: bigint;
139
+ effectiveLeverage: bigint;
140
+ }
141
+
142
+ /** Stability pool statistics */
143
+ export interface StabilityPoolStats {
144
+ totalStaked: bigint;
145
+ exchangeRate: bigint;
146
+ pendingYield: bigint;
147
+ }
148
+
149
+ /** Yield statistics */
150
+ export interface YieldStats {
151
+ totalYieldGenerated: bigint;
152
+ pendingYield: bigint;
153
+ lastHarvestTimestamp: bigint;
154
+ minHarvestInterval: bigint;
155
+ previewedHarvest: bigint;
156
+ }
157
+
158
+ // ============ User Position ============
159
+
160
+ /** User's position in the stability pool */
161
+ export interface UserStabilityPoolPosition {
162
+ shares: bigint;
163
+ balance: bigint;
164
+ }
165
+
166
+ // ============ Event Types ============
167
+
168
+ /** ApUSD minted event */
169
+ export interface ApUSDMintedEvent {
170
+ user: Address;
171
+ lstToken: Address;
172
+ lstAmount: bigint;
173
+ apUSDAmount: bigint;
174
+ feeBPS: bigint;
175
+ }
176
+
177
+ /** ApUSD redeemed event */
178
+ export interface ApUSDRedeemedEvent {
179
+ user: Address;
180
+ lstToken: Address;
181
+ apUSDAmount: bigint;
182
+ lstAmount: bigint;
183
+ feeBPS: bigint;
184
+ }
185
+
186
+ /** xBNB minted event */
187
+ export interface XBNBMintedEvent {
188
+ user: Address;
189
+ lstToken: Address;
190
+ lstAmount: bigint;
191
+ xBNBAmount: bigint;
192
+ feeBPS: bigint;
193
+ }
194
+
195
+ /** xBNB redeemed event */
196
+ export interface XBNBRedeemedEvent {
197
+ user: Address;
198
+ lstToken: Address;
199
+ xBNBAmount: bigint;
200
+ lstAmount: bigint;
201
+ feeBPS: bigint;
202
+ }
203
+
204
+ /** Deposited to stability pool event */
205
+ export interface DepositedEvent {
206
+ user: Address;
207
+ apUSDAmount: bigint;
208
+ sharesReceived: bigint;
209
+ }
210
+
211
+ /** Withdrawn from stability pool event */
212
+ export interface WithdrawnEvent {
213
+ user: Address;
214
+ sharesRedeemed: bigint;
215
+ apUSDReceived: bigint;
216
+ }
217
+
218
+ /** Yield harvested event */
219
+ export interface YieldHarvestedEvent {
220
+ yieldUSD: bigint;
221
+ }
222
+
223
+ // ============ Transaction Result ============
224
+
225
+ /** Result of a transaction */
226
+ export interface TransactionResult {
227
+ hash: Hash;
228
+ wait: () => Promise<TransactionReceipt>;
229
+ }
230
+
231
+ /** Transaction receipt */
232
+ export interface TransactionReceipt {
233
+ blockHash: Hash;
234
+ blockNumber: bigint;
235
+ transactionHash: Hash;
236
+ status: "success" | "reverted";
237
+ }