@stellar-broker/client 0.4.1 → 0.6.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/README.md +160 -109
- package/jest.config.cjs +4 -0
- package/lib/stellarbroker.js +1 -1
- package/lib/stellarbroker.js.map +1 -1
- package/package.json +10 -9
- package/src/asset.js +51 -47
- package/src/authorization.js +65 -0
- package/src/client.js +344 -369
- package/src/errors.js +55 -53
- package/src/estimate.js +4 -3
- package/src/index.d.ts +146 -40
- package/src/index.js +5 -4
- package/src/mediator.js +309 -0
- package/src/quote-request.js +56 -60
- package/src/quote-result.js +93 -0
- package/src/tx-processor.js +133 -161
package/src/errors.js
CHANGED
|
@@ -1,54 +1,56 @@
|
|
|
1
|
-
export class StellarBrokerError extends Error {
|
|
2
|
-
constructor(code, message) {
|
|
3
|
-
super(message)
|
|
4
|
-
this.code = code
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Numeric error code
|
|
9
|
-
* @type {number}
|
|
10
|
-
* @readonly
|
|
11
|
-
*/
|
|
12
|
-
code = 0
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const errors = {
|
|
16
|
-
invalidInitParam(param) {
|
|
17
|
-
return new StellarBrokerError(1, `Invalid parameter value: "${param}"`)
|
|
18
|
-
},
|
|
19
|
-
invalidAuthorizationParam() {
|
|
20
|
-
return new StellarBrokerError(2, 'Invalid authorization secret key or callback provided')
|
|
21
|
-
},
|
|
22
|
-
notConnected() {
|
|
23
|
-
return new StellarBrokerError(3, 'Client not connected to the server')
|
|
24
|
-
},
|
|
25
|
-
quoteNotSet() {
|
|
26
|
-
return new StellarBrokerError(11, 'Price quote not available')
|
|
27
|
-
},
|
|
28
|
-
quoteExpired() {
|
|
29
|
-
return new StellarBrokerError(12, 'Price quote expired')
|
|
30
|
-
},
|
|
31
|
-
quoteError(message) {
|
|
32
|
-
return new StellarBrokerError(13, 'Price quotation error: ' + message)
|
|
33
|
-
},
|
|
34
|
-
invalidQuoteParam(invalidParamName = 'asset', details) {
|
|
35
|
-
return new StellarBrokerError(14, `Invalid quote request parameter: "${invalidParamName}". ${details}`)
|
|
36
|
-
},
|
|
37
|
-
tradeInProgress() {
|
|
38
|
-
return new StellarBrokerError(20, 'Cannot change quote while trade is in progress')
|
|
39
|
-
},
|
|
40
|
-
invalidSwapTx() {
|
|
41
|
-
return new StellarBrokerError(21, 'Invalid swap transaction received from the server')
|
|
42
|
-
},
|
|
43
|
-
failedToSignTx() {
|
|
44
|
-
return new StellarBrokerError(22, 'Failed to sign received transaction')
|
|
45
|
-
},
|
|
46
|
-
unsupportedEventType(type) {
|
|
47
|
-
return new StellarBrokerError(31, 'Unknown event type: ' + type)
|
|
48
|
-
},
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
1
|
+
export class StellarBrokerError extends Error {
|
|
2
|
+
constructor(code, message) {
|
|
3
|
+
super(message)
|
|
4
|
+
this.code = code
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Numeric error code
|
|
9
|
+
* @type {number}
|
|
10
|
+
* @readonly
|
|
11
|
+
*/
|
|
12
|
+
code = 0
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const errors = {
|
|
16
|
+
invalidInitParam(param) {
|
|
17
|
+
return new StellarBrokerError(1, `Invalid parameter value: "${param}"`)
|
|
18
|
+
},
|
|
19
|
+
invalidAuthorizationParam() {
|
|
20
|
+
return new StellarBrokerError(2, 'Invalid authorization secret key or callback provided')
|
|
21
|
+
},
|
|
22
|
+
notConnected() {
|
|
23
|
+
return new StellarBrokerError(3, 'Client not connected to the server')
|
|
24
|
+
},
|
|
25
|
+
quoteNotSet() {
|
|
26
|
+
return new StellarBrokerError(11, 'Price quote not available')
|
|
27
|
+
},
|
|
28
|
+
quoteExpired() {
|
|
29
|
+
return new StellarBrokerError(12, 'Price quote expired')
|
|
30
|
+
},
|
|
31
|
+
quoteError(message) {
|
|
32
|
+
return new StellarBrokerError(13, 'Price quotation error: ' + message)
|
|
33
|
+
},
|
|
34
|
+
invalidQuoteParam(invalidParamName = 'asset', details) {
|
|
35
|
+
return new StellarBrokerError(14, `Invalid quote request parameter: "${invalidParamName}". ${details}`)
|
|
36
|
+
},
|
|
37
|
+
tradeInProgress() {
|
|
38
|
+
return new StellarBrokerError(20, 'Cannot change quote while trade is in progress')
|
|
39
|
+
},
|
|
40
|
+
invalidSwapTx() {
|
|
41
|
+
return new StellarBrokerError(21, 'Invalid swap transaction received from the server')
|
|
42
|
+
},
|
|
43
|
+
failedToSignTx() {
|
|
44
|
+
return new StellarBrokerError(22, 'Failed to sign received transaction')
|
|
45
|
+
},
|
|
46
|
+
unsupportedEventType(type) {
|
|
47
|
+
return new StellarBrokerError(31, 'Unknown event type: ' + type)
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
serverError(message) {
|
|
52
|
+
return new StellarBrokerError(101, message)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
54
56
|
export default errors
|
package/src/estimate.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import errors, {StellarBrokerError} from './errors.js'
|
|
2
2
|
import {validateQuoteRequest} from './quote-request.js'
|
|
3
|
+
import {QuoteResult} from './quote-result.js'
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Request single swap quote estimate without trading
|
|
6
|
-
* @param {
|
|
7
|
-
* @return {Promise<
|
|
7
|
+
* @param {QuoteParams} params
|
|
8
|
+
* @return {Promise<QuoteResult>}
|
|
8
9
|
*/
|
|
9
10
|
export async function estimateSwap(params) {
|
|
10
11
|
const query = Object.entries(validateQuoteRequest(params))
|
|
@@ -15,7 +16,7 @@ export async function estimateSwap(params) {
|
|
|
15
16
|
const res = await fetch(url).then(r => r.json())
|
|
16
17
|
if (res.status !== 'success')
|
|
17
18
|
throw errors.quoteError(res.error || 'Quote not available')
|
|
18
|
-
return res
|
|
19
|
+
return new QuoteResult(res)
|
|
19
20
|
} catch (e) {
|
|
20
21
|
if (e instanceof StellarBrokerError)
|
|
21
22
|
throw e
|
package/src/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {Asset, TransactionI} from "@stellar/stellar-sdk";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Client for StellarBroker service
|
|
5
|
+
*/
|
|
6
|
+
export class StellarBrokerClient {
|
|
4
7
|
/**
|
|
5
8
|
* @param {ClientInitializationParams} params
|
|
6
9
|
*/
|
|
@@ -10,16 +13,16 @@ export default class StellarBrokerClient {
|
|
|
10
13
|
* Trader account public key
|
|
11
14
|
* @type {string}
|
|
12
15
|
*/
|
|
13
|
-
readonly
|
|
16
|
+
readonly trader: string;
|
|
14
17
|
/**
|
|
15
18
|
* Stellar network passphrase
|
|
16
19
|
* @type {string}
|
|
17
20
|
*/
|
|
18
21
|
readonly network: string;
|
|
19
22
|
/**
|
|
20
|
-
* @type {
|
|
23
|
+
* @type {ClientSessionStatus}
|
|
21
24
|
*/
|
|
22
|
-
readonly status:
|
|
25
|
+
readonly status: ClientSessionStatus;
|
|
23
26
|
|
|
24
27
|
/**
|
|
25
28
|
* Connect to the StellarBroker server
|
|
@@ -29,9 +32,9 @@ export default class StellarBrokerClient {
|
|
|
29
32
|
|
|
30
33
|
/**
|
|
31
34
|
* Request swap quote
|
|
32
|
-
* @param {
|
|
35
|
+
* @param {QuoteParams} params - Quote parameters
|
|
33
36
|
*/
|
|
34
|
-
quote(params:
|
|
37
|
+
quote(params: QuoteParams): void;
|
|
35
38
|
|
|
36
39
|
/**
|
|
37
40
|
* Stop quotation/trading
|
|
@@ -40,10 +43,8 @@ export default class StellarBrokerClient {
|
|
|
40
43
|
|
|
41
44
|
/**
|
|
42
45
|
* Confirm current quote and start trading
|
|
43
|
-
* @param {string} account - Trader account address
|
|
44
|
-
* @param {string|ClientAuthorizationCallback} authorization - Authorization method, either account secret key or an authorization callback
|
|
45
46
|
*/
|
|
46
|
-
confirmQuote(
|
|
47
|
+
confirmQuote(): void;
|
|
47
48
|
|
|
48
49
|
/**
|
|
49
50
|
* Add event listener
|
|
@@ -75,9 +76,12 @@ export default class StellarBrokerClient {
|
|
|
75
76
|
/**
|
|
76
77
|
* Request single swap quote estimate without trading
|
|
77
78
|
*/
|
|
78
|
-
export function estimateSwap(params:
|
|
79
|
+
export function estimateSwap(params: QuoteParams): Promise<QuoteResult>;
|
|
79
80
|
|
|
80
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Quote request parameters provided by the client
|
|
83
|
+
*/
|
|
84
|
+
export interface QuoteParams {
|
|
81
85
|
/**
|
|
82
86
|
* Asset to sell
|
|
83
87
|
*/
|
|
@@ -91,53 +95,102 @@ export interface SwapQuoteParams {
|
|
|
91
95
|
*/
|
|
92
96
|
sellingAmount?: string
|
|
93
97
|
/**
|
|
94
|
-
*
|
|
95
|
-
*/
|
|
96
|
-
buyingAmount?: string
|
|
97
|
-
/**
|
|
98
|
-
* Swap slippage tolerance (2% by default)
|
|
98
|
+
* Swap slippage tolerance (0.02 by default - 2%)
|
|
99
99
|
*/
|
|
100
100
|
slippageTolerance?: number
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
|
|
104
103
|
export interface ClientInitializationParams {
|
|
105
104
|
/**
|
|
106
|
-
*
|
|
105
|
+
* Trader account address
|
|
107
106
|
*/
|
|
108
|
-
|
|
107
|
+
account: string;
|
|
109
108
|
/**
|
|
110
|
-
*
|
|
109
|
+
* Partner key
|
|
111
110
|
*/
|
|
112
|
-
|
|
111
|
+
partnerKey?: string;
|
|
113
112
|
/**
|
|
114
|
-
*
|
|
113
|
+
* Authorization method, either account secret key or an authorization callback
|
|
115
114
|
*/
|
|
116
|
-
|
|
115
|
+
authorization: ClientAuthorizationParams;
|
|
117
116
|
}
|
|
118
117
|
|
|
119
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Current client session status
|
|
120
|
+
*/
|
|
121
|
+
export type ClientSessionStatus = "disconnected" | "ready" | "quote" | "trade";
|
|
120
122
|
|
|
121
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Event type generated by the client
|
|
125
|
+
*/
|
|
126
|
+
export type StellarBrokerClientEvent = "quote" | "progress" | "paused" | "finished" | "error";
|
|
122
127
|
|
|
123
|
-
|
|
128
|
+
/**
|
|
129
|
+
* Result status code returned by the server
|
|
130
|
+
*/
|
|
131
|
+
export type QuoteResultStatus = "unfeasible" | "rejected" | "success";
|
|
124
132
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Quote received from the server
|
|
135
|
+
*/
|
|
136
|
+
export interface QuoteResult {
|
|
137
|
+
/**
|
|
138
|
+
* Quote timestamp
|
|
139
|
+
*/
|
|
140
|
+
readonly ts: Date,
|
|
141
|
+
/**
|
|
142
|
+
* Quote status code
|
|
143
|
+
*/
|
|
144
|
+
readonly status: QuoteResultStatus;
|
|
145
|
+
/**
|
|
146
|
+
* Asset to sell
|
|
147
|
+
*/
|
|
148
|
+
readonly sellingAsset: string;
|
|
149
|
+
/**
|
|
150
|
+
* Asset to buy
|
|
151
|
+
*/
|
|
152
|
+
readonly buyingAsset: string;
|
|
153
|
+
/**
|
|
154
|
+
* Swap slippage tolerance
|
|
155
|
+
*/
|
|
156
|
+
readonly slippageTolerance: number;
|
|
157
|
+
/**
|
|
158
|
+
* Amount of the selling asset
|
|
159
|
+
*/
|
|
160
|
+
readonly sellingAmount: string;
|
|
161
|
+
/**
|
|
162
|
+
* Estimated amount of buyingAsset to receive
|
|
163
|
+
*/
|
|
164
|
+
readonly estimatedBuyingAmount?: string;
|
|
165
|
+
/**
|
|
166
|
+
* Equivalent direct path_payment trade estimate
|
|
167
|
+
*/
|
|
168
|
+
readonly directTrade?: QuoteDirectTradeResult;
|
|
169
|
+
/**
|
|
170
|
+
* Difference between quoted price and estimated direct trade
|
|
171
|
+
*/
|
|
172
|
+
readonly profit: string;
|
|
173
|
+
/**
|
|
174
|
+
* Error details from the server (for failed quotes)
|
|
175
|
+
*/
|
|
176
|
+
readonly error?: string;
|
|
136
177
|
}
|
|
137
178
|
|
|
138
|
-
|
|
179
|
+
/**
|
|
180
|
+
* Emulated single `path_payment` trades result corresponding to the quote
|
|
181
|
+
*/
|
|
182
|
+
export interface QuoteDirectTradeResult {
|
|
183
|
+
/**
|
|
184
|
+
* Estimated amount to sell
|
|
185
|
+
*/
|
|
139
186
|
selling: string;
|
|
187
|
+
/**
|
|
188
|
+
* Estimated amount to buy
|
|
189
|
+
*/
|
|
140
190
|
buying: string;
|
|
191
|
+
/**
|
|
192
|
+
* Conversion path
|
|
193
|
+
*/
|
|
141
194
|
path: string[];
|
|
142
195
|
}
|
|
143
196
|
|
|
@@ -150,4 +203,57 @@ export class StellarBrokerError extends Error {
|
|
|
150
203
|
readonly code: number;
|
|
151
204
|
}
|
|
152
205
|
|
|
153
|
-
|
|
206
|
+
/**
|
|
207
|
+
* Async authorization callback for transactions and simulated auth entries
|
|
208
|
+
*/
|
|
209
|
+
export type ClientAuthorizationCallback = (txOrAuthEntry: TransactionI | Buffer) => Promise<TransactionI | Buffer>;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Authorization method, either account secret key or an authorization callback
|
|
213
|
+
*/
|
|
214
|
+
export type ClientAuthorizationParams = string | ClientAuthorizationCallback;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Trader mediator account wrapper
|
|
218
|
+
*/
|
|
219
|
+
export class Mediator {
|
|
220
|
+
/**
|
|
221
|
+
* Create a trader mediator account instance for a given source account
|
|
222
|
+
* @param source - Source account
|
|
223
|
+
* @param sellingAsset - Asset to sell
|
|
224
|
+
* @param buyingAsset - Asset to buy
|
|
225
|
+
* @param sellingAmount - Amount to sell
|
|
226
|
+
* @param authorization - Authorization callback or secret key
|
|
227
|
+
*/
|
|
228
|
+
constructor(source: string, sellingAsset: string | Asset, buyingAsset: string | Asset, sellingAmount: string, authorization: ClientAuthorizationParams)
|
|
229
|
+
|
|
230
|
+
readonly source: string;
|
|
231
|
+
|
|
232
|
+
readonly sellingAsset: Asset;
|
|
233
|
+
|
|
234
|
+
readonly buyingAsset: Asset;
|
|
235
|
+
|
|
236
|
+
readonly sellingAmount: bigint;
|
|
237
|
+
|
|
238
|
+
readonly mediatorAddress: string;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Check if there are any non-disposed mediators that belong to lost swap sessions
|
|
242
|
+
*/
|
|
243
|
+
get hasObsoleteMediators(): boolean;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Retrieve funds from mediator accounts that belong to lost swap sessions
|
|
247
|
+
*/
|
|
248
|
+
disposeObsoleteMediators(): Promise<void>;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Create mediator account and deposit tokens to sell
|
|
252
|
+
*/
|
|
253
|
+
init(): Promise<string>;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Dispose mediator account
|
|
257
|
+
*/
|
|
258
|
+
dispose(address: string): Promise<void>;
|
|
259
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import StellarBrokerClient from './client.js'
|
|
2
|
-
export * from './estimate.js'
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import {StellarBrokerClient} from './client.js'
|
|
2
|
+
export * from './estimate.js'
|
|
3
|
+
export * from './mediator.js'
|
|
4
|
+
|
|
5
|
+
export {StellarBrokerClient}
|