@fivenorth/loop-sdk 0.5.0 → 0.6.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.
Files changed (3) hide show
  1. package/README.md +22 -1
  2. package/dist/index.js +98 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -151,6 +151,27 @@ try {
151
151
  }
152
152
  ```
153
153
 
154
+ #### Transfer (built-in helper)
155
+
156
+ ```javascript
157
+ await loop.wallet.transfer(
158
+ 'receiver::fingerprint',
159
+ '5', // amount (string or number)
160
+ {
161
+ instrument_id: 'Amulet', // defaults to Amulet if omitted
162
+ instrument_admin: 'issuer::fingerprint', // optional; defaults to DSO/Amulet
163
+ },
164
+ {
165
+ requestedAt: new Date().toISOString(), // optional; auto-filled if omitted
166
+ executeBefore: new Date(Date.now() + 24*60*60*1000).toISOString(), // optional
167
+ },
168
+ );
169
+ ```
170
+
171
+ Notes:
172
+ - You must have spendable holdings for the specified instrument (admin + id). If left blank, the SDK defaults to the native token.
173
+ - The helper handles fetching holdings, building the transfer factory payload, and submitting via Wallet Connect.
174
+
154
175
  # API
155
176
 
156
177
  Coming soon
@@ -179,4 +200,4 @@ Upon doing so you can visit http://localhost:3030/ to see the local demo app, se
179
200
  ```
180
201
  bun run build
181
202
  bun publish
182
- ```
203
+ ```
package/dist/index.js CHANGED
@@ -2153,6 +2153,39 @@ class Connection {
2153
2153
  }
2154
2154
  return response.json();
2155
2155
  }
2156
+ async prepareTransfer(authToken, params) {
2157
+ const payload = {
2158
+ recipient: params.recipient,
2159
+ amount: params.amount
2160
+ };
2161
+ if (params.instrument) {
2162
+ if (params.instrument.instrument_admin) {
2163
+ payload.instrument_admin = params.instrument.instrument_admin;
2164
+ }
2165
+ if (params.instrument.instrument_id) {
2166
+ payload.instrument_id = params.instrument.instrument_id;
2167
+ }
2168
+ }
2169
+ if (params.requested_at) {
2170
+ payload.requested_at = params.requested_at;
2171
+ }
2172
+ if (params.execute_before) {
2173
+ payload.execute_before = params.execute_before;
2174
+ }
2175
+ const response = await fetch(`${this.apiUrl}/api/v1/.connect/pair/transfer`, {
2176
+ method: "POST",
2177
+ headers: {
2178
+ "Content-Type": "application/json",
2179
+ Authorization: `Bearer ${authToken}`
2180
+ },
2181
+ body: JSON.stringify(payload)
2182
+ });
2183
+ if (!response.ok) {
2184
+ throw new Error("Failed to prepare transfer.");
2185
+ }
2186
+ const data = await response.json();
2187
+ return data.payload;
2188
+ }
2156
2189
  async verifySession(authToken) {
2157
2190
  const response = await fetch(`${this.apiUrl}/api/v1/.connect/pair/account`, {
2158
2191
  method: "GET",
@@ -2262,6 +2295,42 @@ class Provider {
2262
2295
  async submitTransaction(payload) {
2263
2296
  return this.sendRequest("run_transaction" /* RUN_TRANSACTION */, payload);
2264
2297
  }
2298
+ async transfer(recipient, amount, instrument, options) {
2299
+ const amountStr = typeof amount === "number" ? amount.toString() : amount;
2300
+ const resolveDate = (value, fallbackMs) => {
2301
+ if (value instanceof Date) {
2302
+ return value.toISOString();
2303
+ }
2304
+ if (typeof value === "string" && value.length > 0) {
2305
+ return value;
2306
+ }
2307
+ if (fallbackMs) {
2308
+ return new Date(Date.now() + fallbackMs).toISOString();
2309
+ }
2310
+ return new Date().toISOString();
2311
+ };
2312
+ const requestedAtIso = resolveDate(options?.requestedAt);
2313
+ const executeBeforeIso = resolveDate(options?.executeBefore, 24 * 60 * 60 * 1000);
2314
+ const transferRequest = {
2315
+ recipient,
2316
+ amount: amountStr,
2317
+ instrument: {
2318
+ instrument_admin: instrument?.instrument_admin,
2319
+ instrument_id: instrument?.instrument_id || "Amulet"
2320
+ },
2321
+ requested_at: requestedAtIso,
2322
+ execute_before: executeBeforeIso
2323
+ };
2324
+ const preparedPayload = await this.connection.prepareTransfer(this.auth_token, transferRequest);
2325
+ return this.submitTransaction({
2326
+ commands: preparedPayload.commands,
2327
+ disclosedContracts: preparedPayload.disclosedContracts,
2328
+ packageIdSelectionPreference: preparedPayload.packageIdSelectionPreference,
2329
+ actAs: preparedPayload.actAs,
2330
+ readAs: preparedPayload.readAs,
2331
+ synchronizerId: preparedPayload.synchronizerId
2332
+ });
2333
+ }
2265
2334
  async signMessage(message) {
2266
2335
  return this.sendRequest("sign_raw_message" /* SIGN_RAW_MESSAGE */, message);
2267
2336
  }
@@ -2301,6 +2370,25 @@ class Provider {
2301
2370
  }
2302
2371
  }
2303
2372
 
2373
+ // src/wallet.ts
2374
+ class LoopWallet {
2375
+ getProvider;
2376
+ constructor(getProvider) {
2377
+ this.getProvider = getProvider;
2378
+ }
2379
+ requireProvider() {
2380
+ const provider = this.getProvider();
2381
+ if (!provider) {
2382
+ throw new Error("SDK not connected. Call connect() and wait for acceptance first.");
2383
+ }
2384
+ return provider;
2385
+ }
2386
+ transfer(recipient, amount, instrument, options) {
2387
+ const provider = this.requireProvider();
2388
+ return provider.transfer(recipient, amount, instrument, options);
2389
+ }
2390
+ }
2391
+
2304
2392
  // src/index.ts
2305
2393
  class LoopSDK {
2306
2394
  version = "0.0.1";
@@ -2314,7 +2402,10 @@ class LoopSDK {
2314
2402
  onReject = null;
2315
2403
  overlay = null;
2316
2404
  ticketId = null;
2317
- constructor() {}
2405
+ wallet;
2406
+ constructor() {
2407
+ this.wallet = new LoopWallet(() => this.provider);
2408
+ }
2318
2409
  init({
2319
2410
  appName,
2320
2411
  network,
@@ -2527,6 +2618,12 @@ class LoopSDK {
2527
2618
  this.overlay = null;
2528
2619
  }
2529
2620
  }
2621
+ requireProvider() {
2622
+ if (!this.provider) {
2623
+ throw new Error("SDK not connected. Call connect() and wait for acceptance first.");
2624
+ }
2625
+ return this.provider;
2626
+ }
2530
2627
  }
2531
2628
  var loop = new LoopSDK;
2532
2629
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fivenorth/loop-sdk",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "author": "hello@fivenorth.io",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",