|
| 1 | +# Eclipse Lending SDK for Aleo |
| 2 | + |
| 3 | +A TypeScript SDK for interacting with Eclipse Lending contracts on the Aleo blockchain. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @eclipse/lending-sdk |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Initializing Services |
| 14 | + |
| 15 | +All services (`PoolService`, `VaultService`, `StablecoinService`, `AuctionService`) can be initialized in a similar way. You can pass an `AleoLendingClient` instance or an `ApiClientConfig` object to the service constructor if you need to customize the API endpoint or network. If no arguments are provided, a default `AleoLendingClient` will be used. |
| 16 | + |
| 17 | +```typescript |
| 18 | +import { |
| 19 | + PoolService, |
| 20 | + AleoLendingClient, |
| 21 | + type ApiClientConfig, |
| 22 | +} from "@eclipse/lending-sdk"; |
| 23 | + |
| 24 | +// Option 1: Default client |
| 25 | +const poolServiceDefault = new PoolService(); |
| 26 | + |
| 27 | +// Option 2: With a custom API configuration |
| 28 | +const customConfig: ApiClientConfig = { |
| 29 | + baseUrl: "https://api.explorer.provable.com/v1", // Default, but can be changed |
| 30 | + network: "testnet", // Default, can be "mainnet" |
| 31 | +}; |
| 32 | +const poolServiceWithConfig = new PoolService(customConfig); |
| 33 | + |
| 34 | +// Option 3: With an existing AleoLendingClient instance |
| 35 | +const client = new AleoLendingClient(customConfig); |
| 36 | +const poolServiceWithClient = new PoolService(client); |
| 37 | + |
| 38 | +// You can also specify a custom program ID if you are interacting with a cloned program: |
| 39 | +const specificPoolService = new PoolService( |
| 40 | + client, |
| 41 | + "my_custom_pool_program.aleo" |
| 42 | +); |
| 43 | +``` |
| 44 | + |
| 45 | +### Using the PoolService |
| 46 | + |
| 47 | +To retrieve data related to lending pools (default program ID: `eclipse_lending_pair_template.aleo`): |
| 48 | + |
| 49 | +```typescript |
| 50 | +import { PoolService } from "@eclipse/lending-sdk"; |
| 51 | + |
| 52 | +const poolService = new PoolService(); // Uses default client and program ID |
| 53 | + |
| 54 | +// Example: Get the state of an asset pool using its collateral token ID |
| 55 | +async function fetchPoolState(collateralTokenId: string) { |
| 56 | + try { |
| 57 | + const poolState = await poolService.getAssetPoolState(collateralTokenId); |
| 58 | + if (poolState) { |
| 59 | + console.log(`Pool State for token ${collateralTokenId}:`, poolState); |
| 60 | + } else { |
| 61 | + console.log(`Could not fetch pool state for token ${collateralTokenId}.`); |
| 62 | + } |
| 63 | + } catch (error) { |
| 64 | + console.error("Error fetching pool state:", error); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Example: Get a user's position in a pool |
| 69 | +async function fetchUserPoolPosition(userAddress: string) { |
| 70 | + try { |
| 71 | + const userPosition = await poolService.getUserPoolPosition(userAddress); |
| 72 | + if (userPosition) { |
| 73 | + console.log(`User ${userAddress} position:`, userPosition); |
| 74 | + } else { |
| 75 | + console.log(`Could not fetch position for user ${userAddress}.`); |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + console.error("Error fetching user position:", error); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// Example: Get combined pool data |
| 83 | +async function fetchFullPoolData(collateralTokenId: string) { |
| 84 | + try { |
| 85 | + const fullPoolData = await poolService.getPoolData(collateralTokenId); |
| 86 | + if (fullPoolData) { |
| 87 | + console.log( |
| 88 | + `Full Pool Data for token ${collateralTokenId}:`, |
| 89 | + fullPoolData |
| 90 | + ); |
| 91 | + } else { |
| 92 | + console.log( |
| 93 | + `Could not fetch full pool data for token ${collateralTokenId}.` |
| 94 | + ); |
| 95 | + } |
| 96 | + } catch (error) { |
| 97 | + console.error("Error fetching full pool data:", error); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// --- Call example functions --- |
| 102 | +// Replace with actual token IDs and addresses |
| 103 | +// fetchPoolState("12345field"); |
| 104 | +// fetchUserPoolPosition("aleo1youraddresshere..."); |
| 105 | +// fetchFullPoolData("12345field"); |
| 106 | +``` |
| 107 | + |
| 108 | +### Using other Services (Vault, Stablecoin, Auction) |
| 109 | + |
| 110 | +Usage for `VaultService`, `StablecoinService`, and `AuctionService` follows the same pattern as `PoolService`. Instantiate the service, then call its methods. |
| 111 | + |
| 112 | +Example with `VaultService` (default program ID: `eclipse_lending_usda_vault.aleo`): |
| 113 | + |
| 114 | +```typescript |
| 115 | +import { VaultService } from "@eclipse/lending-sdk"; |
| 116 | + |
| 117 | +const vaultService = new VaultService(); |
| 118 | + |
| 119 | +async function fetchVaultPosition(positionId: string) { |
| 120 | + try { |
| 121 | + const positionMeta = await vaultService.getPositionMeta(positionId); |
| 122 | + if (positionMeta) { |
| 123 | + console.log(`Vault Position ${positionId} Metadata:`, positionMeta); |
| 124 | + } else { |
| 125 | + console.log(`Could not fetch metadata for vault position ${positionId}.`); |
| 126 | + } |
| 127 | + } catch (error) { |
| 128 | + console.error("Error fetching vault position metadata:", error); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// fetchVaultPosition("1"); // Replace with an actual position ID |
| 133 | +``` |
| 134 | + |
| 135 | +### Using the AleoLendingClient Directly |
| 136 | + |
| 137 | +You can also use the `AleoLendingClient` to call specific mapping getters if needed. |
| 138 | + |
| 139 | +```typescript |
| 140 | +import { AleoLendingClient } from "@eclipse/lending-sdk"; |
| 141 | + |
| 142 | +// Create a client (can be configured with baseUrl and network) |
| 143 | +const client = new AleoLendingClient(); |
| 144 | + |
| 145 | +const POOL_PROGRAM_ID = "eclipse_lending_pair_template.aleo"; // Or your specific cloned program ID |
| 146 | + |
| 147 | +// Example: Get total supply for a specific token in a pool |
| 148 | +async function getTotalSupplyExample(tokenId: string) { |
| 149 | + try { |
| 150 | + const totalSupply = await client.getTotalSupply(POOL_PROGRAM_ID, tokenId); |
| 151 | + if (totalSupply !== null) { |
| 152 | + console.log(`Total supply for token ${tokenId}:`, totalSupply); |
| 153 | + } else { |
| 154 | + console.log(`Could not fetch total supply for token ${tokenId}.`); |
| 155 | + } |
| 156 | + } catch (error) { |
| 157 | + console.error("Error fetching total supply:", error); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// getTotalSupplyExample("12345field"); // Replace with an actual token ID |
| 162 | +``` |
| 163 | + |
| 164 | +### Utilities |
| 165 | + |
| 166 | +The SDK also exports utility functions: |
| 167 | + |
| 168 | +```typescript |
| 169 | +import { |
| 170 | + convertAddressToField, |
| 171 | + parseUint, |
| 172 | + parseAddress, |
| 173 | + parseBool, |
| 174 | + parseField, |
| 175 | +} from "@eclipse/lending-sdk"; |
| 176 | + |
| 177 | +// Convert an Aleo address to a field element |
| 178 | +const fieldRepresentation = convertAddressToField("aleo1youraddresshere..."); |
| 179 | +// Note: convertAddressToField returns a BigInt, use .toString() for display or further processing as a string. |
| 180 | +console.log("Field representation:", fieldRepresentation.toString()); |
| 181 | + |
| 182 | +// Parsing functions are used internally by the services but can be used directly if needed. |
| 183 | +``` |
| 184 | + |
| 185 | +## API Reference |
| 186 | + |
| 187 | +This section provides a summary of the main classes and methods. For detailed parameter types, return types, and descriptions, please refer to the JSDoc comments in the source code or the generated TypeDoc documentation. |
| 188 | + |
| 189 | +### `AleoLendingClient` |
| 190 | + |
| 191 | +Low-level client for interacting with Aleo lending contracts via an Aleo Explorer API. |
| 192 | + |
| 193 | +- `constructor(config?: ApiClientConfig)` |
| 194 | +- `getMappingValue(program: string, mapping: string, key: string): Promise<string | null>`: Generic method to fetch a raw mapping value. |
| 195 | + |
| 196 | +#### Pool Mappings (on `AleoLendingClient`) |
| 197 | + |
| 198 | +_(Default Program: `eclipse_lending_pair_template.aleo`)_ |
| 199 | + |
| 200 | +- `getTotalSupply(programId: string, tokenId: string): Promise<number | null>` |
| 201 | +- `getTotalBorrow(programId: string, tokenId: string): Promise<number | null>` |
| 202 | +- `getIndexSupply(programId: string, tokenId: string): Promise<number | null>` |
| 203 | +- `getIndexBorrow(programId: string, tokenId: string): Promise<number | null>` |
| 204 | +- `getLastBlock(programId: string, tokenId: string): Promise<number | null>` |
| 205 | +- `getPrincipalSupply(programId: string, userAddress: string): Promise<number | null>` |
| 206 | +- `getSnapshotSupply(programId: string, userAddress: string): Promise<number | null>` |
| 207 | +- `getPrincipalBorrow(programId: string, userAddress: string): Promise<number | null>` |
| 208 | +- `getSnapshotBorrow(programId: string, userAddress: string): Promise<number | null>` |
| 209 | + |
| 210 | +#### Vault Mappings (on `AleoLendingClient`) |
| 211 | + |
| 212 | +_(Default Program: `eclipse_lending_usda_vault.aleo`)_ |
| 213 | + |
| 214 | +- `getPrivCommit(programId: string, positionId: string): Promise<string | null>` |
| 215 | +- `getIsLiquidated(programId: string, positionId: string): Promise<boolean | null>` |
| 216 | +- `getPositionMeta(programId: string, positionId: string): Promise<VaultPositionMeta | null>` |
| 217 | +- `getNextPositionId(programId: string, keyField?: string): Promise<number | null>` |
| 218 | +- `getLiquidationRefund(programId: string, positionId: string): Promise<number | null>` |
| 219 | +- `getLiquidationCollateral(programId: string, positionId: string): Promise<number | null>` |
| 220 | + |
| 221 | +#### Stablecoin Mappings (on `AleoLendingClient`) |
| 222 | + |
| 223 | +_(Default Program: `floflo_stablecoin_vault.aleo`)_ |
| 224 | + |
| 225 | +- `getCollateralBalance(programId: string, userAddress: string): Promise<number | null>` |
| 226 | +- `getDebtBalance(programId: string, userAddress: string): Promise<number | null>` |
| 227 | + |
| 228 | +#### Auction Mappings (on `AleoLendingClient`) |
| 229 | + |
| 230 | +_(Default Program: `eclipse_lending_auction.aleo`)_ |
| 231 | + |
| 232 | +- `getAuction(programId: string, auctionId: string): Promise<AuctionData | null>` |
| 233 | +- `getNextAuctionId(programId: string, keyField?: string): Promise<number | null>` |
| 234 | + |
| 235 | +### `PoolService` |
| 236 | + |
| 237 | +Service for retrieving data related to lending pools. |
| 238 | + |
| 239 | +- `constructor(clientOrConfig?: AleoLendingClient | ApiClientConfig, programId?: string)` |
| 240 | +- `getAssetPoolState(tokenId: string): Promise<AssetPoolState | null>` |
| 241 | +- `getUserPoolPosition(userAddress: string): Promise<UserPoolPosition | null>` |
| 242 | +- `getPoolData(collateralTokenId: string): Promise<PoolData | null>` |
| 243 | + |
| 244 | +### `VaultService` |
| 245 | + |
| 246 | +Service for retrieving data related to lending vaults. |
| 247 | + |
| 248 | +- `constructor(clientOrConfig?: AleoLendingClient | ApiClientConfig, programId?: string)` |
| 249 | +- `getPositionMeta(positionId: string): Promise<VaultPositionMeta | null>` |
| 250 | +- `isPositionLiquidated(positionId: string): Promise<boolean | null>` |
| 251 | +- `getUserVault(positionId: string, owner?: string): Promise<UserVault | null>` |
| 252 | +- `getLiquidationInfo(positionId: string): Promise<VaultLiquidationInfo | null>` |
| 253 | +- `getNextPositionId(nextIdKey?: string): Promise<number | null>` |
| 254 | +- `getGlobalVaultState(nextIdKey?: string): Promise<GlobalVaultState | null>` |
| 255 | +- `getPrivCommit(positionId: string): Promise<string | null>` |
| 256 | + |
| 257 | +### `StablecoinService` |
| 258 | + |
| 259 | +Service for retrieving data related to a stablecoin contract. |
| 260 | + |
| 261 | +- `constructor(clientOrConfig?: AleoLendingClient | ApiClientConfig, programId?: string)` |
| 262 | +- `getUserBalances(userAddress: string): Promise<UserStablecoinBalances | null>` |
| 263 | +- `getStablecoinInfo(): Promise<StablecoinInfo | null>`: _(Placeholder - data typically from contract constants, not readable from mappings)._ |
| 264 | + |
| 265 | +### `AuctionService` |
| 266 | + |
| 267 | +Service for retrieving data related to lending auctions. |
| 268 | + |
| 269 | +- `constructor(clientOrConfig?: AleoLendingClient | ApiClientConfig, programId?: string)` |
| 270 | +- `getAuctionData(auctionId: string): Promise<AuctionData | null>` |
| 271 | +- `getNextAuctionId(nextIdKey?: string): Promise<number | null>` |
| 272 | +- `getGlobalAuctionState(nextIdKey?: string): Promise<GlobalAuctionState | null>` |
| 273 | + |
| 274 | +### Utility Functions |
| 275 | + |
| 276 | +- `convertAddressToField(address: string): bigint`: Converts an Aleo address string to its `field` representation as a BigInt. |
| 277 | +- `parseUint(raw: string | null, type: "u128" | "u64" | ...): number`: Parses various uint types from string. |
| 278 | +- `parseBool(raw: string | null): boolean | null`: Parses boolean from string. |
| 279 | +- `parseField(raw: string | null): string | null`: Parses field from string. |
| 280 | +- `parseAddress(raw: string | null): string | null`: Parses Aleo address from string. |
| 281 | + |
| 282 | + |
| 283 | +## License |
| 284 | + |
| 285 | +ISC |
0 commit comments