@liquidium/client 0.1.0
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 +289 -0
- package/dist/index.cjs +5833 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2216 -0
- package/dist/index.d.ts +2216 -0
- package/dist/index.js +5802 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/README.md
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+
|
|
4
|
+
<p align="center">
|
|
5
|
+
<img src="../../borrow.svg" alt="Liquidium borrow illustration" width="700" />
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
<h1 align="center">Liquidium SDK</h1>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
Use the Liquidium SDK from TypeScript apps. Start with accountless instant loans.
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="https://github.com/Liquidium-Inc/liquidium-sdk/tree/main/examples/instant-loans-flow"><b>Instant Loan Example</b></a> ·
|
|
16
|
+
<a href="https://github.com/Liquidium-Inc/liquidium-sdk/tree/main/examples/vite-react-dynamic"><b>SDK Method Query Example</b></a> ·
|
|
17
|
+
<a href="#core-api"><b>Core API</b></a>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
## Why Start With Instant Loans
|
|
21
|
+
|
|
22
|
+
- **Accountless loan creation**: create instant loans without requiring a Liquidium profile.
|
|
23
|
+
- **Generated transfer targets**: get collateral deposit and repayment targets from the SDK response.
|
|
24
|
+
- **Reference-based restore**: save `loan.ref` and reload loan state from any browser session or support link.
|
|
25
|
+
- **LTV checks**: use market pools and prices to validate collateral and borrow amounts before loan creation.
|
|
26
|
+
- **Status and repayment quote**: reload lifecycle status, position state, and repayment amount in one call.
|
|
27
|
+
|
|
28
|
+
## Integration Paths
|
|
29
|
+
|
|
30
|
+
Liquidium supports two lending integration paths:
|
|
31
|
+
|
|
32
|
+
| Path | Use when | Main SDK calls |
|
|
33
|
+
| --- | --- | --- |
|
|
34
|
+
| Recommended: accountless instant loans | You want a short checkout-style flow where users borrow against collateral without creating a Liquidium profile first | `client.instantLoans.create(...)`, `client.instantLoans.get(...)`, `client.activities.list(...)` |
|
|
35
|
+
| Account-based profile flows | You want users to keep a Liquidium profile, manage positions across sessions, supply funds, borrow, withdraw, or use ETH contract-interaction deposits | `client.accounts.createProfile(...)`, `client.lending.supply(...)`, `client.lending.borrow(...)`, `client.lending.withdraw(...)`, `client.positions.list(...)` |
|
|
36
|
+
|
|
37
|
+
Use accountless instant loans for new borrow flows unless you need profile-level position management. Use account-based flows when your app owns the full lending dashboard experience.
|
|
38
|
+
|
|
39
|
+
## Quick Start
|
|
40
|
+
|
|
41
|
+
Install the client package:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm install @liquidium/client
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Create an instant loan, display the deposit target, and restore the loan by reference:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { LiquidiumClient, type Pool, type SupplyTarget } from "@liquidium/client";
|
|
51
|
+
|
|
52
|
+
const client = new LiquidiumClient();
|
|
53
|
+
|
|
54
|
+
const [pools, prices] = await Promise.all([
|
|
55
|
+
client.market.listPools(),
|
|
56
|
+
client.market.getAssetPrices(),
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
const collateralPool = requirePool(pools, "BTC");
|
|
60
|
+
const borrowPool = requirePool(pools, "USDC");
|
|
61
|
+
|
|
62
|
+
const collateralAmount = 50_000n; // BTC sats
|
|
63
|
+
const borrowAmount = 9_000_000n; // USDC base units
|
|
64
|
+
|
|
65
|
+
const ltv = client.quote.calculateLtv(
|
|
66
|
+
{
|
|
67
|
+
collateralPoolId: collateralPool.id,
|
|
68
|
+
borrowPoolId: borrowPool.id,
|
|
69
|
+
collateralAmount,
|
|
70
|
+
borrowAmount,
|
|
71
|
+
},
|
|
72
|
+
pools,
|
|
73
|
+
prices
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
if (ltv.validationErrors.length > 0) {
|
|
77
|
+
throw new Error(ltv.validationErrors.map((error) => error.message).join(" "));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const loan = await client.instantLoans.create({
|
|
81
|
+
collateralPoolId: collateralPool.id,
|
|
82
|
+
borrowPoolId: borrowPool.id,
|
|
83
|
+
collateralAsset: "BTC",
|
|
84
|
+
borrowAsset: "USDC",
|
|
85
|
+
collateralAmount,
|
|
86
|
+
borrowAmount,
|
|
87
|
+
ltvMaxBps: ltv.maxAllowedLtvBps,
|
|
88
|
+
depositWindowSeconds: 3_600n,
|
|
89
|
+
borrowDestination: {
|
|
90
|
+
type: "External",
|
|
91
|
+
address: "0x2222222222222222222222222222222222222222",
|
|
92
|
+
},
|
|
93
|
+
refundDestination: {
|
|
94
|
+
type: "External",
|
|
95
|
+
address: "bc1qrefunddestination",
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
console.log("Save this loan reference:", loan.ref);
|
|
100
|
+
console.log("Send collateral to:", formatSupplyTarget(loan.depositTarget));
|
|
101
|
+
|
|
102
|
+
const restoredLoan = await client.instantLoans.get({ ref: loan.ref });
|
|
103
|
+
|
|
104
|
+
console.log("Loan status:", restoredLoan.status);
|
|
105
|
+
console.log("Repay amount:", restoredLoan.repayment.amount.toString());
|
|
106
|
+
console.log("Repay target:", formatSupplyTarget(restoredLoan.repayment.target));
|
|
107
|
+
|
|
108
|
+
const activities = await client.activities.list({ shortRef: loan.ref });
|
|
109
|
+
|
|
110
|
+
console.log("Loan activities:", activities);
|
|
111
|
+
|
|
112
|
+
function requirePool(pools: Pool[], asset: string): Pool {
|
|
113
|
+
const pool = pools.find((candidatePool) => candidatePool.asset === asset);
|
|
114
|
+
|
|
115
|
+
if (!pool) {
|
|
116
|
+
throw new Error(`Missing ${asset} pool.`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (pool.frozen) {
|
|
120
|
+
throw new Error(`${asset} pool is frozen.`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return pool;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function formatSupplyTarget(target: SupplyTarget): string {
|
|
127
|
+
if (target.type === "nativeAddress") {
|
|
128
|
+
return target.address;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return target.account;
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Instant Loan Flow
|
|
136
|
+
|
|
137
|
+
Instant-loan integrations use this sequence:
|
|
138
|
+
|
|
139
|
+
| Step | SDK call | What your app does |
|
|
140
|
+
| --- | --- | --- |
|
|
141
|
+
| Load market data | `client.market.listPools()` and `client.market.getAssetPrices()` | Show supported collateral and borrow assets |
|
|
142
|
+
| Validate amounts | `client.quote.calculateLtv(...)` | Block invalid LTV or frozen-pool input before creating a loan |
|
|
143
|
+
| Create loan | `client.instantLoans.create(...)` | Store `loan.ref` and show `loan.depositTarget` |
|
|
144
|
+
| Track loan | `client.instantLoans.get({ ref })` and `client.activities.list({ shortRef: ref })` | Reload loan state and monitor deposit, borrow, and repayment activity |
|
|
145
|
+
| Repay loan | Read `loan.repayment` | Ask the user to send `loan.repayment.amount` to `loan.repayment.target` |
|
|
146
|
+
|
|
147
|
+
`client.instantLoans.create(...)` returns the generated Liquidium profile, transfer targets, current position state, and repayment quote. Users do not manage the generated profile.
|
|
148
|
+
|
|
149
|
+
## Core API
|
|
150
|
+
|
|
151
|
+
### `client.instantLoans.create(request)`
|
|
152
|
+
|
|
153
|
+
Creates an accountless instant loan and returns generated transfer targets.
|
|
154
|
+
|
|
155
|
+
| Field | Description |
|
|
156
|
+
| --- | --- |
|
|
157
|
+
| `collateralPoolId` | Pool that receives the collateral deposit |
|
|
158
|
+
| `borrowPoolId` | Pool the loan borrows from |
|
|
159
|
+
| `collateralAsset` | Collateral asset symbol, for example `"BTC"` |
|
|
160
|
+
| `borrowAsset` | Borrow asset symbol, for example `"USDC"` |
|
|
161
|
+
| `collateralAmount` | Collateral amount in base units |
|
|
162
|
+
| `borrowAmount` | Borrow amount in base units |
|
|
163
|
+
| `ltvMaxBps` | Maximum LTV in basis points, where `6_000n` is 60% |
|
|
164
|
+
| `depositWindowSeconds` | How long the user has to send collateral |
|
|
165
|
+
| `borrowDestination` | External address that receives borrowed funds |
|
|
166
|
+
| `refundDestination` | External address that receives collateral refunds |
|
|
167
|
+
|
|
168
|
+
`borrowDestination` and `refundDestination` can be address strings or account objects such as `{ type: "External", address: "bc1q..." }`.
|
|
169
|
+
|
|
170
|
+
### `client.instantLoans.get({ ref })`
|
|
171
|
+
|
|
172
|
+
Loads loan state from a saved user-facing reference.
|
|
173
|
+
|
|
174
|
+
Use this for status pages, refreshes, and support links.
|
|
175
|
+
|
|
176
|
+
### `client.instantLoans.get({ loanId })`
|
|
177
|
+
|
|
178
|
+
Loads loan state by numeric canister loan id.
|
|
179
|
+
|
|
180
|
+
Use this when your backend stores the numeric id instead of the short reference.
|
|
181
|
+
|
|
182
|
+
### `client.instantLoans.findByAddress(address)`
|
|
183
|
+
|
|
184
|
+
Finds candidate loans associated with a borrow or refund address.
|
|
185
|
+
|
|
186
|
+
Use this only for recovery. The method returns candidate matches; call `client.instantLoans.get({ ref })` or `client.instantLoans.get({ loanId })` before showing loan state or transfer targets.
|
|
187
|
+
|
|
188
|
+
### `client.quote.calculateLtv(request, pools, prices)`
|
|
189
|
+
|
|
190
|
+
Calculates implied LTV from selected pools, prices, borrow amount, and collateral amount.
|
|
191
|
+
|
|
192
|
+
Use this before `client.instantLoans.create(...)` so your app can block invalid input and choose a safe `ltvMaxBps`.
|
|
193
|
+
|
|
194
|
+
## Response Fields
|
|
195
|
+
|
|
196
|
+
Most instant-loan UIs show or store these fields:
|
|
197
|
+
|
|
198
|
+
| Field | Use |
|
|
199
|
+
| --- | --- |
|
|
200
|
+
| `loan.ref` | Save and show this reference so the loan can be restored later |
|
|
201
|
+
| `loan.status` | Show the lifecycle: `awaiting_deposit`, `deposit_detected`, `active`, `settling`, or `closed` |
|
|
202
|
+
| `loan.depositTarget` | Address or ICRC account where the user sends collateral |
|
|
203
|
+
| `loan.repayment.amount` | Full amount to repay, including fee and interest buffer |
|
|
204
|
+
| `loan.repayment.target` | Address or ICRC account where the user sends repayment |
|
|
205
|
+
| `loan.position` | Current collateral, debt, and interest state for the generated profile |
|
|
206
|
+
|
|
207
|
+
## Amounts
|
|
208
|
+
|
|
209
|
+
The SDK returns amount fields as `bigint` values in the asset's smallest unit.
|
|
210
|
+
|
|
211
|
+
| Asset type | Example |
|
|
212
|
+
| --- | --- |
|
|
213
|
+
| BTC | Satoshis |
|
|
214
|
+
| USDC / USDT | Token base units using the pool decimals |
|
|
215
|
+
|
|
216
|
+
Use `Pool.decimals` from `client.market.listPools()` when converting user-entered decimals to base units.
|
|
217
|
+
|
|
218
|
+
## Status And Activity Tracking
|
|
219
|
+
|
|
220
|
+
Reload loans with `client.instantLoans.get({ ref })` when you need current state, transfer targets, or the latest repayment quote.
|
|
221
|
+
|
|
222
|
+
Use activities to track collateral deposits, borrow outflows, repayment deposits, confirmations, and fee top-ups. The activities module accepts the saved instant-loan reference and resolves the generated profile for you:
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
const activities = await client.activities.list({
|
|
226
|
+
shortRef: loan.ref,
|
|
227
|
+
filter: "active",
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
If you have a receipt or activity id from the flow, you can also load activity status:
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
const activityStatus = await client.activities.getStatus({
|
|
235
|
+
shortRef: loan.ref,
|
|
236
|
+
id: "<activity-or-receipt-id>",
|
|
237
|
+
});
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Example Apps
|
|
241
|
+
|
|
242
|
+
Start browser integrations with the examples.
|
|
243
|
+
|
|
244
|
+
| Example | What it shows |
|
|
245
|
+
| --- | --- |
|
|
246
|
+
| [`examples/instant-loans-flow`](https://github.com/Liquidium-Inc/liquidium-sdk/tree/main/examples/instant-loans-flow) | Instant loan UX with Dynamic wallet connection, pool selection, LTV preview, loan creation, status reload, activity status, and address recovery |
|
|
247
|
+
| [`examples/vite-react-dynamic`](https://github.com/Liquidium-Inc/liquidium-sdk/tree/main/examples/vite-react-dynamic) | Developer tool for calling SDK methods, including instant loan method templates |
|
|
248
|
+
|
|
249
|
+
Run the instant loan example:
|
|
250
|
+
|
|
251
|
+
```bash
|
|
252
|
+
git clone https://github.com/Liquidium-Inc/liquidium-sdk.git
|
|
253
|
+
cd liquidium-sdk
|
|
254
|
+
pnpm install
|
|
255
|
+
cp examples/instant-loans-flow/.env.example examples/instant-loans-flow/.env
|
|
256
|
+
pnpm --filter @liquidium/example-instant-loans-flow dev
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Set `VITE_INFURA_API_KEY` when your flow needs Ethereum reads through Infura.
|
|
260
|
+
|
|
261
|
+
## Browser And Runtime Support
|
|
262
|
+
|
|
263
|
+
Use the SDK in browser apps and modern TypeScript runtimes.
|
|
264
|
+
|
|
265
|
+
| Requirement | Notes |
|
|
266
|
+
| --- | --- |
|
|
267
|
+
| Node.js | 20+ for this repository |
|
|
268
|
+
| Package manager | pnpm 9+ for local development |
|
|
269
|
+
| Browser APIs | `fetch`, `BigInt`, and standard ESM support |
|
|
270
|
+
| Wallet UI | Bring your own wallet provider; the example apps use Dynamic |
|
|
271
|
+
|
|
272
|
+
## Development
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
git clone https://github.com/Liquidium-Inc/liquidium-sdk.git
|
|
276
|
+
cd liquidium-sdk
|
|
277
|
+
pnpm install
|
|
278
|
+
pnpm run build
|
|
279
|
+
pnpm run typecheck
|
|
280
|
+
pnpm run test
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## More API Details
|
|
284
|
+
|
|
285
|
+
This README covers `@liquidium/client`. Start new integrations with `client.instantLoans`; profile-based deposit-address flows and ETH contract-interaction flows are advanced paths.
|
|
286
|
+
|
|
287
|
+
## License
|
|
288
|
+
|
|
289
|
+
MIT
|