@stellar-broker/client 0.7.0 → 0.8.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/lib/stellarbroker.js +1 -1
- package/lib/stellarbroker.js.map +1 -1
- package/package.json +3 -3
- package/src/authorization.js +3 -3
- package/src/client.js +360 -357
- package/src/index.d.ts +294 -277
- package/src/mediator.js +397 -386
- package/src/tx-processor.js +16 -42
package/src/index.d.ts
CHANGED
|
@@ -1,277 +1,294 @@
|
|
|
1
|
-
import {Asset, TransactionI} from "@stellar/stellar-sdk";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Client for StellarBroker service
|
|
5
|
-
*/
|
|
6
|
-
export class StellarBrokerClient {
|
|
7
|
-
/**
|
|
8
|
-
* @param {ClientInitializationParams} params
|
|
9
|
-
*/
|
|
10
|
-
constructor(params: ClientInitializationParams);
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Trader account public key
|
|
14
|
-
* @type {string}
|
|
15
|
-
*/
|
|
16
|
-
readonly trader: string;
|
|
17
|
-
/**
|
|
18
|
-
* Stellar network passphrase
|
|
19
|
-
* @type {string}
|
|
20
|
-
*/
|
|
21
|
-
readonly network: string;
|
|
22
|
-
/**
|
|
23
|
-
* @type {ClientSessionStatus}
|
|
24
|
-
*/
|
|
25
|
-
readonly status: ClientSessionStatus;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Connect to the StellarBroker server
|
|
29
|
-
* @return {Promise<StellarBrokerClient>}
|
|
30
|
-
*/
|
|
31
|
-
connect(): Promise<StellarBrokerClient>;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Request swap quote
|
|
35
|
-
* @param {QuoteParams} params - Quote parameters
|
|
36
|
-
*/
|
|
37
|
-
quote(params: QuoteParams): void;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Stop quotation/trading
|
|
41
|
-
*/
|
|
42
|
-
stop(): void;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Confirm current quote and start trading
|
|
46
|
-
* @param {string} [account] - Trader account address (overrides value provided in the constructor)
|
|
47
|
-
* @param {ClientAuthorizationParams} [authorization] - Authorization params (overrides value provided in the constructor)
|
|
48
|
-
*/
|
|
49
|
-
confirmQuote(account?: string, authorization?: ClientAuthorizationParams): void;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Add event listener
|
|
53
|
-
* @param {StellarBrokerClientEvent} type
|
|
54
|
-
* @param {function} callback
|
|
55
|
-
*/
|
|
56
|
-
on(type: StellarBrokerClientEvent, callback: Function): void;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Add event listener that will be executed once
|
|
60
|
-
* @param {StellarBrokerClientEvent} type
|
|
61
|
-
* @param {function} callback
|
|
62
|
-
*/
|
|
63
|
-
once(type: StellarBrokerClientEvent, callback: Function): void;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Remove event listener
|
|
67
|
-
* @param {StellarBrokerClientEvent} type
|
|
68
|
-
* @param {function} callback
|
|
69
|
-
*/
|
|
70
|
-
off(type: StellarBrokerClientEvent, callback: Function): void;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Close underlying connection and finalize the client
|
|
74
|
-
*/
|
|
75
|
-
close(): void;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Request single swap quote estimate without trading
|
|
80
|
-
*/
|
|
81
|
-
export function estimateSwap(params: QuoteParams): Promise<QuoteResult>;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Quote request parameters provided by the client
|
|
85
|
-
*/
|
|
86
|
-
export interface QuoteParams {
|
|
87
|
-
/**
|
|
88
|
-
* Asset to sell
|
|
89
|
-
*/
|
|
90
|
-
sellingAsset: string
|
|
91
|
-
/**
|
|
92
|
-
* Asset to buy
|
|
93
|
-
*/
|
|
94
|
-
buyingAsset: string
|
|
95
|
-
/**
|
|
96
|
-
* Amount of selling asset
|
|
97
|
-
*/
|
|
98
|
-
sellingAmount?: string
|
|
99
|
-
/**
|
|
100
|
-
* Swap slippage tolerance (0.02 by default - 2%)
|
|
101
|
-
*/
|
|
102
|
-
slippageTolerance?: number
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export interface ClientInitializationParams {
|
|
106
|
-
/**
|
|
107
|
-
* Partner key
|
|
108
|
-
*/
|
|
109
|
-
partnerKey?: string;
|
|
110
|
-
/**
|
|
111
|
-
* Trader account address
|
|
112
|
-
*/
|
|
113
|
-
account?: string;
|
|
114
|
-
/**
|
|
115
|
-
* Authorization method, either account secret key or an authorization callback
|
|
116
|
-
*/
|
|
117
|
-
authorization?: ClientAuthorizationParams;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
*
|
|
149
|
-
*/
|
|
150
|
-
readonly
|
|
151
|
-
/**
|
|
152
|
-
*
|
|
153
|
-
*/
|
|
154
|
-
readonly
|
|
155
|
-
/**
|
|
156
|
-
*
|
|
157
|
-
*/
|
|
158
|
-
readonly
|
|
159
|
-
/**
|
|
160
|
-
*
|
|
161
|
-
*/
|
|
162
|
-
readonly
|
|
163
|
-
/**
|
|
164
|
-
*
|
|
165
|
-
*/
|
|
166
|
-
readonly
|
|
167
|
-
/**
|
|
168
|
-
*
|
|
169
|
-
*/
|
|
170
|
-
readonly
|
|
171
|
-
/**
|
|
172
|
-
*
|
|
173
|
-
*/
|
|
174
|
-
readonly
|
|
175
|
-
/**
|
|
176
|
-
*
|
|
177
|
-
*/
|
|
178
|
-
readonly
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
*
|
|
195
|
-
*/
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
*
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
*
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
1
|
+
import {Asset, TransactionI} from "@stellar/stellar-sdk";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Client for StellarBroker service
|
|
5
|
+
*/
|
|
6
|
+
export class StellarBrokerClient {
|
|
7
|
+
/**
|
|
8
|
+
* @param {ClientInitializationParams} params
|
|
9
|
+
*/
|
|
10
|
+
constructor(params: ClientInitializationParams);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Trader account public key
|
|
14
|
+
* @type {string}
|
|
15
|
+
*/
|
|
16
|
+
readonly trader: string;
|
|
17
|
+
/**
|
|
18
|
+
* Stellar network passphrase
|
|
19
|
+
* @type {string}
|
|
20
|
+
*/
|
|
21
|
+
readonly network: string;
|
|
22
|
+
/**
|
|
23
|
+
* @type {ClientSessionStatus}
|
|
24
|
+
*/
|
|
25
|
+
readonly status: ClientSessionStatus;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Connect to the StellarBroker server
|
|
29
|
+
* @return {Promise<StellarBrokerClient>}
|
|
30
|
+
*/
|
|
31
|
+
connect(): Promise<StellarBrokerClient>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Request swap quote
|
|
35
|
+
* @param {QuoteParams} params - Quote parameters
|
|
36
|
+
*/
|
|
37
|
+
quote(params: QuoteParams): void;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Stop quotation/trading
|
|
41
|
+
*/
|
|
42
|
+
stop(): void;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Confirm current quote and start trading
|
|
46
|
+
* @param {string} [account] - Trader account address (overrides value provided in the constructor)
|
|
47
|
+
* @param {ClientAuthorizationParams} [authorization] - Authorization params (overrides value provided in the constructor)
|
|
48
|
+
*/
|
|
49
|
+
confirmQuote(account?: string, authorization?: ClientAuthorizationParams): void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Add event listener
|
|
53
|
+
* @param {StellarBrokerClientEvent} type
|
|
54
|
+
* @param {function} callback
|
|
55
|
+
*/
|
|
56
|
+
on(type: StellarBrokerClientEvent, callback: Function): void;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Add event listener that will be executed once
|
|
60
|
+
* @param {StellarBrokerClientEvent} type
|
|
61
|
+
* @param {function} callback
|
|
62
|
+
*/
|
|
63
|
+
once(type: StellarBrokerClientEvent, callback: Function): void;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Remove event listener
|
|
67
|
+
* @param {StellarBrokerClientEvent} type
|
|
68
|
+
* @param {function} callback
|
|
69
|
+
*/
|
|
70
|
+
off(type: StellarBrokerClientEvent, callback: Function): void;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Close underlying connection and finalize the client
|
|
74
|
+
*/
|
|
75
|
+
close(): void;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Request single swap quote estimate without trading
|
|
80
|
+
*/
|
|
81
|
+
export function estimateSwap(params: QuoteParams): Promise<QuoteResult>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Quote request parameters provided by the client
|
|
85
|
+
*/
|
|
86
|
+
export interface QuoteParams {
|
|
87
|
+
/**
|
|
88
|
+
* Asset to sell
|
|
89
|
+
*/
|
|
90
|
+
sellingAsset: string
|
|
91
|
+
/**
|
|
92
|
+
* Asset to buy
|
|
93
|
+
*/
|
|
94
|
+
buyingAsset: string
|
|
95
|
+
/**
|
|
96
|
+
* Amount of selling asset
|
|
97
|
+
*/
|
|
98
|
+
sellingAmount?: string
|
|
99
|
+
/**
|
|
100
|
+
* Swap slippage tolerance (0.02 by default - 2%)
|
|
101
|
+
*/
|
|
102
|
+
slippageTolerance?: number
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface ClientInitializationParams {
|
|
106
|
+
/**
|
|
107
|
+
* Partner key
|
|
108
|
+
*/
|
|
109
|
+
partnerKey?: string;
|
|
110
|
+
/**
|
|
111
|
+
* Trader account address
|
|
112
|
+
*/
|
|
113
|
+
account?: string;
|
|
114
|
+
/**
|
|
115
|
+
* Authorization method, either account secret key or an authorization callback
|
|
116
|
+
*/
|
|
117
|
+
authorization?: ClientAuthorizationParams;
|
|
118
|
+
/**
|
|
119
|
+
* Stellar network passphrase (pubnet by default)
|
|
120
|
+
*/
|
|
121
|
+
network?: string;
|
|
122
|
+
/**
|
|
123
|
+
* StellarBroker server origin (https://api.stellar.broker by default)
|
|
124
|
+
*/
|
|
125
|
+
origin?: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Current client session status
|
|
130
|
+
*/
|
|
131
|
+
export type ClientSessionStatus = "disconnected" | "ready" | "quote" | "trade";
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Event type generated by the client
|
|
135
|
+
*/
|
|
136
|
+
export type StellarBrokerClientEvent = "quote" | "progress" | "paused" | "finished" | "error";
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Result status code returned by the server
|
|
140
|
+
*/
|
|
141
|
+
export type QuoteResultStatus = "unfeasible" | "rejected" | "success";
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Quote received from the server
|
|
145
|
+
*/
|
|
146
|
+
export interface QuoteResult {
|
|
147
|
+
/**
|
|
148
|
+
* Quote timestamp
|
|
149
|
+
*/
|
|
150
|
+
readonly ts: Date,
|
|
151
|
+
/**
|
|
152
|
+
* Quote status code
|
|
153
|
+
*/
|
|
154
|
+
readonly status: QuoteResultStatus;
|
|
155
|
+
/**
|
|
156
|
+
* Asset to sell
|
|
157
|
+
*/
|
|
158
|
+
readonly sellingAsset: string;
|
|
159
|
+
/**
|
|
160
|
+
* Asset to buy
|
|
161
|
+
*/
|
|
162
|
+
readonly buyingAsset: string;
|
|
163
|
+
/**
|
|
164
|
+
* Swap slippage tolerance
|
|
165
|
+
*/
|
|
166
|
+
readonly slippageTolerance: number;
|
|
167
|
+
/**
|
|
168
|
+
* Amount of the selling asset
|
|
169
|
+
*/
|
|
170
|
+
readonly sellingAmount: string;
|
|
171
|
+
/**
|
|
172
|
+
* Estimated amount of buyingAsset to receive
|
|
173
|
+
*/
|
|
174
|
+
readonly estimatedBuyingAmount?: string;
|
|
175
|
+
/**
|
|
176
|
+
* Equivalent direct path_payment trade estimate
|
|
177
|
+
*/
|
|
178
|
+
readonly directTrade?: QuoteDirectTradeResult;
|
|
179
|
+
/**
|
|
180
|
+
* Difference between quoted price and estimated direct trade
|
|
181
|
+
*/
|
|
182
|
+
readonly profit: string;
|
|
183
|
+
/**
|
|
184
|
+
* Error details from the server (for failed quotes)
|
|
185
|
+
*/
|
|
186
|
+
readonly error?: string;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Emulated single `path_payment` trades result corresponding to the quote
|
|
191
|
+
*/
|
|
192
|
+
export interface QuoteDirectTradeResult {
|
|
193
|
+
/**
|
|
194
|
+
* Estimated amount to sell
|
|
195
|
+
*/
|
|
196
|
+
selling: string;
|
|
197
|
+
/**
|
|
198
|
+
* Estimated amount to buy
|
|
199
|
+
*/
|
|
200
|
+
buying: string;
|
|
201
|
+
/**
|
|
202
|
+
* Conversion path
|
|
203
|
+
*/
|
|
204
|
+
path: string[];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export class StellarBrokerError extends Error {
|
|
208
|
+
/**
|
|
209
|
+
* Numeric error code
|
|
210
|
+
* @type {number}
|
|
211
|
+
* @readonly
|
|
212
|
+
*/
|
|
213
|
+
readonly code: number;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Async authorization callback for transactions and simulated auth entries
|
|
218
|
+
*/
|
|
219
|
+
export type ClientAuthorizationCallback = (txOrAuthEntry: TransactionI | Uint8Array) => Promise<TransactionI | Uint8Array>;
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Authorization method, either account secret key or an authorization callback
|
|
223
|
+
*/
|
|
224
|
+
export type ClientAuthorizationParams = string | ClientAuthorizationCallback;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Trader mediator account wrapper
|
|
228
|
+
*/
|
|
229
|
+
export class Mediator {
|
|
230
|
+
/**
|
|
231
|
+
* Create a trader mediator account instance for a given source account
|
|
232
|
+
* @param source - Source account
|
|
233
|
+
* @param sellingAsset - Asset to sell
|
|
234
|
+
* @param buyingAsset - Asset to buy
|
|
235
|
+
* @param sellingAmount - Amount to sell
|
|
236
|
+
* @param authorization - Authorization callback or secret key
|
|
237
|
+
* @param [reserveFeeAmount] - Amount reserved to cover tx fees (all unused funds will be refunded)
|
|
238
|
+
*/
|
|
239
|
+
constructor(source: string, sellingAsset: string | Asset, buyingAsset: string | Asset, sellingAmount: string, authorization: ClientAuthorizationParams, reserveFeeAmount?: number)
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Stellar network passphrase used by mediator accounts (pubnet by default)
|
|
243
|
+
*/
|
|
244
|
+
static network: string;
|
|
245
|
+
/**
|
|
246
|
+
* Horizon server used by mediator accounts (https://horizon.stellar.org by default)
|
|
247
|
+
*/
|
|
248
|
+
static horizonUrl: string;
|
|
249
|
+
|
|
250
|
+
readonly source: string;
|
|
251
|
+
|
|
252
|
+
readonly sellingAsset: Asset;
|
|
253
|
+
|
|
254
|
+
readonly buyingAsset: Asset;
|
|
255
|
+
|
|
256
|
+
readonly sellingAmount: bigint;
|
|
257
|
+
|
|
258
|
+
readonly mediatorAddress: string;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Check if there are any non-disposed mediators that belong to lost swap sessions
|
|
262
|
+
*/
|
|
263
|
+
get hasObsoleteMediators(): boolean;
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Check if there are any non-disposed mediators that belong to lost swap sessions
|
|
267
|
+
* @param source - Initiator account that created a mediator
|
|
268
|
+
* @param [storagePrefix] - Local storage key prefix
|
|
269
|
+
*/
|
|
270
|
+
static hasObsoleteMediators(source: string, storagePrefix?: string): boolean
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Retrieve funds from mediator accounts that belong to lost swap sessions
|
|
274
|
+
*/
|
|
275
|
+
disposeObsoleteMediators(): Promise<void>;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Retrieve funds from mediator accounts that belong to lost swap sessions
|
|
279
|
+
* @param source - Initiator account that created a mediator
|
|
280
|
+
* @param authorization - Authorization callback or secret key
|
|
281
|
+
* @param [storagePrefix] - Local storage key prefix
|
|
282
|
+
*/
|
|
283
|
+
static disposeObsoleteMediators(source: string, authorization: ClientAuthorizationParams, storagePrefix?: string): Promise<void>;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Create mediator account and deposit tokens to sell
|
|
287
|
+
*/
|
|
288
|
+
init(): Promise<string>;
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Dispose mediator account
|
|
292
|
+
*/
|
|
293
|
+
dispose(address?: string): Promise<void>;
|
|
294
|
+
}
|