@atomiqlabs/sdk 8.8.3 → 8.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/api/index.d.ts +1 -0
  2. package/api/index.js +3 -0
  3. package/dist/ApiList.d.ts +37 -0
  4. package/dist/ApiList.js +30 -0
  5. package/dist/api/ApiEndpoints.d.ts +393 -0
  6. package/dist/api/ApiEndpoints.js +2 -0
  7. package/dist/api/ApiParser.d.ts +10 -0
  8. package/dist/api/ApiParser.js +134 -0
  9. package/dist/api/ApiTypes.d.ts +157 -0
  10. package/dist/api/ApiTypes.js +75 -0
  11. package/dist/api/SerializedAction.d.ts +40 -0
  12. package/dist/api/SerializedAction.js +59 -0
  13. package/dist/api/SwapperApi.d.ts +50 -0
  14. package/dist/api/SwapperApi.js +431 -0
  15. package/dist/api/index.d.ts +5 -0
  16. package/dist/api/index.js +24 -0
  17. package/dist/events/UnifiedSwapEventListener.d.ts +4 -3
  18. package/dist/events/UnifiedSwapEventListener.js +8 -2
  19. package/dist/http/HttpUtils.d.ts +4 -2
  20. package/dist/http/HttpUtils.js +10 -4
  21. package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +2 -1
  22. package/dist/http/paramcoders/client/StreamingFetchPromise.js +3 -2
  23. package/dist/index.d.ts +1 -0
  24. package/dist/index.js +1 -0
  25. package/dist/intermediaries/IntermediaryDiscovery.d.ts +7 -2
  26. package/dist/intermediaries/IntermediaryDiscovery.js +4 -4
  27. package/dist/intermediaries/apis/IntermediaryAPI.d.ts +171 -14
  28. package/dist/intermediaries/apis/IntermediaryAPI.js +174 -28
  29. package/dist/intermediaries/auth/SignedKeyBasedAuth.d.ts +14 -0
  30. package/dist/intermediaries/auth/SignedKeyBasedAuth.js +68 -0
  31. package/dist/storage/IUnifiedStorage.d.ts +45 -3
  32. package/dist/storage/UnifiedSwapStorage.d.ts +8 -2
  33. package/dist/storage/UnifiedSwapStorage.js +46 -8
  34. package/dist/swapper/Swapper.d.ts +36 -3
  35. package/dist/swapper/Swapper.js +54 -18
  36. package/dist/swapper/SwapperUtils.d.ts +18 -2
  37. package/dist/swapper/SwapperUtils.js +39 -1
  38. package/dist/swaps/ISwap.d.ts +70 -9
  39. package/dist/swaps/ISwap.js +28 -6
  40. package/dist/swaps/ISwapWrapper.d.ts +11 -1
  41. package/dist/swaps/ISwapWrapper.js +23 -3
  42. package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +1 -1
  43. package/dist/swaps/escrow_swaps/IEscrowSwap.js +4 -2
  44. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +2 -1
  45. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +2 -2
  46. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +3 -1
  47. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +3 -2
  48. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +47 -31
  49. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +201 -67
  50. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +3 -1
  51. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +6 -6
  52. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +82 -15
  53. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +304 -98
  54. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +3 -1
  55. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +6 -6
  56. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +75 -42
  57. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +424 -87
  58. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +3 -1
  59. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +7 -7
  60. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +54 -11
  61. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +214 -41
  62. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +2 -1
  63. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +7 -8
  64. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +3 -1
  65. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +5 -5
  66. package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +76 -19
  67. package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +290 -51
  68. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +3 -1
  69. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +5 -5
  70. package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +53 -12
  71. package/dist/swaps/trusted/ln/LnForGasSwap.js +163 -49
  72. package/dist/swaps/trusted/ln/LnForGasWrapper.js +1 -2
  73. package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +14 -13
  74. package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +30 -47
  75. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +3 -1
  76. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +4 -4
  77. package/dist/types/SwapExecutionAction.d.ts +141 -34
  78. package/dist/types/SwapExecutionAction.js +104 -0
  79. package/dist/types/SwapExecutionStep.d.ts +144 -0
  80. package/dist/types/SwapExecutionStep.js +87 -0
  81. package/dist/types/TokenAmount.d.ts +6 -0
  82. package/dist/types/TokenAmount.js +26 -1
  83. package/dist/utils/BitcoinUtils.d.ts +2 -0
  84. package/dist/utils/BitcoinUtils.js +34 -1
  85. package/dist/utils/Utils.d.ts +3 -1
  86. package/dist/utils/Utils.js +7 -1
  87. package/package.json +7 -4
  88. package/src/api/ApiEndpoints.ts +427 -0
  89. package/src/api/ApiParser.ts +138 -0
  90. package/src/api/ApiTypes.ts +229 -0
  91. package/src/api/SerializedAction.ts +97 -0
  92. package/src/api/SwapperApi.ts +545 -0
  93. package/src/api/index.ts +5 -0
  94. package/src/events/UnifiedSwapEventListener.ts +11 -3
  95. package/src/http/HttpUtils.ts +10 -4
  96. package/src/http/paramcoders/client/StreamingFetchPromise.ts +4 -2
  97. package/src/index.ts +1 -0
  98. package/src/intermediaries/IntermediaryDiscovery.ts +9 -2
  99. package/src/intermediaries/apis/IntermediaryAPI.ts +314 -30
  100. package/src/intermediaries/auth/SignedKeyBasedAuth.ts +69 -0
  101. package/src/storage/IUnifiedStorage.ts +45 -4
  102. package/src/storage/UnifiedSwapStorage.ts +42 -8
  103. package/src/swapper/Swapper.ts +87 -18
  104. package/src/swapper/SwapperUtils.ts +42 -2
  105. package/src/swaps/ISwap.ts +88 -16
  106. package/src/swaps/ISwapWrapper.ts +28 -3
  107. package/src/swaps/escrow_swaps/IEscrowSwap.ts +5 -3
  108. package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +3 -1
  109. package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +4 -1
  110. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +264 -67
  111. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +6 -4
  112. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +390 -89
  113. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +6 -4
  114. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +548 -94
  115. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +7 -5
  116. package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +276 -45
  117. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +7 -6
  118. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +5 -3
  119. package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +393 -57
  120. package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +5 -3
  121. package/src/swaps/trusted/ln/LnForGasSwap.ts +211 -47
  122. package/src/swaps/trusted/ln/LnForGasWrapper.ts +1 -2
  123. package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +32 -51
  124. package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +5 -3
  125. package/src/types/SwapExecutionAction.ts +266 -43
  126. package/src/types/SwapExecutionStep.ts +224 -0
  127. package/src/types/TokenAmount.ts +36 -2
  128. package/src/utils/BitcoinUtils.ts +32 -0
  129. package/src/utils/Utils.ts +10 -1
  130. package/src/intermediaries/apis/TrustedIntermediaryAPI.ts +0 -258
