@cosmosapp/pay_sdk 1.1.0 → 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.cjs CHANGED
@@ -218,6 +218,13 @@ var ProductKind = {
218
218
  OneTime: "one_time",
219
219
  Link: "link"
220
220
  };
221
+ var SwapStatus = {
222
+ Pending: "PENDING",
223
+ Submitted: "SUBMITTED",
224
+ Succeeded: "SUCCEEDED",
225
+ Failed: "FAILED",
226
+ Expired: "EXPIRED"
227
+ };
221
228
 
222
229
  // src/structures/PaymentIntent.ts
223
230
  var PaymentIntent = class extends Base {
@@ -631,6 +638,155 @@ var ProductManager = class extends BaseManager {
631
638
  }
632
639
  };
633
640
 
641
+ // src/structures/Swap.ts
642
+ var Swap = class extends Base {
643
+ constructor(client, data) {
644
+ super(client, data);
645
+ }
646
+ _patch(data) {
647
+ super._patch(data);
648
+ this.status = data.status;
649
+ this.network = data.network;
650
+ this.source = data.source;
651
+ this.destination = data.destination;
652
+ this.sendAsset = data.sendAsset;
653
+ this.sendAssetIssuer = data.sendAssetIssuer;
654
+ this.sendAmount = data.sendAmount;
655
+ this.feeAmount = data.feeAmount;
656
+ this.feeBps = data.feeBps;
657
+ this.swapAmount = data.swapAmount;
658
+ this.destAsset = data.destAsset;
659
+ this.destAssetIssuer = data.destAssetIssuer;
660
+ this.destEstimated = data.destEstimated;
661
+ this.destMin = data.destMin;
662
+ this.slippageBps = data.slippageBps;
663
+ this.path = data.path;
664
+ this.memo = data.memo;
665
+ this.xdr = data.xdr;
666
+ this.uri = data.uri;
667
+ this.txHash = data.txHash;
668
+ this.qr = data.qr;
669
+ this.createdAt = new Date(data.createdAt);
670
+ this.updatedAt = new Date(data.updatedAt);
671
+ return this;
672
+ }
673
+ /** Whether the swap has settled on-chain. */
674
+ get isSucceeded() {
675
+ return this.status === SwapStatus.Succeeded;
676
+ }
677
+ /** Whether the swap is still awaiting submission. */
678
+ get isPending() {
679
+ return this.status === SwapStatus.Pending;
680
+ }
681
+ /** Human-readable send asset label (`XLM` for native). */
682
+ get sendAssetLabel() {
683
+ return !this.sendAsset || this.sendAsset === "native" ? "XLM" : this.sendAsset;
684
+ }
685
+ /** Human-readable destination asset label (`XLM` for native). */
686
+ get destAssetLabel() {
687
+ return !this.destAsset || this.destAsset === "native" ? "XLM" : this.destAsset;
688
+ }
689
+ /** Re-fetch this swap from the API, returning a fresh instance. */
690
+ fetch() {
691
+ return this.client.swaps.fetch(this.id);
692
+ }
693
+ /**
694
+ * Relay the signed transaction for this swap. Mutates this instance from the
695
+ * refreshed swap in the outcome and returns the outcome.
696
+ */
697
+ async submit(options) {
698
+ const outcome = await this.client.swaps.submit(this.id, options);
699
+ this._patch(outcome.swap.toJSON());
700
+ return outcome;
701
+ }
702
+ toJSON() {
703
+ return {
704
+ id: this.id,
705
+ status: this.status,
706
+ network: this.network,
707
+ source: this.source,
708
+ destination: this.destination,
709
+ sendAsset: this.sendAsset,
710
+ sendAssetIssuer: this.sendAssetIssuer,
711
+ sendAmount: this.sendAmount,
712
+ feeAmount: this.feeAmount,
713
+ feeBps: this.feeBps,
714
+ swapAmount: this.swapAmount,
715
+ destAsset: this.destAsset,
716
+ destAssetIssuer: this.destAssetIssuer,
717
+ destEstimated: this.destEstimated,
718
+ destMin: this.destMin,
719
+ slippageBps: this.slippageBps,
720
+ path: this.path,
721
+ memo: this.memo,
722
+ xdr: this.xdr,
723
+ uri: this.uri,
724
+ txHash: this.txHash,
725
+ qr: this.qr,
726
+ createdAt: this.createdAt.toISOString(),
727
+ updatedAt: this.updatedAt.toISOString()
728
+ };
729
+ }
730
+ };
731
+
732
+ // src/managers/SwapManager.ts
733
+ var SwapManager = class extends BaseManager {
734
+ constructor() {
735
+ super(...arguments);
736
+ this.route = "/swaps";
737
+ }
738
+ /** Price a swap without creating it. Returns a plain quote (not a {@link Swap}). */
739
+ async quote(options) {
740
+ return this.rest.post(`${this.route}/quote`, {
741
+ body: options
742
+ });
743
+ }
744
+ /** Create a swap (unsigned XDR + SEP-7 URI + QR). */
745
+ async create(options) {
746
+ const data = await this.rest.post(this.route, { body: options });
747
+ return this._add(new Swap(this.client, data));
748
+ }
749
+ /** Get a single swap by id. */
750
+ async fetch(id) {
751
+ const data = await this.rest.get(`${this.route}/${id}`);
752
+ return this._add(new Swap(this.client, data));
753
+ }
754
+ /** List the consumer's swaps (paginated). */
755
+ async list(options = {}) {
756
+ const data = await this.rest.get(this.route, {
757
+ query: {
758
+ status: options.status,
759
+ take: options.take,
760
+ skip: options.skip
761
+ }
762
+ });
763
+ return {
764
+ items: data.data.map((d) => this._add(new Swap(this.client, d))),
765
+ total: data.total,
766
+ take: data.take,
767
+ skip: data.skip
768
+ };
769
+ }
770
+ /**
771
+ * Relay a signed swap transaction to the network. Finalizes status
772
+ * server-side and returns the outcome with the refreshed swap materialized.
773
+ */
774
+ async submit(id, options) {
775
+ const data = await this.rest.post(
776
+ `${this.route}/${id}/submit`,
777
+ { body: options }
778
+ );
779
+ return {
780
+ submitted: data.submitted,
781
+ status: data.status,
782
+ txHash: data.txHash ?? null,
783
+ reason: data.reason ?? null,
784
+ resultCodes: data.resultCodes ?? null,
785
+ swap: this._add(new Swap(this.client, data.swap))
786
+ };
787
+ }
788
+ };
789
+
634
790
  // src/structures/WebhookDelivery.ts
