@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/CHANGELOG.md +18 -8
- package/LICENSE +273 -18
- package/README.md +269 -8
- package/dist/chunk-TZDZBVFS.js +216 -0
- package/dist/chunk-TZDZBVFS.js.map +1 -0
- package/dist/index.cjs +333 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +117 -427
- package/dist/index.d.ts +117 -427
- package/dist/index.js +162 -67
- package/dist/index.js.map +1 -1
- package/dist/intent-input-Ch8ogEg-.d.cts +750 -0
- package/dist/intent-input-Ch8ogEg-.d.ts +750 -0
- package/dist/web/index.cjs +1260 -0
- package/dist/web/index.cjs.map +1 -0
- package/dist/web/index.d.cts +758 -0
- package/dist/web/index.d.ts +758 -0
- package/dist/web/index.js +1015 -0
- package/dist/web/index.js.map +1 -0
- package/llms/00-overview.md +43 -0
- package/llms/01-installation.md +35 -0
- package/llms/02-authentication.md +48 -0
- package/llms/03-quickstart.md +57 -0
- package/llms/04-client.md +54 -0
- package/llms/05-payment-intents.md +79 -0
- package/llms/06-web-client.md +113 -0
- package/llms/07-webhooks.md +109 -0
- package/llms/08-products-customers.md +52 -0
- package/llms/09-analytics-health.md +25 -0
- package/llms/10-assets-wallets-addresses.md +57 -0
- package/llms/11-errors-observability.md +47 -0
- package/llms/12-configuration.md +43 -0
- package/llms/13-types-reference.md +78 -0
- package/llms/14-recipes.md +87 -0
- package/llms/README.md +40 -0
- package/llms/llms-full.txt +977 -0
- package/llms/llms.txt +45 -0
- package/package.json +44 -13
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// src/common/addresses.ts
|
|
2
|
+
var G_ADDRESS = /^G[A-Z2-7]{55}$/;
|
|
3
|
+
var M_ADDRESS = /^M[A-Z2-7]{68}$/;
|
|
4
|
+
var C_ADDRESS = /^C[A-Z2-7]{55}$/;
|
|
5
|
+
function isStellarAddress(value) {
|
|
6
|
+
return typeof value === "string" && G_ADDRESS.test(value);
|
|
7
|
+
}
|
|
8
|
+
function isMuxedAddress(value) {
|
|
9
|
+
return typeof value === "string" && M_ADDRESS.test(value);
|
|
10
|
+
}
|
|
11
|
+
function isContractAddress(value) {
|
|
12
|
+
return typeof value === "string" && C_ADDRESS.test(value);
|
|
13
|
+
}
|
|
14
|
+
function isAddress(value) {
|
|
15
|
+
return isStellarAddress(value) || isMuxedAddress(value) || isContractAddress(value);
|
|
16
|
+
}
|
|
17
|
+
function assertAddress(value, label = "address") {
|
|
18
|
+
if (!isAddress(value)) {
|
|
19
|
+
throw new TypeError(`Invalid Stellar ${label}: ${String(value)}`);
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
var AddressBook = class {
|
|
24
|
+
constructor(initial = {}) {
|
|
25
|
+
this.entries = /* @__PURE__ */ new Map();
|
|
26
|
+
for (const [name, address] of Object.entries(initial)) {
|
|
27
|
+
this.define(name, address);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** Register (or overwrite) a named address. The address is validated. */
|
|
31
|
+
define(name, address) {
|
|
32
|
+
this.entries.set(name, assertAddress(address, `address for "${name}"`));
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
/** Whether a name is registered. */
|
|
36
|
+
has(name) {
|
|
37
|
+
return this.entries.has(name);
|
|
38
|
+
}
|
|
39
|
+
/** Look up a registered address by name. */
|
|
40
|
+
get(name) {
|
|
41
|
+
return this.entries.get(name);
|
|
42
|
+
}
|
|
43
|
+
/** Remove a named address. */
|
|
44
|
+
delete(name) {
|
|
45
|
+
return this.entries.delete(name);
|
|
46
|
+
}
|
|
47
|
+
/** All registered names. */
|
|
48
|
+
names() {
|
|
49
|
+
return [...this.entries.keys()];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Resolve a reference to a concrete address: a registered name is substituted,
|
|
53
|
+
* any other string is returned unchanged (so raw `G…`, muxed `M…`, federation
|
|
54
|
+
* `name*domain`, etc. all keep working).
|
|
55
|
+
*/
|
|
56
|
+
resolve(ref) {
|
|
57
|
+
return this.entries.get(ref) ?? ref;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
var addresses = new AddressBook();
|
|
61
|
+
function resolveAddress(ref, book = addresses) {
|
|
62
|
+
return book.resolve(ref);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/common/assets.ts
|
|
66
|
+
var NATIVE = { code: "XLM", issuer: null, name: "Stellar Lumens" };
|
|
67
|
+
var Assets = {
|
|
68
|
+
/** Native lumens. */
|
|
69
|
+
XLM: NATIVE,
|
|
70
|
+
/** USD Coin by Circle (mainnet). */
|
|
71
|
+
USDC: {
|
|
72
|
+
code: "USDC",
|
|
73
|
+
issuer: "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
|
|
74
|
+
name: "USD Coin",
|
|
75
|
+
network: "public"
|
|
76
|
+
},
|
|
77
|
+
/** Euro Coin by Circle (mainnet). */
|
|
78
|
+
EURC: {
|
|
79
|
+
code: "EURC",
|
|
80
|
+
issuer: "GDHU6WRG4IEQXM5NZ4BMPKOXHW76MZM4Y2IEMFDVXBSDP6SJY4ITNPP2",
|
|
81
|
+
name: "Euro Coin",
|
|
82
|
+
network: "public"
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
var TestnetAssets = {
|
|
86
|
+
XLM: NATIVE,
|
|
87
|
+
USDC: {
|
|
88
|
+
code: "USDC",
|
|
89
|
+
issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
|
|
90
|
+
name: "USD Coin",
|
|
91
|
+
network: "testnet"
|
|
92
|
+
},
|
|
93
|
+
EURC: {
|
|
94
|
+
code: "EURC",
|
|
95
|
+
issuer: "GB3Q6QDZYTHWT7E5PVS3W7FUT5GVAFC5KSZFFLPU25GO7VTC3NM2ZTVO",
|
|
96
|
+
name: "Euro Coin",
|
|
97
|
+
network: "testnet"
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
function isNativeAsset(ref) {
|
|
101
|
+
if (typeof ref === "string") {
|
|
102
|
+
const code = ref.trim().toLowerCase();
|
|
103
|
+
return code === "" || code === "xlm" || code === "native";
|
|
104
|
+
}
|
|
105
|
+
return !ref.issuer || isNativeAsset(ref.code);
|
|
106
|
+
}
|
|
107
|
+
function defineAsset(def) {
|
|
108
|
+
if (!def.code) throw new TypeError("defineAsset requires a `code`.");
|
|
109
|
+
if (def.issuer != null && !isStellarAddress(def.issuer)) {
|
|
110
|
+
throw new TypeError(`defineAsset: invalid issuer for ${def.code}: ${def.issuer}`);
|
|
111
|
+
}
|
|
112
|
+
if (def.contract != null && !/^C[A-Z2-7]{55}$/.test(def.contract)) {
|
|
113
|
+
throw new TypeError(`defineAsset: invalid contract for ${def.code}: ${def.contract}`);
|
|
114
|
+
}
|
|
115
|
+
return Object.freeze({ ...def });
|
|
116
|
+
}
|
|
117
|
+
function resolveAsset(ref) {
|
|
118
|
+
if (isNativeAsset(ref)) return { code: "XLM", issuer: null };
|
|
119
|
+
if (typeof ref === "string") return { code: ref, issuer: null };
|
|
120
|
+
return {
|
|
121
|
+
code: ref.code,
|
|
122
|
+
issuer: ref.issuer ?? null,
|
|
123
|
+
...ref.contract ? { contract: ref.contract } : {}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/common/intent-input.ts
|
|
128
|
+
function resolveIntentBody(options, book = addresses) {
|
|
129
|
+
const out = { ...options };
|
|
130
|
+
if (typeof out["destination"] === "string") {
|
|
131
|
+
out["destination"] = resolveAddress(out["destination"], book);
|
|
132
|
+
}
|
|
133
|
+
if (typeof out["source"] === "string") {
|
|
134
|
+
out["source"] = resolveAddress(out["source"], book);
|
|
135
|
+
}
|
|
136
|
+
if (out["asset"] != null) {
|
|
137
|
+
const { code, issuer } = resolveAsset(out["asset"]);
|
|
138
|
+
delete out["asset"];
|
|
139
|
+
if (issuer) {
|
|
140
|
+
out["assetCode"] ??= code;
|
|
141
|
+
out["assetIssuer"] ??= issuer;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// src/util/Constants.ts
|
|
148
|
+
var version = "1.2.0";
|
|
149
|
+
var DEFAULT_BASE_URL = "https://api.cosmospay.lat/cosmos-api";
|
|
150
|
+
var DEFAULT_VERSION = "v1";
|
|
151
|
+
var DEFAULT_TIMEOUT = 3e4;
|
|
152
|
+
var DEFAULT_RETRIES = 2;
|
|
153
|
+
var SIGNATURE_HEADER = "x-cosmos-signature";
|
|
154
|
+
var Events = {
|
|
155
|
+
Debug: "debug",
|
|
156
|
+
Request: "request",
|
|
157
|
+
Response: "response",
|
|
158
|
+
RateLimited: "rateLimited"
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// src/errors/CosmosPayError.ts
|
|
162
|
+
var CosmosPayAPIError = class _CosmosPayAPIError extends Error {
|
|
163
|
+
constructor(options) {
|
|
164
|
+
const message = _CosmosPayAPIError.resolveMessage(options.status, options.body);
|
|
165
|
+
super(message);
|
|
166
|
+
this.name = "CosmosPayAPIError";
|
|
167
|
+
this.status = options.status;
|
|
168
|
+
this.method = options.method;
|
|
169
|
+
this.url = options.url;
|
|
170
|
+
this.body = options.body;
|
|
171
|
+
this.code = _CosmosPayAPIError.resolveCode(options.body);
|
|
172
|
+
Error.captureStackTrace?.(this, _CosmosPayAPIError);
|
|
173
|
+
}
|
|
174
|
+
static resolveMessage(status, body) {
|
|
175
|
+
if (body && typeof body === "object") {
|
|
176
|
+
const maybe = body;
|
|
177
|
+
const msg = maybe.message ?? maybe.error;
|
|
178
|
+
if (Array.isArray(msg)) return `[${status}] ${msg.join(", ")}`;
|
|
179
|
+
if (typeof msg === "string") return `[${status}] ${msg}`;
|
|
180
|
+
}
|
|
181
|
+
if (typeof body === "string" && body.length) return `[${status}] ${body}`;
|
|
182
|
+
return `[${status}] Request failed`;
|
|
183
|
+
}
|
|
184
|
+
static resolveCode(body) {
|
|
185
|
+
if (body && typeof body === "object") {
|
|
186
|
+
const maybe = body;
|
|
187
|
+
if (typeof maybe.code === "string") return maybe.code;
|
|
188
|
+
if (typeof maybe.error === "string") return maybe.error;
|
|
189
|
+
}
|
|
190
|
+
return void 0;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
var CosmosPayRequestError = class _CosmosPayRequestError extends Error {
|
|
194
|
+
constructor(options) {
|
|
195
|
+
const reason = options.cause instanceof Error ? options.cause.message : String(options.cause);
|
|
196
|
+
super(`Request to ${options.method} ${options.url} failed: ${reason}`);
|
|
197
|
+
this.name = "CosmosPayRequestError";
|
|
198
|
+
this.method = options.method;
|
|
199
|
+
this.url = options.url;
|
|
200
|
+
this.cause = options.cause;
|
|
201
|
+
Error.captureStackTrace?.(this, _CosmosPayRequestError);
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// src/common/wallets.ts
|
|
206
|
+
var Wallets = {
|
|
207
|
+
FREIGHTER: "freighter",
|
|
208
|
+
XBULL: "xbull",
|
|
209
|
+
RABET: "rabet",
|
|
210
|
+
LOBSTR: "lobstr",
|
|
211
|
+
ALBEDO: "albedo"
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
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 };
|
|
215
|
+
//# sourceMappingURL=chunk-TZDZBVFS.js.map
|
|
216
|
+
//# sourceMappingURL=chunk-TZDZBVFS.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/common/addresses.ts","../src/common/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M,OAAA,GAAU,MAAA,CAAO,OAAA,CAAQ,KAAK,CAAA;AAC/E,IAAA,KAAA,CAAM,CAAA,WAAA,EAAc,QAAQ,MAAM,CAAA,CAAA,EAAI,QAAQ,GAAG,CAAA,SAAA,EAAY,MAAM,CAAA,CAAE,CAAA;AACrE,IAAA,IAAA,CAAK,IAAA,GAAO,uBAAA;AACZ,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,MAAM,OAAA,CAAQ,GAAA;AACnB,IAAA,IAAA,CAAK,QAAQ,OAAA,CAAQ,KAAA;AACrB,IAAA,KAAA,CAAM,iBAAA,GAAoB,MAAM,sBAAqB,CAAA;AAAA,EACvD;AACF;;;ACpEO,IAAM,OAAA,GAAU;AAAA,EACrB,SAAA,EAAW,WAAA;AAAA,EACX,KAAA,EAAO,OAAA;AAAA,EACP,KAAA,EAAO,OAAA;AAAA,EACP,MAAA,EAAQ,QAAA;AAAA,EACR,MAAA,EAAQ;AACV","file":"chunk-TZDZBVFS.js","sourcesContent":["/**\n * Stellar address helpers + a tiny named-address book.\n *\n * So integrators don't have to sprinkle raw `G...` strings everywhere (and risk\n * typos), they can name the accounts they care about once and reference them by\n * name. A plain address string always keeps working — `resolveAddress` only\n * substitutes registered names and otherwise passes the value straight through.\n */\n\n// Stellar strkeys are base32 (RFC 4648 alphabet, no padding). These checks are\n// length/prefix sanity checks, not full checksum validation.\nconst G_ADDRESS = /^G[A-Z2-7]{55}$/; // ed25519 public key (account)\nconst M_ADDRESS = /^M[A-Z2-7]{68}$/; // muxed account\nconst C_ADDRESS = /^C[A-Z2-7]{55}$/; // contract id\n\n/** Whether a value looks like a Stellar account address (`G…`). */\nexport function isStellarAddress(value: unknown): value is string {\n return typeof value === 'string' && G_ADDRESS.test(value);\n}\n\n/** Whether a value looks like a muxed account address (`M…`). */\nexport function isMuxedAddress(value: unknown): value is string {\n return typeof value === 'string' && M_ADDRESS.test(value);\n}\n\n/** Whether a value looks like a Soroban contract id (`C…`). */\nexport function isContractAddress(value: unknown): value is string {\n return typeof value === 'string' && C_ADDRESS.test(value);\n}\n\n/** Whether a value is any kind of Stellar address (account, muxed or contract). */\nexport function isAddress(value: unknown): value is string {\n return isStellarAddress(value) || isMuxedAddress(value) || isContractAddress(value);\n}\n\n/** Throw unless `value` is a valid Stellar account/muxed/contract address. */\nexport function assertAddress(value: unknown, label = 'address'): string {\n if (!isAddress(value)) {\n throw new TypeError(`Invalid Stellar ${label}: ${String(value)}`);\n }\n return value;\n}\n\n/**\n * A registry of named Stellar addresses. Register the accounts you use a lot —\n * your merchant payout account, a treasury, frequent counterparties — and refer\n * to them by name anywhere an address is accepted.\n *\n * @example\n * addresses.define('merchant', 'GC...');\n * await client.paymentIntents.createPay({ destination: 'merchant', amount: '10' });\n */\nexport class AddressBook {\n private readonly entries = new Map<string, string>();\n\n constructor(initial: Record<string, string> = {}) {\n for (const [name, address] of Object.entries(initial)) {\n this.define(name, address);\n }\n }\n\n /** Register (or overwrite) a named address. The address is validated. */\n public define(name: string, address: string): this {\n this.entries.set(name, assertAddress(address, `address for \"${name}\"`));\n return this;\n }\n\n /** Whether a name is registered. */\n public has(name: string): boolean {\n return this.entries.has(name);\n }\n\n /** Look up a registered address by name. */\n public get(name: string): string | undefined {\n return this.entries.get(name);\n }\n\n /** Remove a named address. */\n public delete(name: string): boolean {\n return this.entries.delete(name);\n }\n\n /** All registered names. */\n public names(): string[] {\n return [...this.entries.keys()];\n }\n\n /**\n * Resolve a reference to a concrete address: a registered name is substituted,\n * any other string is returned unchanged (so raw `G…`, muxed `M…`, federation\n * `name*domain`, etc. all keep working).\n */\n public resolve(ref: string): string {\n return this.entries.get(ref) ?? ref;\n }\n}\n\n/** Process-wide default address book. Use `addresses.define(...)` to populate it. */\nexport const addresses = new AddressBook();\n\n/** Anything accepted where an address is expected: a raw address or a name in a book. */\nexport type AddressRef = string;\n\n/**\n * Resolve an {@link AddressRef} using the given book (the shared {@link addresses}\n * by default). Lenient: unknown strings pass through unchanged.\n */\nexport function resolveAddress(ref: AddressRef, book: AddressBook = addresses): string {\n return book.resolve(ref);\n}\n","/**\n * A typed, extensible catalog of Stellar assets.\n *\n * Instead of hunting down an asset's issuer (or pasting the wrong one), use the\n * built-in catalog — `Assets.USDC`, `Assets.EURC`, `Assets.XLM` — which carries\n * the verified issuer for you. Need something else? `defineAsset(...)` makes a\n * typed asset you can reuse. And a plain code string still works everywhere.\n *\n * ⚠️ Issuers are network-specific. The {@link Assets} catalog targets **mainnet**;\n * {@link TestnetAssets} carries the testnet issuers. Bare code strings (e.g.\n * `'USDC'`) are treated as *code only* (no issuer) for backwards compatibility —\n * use the catalog object or `defineAsset` when you want the issuer filled in.\n */\n\nimport { isStellarAddress } from '@/common/addresses';\n\n/** A fully-described Stellar asset. */\nexport interface AssetDefinition {\n /** Asset code, e.g. `USDC`. Use `XLM` (or omit the issuer) for native lumens. */\n code: string;\n /** Issuer account (`G…`). `null`/omitted means the native asset (XLM). */\n issuer?: string | null;\n /** Optional Soroban contract id (`C…`) for the SAC / token contract. */\n contract?: string;\n /** Human-readable name. */\n name?: string;\n /** Network the `issuer` is valid on. */\n network?: 'public' | 'testnet';\n}\n\n/** Anything accepted where an asset is expected: a code string or a definition. */\nexport type AssetRef = string | AssetDefinition;\n\n/** Normalized asset: a code plus a concrete issuer (or `null` for native). */\nexport interface ResolvedAsset {\n /** `XLM` for the native asset, otherwise the asset code. */\n code: string;\n /** Issuer `G…`, or `null` for the native asset. */\n issuer: string | null;\n /** Optional Soroban contract id. */\n contract?: string;\n}\n\nconst NATIVE: AssetDefinition = { code: 'XLM', issuer: null, name: 'Stellar Lumens' };\n\n/**\n * Built-in **mainnet** assets with verified issuers. Extend with your own via\n * {@link defineAsset}. Issuers sourced from the official issuers (Circle for\n * USDC/EURC).\n */\nexport const Assets = {\n /** Native lumens. */\n XLM: NATIVE,\n /** USD Coin by Circle (mainnet). */\n USDC: {\n code: 'USDC',\n issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN',\n name: 'USD Coin',\n network: 'public',\n },\n /** Euro Coin by Circle (mainnet). */\n EURC: {\n code: 'EURC',\n issuer: 'GDHU6WRG4IEQXM5NZ4BMPKOXHW76MZM4Y2IEMFDVXBSDP6SJY4ITNPP2',\n name: 'Euro Coin',\n network: 'public',\n },\n} as const satisfies Record<string, AssetDefinition>;\n\n/** Built-in **testnet** assets (Circle's Test SDF Network issuers). */\nexport const TestnetAssets = {\n XLM: NATIVE,\n USDC: {\n code: 'USDC',\n issuer: 'GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5',\n name: 'USD Coin',\n network: 'testnet',\n },\n EURC: {\n code: 'EURC',\n issuer: 'GB3Q6QDZYTHWT7E5PVS3W7FUT5GVAFC5KSZFFLPU25GO7VTC3NM2ZTVO',\n name: 'Euro Coin',\n network: 'testnet',\n },\n} as const satisfies Record<string, AssetDefinition>;\n\n/** Whether a reference denotes the native asset (XLM). */\nexport function isNativeAsset(ref: AssetRef): boolean {\n if (typeof ref === 'string') {\n const code = ref.trim().toLowerCase();\n return code === '' || code === 'xlm' || code === 'native';\n }\n return !ref.issuer || isNativeAsset(ref.code);\n}\n\n/**\n * Create a reusable, typed custom asset. Validates the issuer/contract format so\n * a typo surfaces immediately rather than on-chain.\n *\n * @example\n * const AQUA = defineAsset({ code: 'AQUA', issuer: 'GBNZ...', name: 'Aquarius' });\n * await client.paymentIntents.createPay({ destination, amount: '5', asset: AQUA });\n */\nexport function defineAsset(def: AssetDefinition): AssetDefinition {\n if (!def.code) throw new TypeError('defineAsset requires a `code`.');\n if (def.issuer != null && !isStellarAddress(def.issuer)) {\n throw new TypeError(`defineAsset: invalid issuer for ${def.code}: ${def.issuer}`);\n }\n if (def.contract != null && !/^C[A-Z2-7]{55}$/.test(def.contract)) {\n throw new TypeError(`defineAsset: invalid contract for ${def.code}: ${def.contract}`);\n }\n return Object.freeze({ ...def });\n}\n\n/**\n * Normalize an {@link AssetRef} into `{ code, issuer, contract }`.\n *\n * - A bare string is treated as *code only* (`issuer: null`) — pass a catalog\n * object or `defineAsset(...)` result when you want the issuer included.\n * - The native asset normalizes to `{ code: 'XLM', issuer: null }`.\n */\nexport function resolveAsset(ref: AssetRef): ResolvedAsset {\n if (isNativeAsset(ref)) return { code: 'XLM', issuer: null };\n if (typeof ref === 'string') return { code: ref, issuer: null };\n return {\n code: ref.code,\n issuer: ref.issuer ?? null,\n ...(ref.contract ? { contract: ref.contract } : {}),\n };\n}\n","/**\n * Shared resolution of \"create intent\" input, used by both the server SDK and\n * the web client so the convenience layers behave identically: a typed\n * {@link AssetRef} (e.g. `Assets.USDC`) is expanded into `assetCode`/`assetIssuer`,\n * and named addresses are substituted via an {@link AddressBook}.\n */\n\nimport { addresses as defaultBook, AddressBook, resolveAddress } from '@/common/addresses';\nimport { resolveAsset, type AssetRef } from '@/common/assets';\n\n/** Fields the resolver understands (everything else passes through untouched). */\nexport interface ResolvableIntentInput {\n destination?: string;\n source?: string;\n /** Typed asset (`Assets.USDC`, `defineAsset(...)`) — fills code + issuer. */\n asset?: AssetRef;\n assetCode?: string;\n assetIssuer?: string;\n}\n\n/**\n * Expand `asset` into `assetCode`/`assetIssuer` and resolve named\n * `destination`/`source` addresses. Existing `assetCode`/`assetIssuer` win over a\n * provided `asset`; native assets leave the code unset (the API treats an omitted\n * asset as native XLM). Returns a new object — the input is not mutated.\n */\nexport function resolveIntentBody<T extends ResolvableIntentInput>(\n options: T,\n book: AddressBook = defaultBook,\n): Omit<T, 'asset'> {\n const out: Record<string, unknown> = { ...(options as Record<string, unknown>) };\n\n if (typeof out['destination'] === 'string') {\n out['destination'] = resolveAddress(out['destination'], book);\n }\n if (typeof out['source'] === 'string') {\n out['source'] = resolveAddress(out['source'], book);\n }\n\n if (out['asset'] != null) {\n const { code, issuer } = resolveAsset(out['asset'] as AssetRef);\n delete out['asset'];\n if (issuer) {\n // Non-native: fill code + issuer unless the caller set them explicitly.\n out['assetCode'] ??= code;\n out['assetIssuer'] ??= issuer;\n }\n // Native (issuer == null) → leave assetCode/assetIssuer omitted = native XLM.\n }\n\n return out as Omit<T, 'asset'>;\n}\n","/** Library version, kept in sync with package.json. */\nexport const version = '1.2.0';\n\n/**\n * Default base URL of the Cosmos Pay gateway. Maintained by Cosmos Pay — users\n * never need to set this; it's part of the {@link shared} configuration. Override\n * only when self-hosting or pointing at a staging gateway.\n *\n * This is the public APISIX gateway: requests under `/cosmos-api/*` are\n * authenticated (API key) and proxied to the Payments API. The version segment\n * (`/v1`, see {@link DEFAULT_VERSION}) is appended by the REST layer, producing\n * e.g. `https://api.cosmospay.lat/cosmos-api/v1/payment-intents/pay`.\n */\nexport const DEFAULT_BASE_URL = 'https://api.cosmospay.lat/cosmos-api';\n\n/** Default API version segment prefixed to every route (`/v1/...`). */\nexport const DEFAULT_VERSION = 'v1';\n\n/** Default request timeout in milliseconds. */\nexport const DEFAULT_TIMEOUT = 30_000;\n\n/** Default number of retries for transient failures (network / 429 / 5xx). */\nexport const DEFAULT_RETRIES = 2;\n\n/** Default HMAC signature header sent by the Cosmos Pay webhook dispatcher. */\nexport const SIGNATURE_HEADER = 'x-cosmos-signature';\n\n/**\n * Client debug/telemetry events. The {@link Client} extends EventEmitter and\n * emits these for observability (`debug`/`rateLimit`-style events).\n */\nexport const Events = {\n Debug: 'debug',\n Request: 'request',\n Response: 'response',\n RateLimited: 'rateLimited',\n} as const;\nexport type ClientEvent = (typeof Events)[keyof typeof Events];\n","/**\n * Error thrown when the Cosmos Pay API responds with a non-2xx status.\n *\n * Carries the HTTP status, the parsed response body and the request context so\n * callers can branch on `error.status` (e.g. 404 → not found, 403 → gateway).\n */\nexport class CosmosPayAPIError extends Error {\n /** HTTP status code returned by the API. */\n public readonly status: number;\n /** HTTP method of the failed request. */\n public readonly method: string;\n /** Absolute URL of the failed request. */\n public readonly url: string;\n /** Parsed response body (object when JSON, string otherwise, or null). */\n public readonly body: unknown;\n /** Machine-readable error code extracted from the body when present. */\n public readonly code: string | undefined;\n\n constructor(options: {\n status: number;\n method: string;\n url: string;\n body: unknown;\n }) {\n const message = CosmosPayAPIError.resolveMessage(options.status, options.body);\n super(message);\n this.name = 'CosmosPayAPIError';\n this.status = options.status;\n this.method = options.method;\n this.url = options.url;\n this.body = options.body;\n this.code = CosmosPayAPIError.resolveCode(options.body);\n Error.captureStackTrace?.(this, CosmosPayAPIError);\n }\n\n private static resolveMessage(status: number, body: unknown): string {\n if (body && typeof body === 'object') {\n const maybe = body as { message?: unknown; error?: unknown };\n const msg = maybe.message ?? maybe.error;\n if (Array.isArray(msg)) return `[${status}] ${msg.join(', ')}`;\n if (typeof msg === 'string') return `[${status}] ${msg}`;\n }\n if (typeof body === 'string' && body.length) return `[${status}] ${body}`;\n return `[${status}] Request failed`;\n }\n\n private static resolveCode(body: unknown): string | undefined {\n if (body && typeof body === 'object') {\n const maybe = body as { code?: unknown; error?: unknown };\n if (typeof maybe.code === 'string') return maybe.code;\n if (typeof maybe.error === 'string') return maybe.error;\n }\n return undefined;\n }\n}\n\n/**\n * Error thrown when a request never reaches the API: network failure, abort or\n * timeout. Distinct from {@link CosmosPayAPIError}, which always has a status.\n */\nexport class CosmosPayRequestError extends Error {\n public readonly method: string;\n public readonly url: string;\n public readonly cause: unknown;\n\n constructor(options: { method: string; url: string; cause: unknown }) {\n const reason =\n options.cause instanceof Error ? options.cause.message : String(options.cause);\n super(`Request to ${options.method} ${options.url} failed: ${reason}`);\n this.name = 'CosmosPayRequestError';\n this.method = options.method;\n this.url = options.url;\n this.cause = options.cause;\n Error.captureStackTrace?.(this, CosmosPayRequestError);\n }\n}\n","/**\n * Typed identifiers for the wallets the web client knows about, so you can write\n * `Wallets.FREIGHTER` instead of remembering the `'freighter'` magic string —\n * while any custom adapter id still works.\n */\n\n/** Built-in wallet ids (matches each adapter's `id`). */\nexport const Wallets = {\n FREIGHTER: 'freighter',\n XBULL: 'xbull',\n RABET: 'rabet',\n LOBSTR: 'lobstr',\n ALBEDO: 'albedo',\n} as const;\n\n/** A known wallet id, or any custom adapter id (autocomplete for the built-ins). */\nexport type WalletId = (typeof Wallets)[keyof typeof Wallets] | (string & {});\n"]}
|
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 {
|
|
@@ -315,6 +322,152 @@ var PaymentIntent = class extends Base {
|
|
|
315
322
|
}
|
|
316
323
|
};
|
|
317
324
|
|
|
325
|
+
// src/common/addresses.ts
|
|
326
|
+
var G_ADDRESS = /^G[A-Z2-7]{55}$/;
|
|
327
|
+
var M_ADDRESS = /^M[A-Z2-7]{68}$/;
|
|
328
|
+
var C_ADDRESS = /^C[A-Z2-7]{55}$/;
|
|
329
|
+
function isStellarAddress(value) {
|
|
330
|
+
return typeof value === "string" && G_ADDRESS.test(value);
|
|
331
|
+
}
|
|
332
|
+
function isMuxedAddress(value) {
|
|
333
|
+
return typeof value === "string" && M_ADDRESS.test(value);
|
|
334
|
+
}
|
|
335
|
+
function isContractAddress(value) {
|
|
336
|
+
return typeof value === "string" && C_ADDRESS.test(value);
|
|
337
|
+
}
|
|
338
|
+
function isAddress(value) {
|
|
339
|
+
return isStellarAddress(value) || isMuxedAddress(value) || isContractAddress(value);
|
|
340
|
+
}
|
|
341
|
+
function assertAddress(value, label = "address") {
|
|
342
|
+
if (!isAddress(value)) {
|
|
343
|
+
throw new TypeError(`Invalid Stellar ${label}: ${String(value)}`);
|
|
344
|
+
}
|
|
345
|
+
return value;
|
|
346
|
+
}
|
|
347
|
+
var AddressBook = class {
|
|
348
|
+
constructor(initial = {}) {
|
|
349
|
+
this.entries = /* @__PURE__ */ new Map();
|
|
350
|
+
for (const [name, address] of Object.entries(initial)) {
|
|
351
|
+
this.define(name, address);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/** Register (or overwrite) a named address. The address is validated. */
|
|
355
|
+
define(name, address) {
|
|
356
|
+
this.entries.set(name, assertAddress(address, `address for "${name}"`));
|
|
357
|
+
return this;
|
|
358
|
+
}
|
|
359
|
+
/** Whether a name is registered. */
|
|
360
|
+
has(name) {
|
|
361
|
+
return this.entries.has(name);
|
|
362
|
+
}
|
|
363
|
+
/** Look up a registered address by name. */
|
|
364
|
+
get(name) {
|
|
365
|
+
return this.entries.get(name);
|
|
366
|
+
}
|
|
367
|
+
/** Remove a named address. */
|
|
368
|
+
delete(name) {
|
|
369
|
+
return this.entries.delete(name);
|
|
370
|
+
}
|
|
371
|
+
/** All registered names. */
|
|
372
|
+
names() {
|
|
373
|
+
return [...this.entries.keys()];
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Resolve a reference to a concrete address: a registered name is substituted,
|
|
377
|
+
* any other string is returned unchanged (so raw `G…`, muxed `M…`, federation
|
|
378
|
+
* `name*domain`, etc. all keep working).
|
|
379
|
+
*/
|
|
380
|
+
resolve(ref) {
|
|
381
|
+
return this.entries.get(ref) ?? ref;
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
var addresses = new AddressBook();
|
|
385
|
+
function resolveAddress(ref, book = addresses) {
|
|
386
|
+
return book.resolve(ref);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// src/common/assets.ts
|
|
390
|
+
var NATIVE = { code: "XLM", issuer: null, name: "Stellar Lumens" };
|
|
391
|
+
var Assets = {
|
|
392
|
+
/** Native lumens. */
|
|
393
|
+
XLM: NATIVE,
|
|
394
|
+
/** USD Coin by Circle (mainnet). */
|
|
395
|
+
USDC: {
|
|
396
|
+
code: "USDC",
|
|
397
|
+
issuer: "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
|
|
398
|
+
name: "USD Coin",
|
|
399
|
+
network: "public"
|
|
400
|
+
},
|
|
401
|
+
/** Euro Coin by Circle (mainnet). */
|
|
402
|
+
EURC: {
|
|
403
|
+
code: "EURC",
|
|
404
|
+
issuer: "GDHU6WRG4IEQXM5NZ4BMPKOXHW76MZM4Y2IEMFDVXBSDP6SJY4ITNPP2",
|
|
405
|
+
name: "Euro Coin",
|
|
406
|
+
network: "public"
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
var TestnetAssets = {
|
|
410
|
+
XLM: NATIVE,
|
|
411
|
+
USDC: {
|
|
412
|
+
code: "USDC",
|
|
413
|
+
issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
|
|
414
|
+
name: "USD Coin",
|
|
415
|
+
network: "testnet"
|
|
416
|
+
},
|
|
417
|
+
EURC: {
|
|
418
|
+
code: "EURC",
|
|
419
|
+
issuer: "GB3Q6QDZYTHWT7E5PVS3W7FUT5GVAFC5KSZFFLPU25GO7VTC3NM2ZTVO",
|
|
420
|
+
name: "Euro Coin",
|
|
421
|
+
network: "testnet"
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
function isNativeAsset(ref) {
|
|
425
|
+
if (typeof ref === "string") {
|
|
426
|
+
const code = ref.trim().toLowerCase();
|
|
427
|
+
return code === "" || code === "xlm" || code === "native";
|
|
428
|
+
}
|
|
429
|
+
return !ref.issuer || isNativeAsset(ref.code);
|
|
430
|
+
}
|
|
431
|
+
function defineAsset(def) {
|
|
432
|
+
if (!def.code) throw new TypeError("defineAsset requires a `code`.");
|
|
433
|
+
if (def.issuer != null && !isStellarAddress(def.issuer)) {
|
|
434
|
+
throw new TypeError(`defineAsset: invalid issuer for ${def.code}: ${def.issuer}`);
|
|
435
|
+
}
|
|
436
|
+
if (def.contract != null && !/^C[A-Z2-7]{55}$/.test(def.contract)) {
|
|
437
|
+
throw new TypeError(`defineAsset: invalid contract for ${def.code}: ${def.contract}`);
|
|
438
|
+
}
|
|
439
|
+
return Object.freeze({ ...def });
|
|
440
|
+
}
|
|
441
|
+
function resolveAsset(ref) {
|
|
442
|
+
if (isNativeAsset(ref)) return { code: "XLM", issuer: null };
|
|
443
|
+
if (typeof ref === "string") return { code: ref, issuer: null };
|
|
444
|
+
return {
|
|
445
|
+
code: ref.code,
|
|
446
|
+
issuer: ref.issuer ?? null,
|
|
447
|
+
...ref.contract ? { contract: ref.contract } : {}
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// src/common/intent-input.ts
|
|
452
|
+
function resolveIntentBody(options, book = addresses) {
|
|
453
|
+
const out = { ...options };
|
|
454
|
+
if (typeof out["destination"] === "string") {
|
|
455
|
+
out["destination"] = resolveAddress(out["destination"], book);
|
|
456
|
+
}
|
|
457
|
+
if (typeof out["source"] === "string") {
|
|
458
|
+
out["source"] = resolveAddress(out["source"], book);
|
|
459
|
+
}
|
|
460
|
+
if (out["asset"] != null) {
|
|
461
|
+
const { code, issuer } = resolveAsset(out["asset"]);
|
|
462
|
+
delete out["asset"];
|
|
463
|
+
if (issuer) {
|
|
464
|
+
out["assetCode"] ??= code;
|
|
465
|
+
out["assetIssuer"] ??= issuer;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return out;
|
|
469
|
+
}
|
|
470
|
+
|
|
318
471
|
// src/managers/PaymentIntentManager.ts
|
|
319
472
|
var PaymentIntentManager = class extends BaseManager {
|
|
320
473
|
constructor() {
|
|
@@ -324,14 +477,14 @@ var PaymentIntentManager = class extends BaseManager {
|
|
|
324
477
|
/** Create a SEP-7 `tx` intent (source known → unsigned XDR + tx URI + QR). */
|
|
325
478
|
async createTx(options) {
|
|
326
479
|
const data = await this.rest.post(`${this.route}/tx`, {
|
|
327
|
-
body: options
|
|
480
|
+
body: resolveIntentBody(options)
|
|
328
481
|
});
|
|
329
482
|
return this._add(new PaymentIntent(this.client, data));
|
|
330
483
|
}
|
|
331
484
|
/** Create a SEP-7 `pay` intent (no source → pay URI + QR, no XDR). */
|
|
332
485
|
async createPay(options) {
|
|
333
486
|
const data = await this.rest.post(`${this.route}/pay`, {
|
|
334
|
-
body: options
|
|
487
|
+
body: resolveIntentBody(options)
|
|
335
488
|
});
|
|
336
489
|
return this._add(new PaymentIntent(this.client, data));
|
|
337
490
|
}
|
|
@@ -485,6 +638,155 @@ var ProductManager = class extends BaseManager {
|
|
|
485
638
|
}
|
|
486
639
|
};
|
|
487
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
|
+
|
|
488
790
|
// src/structures/WebhookDelivery.ts
|
|
489
791
|
var WebhookDelivery = class extends Base {
|
|
490
792
|
constructor(client, data) {
|
|
@@ -597,7 +899,7 @@ var WebhookEndpoint = class extends Base {
|
|
|
597
899
|
};
|
|
598
900
|
|
|
599
901
|
// src/util/Constants.ts
|
|
600
|
-
var version = "1.
|
|
902
|
+
var version = "1.2.0";
|
|
601
903
|
var DEFAULT_BASE_URL = "https://api.cosmospay.lat/cosmos-api";
|
|
602
904
|
var DEFAULT_VERSION = "v1";
|
|
603
905
|
var DEFAULT_TIMEOUT = 3e4;
|
|
@@ -1147,6 +1449,7 @@ var Client = class extends events.EventEmitter {
|
|
|
1147
1449
|
this.paymentIntents = new PaymentIntentManager(this);
|
|
1148
1450
|
this.webhooks = new WebhookManager(this);
|
|
1149
1451
|
this.products = new ProductManager(this);
|
|
1452
|
+
this.swaps = new SwapManager(this);
|
|
1150
1453
|
this.customers = new CustomerManager(this);
|
|
1151
1454
|
this.analytics = new AnalyticsManager(this);
|
|
1152
1455
|
this.health = new HealthManager(this);
|
|
@@ -1168,7 +1471,18 @@ var Client = class extends events.EventEmitter {
|
|
|
1168
1471
|
}
|
|
1169
1472
|
};
|
|
1170
1473
|
|
|
1474
|
+
// src/common/wallets.ts
|
|
1475
|
+
var Wallets = {
|
|
1476
|
+
FREIGHTER: "freighter",
|
|
1477
|
+
XBULL: "xbull",
|
|
1478
|
+
RABET: "rabet",
|
|
1479
|
+
LOBSTR: "lobstr",
|
|
1480
|
+
ALBEDO: "albedo"
|
|
1481
|
+
};
|
|
1482
|
+
|
|
1483
|
+
exports.AddressBook = AddressBook;
|
|
1171
1484
|
exports.AnalyticsManager = AnalyticsManager;
|
|
1485
|
+
exports.Assets = Assets;
|
|
1172
1486
|
exports.Base = Base;
|
|
1173
1487
|
exports.BaseManager = BaseManager;
|
|
1174
1488
|
exports.Client = Client;
|
|
@@ -1191,6 +1505,11 @@ exports.ProductKind = ProductKind;
|
|
|
1191
1505
|
exports.ProductManager = ProductManager;
|
|
1192
1506
|
exports.REST = REST;
|
|
1193
1507
|
exports.SIGNATURE_HEADER = SIGNATURE_HEADER;
|
|
1508
|
+
exports.Swap = Swap;
|
|
1509
|
+
exports.SwapManager = SwapManager;
|
|
1510
|
+
exports.SwapStatus = SwapStatus;
|
|
1511
|
+
exports.TestnetAssets = TestnetAssets;
|
|
1512
|
+
exports.Wallets = Wallets;
|
|
1194
1513
|
exports.WebhookDelivery = WebhookDelivery;
|
|
1195
1514
|
exports.WebhookDeliveryStatus = WebhookDeliveryStatus;
|
|
1196
1515
|
exports.WebhookEndpoint = WebhookEndpoint;
|
|
@@ -1198,6 +1517,17 @@ exports.WebhookEventType = WebhookEventType;
|
|
|
1198
1517
|
exports.WebhookManager = WebhookManager;
|
|
1199
1518
|
exports.WebhookSignatureError = WebhookSignatureError;
|
|
1200
1519
|
exports.Webhooks = Webhooks;
|
|
1520
|
+
exports.addresses = addresses;
|
|
1521
|
+
exports.assertAddress = assertAddress;
|
|
1522
|
+
exports.defineAsset = defineAsset;
|
|
1523
|
+
exports.isAddress = isAddress;
|
|
1524
|
+
exports.isContractAddress = isContractAddress;
|
|
1525
|
+
exports.isMuxedAddress = isMuxedAddress;
|
|
1526
|
+
exports.isNativeAsset = isNativeAsset;
|
|
1527
|
+
exports.isStellarAddress = isStellarAddress;
|
|
1528
|
+
exports.resolveAddress = resolveAddress;
|
|
1529
|
+
exports.resolveAsset = resolveAsset;
|
|
1530
|
+
exports.resolveIntentBody = resolveIntentBody;
|
|
1201
1531
|
exports.resolveShared = resolveShared;
|
|
1202
1532
|
exports.shared = shared;
|
|
1203
1533
|
exports.version = version;
|