@fivenorth/loop-sdk 0.11.2 → 0.12.1
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 +43 -0
- package/dist/connection.d.ts +12 -1
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +122 -6
- package/dist/errors.d.ts +15 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +15 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +143 -6
- package/dist/server/index.d.ts +6 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +36 -0
- package/dist/types.d.ts +15 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -301,6 +301,49 @@ const result = await loop.executeTransaction(preparedPayload);
|
|
|
301
301
|
console.log('Transfer result:', result);
|
|
302
302
|
```
|
|
303
303
|
|
|
304
|
+
### Handling pending network gas in the server SDK
|
|
305
|
+
|
|
306
|
+
Server SDK transactions use an after-execution network gas model. If a previous transaction created unpaid network gas, the next transaction attempt may fail with `PaymentRequiredError`.
|
|
307
|
+
|
|
308
|
+
Best practice:
|
|
309
|
+
|
|
310
|
+
- call `estimateGas(...)` before submitting a transaction if you want to inspect the expected network gas first
|
|
311
|
+
- check for due gas before you submit a transaction, and pay it first if present
|
|
312
|
+
|
|
313
|
+
```javascript
|
|
314
|
+
const gasEstimate = await loop.estimateGas(preparedPayload);
|
|
315
|
+
console.log('Estimated network gas:', gasEstimate.estimated_gas_amount);
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
```javascript
|
|
319
|
+
const dueGas = await loop.checkDueGas();
|
|
320
|
+
|
|
321
|
+
if (dueGas.pending && dueGas.tracking_id) {
|
|
322
|
+
await loop.payGas(dueGas.tracking_id);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
await loop.executeTransaction(preparedPayload);
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
You should still handle `PaymentRequiredError` as a fallback:
|
|
329
|
+
|
|
330
|
+
```javascript
|
|
331
|
+
import { loop, PaymentRequiredError } from '@fivenorth/loop-sdk/server';
|
|
332
|
+
|
|
333
|
+
try {
|
|
334
|
+
await loop.executeTransaction(preparedPayload);
|
|
335
|
+
} catch (error) {
|
|
336
|
+
if (error instanceof PaymentRequiredError) {
|
|
337
|
+
const dueGas = await loop.checkDueGas(error.trackingId);
|
|
338
|
+
console.log('Pending network gas amount:', dueGas.gas_amount);
|
|
339
|
+
|
|
340
|
+
await loop.payGas(error.trackingId);
|
|
341
|
+
} else {
|
|
342
|
+
throw error;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
```
|
|
346
|
+
|
|
304
347
|
Example ideas:
|
|
305
348
|
- List pending transfers
|
|
306
349
|
- Accept a pending transfer
|
package/dist/connection.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Network, Account, Holding, TransferRequest, PreparedTransferPayload, ExchangeApiKeyResponse, TransactionPayload, PreparedSubmissionResponse, ExecuteSubmissionResquest } from './types';
|
|
1
|
+
import type { Network, Account, Holding, TransferRequest, PreparedTransferPayload, ExchangeApiKeyResponse, TransactionPayload, PreparedSubmissionResponse, ExecuteSubmissionResquest, PendingGasResponse, EstimatedGasResponse } from './types';
|
|
2
2
|
import { SessionInfo } from './session';
|
|
3
3
|
export declare class Connection {
|
|
4
4
|
walletUrl: string;
|
|
@@ -32,8 +32,19 @@ export declare class Connection {
|
|
|
32
32
|
signature: string;
|
|
33
33
|
epoch: number;
|
|
34
34
|
}): Promise<ExchangeApiKeyResponse>;
|
|
35
|
+
private parseErrorResponse;
|
|
36
|
+
private errorMessage;
|
|
35
37
|
prepareTransaction(session: SessionInfo, params: TransactionPayload): Promise<PreparedSubmissionResponse>;
|
|
38
|
+
estimateGas(session: SessionInfo, params: TransactionPayload): Promise<EstimatedGasResponse>;
|
|
36
39
|
executeTransaction(session: SessionInfo, params: ExecuteSubmissionResquest): Promise<PreparedSubmissionResponse>;
|
|
40
|
+
getPendingGas(userApiKey: string, trackingId?: string): Promise<PendingGasResponse>;
|
|
41
|
+
preparePendingGas(userApiKey: string, trackingId?: string): Promise<{
|
|
42
|
+
transaction_hash: string;
|
|
43
|
+
}>;
|
|
44
|
+
executePendingGas(userApiKey: string, params: {
|
|
45
|
+
transaction_hash: string;
|
|
46
|
+
signature: string;
|
|
47
|
+
}): Promise<any>;
|
|
37
48
|
private websocketUrl;
|
|
38
49
|
private attachWebSocket;
|
|
39
50
|
}
|
package/dist/connection.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,OAAO,EACP,OAAO,EACP,OAAO,EACP,eAAe,EACf,uBAAuB,EAEvB,sBAAsB,EACtB,kBAAkB,EAClB,0BAA0B,EAC1B,yBAAyB,
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,OAAO,EACP,OAAO,EACP,OAAO,EACP,eAAe,EACf,uBAAuB,EAEvB,sBAAsB,EACtB,kBAAkB,EAClB,0BAA0B,EAC1B,yBAAyB,EACzB,kBAAkB,EAClB,oBAAoB,EACvB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAGxC,qBAAa,UAAU;IACZ,SAAS,EAAE,MAAM,CAA4B;IAC7C,MAAM,EAAE,MAAM,CAA4B;IAC1C,EAAE,EAAE,SAAS,GAAG,IAAI,CAAQ;IACnC,OAAO,CAAC,OAAO,CAAmB;IAClC,OAAO,CAAC,QAAQ,CAAuB;IACvC,OAAO,CAAC,gBAAgB,CAAgD;IACxE,OAAO,CAAC,gBAAgB,CAA8B;IACtD,OAAO,CAAC,MAAM,CAA+D;gBAEjE,EAAE,OAAO,EAAE,SAAS,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,EAAE,WAAW,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAoC5F,kBAAkB,CAAC,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,yBAAyB,GAAG,OAAO,CAAC,0BAA0B,CAAC;IA+BhH,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAqBnF,iBAAiB,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,gBAAgB,EAAE,MAAM,CAAA;KAAE,CAAC;IAkBjG,iBAAiB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE;QAAE,gBAAgB,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,GAAG,CAAC;IAmBlH,OAAO,CAAC,YAAY;IAKpB,OAAO,CAAC,eAAe;CAqC1B"}
|
package/dist/connection.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { UnauthorizedError } from './errors';
|
|
1
|
+
import { PaymentRequiredError, UnauthorizedError } from './errors';
|
|
2
2
|
import { generateRequestId } from './provider';
|
|
3
3
|
export class Connection {
|
|
4
4
|
walletUrl = 'https://cantonloop.com';
|
|
@@ -220,9 +220,30 @@ export class Connection {
|
|
|
220
220
|
}
|
|
221
221
|
return response.json();
|
|
222
222
|
}
|
|
223
|
+
async parseErrorResponse(response) {
|
|
224
|
+
const text = await response.text();
|
|
225
|
+
if (!text) {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
return JSON.parse(text);
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
return text;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
errorMessage(details, fallback) {
|
|
236
|
+
if (typeof details === 'string' && details.length > 0) {
|
|
237
|
+
return details;
|
|
238
|
+
}
|
|
239
|
+
if (typeof details?.message === 'string' && details.message.length > 0) {
|
|
240
|
+
return details.message;
|
|
241
|
+
}
|
|
242
|
+
return fallback;
|
|
243
|
+
}
|
|
223
244
|
// send transaction to v2/interactive-submisison/prepare endpoint to get the prepared transaction
|
|
224
|
-
prepareTransaction(session, params) {
|
|
225
|
-
|
|
245
|
+
async prepareTransaction(session, params) {
|
|
246
|
+
const response = await fetch(`${this.apiUrl}/api/v1/.connect/tickets/prepare-transaction`, {
|
|
226
247
|
method: 'POST',
|
|
227
248
|
headers: {
|
|
228
249
|
'Content-Type': 'application/json',
|
|
@@ -232,14 +253,54 @@ export class Connection {
|
|
|
232
253
|
payload: params,
|
|
233
254
|
ticket_id: session.ticketId,
|
|
234
255
|
})
|
|
235
|
-
})
|
|
256
|
+
});
|
|
257
|
+
if (response.status === 402) {
|
|
258
|
+
throw new PaymentRequiredError(await this.parseErrorResponse(response));
|
|
259
|
+
}
|
|
260
|
+
if (!response.ok) {
|
|
261
|
+
const details = await this.parseErrorResponse(response);
|
|
262
|
+
throw new Error(this.errorMessage(details, `Failed to prepare transaction with status ${response.status}.`));
|
|
263
|
+
}
|
|
264
|
+
return response.json();
|
|
265
|
+
}
|
|
266
|
+
async estimateGas(session, params) {
|
|
267
|
+
const response = await fetch(`${this.apiUrl}/api/v1/.connect/tickets/estimate-gas`, {
|
|
268
|
+
method: 'POST',
|
|
269
|
+
headers: {
|
|
270
|
+
'Content-Type': 'application/json',
|
|
271
|
+
'Authorization': `Bearer ${session.userApiKey}`,
|
|
272
|
+
},
|
|
273
|
+
body: JSON.stringify({
|
|
274
|
+
ticket_id: session.ticketId,
|
|
275
|
+
request_id: generateRequestId(),
|
|
276
|
+
payload: {
|
|
277
|
+
commands: params.commands,
|
|
278
|
+
disclosedContracts: params.disclosedContracts,
|
|
279
|
+
packageIdSelectionPreference: params.packageIdSelectionPreference,
|
|
280
|
+
actAs: params.actAs,
|
|
281
|
+
readAs: params.readAs,
|
|
282
|
+
synchronizerId: params.synchronizerId,
|
|
283
|
+
},
|
|
284
|
+
}),
|
|
285
|
+
});
|
|
286
|
+
if (!response.ok) {
|
|
287
|
+
const details = await this.parseErrorResponse(response);
|
|
288
|
+
throw new Error(this.errorMessage(details, `Failed to estimate gas with status ${response.status}.`));
|
|
289
|
+
}
|
|
290
|
+
const data = await response.json();
|
|
291
|
+
return {
|
|
292
|
+
requires_gas: data?.requiresFee,
|
|
293
|
+
can_execute: data?.canExecute,
|
|
294
|
+
estimated_gas_amount: data?.estimatedFeeAmount,
|
|
295
|
+
estimated_gas_asset: data?.estimatedFeeAsset,
|
|
296
|
+
};
|
|
236
297
|
}
|
|
237
298
|
// execute a signed transaction with v2/interactive-submisison/execute endpoint
|
|
238
299
|
async executeTransaction(session, params) {
|
|
239
300
|
if (!session.ticketId) {
|
|
240
301
|
throw new Error('Ticket ID is required');
|
|
241
302
|
}
|
|
242
|
-
const
|
|
303
|
+
const response = await fetch(`${this.apiUrl}/api/v1/.connect/tickets/execute-transaction`, {
|
|
243
304
|
method: 'POST',
|
|
244
305
|
headers: {
|
|
245
306
|
'Content-Type': 'application/json',
|
|
@@ -253,7 +314,62 @@ export class Connection {
|
|
|
253
314
|
transaction_data: params.transaction_data,
|
|
254
315
|
}),
|
|
255
316
|
});
|
|
256
|
-
|
|
317
|
+
if (response.status === 402) {
|
|
318
|
+
throw new PaymentRequiredError(await this.parseErrorResponse(response));
|
|
319
|
+
}
|
|
320
|
+
if (!response.ok) {
|
|
321
|
+
const details = await this.parseErrorResponse(response);
|
|
322
|
+
throw new Error(this.errorMessage(details, `Failed to execute transaction with status ${response.status}.`));
|
|
323
|
+
}
|
|
324
|
+
return response.json();
|
|
325
|
+
}
|
|
326
|
+
async getPendingGas(userApiKey, trackingId) {
|
|
327
|
+
const url = new URL(`${this.apiUrl}/api/v1/transfer/pending-fee`);
|
|
328
|
+
if (trackingId) {
|
|
329
|
+
url.searchParams.set('tracking_id', trackingId);
|
|
330
|
+
}
|
|
331
|
+
const response = await fetch(url.toString(), {
|
|
332
|
+
method: 'GET',
|
|
333
|
+
headers: {
|
|
334
|
+
'Content-Type': 'application/json',
|
|
335
|
+
'Authorization': `Bearer ${userApiKey}`,
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
if (!response.ok) {
|
|
339
|
+
const details = await this.parseErrorResponse(response);
|
|
340
|
+
throw new Error(this.errorMessage(details, 'Failed to get pending gas.'));
|
|
341
|
+
}
|
|
342
|
+
return await response.json();
|
|
343
|
+
}
|
|
344
|
+
async preparePendingGas(userApiKey, trackingId) {
|
|
345
|
+
const response = await fetch(`${this.apiUrl}/api/v1/transfer/pending-fee/prepare`, {
|
|
346
|
+
method: 'POST',
|
|
347
|
+
headers: {
|
|
348
|
+
'Content-Type': 'application/json',
|
|
349
|
+
'Authorization': `Bearer ${userApiKey}`,
|
|
350
|
+
},
|
|
351
|
+
body: JSON.stringify(trackingId ? { tracking_id: trackingId } : {}),
|
|
352
|
+
});
|
|
353
|
+
if (!response.ok) {
|
|
354
|
+
const details = await this.parseErrorResponse(response);
|
|
355
|
+
throw new Error(this.errorMessage(details, 'Failed to prepare pending gas.'));
|
|
356
|
+
}
|
|
357
|
+
return response.json();
|
|
358
|
+
}
|
|
359
|
+
async executePendingGas(userApiKey, params) {
|
|
360
|
+
const response = await fetch(`${this.apiUrl}/api/v1/transfer/pending-fee/execute`, {
|
|
361
|
+
method: 'POST',
|
|
362
|
+
headers: {
|
|
363
|
+
'Content-Type': 'application/json',
|
|
364
|
+
'Authorization': `Bearer ${userApiKey}`,
|
|
365
|
+
},
|
|
366
|
+
body: JSON.stringify(params),
|
|
367
|
+
});
|
|
368
|
+
if (!response.ok) {
|
|
369
|
+
const details = await this.parseErrorResponse(response);
|
|
370
|
+
throw new Error(this.errorMessage(details, 'Failed to execute pending gas.'));
|
|
371
|
+
}
|
|
372
|
+
return response.json();
|
|
257
373
|
}
|
|
258
374
|
websocketUrl(ticketId) {
|
|
259
375
|
return `${this.network === 'local' ? 'ws' : 'wss'}://${this.apiUrl.replace('https://', '').replace('http://', '')}/api/v1/.connect/pair/ws/${encodeURIComponent(ticketId)}`;
|
package/dist/errors.d.ts
CHANGED
|
@@ -9,6 +9,21 @@ export declare class UnauthorizedError extends Error {
|
|
|
9
9
|
code?: string;
|
|
10
10
|
constructor(code?: string);
|
|
11
11
|
}
|
|
12
|
+
export declare class PaymentRequiredError extends Error {
|
|
13
|
+
code?: string;
|
|
14
|
+
trackingId?: string;
|
|
15
|
+
gasAmount?: string;
|
|
16
|
+
status?: string;
|
|
17
|
+
expiresAt?: string;
|
|
18
|
+
constructor(details?: {
|
|
19
|
+
message?: string;
|
|
20
|
+
code?: string;
|
|
21
|
+
tracking_id?: string;
|
|
22
|
+
gas_amount?: string;
|
|
23
|
+
status?: string;
|
|
24
|
+
expires_at?: string;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
12
27
|
export declare function extractErrorCode(message: any): string | null;
|
|
13
28
|
export declare function isUnauthCode(code: string | null | undefined): code is string;
|
|
14
29
|
//# sourceMappingURL=errors.d.ts.map
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,mBAAoB,SAAQ,KAAK;gBAC9B,OAAO,EAAE,MAAM;CAG9B;AAED,qBAAa,kBAAmB,SAAQ,KAAK;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;gBACT,OAAO,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM;CAI9C;AAED,qBAAa,iBAAkB,SAAQ,KAAK;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;gBACT,IAAI,CAAC,EAAE,MAAM;CAI5B;AAID,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,GAAG,GAAG,MAAM,GAAG,IAAI,CAQ5D;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,IAAI,IAAI,MAAM,CAK5E"}
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,mBAAoB,SAAQ,KAAK;gBAC9B,OAAO,EAAE,MAAM;CAG9B;AAED,qBAAa,kBAAmB,SAAQ,KAAK;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;gBACT,OAAO,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM;CAI9C;AAED,qBAAa,iBAAkB,SAAQ,KAAK;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;gBACT,IAAI,CAAC,EAAE,MAAM;CAI5B;AAED,qBAAa,oBAAqB,SAAQ,KAAK;IACpC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;gBAEd,OAAO,CAAC,EAAE;QAClB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,UAAU,CAAC,EAAE,MAAM,CAAC;KACvB;CAQJ;AAID,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,GAAG,GAAG,MAAM,GAAG,IAAI,CAQ5D;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,IAAI,IAAI,MAAM,CAK5E"}
|
package/dist/errors.js
CHANGED
|
@@ -17,6 +17,21 @@ export class UnauthorizedError extends Error {
|
|
|
17
17
|
this.code = code;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
+
export class PaymentRequiredError extends Error {
|
|
21
|
+
code;
|
|
22
|
+
trackingId;
|
|
23
|
+
gasAmount;
|
|
24
|
+
status;
|
|
25
|
+
expiresAt;
|
|
26
|
+
constructor(details) {
|
|
27
|
+
super(details?.message || 'Payment required');
|
|
28
|
+
this.code = details?.code;
|
|
29
|
+
this.trackingId = details?.tracking_id;
|
|
30
|
+
this.gasAmount = details?.gas_amount;
|
|
31
|
+
this.status = details?.status;
|
|
32
|
+
this.expiresAt = details?.expires_at;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
20
35
|
const UNAUTH_CODES = new Set(['UNAUTHENTICATED', 'UNAUTHORIZED', 'SESSION_EXPIRED', 'LOGGED_OUT']);
|
|
21
36
|
export function extractErrorCode(message) {
|
|
22
37
|
if (typeof message?.error?.code === 'string' && message.error.code.length > 0) {
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAqB,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEzD,OAAO,KAAK,EAGX,OAAO,EAEP,MAAM,EACN,sBAAsB,EACtB,MAAM,SAAS,CAAC;AAIjB,cAAM,OAAO;IACZ,OAAO,CAAC,OAAO,CAAoB;IAEnC,OAAO,CAAC,OAAO,CAAqB;IACpC,OAAO,CAAC,UAAU,CAA2B;IAC7C,OAAO,CAAC,OAAO,CAA4B;IAC3C,OAAO,CAAC,QAAQ,CAAyB;IACzC,OAAO,CAAC,QAAQ,CAA4B;IAC5C,OAAO,CAAC,kBAAkB,CAA4B;IACtD,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,WAAW,CAAC,CAAS;IAE7B,OAAO,CAAC,QAAQ,CAA+C;IAC/D,OAAO,CAAC,QAAQ,CAA6B;IAC3C,OAAO,CAAC,mBAAmB,CAA0E;IACvG,OAAO,CAAC,OAAO,CAA+B;IACvC,MAAM,EAAE,MAAM,CAAC;;IAM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CvB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IA8B5B,OAAO;IA2Cb,OAAO,CAAC,sBAAsB;IA8DvB,aAAa,IAAI,MAAM;IAO9B,OAAO,CAAC,eAAe;IASvB,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,aAAa;IAqBrB,OAAO,CAAC,UAAU;IAuClB,OAAO,CAAC,iBAAiB;IAyIzB,OAAO,CAAC,UAAU;IAoFlB,OAAO,CAAC,UAAU;IAOlB,MAAM;IAQN,OAAO,CAAC,eAAe;IASvB,OAAO,CAAC,mBAAmB;CAO3B;AAED,eAAO,MAAM,IAAI,SAAgB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -2091,6 +2091,22 @@ class UnauthorizedError extends Error {
|
|
|
2091
2091
|
this.code = code;
|
|
2092
2092
|
}
|
|
2093
2093
|
}
|
|
2094
|
+
|
|
2095
|
+
class PaymentRequiredError extends Error {
|
|
2096
|
+
code;
|
|
2097
|
+
trackingId;
|
|
2098
|
+
gasAmount;
|
|
2099
|
+
status;
|
|
2100
|
+
expiresAt;
|
|
2101
|
+
constructor(details) {
|
|
2102
|
+
super(details?.message || "Payment required");
|
|
2103
|
+
this.code = details?.code;
|
|
2104
|
+
this.trackingId = details?.tracking_id;
|
|
2105
|
+
this.gasAmount = details?.gas_amount;
|
|
2106
|
+
this.status = details?.status;
|
|
2107
|
+
this.expiresAt = details?.expires_at;
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2094
2110
|
var UNAUTH_CODES = new Set(["UNAUTHENTICATED", "UNAUTHORIZED", "SESSION_EXPIRED", "LOGGED_OUT"]);
|
|
2095
2111
|
function extractErrorCode(message) {
|
|
2096
2112
|
if (typeof message?.error?.code === "string" && message.error.code.length > 0) {
|
|
@@ -2565,8 +2581,28 @@ class Connection {
|
|
|
2565
2581
|
}
|
|
2566
2582
|
return response.json();
|
|
2567
2583
|
}
|
|
2568
|
-
|
|
2569
|
-
|
|
2584
|
+
async parseErrorResponse(response) {
|
|
2585
|
+
const text = await response.text();
|
|
2586
|
+
if (!text) {
|
|
2587
|
+
return null;
|
|
2588
|
+
}
|
|
2589
|
+
try {
|
|
2590
|
+
return JSON.parse(text);
|
|
2591
|
+
} catch {
|
|
2592
|
+
return text;
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
errorMessage(details, fallback) {
|
|
2596
|
+
if (typeof details === "string" && details.length > 0) {
|
|
2597
|
+
return details;
|
|
2598
|
+
}
|
|
2599
|
+
if (typeof details?.message === "string" && details.message.length > 0) {
|
|
2600
|
+
return details.message;
|
|
2601
|
+
}
|
|
2602
|
+
return fallback;
|
|
2603
|
+
}
|
|
2604
|
+
async prepareTransaction(session, params) {
|
|
2605
|
+
const response = await fetch(`${this.apiUrl}/api/v1/.connect/tickets/prepare-transaction`, {
|
|
2570
2606
|
method: "POST",
|
|
2571
2607
|
headers: {
|
|
2572
2608
|
"Content-Type": "application/json",
|
|
@@ -2576,13 +2612,53 @@ class Connection {
|
|
|
2576
2612
|
payload: params,
|
|
2577
2613
|
ticket_id: session.ticketId
|
|
2578
2614
|
})
|
|
2579
|
-
})
|
|
2615
|
+
});
|
|
2616
|
+
if (response.status === 402) {
|
|
2617
|
+
throw new PaymentRequiredError(await this.parseErrorResponse(response));
|
|
2618
|
+
}
|
|
2619
|
+
if (!response.ok) {
|
|
2620
|
+
const details = await this.parseErrorResponse(response);
|
|
2621
|
+
throw new Error(this.errorMessage(details, `Failed to prepare transaction with status ${response.status}.`));
|
|
2622
|
+
}
|
|
2623
|
+
return response.json();
|
|
2624
|
+
}
|
|
2625
|
+
async estimateGas(session, params) {
|
|
2626
|
+
const response = await fetch(`${this.apiUrl}/api/v1/.connect/tickets/estimate-gas`, {
|
|
2627
|
+
method: "POST",
|
|
2628
|
+
headers: {
|
|
2629
|
+
"Content-Type": "application/json",
|
|
2630
|
+
Authorization: `Bearer ${session.userApiKey}`
|
|
2631
|
+
},
|
|
2632
|
+
body: JSON.stringify({
|
|
2633
|
+
ticket_id: session.ticketId,
|
|
2634
|
+
request_id: generateRequestId(),
|
|
2635
|
+
payload: {
|
|
2636
|
+
commands: params.commands,
|
|
2637
|
+
disclosedContracts: params.disclosedContracts,
|
|
2638
|
+
packageIdSelectionPreference: params.packageIdSelectionPreference,
|
|
2639
|
+
actAs: params.actAs,
|
|
2640
|
+
readAs: params.readAs,
|
|
2641
|
+
synchronizerId: params.synchronizerId
|
|
2642
|
+
}
|
|
2643
|
+
})
|
|
2644
|
+
});
|
|
2645
|
+
if (!response.ok) {
|
|
2646
|
+
const details = await this.parseErrorResponse(response);
|
|
2647
|
+
throw new Error(this.errorMessage(details, `Failed to estimate gas with status ${response.status}.`));
|
|
2648
|
+
}
|
|
2649
|
+
const data = await response.json();
|
|
2650
|
+
return {
|
|
2651
|
+
requires_gas: data?.requiresFee,
|
|
2652
|
+
can_execute: data?.canExecute,
|
|
2653
|
+
estimated_gas_amount: data?.estimatedFeeAmount,
|
|
2654
|
+
estimated_gas_asset: data?.estimatedFeeAsset
|
|
2655
|
+
};
|
|
2580
2656
|
}
|
|
2581
2657
|
async executeTransaction(session, params) {
|
|
2582
2658
|
if (!session.ticketId) {
|
|
2583
2659
|
throw new Error("Ticket ID is required");
|
|
2584
2660
|
}
|
|
2585
|
-
const
|
|
2661
|
+
const response = await fetch(`${this.apiUrl}/api/v1/.connect/tickets/execute-transaction`, {
|
|
2586
2662
|
method: "POST",
|
|
2587
2663
|
headers: {
|
|
2588
2664
|
"Content-Type": "application/json",
|
|
@@ -2596,7 +2672,62 @@ class Connection {
|
|
|
2596
2672
|
transaction_data: params.transaction_data
|
|
2597
2673
|
})
|
|
2598
2674
|
});
|
|
2599
|
-
|
|
2675
|
+
if (response.status === 402) {
|
|
2676
|
+
throw new PaymentRequiredError(await this.parseErrorResponse(response));
|
|
2677
|
+
}
|
|
2678
|
+
if (!response.ok) {
|
|
2679
|
+
const details = await this.parseErrorResponse(response);
|
|
2680
|
+
throw new Error(this.errorMessage(details, `Failed to execute transaction with status ${response.status}.`));
|
|
2681
|
+
}
|
|
2682
|
+
return response.json();
|
|
2683
|
+
}
|
|
2684
|
+
async getPendingGas(userApiKey, trackingId) {
|
|
2685
|
+
const url = new URL(`${this.apiUrl}/api/v1/transfer/pending-fee`);
|
|
2686
|
+
if (trackingId) {
|
|
2687
|
+
url.searchParams.set("tracking_id", trackingId);
|
|
2688
|
+
}
|
|
2689
|
+
const response = await fetch(url.toString(), {
|
|
2690
|
+
method: "GET",
|
|
2691
|
+
headers: {
|
|
2692
|
+
"Content-Type": "application/json",
|
|
2693
|
+
Authorization: `Bearer ${userApiKey}`
|
|
2694
|
+
}
|
|
2695
|
+
});
|
|
2696
|
+
if (!response.ok) {
|
|
2697
|
+
const details = await this.parseErrorResponse(response);
|
|
2698
|
+
throw new Error(this.errorMessage(details, "Failed to get pending gas."));
|
|
2699
|
+
}
|
|
2700
|
+
return await response.json();
|
|
2701
|
+
}
|
|
2702
|
+
async preparePendingGas(userApiKey, trackingId) {
|
|
2703
|
+
const response = await fetch(`${this.apiUrl}/api/v1/transfer/pending-fee/prepare`, {
|
|
2704
|
+
method: "POST",
|
|
2705
|
+
headers: {
|
|
2706
|
+
"Content-Type": "application/json",
|
|
2707
|
+
Authorization: `Bearer ${userApiKey}`
|
|
2708
|
+
},
|
|
2709
|
+
body: JSON.stringify(trackingId ? { tracking_id: trackingId } : {})
|
|
2710
|
+
});
|
|
2711
|
+
if (!response.ok) {
|
|
2712
|
+
const details = await this.parseErrorResponse(response);
|
|
2713
|
+
throw new Error(this.errorMessage(details, "Failed to prepare pending gas."));
|
|
2714
|
+
}
|
|
2715
|
+
return response.json();
|
|
2716
|
+
}
|
|
2717
|
+
async executePendingGas(userApiKey, params) {
|
|
2718
|
+
const response = await fetch(`${this.apiUrl}/api/v1/transfer/pending-fee/execute`, {
|
|
2719
|
+
method: "POST",
|
|
2720
|
+
headers: {
|
|
2721
|
+
"Content-Type": "application/json",
|
|
2722
|
+
Authorization: `Bearer ${userApiKey}`
|
|
2723
|
+
},
|
|
2724
|
+
body: JSON.stringify(params)
|
|
2725
|
+
});
|
|
2726
|
+
if (!response.ok) {
|
|
2727
|
+
const details = await this.parseErrorResponse(response);
|
|
2728
|
+
throw new Error(this.errorMessage(details, "Failed to execute pending gas."));
|
|
2729
|
+
}
|
|
2730
|
+
return response.json();
|
|
2600
2731
|
}
|
|
2601
2732
|
websocketUrl(ticketId) {
|
|
2602
2733
|
return `${this.network === "local" ? "ws" : "wss"}://${this.apiUrl.replace("https://", "").replace("http://", "")}/api/v1/.connect/pair/ws/${encodeURIComponent(ticketId)}`;
|
|
@@ -2786,7 +2917,7 @@ class LoopWallet {
|
|
|
2786
2917
|
|
|
2787
2918
|
// src/index.ts
|
|
2788
2919
|
class LoopSDK {
|
|
2789
|
-
version = "0.
|
|
2920
|
+
version = "0.12.0";
|
|
2790
2921
|
appName = "Unknown";
|
|
2791
2922
|
connection = null;
|
|
2792
2923
|
session = null;
|
|
@@ -3241,5 +3372,11 @@ class LoopSDK {
|
|
|
3241
3372
|
var loop = new LoopSDK;
|
|
3242
3373
|
export {
|
|
3243
3374
|
loop,
|
|
3375
|
+
isUnauthCode,
|
|
3376
|
+
extractErrorCode,
|
|
3377
|
+
UnauthorizedError,
|
|
3378
|
+
RequestTimeoutError,
|
|
3379
|
+
RejectRequestError,
|
|
3380
|
+
PaymentRequiredError,
|
|
3244
3381
|
MessageType
|
|
3245
3382
|
};
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Provider } from '../provider';
|
|
2
|
-
import type { Network, TransactionPayload } from '../types';
|
|
2
|
+
import type { Network, TransactionPayload, PendingGasResponse, EstimatedGasResponse } from '../types';
|
|
3
3
|
import { Signer } from './signer';
|
|
4
4
|
export declare class LoopSDK {
|
|
5
5
|
private signer?;
|
|
@@ -18,6 +18,11 @@ export declare class LoopSDK {
|
|
|
18
18
|
getSigner(): Signer;
|
|
19
19
|
getProvider(): Provider;
|
|
20
20
|
executeTransaction(payload: TransactionPayload): Promise<any>;
|
|
21
|
+
checkDueGas(trackingId?: string): Promise<PendingGasResponse>;
|
|
22
|
+
estimateGas(payload: TransactionPayload): Promise<EstimatedGasResponse>;
|
|
23
|
+
payGas(trackingId: string): Promise<any>;
|
|
21
24
|
}
|
|
22
25
|
export declare const loop: LoopSDK;
|
|
26
|
+
export * from '../errors';
|
|
27
|
+
export * from '../types';
|
|
23
28
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAsB,MAAM,aAAa,CAAC;AAG3D,OAAO,KAAK,EAAE,OAAO,EAAyE,kBAAkB,EAAyD,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAsB,MAAM,aAAa,CAAC;AAG3D,OAAO,KAAK,EAAE,OAAO,EAAyE,kBAAkB,EAAyD,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAEpO,OAAO,EAAa,MAAM,EAAE,MAAM,UAAU,CAAC;AA8C7C,qBAAa,OAAO;IAChB,OAAO,CAAC,MAAM,CAAC,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAC,CAAc;IAC/B,OAAO,CAAC,UAAU,CAAC,CAAa;IAChC,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,OAAO,CAAC,CAAc;IAE9B,IAAI,CAAC,EAAC,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAC,EAAE;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,OAAO,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAC;IASzI,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAoCnC,SAAS,IAAI,MAAM;IAOnB,WAAW,IAAI,QAAQ;IAOjB,kBAAkB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC;IAwB7D,WAAW,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAQ7D,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAQvE,MAAM,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;CA0BxD;AAED,eAAO,MAAM,IAAI,SAAgB,CAAC;AAClC,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}
|
package/dist/server/index.js
CHANGED
|
@@ -2,6 +2,8 @@ import { Provider } from '../provider';
|
|
|
2
2
|
import { Connection } from '../connection';
|
|
3
3
|
import { SessionInfo } from '../session';
|
|
4
4
|
import { getSigner } from './signer';
|
|
5
|
+
const PAY_GAS_WAIT_MS = 10_000;
|
|
6
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
5
7
|
class RpcProvider extends Provider {
|
|
6
8
|
ticket_id;
|
|
7
9
|
user_api_key;
|
|
@@ -113,5 +115,39 @@ export class LoopSDK {
|
|
|
113
115
|
});
|
|
114
116
|
return submissionResponse;
|
|
115
117
|
}
|
|
118
|
+
async checkDueGas(trackingId) {
|
|
119
|
+
if (!this.connection || !this.session) {
|
|
120
|
+
throw new Error('Provider and session are required');
|
|
121
|
+
}
|
|
122
|
+
return await this.connection.getPendingGas(this.session.userApiKey, trackingId);
|
|
123
|
+
}
|
|
124
|
+
async estimateGas(payload) {
|
|
125
|
+
if (!this.connection || !this.session) {
|
|
126
|
+
throw new Error('Provider and session are required');
|
|
127
|
+
}
|
|
128
|
+
return await this.connection.estimateGas(this.session, payload);
|
|
129
|
+
}
|
|
130
|
+
async payGas(trackingId) {
|
|
131
|
+
if (!this.provider || !this.signer || !this.connection || !this.session) {
|
|
132
|
+
throw new Error('Provider and signer are required');
|
|
133
|
+
}
|
|
134
|
+
const pendingGas = await this.checkDueGas(trackingId);
|
|
135
|
+
if (!pendingGas.pending) {
|
|
136
|
+
throw new Error(`Pending gas not found for tracking_id ${trackingId}.`);
|
|
137
|
+
}
|
|
138
|
+
const preparedGas = await this.connection.preparePendingGas(this.session.userApiKey, trackingId);
|
|
139
|
+
if (!preparedGas?.transaction_hash) {
|
|
140
|
+
throw new Error('Failed to prepare pending gas.');
|
|
141
|
+
}
|
|
142
|
+
const signedTransactionHash = this.getSigner().signTransactionHash(preparedGas.transaction_hash);
|
|
143
|
+
const result = await this.connection.executePendingGas(this.session.userApiKey, {
|
|
144
|
+
transaction_hash: preparedGas.transaction_hash,
|
|
145
|
+
signature: signedTransactionHash,
|
|
146
|
+
});
|
|
147
|
+
await wait(PAY_GAS_WAIT_MS);
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
116
150
|
}
|
|
117
151
|
export const loop = new LoopSDK();
|
|
152
|
+
export * from '../errors';
|
|
153
|
+
export * from '../types';
|
package/dist/types.d.ts
CHANGED
|
@@ -113,4 +113,19 @@ export type ExecuteSubmissionResquest = {
|
|
|
113
113
|
transaction_data: string;
|
|
114
114
|
signature: string;
|
|
115
115
|
};
|
|
116
|
+
export type PendingGasResponse = {
|
|
117
|
+
pending: boolean;
|
|
118
|
+
tracking_id?: string;
|
|
119
|
+
gas_amount?: string;
|
|
120
|
+
status?: string;
|
|
121
|
+
origin?: string;
|
|
122
|
+
expires_at?: string;
|
|
123
|
+
request_id?: string;
|
|
124
|
+
};
|
|
125
|
+
export type EstimatedGasResponse = {
|
|
126
|
+
requires_gas: boolean;
|
|
127
|
+
can_execute: boolean;
|
|
128
|
+
estimated_gas_amount?: string;
|
|
129
|
+
estimated_gas_asset?: string;
|
|
130
|
+
};
|
|
116
131
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAEnE,MAAM,MAAM,OAAO,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,CAAC;AAE3F,MAAM,MAAM,oBAAoB,GAAG,eAAe,GAAG,SAAS,GAAG,SAAS,CAAC;AAC3E,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,kBAAkB,CAAC,EAAE,oBAAoB,CAAC;CAC3C,CAAC;AAEF,oBAAY,WAAW;IACrB,gBAAgB,qBAAqB;IACrC,gBAAgB,qBAAqB;IAErC,eAAe,oBAAoB;IACnC,qBAAqB,0BAA0B;IAE/C,gBAAgB,qBAAqB;IACrC,yBAAyB,8BAA8B;IACvD,cAAc,mBAAmB;CAClC;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,aAAa,EAAE,YAAY,CAAC;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,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