@@ -0,0 +1,157 @@
1
+ import { Token } from "../types/Token";
2
+ import { TokenAmount } from "../types/TokenAmount";
3
+ import { LNURLPay, LNURLPayParamsWithUrl } from "../types/lnurl/LNURLPay";
4
+ import { LNURLWithdraw, LNURLWithdrawParamsWithUrl } from "../types/lnurl/LNURLWithdraw";
5
+ import { Swapper } from "../swapper/Swapper";
6
+ /**
7
+ * Unified amount type for all API responses
8
+ *
9
+ * @category API
10
+ */
11
+ export type ApiAmount = {
12
+ /** Decimal format of the amount, e.g. "1.5" */
13
+ amount: string;
14
+ /** Raw base units as string, e.g. "1500000000000000000" */
15
+ rawAmount: string;
16
+ /** Token decimals, e.g. 18 */
17
+ decimals: number;
18
+ /** Token ticker, e.g. "STRK" */
19
+ symbol: string;
20
+ /** Chain identifier, e.g. "STARKNET", "BITCOIN", "LIGHTNING" */
21
+ chain: string;
22
+ };
23
+ /**
24
+ * Serializable token representation for API responses
25
+ *
26
+ * @category API
27
+ */
28
+ export type ApiToken = {
29
+ /** Canonical token identifier accepted by the API, e.g. "BITCOIN-BTC", "LIGHTNING-BTC", "STARKNET-STRK" */
30
+ id: string;
31
+ /** Chain identifier, e.g. "STARKNET", "BITCOIN", "LIGHTNING" */
32
+ chainId: string;
33
+ /** Token ticker, e.g. "STRK" */
34
+ ticker: string;
35
+ /** Full token name */
36
+ name: string;
37
+ /** Decimal places of the token */
38
+ decimals: number;
39
+ /** Token contract address, or empty string for BTC on Bitcoin/Lightning */
40
+ address: string;
41
+ };
42
+ /**
43
+ * Serializable LNURL-pay representation for API responses
44
+ *
45
+ * @category API
46
+ */
47
+ export type ApiLNURLPay = {
48
+ /** Marks the LNURL payload as an LNURL-pay response. */
49
+ type: "pay";
50
+ /** Minimum payable amount supported by the LNURL-pay endpoint. */
51
+ min: ApiAmount;
52
+ /** Maximum payable amount supported by the LNURL-pay endpoint. */
53
+ max: ApiAmount;
54
+ /** Maximum comment length accepted by the LNURL-pay endpoint. */
55
+ commentMaxLength: number;
56
+ /** Short human-readable description of the payee, when provided by the LNURL service. */
57
+ shortDescription?: string;
58
+ /** Longer human-readable description of the payee, when provided by the LNURL service. */
59
+ longDescription?: string;
60
+ /** Optional icon for the payee, usually encoded as a data URL. */
61
+ icon?: string;
62
+ /** Raw LNURL-pay metadata and callback parameters. */
63
+ params: LNURLPayParamsWithUrl;
64
+ };
65
+ /**
66
+ * Serializable LNURL-withdraw representation for API responses
67
+ *
68
+ * @category API
69
+ */
70
+ export type ApiLNURLWithdraw = {
71
+ /** Marks the LNURL payload as an LNURL-withdraw response. */
72
+ type: "withdraw";
73
+ /** Minimum withdrawable amount supported by the LNURL-withdraw endpoint. */
74
+ min: ApiAmount;
75
+ /** Maximum withdrawable amount supported by the LNURL-withdraw endpoint. */
76
+ max: ApiAmount;
77
+ /** Raw LNURL-withdraw metadata and callback parameters. */
78
+ params: LNURLWithdrawParamsWithUrl;
79
+ };
80
+ /**
81
+ * Serializable LNURL representation for API responses
82
+ *
83
+ * @category API
84
+ */
85
+ export type ApiLNURL = ApiLNURLPay | ApiLNURLWithdraw;
86
+ /**
87
+ * Converts a TokenAmount to the serializable ApiAmount format
88
+ *
89
+ * @category API
90
+ */
91
+ export declare function toApiAmount(tokenAmount: TokenAmount): ApiAmount;
92
+ /**
93
+ * Converts a Token to the serializable ApiToken format
94
+ *
95
+ * @category API
96
+ */
97
+ export declare function toApiToken(token: Token): ApiToken;
98
+ /**
99
+ * Converts LNURL data to the serializable API format
100
+ *
101
+ * @category API
102
+ */
103
+ export declare function toApiLNURL(lnurl: LNURLPay | LNURLWithdraw, swapper: Swapper<any>): ApiLNURL;
104
+ /**
105
+ * Maps a TypeScript type to its schema type string representation
106
+ *
107
+ * @category API
108
+ */
109
+ type TypeToSchemaType<T> = NonNullable<T> extends string ? "string" : NonNullable<T> extends number ? "number" : NonNullable<T> extends bigint ? "bigint" : NonNullable<T> extends boolean ? "boolean" : NonNullable<T> extends any[] ? "array" : "object";
110
+ /**
111
+ * Schema definition for a single API input field.
112
+ *
113
+ * @category API
114
+ */
115
+ export type InputSchemaField<T = unknown> = {
116
+ /** Primitive schema type inferred from the TypeScript field type. */
117
+ type: TypeToSchemaType<T>;
118
+ /** Whether the field is required by the endpoint input parser. */
119
+ required: boolean;
120
+ /** Human-readable description of the field exposed by the API schema. */
121
+ description: string;
122
+ /** Nested schema properties for object-like fields. */
123
+ properties?: T extends readonly any[] ? never : T extends object ? {
124
+ [K in keyof T]-?: InputSchemaField<T[K]>;
125
+ } : never;
126
+ /** Schema definition for array items when the field is an array. */
127
+ items?: T extends readonly (infer U)[] ? InputSchemaField<U> : never;
128
+ /** Enumerated allowed values for string, number, or bigint fields when constrained. */
129
+ allowedValues?: NonNullable<T> extends string | number | bigint ? NonNullable<T>[] : never;
130
+ };
131
+ /**
132
+ * Schema definition describing the accepted input shape for an API endpoint.
133
+ *
134
+ * @category API
135
+ */
136
+ export type InputSchema<TInput> = {
137
+ [K in keyof TInput]-?: InputSchemaField<TInput[K]>;
138
+ };
139
+ /**
140
+ * Typed API endpoint definition for framework-agnostic integration
141
+ *
142
+ * @category API
143
+ */
144
+ export type ApiEndpoint<TInput, TOutput, Type extends "GET" | "POST"> = {
145
+ /** HTTP method used by the endpoint. */
146
+ type: Type;
147
+ /** Human-readable description of what this endpoint does, written for AI agent consumption. */
148
+ description: string;
149
+ /** Structured schema describing the accepted input payload. */
150
+ inputSchema: InputSchema<TInput>;
151
+ /** Typed endpoint implementation that receives already-validated input. */
152
+ callback: (input: TInput, abortSignal?: AbortSignal) => Promise<TOutput>;
153
+ /** Raw endpoint implementation that parses unknown input into the typed callback. */
154
+ callbackRaw: (input: unknown, abortSignal?: AbortSignal) => Promise<TOutput>;
155
+ };
156
+ export declare function createApiEndpoint<TInput, TOutput, Type extends "GET" | "POST">(type: Type, description: string, callback: (input: TInput, abortSignal?: AbortSignal) => Promise<TOutput>, inputSchema: InputSchema<TInput>): ApiEndpoint<TInput, TOutput, Type>;
157
+ export {};
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createApiEndpoint = exports.toApiLNURL = exports.toApiToken = exports.toApiAmount = void 0;
4
+ const Token_1 = require("../types/Token");
5
+ const TokenAmount_1 = require("../types/TokenAmount");
6
+ const ApiParser_1 = require("./ApiParser");
7
+ /**
8
+ * Converts a TokenAmount to the serializable ApiAmount format
9
+ *
10
+ * @category API
11
+ */
12
+ function toApiAmount(tokenAmount) {
13
+ return {
14
+ amount: tokenAmount.amount,
15
+ rawAmount: tokenAmount.rawAmount != null ? tokenAmount.rawAmount.toString() : "0",
16
+ decimals: tokenAmount.token.decimals,
17
+ symbol: tokenAmount.token.ticker,
18
+ chain: tokenAmount.token.chainId
19
+ };
20
+ }
21
+ exports.toApiAmount = toApiAmount;
22
+ /**
23
+ * Converts a Token to the serializable ApiToken format
24
+ *
25
+ * @category API
26
+ */
27
+ function toApiToken(token) {
28
+ return {
29
+ id: `${token.chainId}-${token.ticker}`,
30
+ chainId: token.chainId,
31
+ ticker: token.ticker,
32
+ name: token.name,
33
+ decimals: token.decimals,
34
+ address: token.address
35
+ };
36
+ }
37
+ exports.toApiToken = toApiToken;
38
+ /**
39
+ * Converts LNURL data to the serializable API format
40
+ *
41
+ * @category API
42
+ */
43
+ function toApiLNURL(lnurl, swapper) {
44
+ if (lnurl.type === "pay") {
45
+ return {
46
+ type: "pay",
47
+ min: toApiAmount((0, TokenAmount_1.toTokenAmount)(lnurl.min, Token_1.BitcoinTokens.BTCLN, swapper.prices)),
48
+ max: toApiAmount((0, TokenAmount_1.toTokenAmount)(lnurl.max, Token_1.BitcoinTokens.BTCLN, swapper.prices)),
49
+ commentMaxLength: lnurl.commentMaxLength,
50
+ ...(lnurl.shortDescription != null ? { shortDescription: lnurl.shortDescription } : {}),
51
+ ...(lnurl.longDescription != null ? { longDescription: lnurl.longDescription } : {}),
52
+ ...(lnurl.icon != null ? { icon: lnurl.icon } : {}),
53
+ params: lnurl.params
54
+ };
55
+ }
56
+ return {
57
+ type: "withdraw",
58
+ min: toApiAmount((0, TokenAmount_1.toTokenAmount)(lnurl.min, Token_1.BitcoinTokens.BTCLN, swapper.prices)),
59
+ max: toApiAmount((0, TokenAmount_1.toTokenAmount)(lnurl.max, Token_1.BitcoinTokens.BTCLN, swapper.prices)),
60
+ params: lnurl.params
61
+ };
62
+ }
63
+ exports.toApiLNURL = toApiLNURL;
64
+ function createApiEndpoint(type, description, callback, inputSchema) {
65
+ return {
66
+ type,
67
+ description,
68
+ callback,
69
+ inputSchema,
70
+ callbackRaw: (input, abortSignal) => {
71
+ return callback((0, ApiParser_1.parseApiInput)(inputSchema, input), abortSignal);
72
+ }
73
+ };
74
+ }
75
+ exports.createApiEndpoint = createApiEndpoint;
@@ -0,0 +1,40 @@
1
+ import { SwapExecutionAction } from "../types/SwapExecutionAction";
2
+ import { ApiAmount } from "./ApiTypes";
3
+ /**
4
+ * Strips non-serializable fields (functions, complex objects) from SwapExecutionAction types.
5
+ * Uses key-remapping to truly omit function keys from the resulting type.
6
+ *
7
+ * - SignSmartChainTransaction: functions removed, txs → string[]
8
+ * - SignPSBT: functions removed, txs[].psbt (Transaction object) omitted
9
+ * - SendToAddress: functions removed, txs[].amount (TokenAmount) → ApiAmount
10
+ * - Wait: functions removed, data fields kept as-is
11
+ *
12
+ * @category API
13
+ */
14
+ export type SerializedAction<T extends SwapExecutionAction> = T["type"] extends "SignSmartChainTransaction" ? {
15
+ [K in keyof T as T[K] extends Function ? never : K]: K extends "txs" ? string[] : T[K];
16
+ } : T["type"] extends "SignPSBT" ? {
17
+ [K in keyof T as T[K] extends Function ? never : K]: K extends "txs" ? (T extends {
18
+ txs: (infer U)[];
19
+ } ? Omit<U, "psbt">[] : never) : T[K];
20
+ } : T["type"] extends "SendToAddress" ? {
21
+ [K in keyof T as T[K] extends Function ? never : K]: K extends "txs" ? (T extends {
22
+ txs: (infer U)[];
23
+ } ? (Omit<U, "amount"> & {
24
+ amount: ApiAmount;
25
+ })[] : never) : T[K];
26
+ } : {
27
+ [K in keyof T as T[K] extends Function ? never : K]: T[K];
28
+ };
29
+ /**
30
+ * Runtime serializer that strips non-serializable fields from a SwapExecutionAction.
31
+ * Matches the compile-time SerializedAction<T> type.
32
+ *
33
+ * @param action The swap execution action to serialize
34
+ * @param txSerializer Optional chain-specific transaction serializer for SignSmartChainTransaction actions.
35
+ * Accepts the chain identifier and raw transaction, returns the serialized string.
36
+ * Falls back to JSON.stringify if not provided.
37
+ *
38
+ * @category API
39
+ */
40
+ export declare function serializeAction(action: SwapExecutionAction, txSerializer?: (chainId: string, tx: any) => Promise<string>): Promise<SerializedAction<SwapExecutionAction>>;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serializeAction = void 0;
4
+ const ApiTypes_1 = require("./ApiTypes");
5
+ /**
6
+ * Runtime serializer that strips non-serializable fields from a SwapExecutionAction.
7
+ * Matches the compile-time SerializedAction<T> type.
8
+ *
9
+ * @param action The swap execution action to serialize
10
+ * @param txSerializer Optional chain-specific transaction serializer for SignSmartChainTransaction actions.
11
+ * Accepts the chain identifier and raw transaction, returns the serialized string.
12
+ * Falls back to JSON.stringify if not provided.
13
+ *
14
+ * @category API
15
+ */
16
+ async function serializeAction(action, txSerializer) {
17
+ switch (action.type) {
18
+ case "SendToAddress": {
19
+ const { waitForTransactions, ...rest } = action;
20
+ return {
21
+ ...rest,
22
+ txs: rest.txs.map(tx => ({
23
+ type: tx.type,
24
+ address: tx.address,
25
+ hyperlink: tx.hyperlink,
26
+ amount: (0, ApiTypes_1.toApiAmount)(tx.amount)
27
+ }))
28
+ };
29
+ }
30
+ case "SignPSBT": {
31
+ const { submitPsbt, txs, ...rest } = action;
32
+ return {
33
+ ...rest,
34
+ txs: txs.map(tx => {
35
+ const { psbt, ...txRest } = tx;
36
+ return txRest;
37
+ })
38
+ };
39
+ }
40
+ case "SignSmartChainTransaction": {
41
+ const { submitTransactions, ...rest } = action;
42
+ return {
43
+ ...rest,
44
+ txs: await Promise.all(rest.txs.map(tx => txSerializer != null
45
+ ? txSerializer(rest.chain, tx)
46
+ : Promise.resolve(JSON.stringify(tx))))
47
+ };
48
+ }
49
+ case "Wait": {
50
+ const { wait, ...rest } = action;
51
+ return rest;
52
+ }
53
+ default: {
54
+ const _exhaustive = action;
55
+ throw new Error(`Unknown action type: ${_exhaustive.type}`);
56
+ }
57
+ }
58
+ }
59
+ exports.serializeAction = serializeAction;
@@ -0,0 +1,50 @@
1
+ import { MultiChain, Swapper } from "../swapper/Swapper";
2
+ import { ApiEndpoint } from "./ApiTypes";
3
+ import { CreateSwapInput, CreateSwapOutput, GetSpendableBalanceInput, GetSpendableBalanceOutput, GetSupportedTokensInput, GetSupportedTokensOutput, GetSwapCounterTokensInput, GetSwapCounterTokensOutput, GetSwapLimitsInput, GetSwapLimitsOutput, GetSwapStatusInput, GetSwapStatusOutput, ListPendingSwapsInput, ListPendingSwapsOutput, ListSwapsInput, ListSwapsOutput, ParseAddressInput, ParseAddressOutput, SettleWithLnurlInput, SettleWithLnurlOutput, SubmitTransactionInput, SubmitTransactionOutput } from "./ApiEndpoints";
4
+ export type SwapperApiConfig = {
5
+ syncOnGetStatus?: boolean;
6
+ idempotentTxSubmission?: boolean;
7
+ };
8
+ export declare class SwapperApi<T extends MultiChain> {
9
+ private swapper;
10
+ private readonly config?;
11
+ readonly endpoints: {
12
+ createSwap: ApiEndpoint<CreateSwapInput, CreateSwapOutput, "POST">;
13
+ listSwaps: ApiEndpoint<ListSwapsInput, ListSwapsOutput, "GET">;
14
+ listPendingSwaps: ApiEndpoint<ListPendingSwapsInput, ListPendingSwapsOutput, "GET">;
15
+ getSupportedTokens: ApiEndpoint<GetSupportedTokensInput, GetSupportedTokensOutput, "GET">;
16
+ getSwapCounterTokens: ApiEndpoint<GetSwapCounterTokensInput, GetSwapCounterTokensOutput, "GET">;
17
+ getSwapLimits: ApiEndpoint<GetSwapLimitsInput, GetSwapLimitsOutput, "GET">;
18
+ parseAddress: ApiEndpoint<ParseAddressInput, ParseAddressOutput, "GET">;
19
+ getSpendableBalance: ApiEndpoint<GetSpendableBalanceInput, GetSpendableBalanceOutput, "GET">;
20
+ getSwapStatus: ApiEndpoint<GetSwapStatusInput, GetSwapStatusOutput, "GET">;
21
+ submitTransaction: ApiEndpoint<SubmitTransactionInput, SubmitTransactionOutput, "POST">;
22
+ settleWithLnurl: ApiEndpoint<SettleWithLnurlInput, SettleWithLnurlOutput, "POST">;
23
+ };
24
+ constructor(swapper: Swapper<T>, config?: SwapperApiConfig | undefined);
25
+ private txSerializer;
26
+ init(): Promise<void>;
27
+ /**
28
+ * Should be ran periodically, this synchronizes the swap's state with the on-chain data and also purges
29
+ * expired swaps from the persistent storage
30
+ */
31
+ sync(): Promise<void>;
32
+ /**
33
+ * Optionally good to run this periodically, such that any LPs that are dropped off because they are unresponsive
34
+ * can be found again.
35
+ */
36
+ reloadLps(): Promise<void>;
37
+ private createSwap;
38
+ private validateSwapListInput;
39
+ private createListedSwapOutputs;
40
+ private listSwaps;
41
+ private listPendingSwaps;
42
+ private getSupportedTokens;
43
+ private getSwapCounterTokens;
44
+ private getSwapLimits;
45
+ private parseAddress;
46
+ private getSpendableBalance;
47
+ private getSwapStatus;
48
+ private submitTransaction;
49
+ private settleWithLnurl;
50
+ }