@cosmosapp/pay_sdk 1.0.1 → 1.2.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/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
+ import { version, DEFAULT_VERSION, DEFAULT_BASE_URL, DEFAULT_TIMEOUT, DEFAULT_RETRIES, Events, CosmosPayAPIError, CosmosPayRequestError, resolveIntentBody, SIGNATURE_HEADER } from './chunk-TZDZBVFS.js';
2
+ export { AddressBook, Assets, CosmosPayAPIError, CosmosPayRequestError, DEFAULT_BASE_URL, DEFAULT_RETRIES, DEFAULT_TIMEOUT, DEFAULT_VERSION, Events, SIGNATURE_HEADER, TestnetAssets, Wallets, addresses, assertAddress, defineAsset, isAddress, isContractAddress, isMuxedAddress, isNativeAsset, isStellarAddress, resolveAddress, resolveAsset, resolveIntentBody, version } from './chunk-TZDZBVFS.js';
1
3
  import { EventEmitter } from 'events';
2
4
  import { createHmac, timingSafeEqual } from 'crypto';
3
5
 
4
- // src/client/Client.ts
5
-
6
6
  // src/managers/AnalyticsManager.ts
7
7
  var AnalyticsManager = class {
8
8
  constructor(client) {
@@ -216,6 +216,13 @@ var ProductKind = {
216
216
  OneTime: "one_time",
217
217
  Link: "link"
218
218
  };
219
+ var SwapStatus = {
220
+ Pending: "PENDING",
221
+ Submitted: "SUBMITTED",
222
+ Succeeded: "SUCCEEDED",
223
+ Failed: "FAILED",
224
+ Expired: "EXPIRED"
225
+ };
219
226
 
220
227
  // src/structures/PaymentIntent.ts
221
228
  var PaymentIntent = class extends Base {
@@ -322,14 +329,14 @@ var PaymentIntentManager = class extends BaseManager {
322
329
  /** Create a SEP-7 `tx` intent (source known → unsigned XDR + tx URI + QR). */
323
330
  async createTx(options) {
324
331
  const data = await this.rest.post(`${this.route}/tx`, {
325
- body: options
332
+ body: resolveIntentBody(options)
326
333
  });
327
334
  return this._add(new PaymentIntent(this.client, data));
328
335
  }
329
336
  /** Create a SEP-7 `pay` intent (no source → pay URI + QR, no XDR). */
330
337
  async createPay(options) {
331
338
  const data = await this.rest.post(`${this.route}/pay`, {
332
- body: options
339
+ body: resolveIntentBody(options)
333
340
  });
334
341
  return this._add(new PaymentIntent(this.client, data));
335
342
  }
@@ -483,6 +490,155 @@ var ProductManager = class extends BaseManager {
483
490
  }
484
491
  };
485
492
 
493
+ // src/structures/Swap.ts
494
+ var Swap = class extends Base {
495
+ constructor(client, data) {
496
+ super(client, data);
497
+ }
498
+ _patch(data) {
499
+ super._patch(data);
500
+ this.status = data.status;
501
+ this.network = data.network;
502
+ this.source = data.source;
503
+ this.destination = data.destination;
504
+ this.sendAsset = data.sendAsset;
505
+ this.sendAssetIssuer = data.sendAssetIssuer;
506
+ this.sendAmount = data.sendAmount;
507
+ this.feeAmount = data.feeAmount;
508
+ this.feeBps = data.feeBps;
509
+ this.swapAmount = data.swapAmount;
510
+ this.destAsset = data.destAsset;
511
+ this.destAssetIssuer = data.destAssetIssuer;
512
+ this.destEstimated = data.destEstimated;
513
+ this.destMin = data.destMin;
514
+ this.slippageBps = data.slippageBps;
515
+ this.path = data.path;
516
+ this.memo = data.memo;
517
+ this.xdr = data.xdr;
518
+ this.uri = data.uri;
519
+ this.txHash = data.txHash;
520
+ this.qr = data.qr;
521
+ this.createdAt = new Date(data.createdAt);
522
+ this.updatedAt = new Date(data.updatedAt);
523
+ return this;
524
+ }
525
+ /** Whether the swap has settled on-chain. */
526
+ get isSucceeded() {
527
+ return this.status === SwapStatus.Succeeded;
528
+ }
529
+ /** Whether the swap is still awaiting submission. */
530
+ get isPending() {
531
+ return this.status === SwapStatus.Pending;
532
+ }
533
+ /** Human-readable send asset label (`XLM` for native). */
534
+ get sendAssetLabel() {
535
+ return !this.sendAsset || this.sendAsset === "native" ? "XLM" : this.sendAsset;
536
+ }
537
+ /** Human-readable destination asset label (`XLM` for native). */
538
+ get destAssetLabel() {
539
+ return !this.destAsset || this.destAsset === "native" ? "XLM" : this.destAsset;
540
+ }
541
+ /** Re-fetch this swap from the API, returning a fresh instance. */
542
+ fetch() {
543
+ return this.client.swaps.fetch(this.id);
544
+ }
545
+ /**
546
+ * Relay the signed transaction for this swap. Mutates this instance from the
547
+ * refreshed swap in the outcome and returns the outcome.
548
+ */
549
+ async submit(options) {
550
+ const outcome = await this.client.swaps.submit(this.id, options);
551
+ this._patch(outcome.swap.toJSON());
552
+ return outcome;
553
+ }
554
+ toJSON() {
555
+ return {
556
+ id: this.id,
557
+ status: this.status,
558
+ network: this.network,
559
+ source: this.source,
560
+ destination: this.destination,
561
+ sendAsset: this.sendAsset,
562
+ sendAssetIssuer: this.sendAssetIssuer,
563
+ sendAmount: this.sendAmount,
564
+ feeAmount: this.feeAmount,
565
+ feeBps: this.feeBps,
566
+ swapAmount: this.swapAmount,
567
+ destAsset: this.destAsset,
568
+ destAssetIssuer: this.destAssetIssuer,
569
+ destEstimated: this.destEstimated,
570
+ destMin: this.destMin,
571
+ slippageBps: this.slippageBps,
572
+ path: this.path,
573
+ memo: this.memo,
574
+ xdr: this.xdr,
575
+ uri: this.uri,
576
+ txHash: this.txHash,
577
+ qr: this.qr,
578
+ createdAt: this.createdAt.toISOString(),
579
+ updatedAt: this.updatedAt.toISOString()
580
+ };
581
+ }
582
+ };
583
+
584
+ // src/managers/SwapManager.ts
585
+ var SwapManager = class extends BaseManager {
586
+ constructor() {
587
+ super(...arguments);
588
+ this.route = "/swaps";
589
+ }
590
+ /** Price a swap without creating it. Returns a plain quote (not a {@link Swap}). */
591
+ async quote(options) {
592
+ return this.rest.post(`${this.route}/quote`, {
593
+ body: options
594
+ });
595
+ }
596
+ /** Create a swap (unsigned XDR + SEP-7 URI + QR). */
597
+ async create(options) {
598
+ const data = await this.rest.post(this.route, { body: options });
599
+ return this._add(new Swap(this.client, data));
600
+ }
601
+ /** Get a single swap by id. */
602
+ async fetch(id) {
603
+ const data = await this.rest.get(`${this.route}/${id}`);
604
+ return this._add(new Swap(this.client, data));
605
+ }
606
+ /** List the consumer's swaps (paginated). */
607
+ async list(options = {}) {
608
+ const data = await this.rest.get(this.route, {
609
+ query: {
610
+ status: options.status,
611
+ take: options.take,
612
+ skip: options.skip
613
+ }
614
+ });
615
+ return {
616
+ items: data.data.map((d) => this._add(new Swap(this.client, d))),
617
+ total: data.total,
618
+ take: data.take,
619
+ skip: data.skip
620
+ };
621
+ }
622
+ /**
623
+ * Relay a signed swap transaction to the network. Finalizes status
624
+ * server-side and returns the outcome with the refreshed swap materialized.
625
+ */
626
+ async submit(id, options) {
627
+ const data = await this.rest.post(
628
+ `${this.route}/${id}/submit`,
629
+ { body: options }
630
+ );
631
+ return {
632
+ submitted: data.submitted,
633
+ status: data.status,
634
+ txHash: data.txHash ?? null,
635
+ reason: data.reason ?? null,
636
+ resultCodes: data.resultCodes ?? null,
637
+ swap: this._add(new Swap(this.client, data.swap))
638
+ };
639
+ }
640
+ };
641
+
486
642
  // src/structures/WebhookDelivery.ts
487
643
  var WebhookDelivery = class extends Base {
488
644
  constructor(client, data) {
@@ -593,22 +749,6 @@ var WebhookEndpoint = class extends Base {
593
749
  };
594
750
  }
595
751
  };
596
-
597
- // src/util/Constants.ts
598
- var version = "1.0.0";
599
- var DEFAULT_BASE_URL = "https://api.cosmospay.lat/cosmos-api";
600
- var DEFAULT_VERSION = "v1";
601
- var DEFAULT_TIMEOUT = 3e4;
602
- var DEFAULT_RETRIES = 2;
603
- var SIGNATURE_HEADER = "x-cosmos-signature";
604
- var Events = {
605
- Debug: "debug",
606
- Request: "request",
607
- Response: "response",
608
- RateLimited: "rateLimited"
609
- };
610
-
611
- // src/util/Webhooks.ts
612
752
  var WebhookSignatureError = class extends Error {
613
753
  constructor(message) {
614
754
  super(message);
@@ -869,52 +1009,6 @@ function extractRawBody(req) {
869
1009
  }
870
1010
  return "";
871
1011
  }
872
-
873
- // src/errors/CosmosPayError.ts
874
- var CosmosPayAPIError = class _CosmosPayAPIError extends Error {
875
- constructor(options) {
876
- const message = _CosmosPayAPIError.resolveMessage(options.status, options.body);
877
- super(message);
878
- this.name = "CosmosPayAPIError";
879
- this.status = options.status;
880
- this.method = options.method;
881
- this.url = options.url;
882
- this.body = options.body;
883
- this.code = _CosmosPayAPIError.resolveCode(options.body);
884
- Error.captureStackTrace?.(this, _CosmosPayAPIError);
885
- }
886
- static resolveMessage(status, body) {
887
- if (body && typeof body === "object") {
888
- const maybe = body;
889
- const msg = maybe.message ?? maybe.error;
890
- if (Array.isArray(msg)) return `[${status}] ${msg.join(", ")}`;
891
- if (typeof msg === "string") return `[${status}] ${msg}`;
892
- }
893
- if (typeof body === "string" && body.length) return `[${status}] ${body}`;
894
- return `[${status}] Request failed`;
895
- }
896
- static resolveCode(body) {
897
- if (body && typeof body === "object") {
898
- const maybe = body;
899
- if (typeof maybe.code === "string") return maybe.code;
900
- if (typeof maybe.error === "string") return maybe.error;
901
- }
902
- return void 0;
903
- }
904
- };
905
- var CosmosPayRequestError = class _CosmosPayRequestError extends Error {
906
- constructor(options) {
907
- const reason = options.cause instanceof Error ? options.cause.message : String(options.cause);
908
- super(`Request to ${options.method} ${options.url} failed: ${reason}`);
909
- this.name = "CosmosPayRequestError";
910
- this.method = options.method;
911
- this.url = options.url;
912
- this.cause = options.cause;
913
- Error.captureStackTrace?.(this, _CosmosPayRequestError);
914
- }
915
- };
916
-
917
- // src/rest/REST.ts
918
1012
  var RETRYABLE_STATUS = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
919
1013
  var REST = class extends EventEmitter {
920
1014
  constructor(options) {
@@ -1145,6 +1239,7 @@ var Client = class extends EventEmitter {
1145
1239
  this.paymentIntents = new PaymentIntentManager(this);
1146
1240
  this.webhooks = new WebhookManager(this);
1147
1241
  this.products = new ProductManager(this);
1242
+ this.swaps = new SwapManager(this);
1148
1243
  this.customers = new CustomerManager(this);
1149
1244
  this.analytics = new AnalyticsManager(this);
1150
1245
  this.health = new HealthManager(this);
@@ -1166,6 +1261,6 @@ var Client = class extends EventEmitter {
1166
1261
  }
1167
1262
  };
1168
1263
 
1169
- export { AnalyticsManager, Base, BaseManager, Client, CosmosPayAPIError, CosmosPayRequestError, Customer, CustomerManager, DEFAULT_BASE_URL, DEFAULT_RETRIES, DEFAULT_TIMEOUT, DEFAULT_VERSION, Events, HealthManager, PaymentIntent, PaymentIntentKind, PaymentIntentManager, PaymentIntentStatus, Product, ProductKind, ProductManager, REST, SIGNATURE_HEADER, WebhookDelivery, WebhookDeliveryStatus, WebhookEndpoint, WebhookEventType, WebhookManager, WebhookSignatureError, Webhooks, resolveShared, shared, version };
1264
+ export { AnalyticsManager, Base, BaseManager, Client, Customer, CustomerManager, HealthManager, PaymentIntent, PaymentIntentKind, PaymentIntentManager, PaymentIntentStatus, Product, ProductKind, ProductManager, REST, Swap, SwapManager, SwapStatus, WebhookDelivery, WebhookDeliveryStatus, WebhookEndpoint, WebhookEventType, WebhookManager, WebhookSignatureError, Webhooks, resolveShared, shared };
1170
1265
  //# sourceMappingURL=index.js.map
1171
1266
  //# sourceMappingURL=index.js.map