@stellar-broker/client 0.6.3 → 0.6.5
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 +5 -0
- package/package.json +1 -1
- package/src/index.d.ts +15 -0
- package/src/mediator.js +30 -6
package/README.md
CHANGED
|
@@ -131,6 +131,7 @@ directly or create alternative logic based on it.
|
|
|
131
131
|
```js
|
|
132
132
|
import {Mediator} from '@stellar-broker/client'
|
|
133
133
|
|
|
134
|
+
const source = 'GDNDS76YATRWMRQ7SGCI7FG3Y4A2MREDSKWDGLEB75IX5W2T25HA4DS3'
|
|
134
135
|
const sellingAsset = 'XLM'
|
|
135
136
|
const buyingAsset = 'AQUA-GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA'
|
|
136
137
|
const sellingAmount = '10'
|
|
@@ -158,4 +159,8 @@ console.log('Mediator account disposed')
|
|
|
158
159
|
if (mediator.hasObsoleteMediators) {
|
|
159
160
|
await mediator.disposeObsoleteMediators()
|
|
160
161
|
}
|
|
162
|
+
//or using static methods
|
|
163
|
+
if (Mediator.hasObsoleteMediators(source)) {
|
|
164
|
+
await Mediator.disposeObsoleteMediators(source, signTx)
|
|
165
|
+
}
|
|
161
166
|
```
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -244,11 +244,26 @@ export class Mediator {
|
|
|
244
244
|
*/
|
|
245
245
|
get hasObsoleteMediators(): boolean;
|
|
246
246
|
|
|
247
|
+
/**
|
|
248
|
+
* Check if there are any non-disposed mediators that belong to lost swap sessions
|
|
249
|
+
* @param source - Initiator account that created a mediator
|
|
250
|
+
* @param [storagePrefix] - Local storage key prefix
|
|
251
|
+
*/
|
|
252
|
+
static hasObsoleteMediators(source: string, storagePrefix?: string): boolean
|
|
253
|
+
|
|
247
254
|
/**
|
|
248
255
|
* Retrieve funds from mediator accounts that belong to lost swap sessions
|
|
249
256
|
*/
|
|
250
257
|
disposeObsoleteMediators(): Promise<void>;
|
|
251
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Retrieve funds from mediator accounts that belong to lost swap sessions
|
|
261
|
+
* @param source - Initiator account that created a mediator
|
|
262
|
+
* @param authorization - Authorization callback or secret key
|
|
263
|
+
* @param [storagePrefix] - Local storage key prefix
|
|
264
|
+
*/
|
|
265
|
+
static disposeObsoleteMediators(source: string, authorization: ClientAuthorizationParams, storagePrefix?: string): Promise<void>;
|
|
266
|
+
|
|
252
267
|
/**
|
|
253
268
|
* Create mediator account and deposit tokens to sell
|
|
254
269
|
*/
|
package/src/mediator.js
CHANGED
|
@@ -5,6 +5,7 @@ import {AuthorizationWrapper} from './authorization.js'
|
|
|
5
5
|
|
|
6
6
|
//additional XLM amount to cover tx fees
|
|
7
7
|
const feesReserve = '3'
|
|
8
|
+
const defaultStoragePrefix = 'msb_'
|
|
8
9
|
|
|
9
10
|
export class Mediator {
|
|
10
11
|
/**
|
|
@@ -13,7 +14,7 @@ export class Mediator {
|
|
|
13
14
|
* @param {string|Asset} sellingAsset - Identifier of the asset to sell
|
|
14
15
|
* @param {string|Asset} buyingAsset - Identifier of the asset to buy
|
|
15
16
|
* @param {string} sellingAmount - Asset amount to sell
|
|
16
|
-
* @param {ClientAuthorizationParams} authorization - Authorization
|
|
17
|
+
* @param {ClientAuthorizationParams} authorization - Authorization callback or secret key
|
|
17
18
|
*/
|
|
18
19
|
constructor(source, sellingAsset, buyingAsset, sellingAmount, authorization) {
|
|
19
20
|
if (!StrKey.isValidEd25519PublicKey(source))
|
|
@@ -81,15 +82,25 @@ export class Mediator {
|
|
|
81
82
|
* @type {string}
|
|
82
83
|
* @private
|
|
83
84
|
*/
|
|
84
|
-
storagePrefix =
|
|
85
|
+
storagePrefix = defaultStoragePrefix
|
|
85
86
|
|
|
86
87
|
/**
|
|
87
88
|
* Check if there are any non-disposed mediators that belong to lost swap sessions
|
|
88
89
|
* @return {boolean}
|
|
89
90
|
*/
|
|
90
91
|
get hasObsoleteMediators() {
|
|
92
|
+
return this.constructor.hasObsoleteMediators(this.source, this.storagePrefix)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Check if there are any non-disposed mediators that belong to lost swap sessions
|
|
97
|
+
* @param {string} source - Initiator account that created a mediator
|
|
98
|
+
* @param {string} [storagePrefix] - Local storage key prefix
|
|
99
|
+
* @return {boolean}
|
|
100
|
+
*/
|
|
101
|
+
static hasObsoleteMediators(source, storagePrefix = defaultStoragePrefix) {
|
|
91
102
|
return Object.entries(localStorage).some(([key, initiator]) =>
|
|
92
|
-
initiator ===
|
|
103
|
+
initiator === source && key.startsWith(storagePrefix))
|
|
93
104
|
}
|
|
94
105
|
|
|
95
106
|
/**
|
|
@@ -104,6 +115,19 @@ export class Mediator {
|
|
|
104
115
|
}
|
|
105
116
|
}
|
|
106
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Retrieve funds from mediator accounts that belong to lost swap sessions
|
|
120
|
+
* @param {string} source - Initiator account that created a mediator
|
|
121
|
+
* @param {ClientAuthorizationParams} authorization - Authorization callback or secret key
|
|
122
|
+
* @param {string} [storagePrefix] - Local storage key prefix
|
|
123
|
+
* @return {Promise}
|
|
124
|
+
*/
|
|
125
|
+
static async disposeObsoleteMediators(source, authorization, storagePrefix = defaultStoragePrefix) {
|
|
126
|
+
const wrapper = new Mediator(source, 'XLM', 'XLM', '0', authorization)
|
|
127
|
+
wrapper.storagePrefix = storagePrefix
|
|
128
|
+
await wrapper.disposeObsoleteMediators()
|
|
129
|
+
}
|
|
130
|
+
|
|
107
131
|
/**
|
|
108
132
|
* Create mediator account and deposit tokens to sell
|
|
109
133
|
* @return {Promise<string>} - Newly created mediator secret key
|
|
@@ -270,7 +294,7 @@ export class Mediator {
|
|
|
270
294
|
tx = await this.authorization.authorize(tx)
|
|
271
295
|
}
|
|
272
296
|
//execute the tx
|
|
273
|
-
const res = await this.createHorizon().submitTransaction(tx, {skipMemoRequiredCheck: true})
|
|
297
|
+
const res = await this.constructor.createHorizon().submitTransaction(tx, {skipMemoRequiredCheck: true})
|
|
274
298
|
if (!res.successful)
|
|
275
299
|
throw new Error('Failed to create mediator account')
|
|
276
300
|
}
|
|
@@ -282,7 +306,7 @@ export class Mediator {
|
|
|
282
306
|
*/
|
|
283
307
|
async loadAccount(address) {
|
|
284
308
|
try {
|
|
285
|
-
return this.createHorizon().loadAccount(address)
|
|
309
|
+
return this.constructor.createHorizon().loadAccount(address)
|
|
286
310
|
} catch (e) {
|
|
287
311
|
console.error(e)
|
|
288
312
|
}
|
|
@@ -292,7 +316,7 @@ export class Mediator {
|
|
|
292
316
|
* @return {HorizonServer}
|
|
293
317
|
* @private
|
|
294
318
|
*/
|
|
295
|
-
createHorizon() {
|
|
319
|
+
static createHorizon() {
|
|
296
320
|
return new Horizon.Server('https://horizon.stellar.org')
|
|
297
321
|
}
|
|
298
322
|
}
|