635
791
  var WebhookDelivery = class extends Base {
636
792
  constructor(client, data) {
@@ -743,7 +899,7 @@ var WebhookEndpoint = class extends Base {
743
899
  };
744
900
 
745
901
  // src/util/Constants.ts
746
- var version = "1.1.0";
902
+ var version = "1.2.0";
747
903
  var DEFAULT_BASE_URL = "https://api.cosmospay.lat/cosmos-api";
748
904
  var DEFAULT_VERSION = "v1";
749
905
  var DEFAULT_TIMEOUT = 3e4;
@@ -1293,6 +1449,7 @@ var Client = class extends events.EventEmitter {
1293
1449
  this.paymentIntents = new PaymentIntentManager(this);
1294
1450
  this.webhooks = new WebhookManager(this);
1295
1451
  this.products = new ProductManager(this);
1452
+ this.swaps = new SwapManager(this);
1296
1453
  this.customers = new CustomerManager(this);
1297
1454
  this.analytics = new AnalyticsManager(this);
1298
1455
  this.health = new HealthManager(this);
@@ -1348,6 +1505,9 @@ exports.ProductKind = ProductKind;
1348
1505
  exports.ProductManager = ProductManager;
1349
1506
  exports.REST = REST;
1350
1507
  exports.SIGNATURE_HEADER = SIGNATURE_HEADER;
1508
+ exports.Swap = Swap;
1509
+ exports.SwapManager = SwapManager;
1510
+ exports.SwapStatus = SwapStatus;
1351
1511
  exports.TestnetAssets = TestnetAssets;
1352
1512
  exports.Wallets = Wallets;
1353
1513
  exports.WebhookDelivery = WebhookDelivery;