@stellar-broker/client 0.6.3 → 0.6.4

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 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)
165
+ }
161
166
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stellar-broker/client",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "Client library for StellarBroker service",
5
5
  "author": "team@stellar.broker",
6
6
  "homepage": "https://stellar.broker",
package/src/index.d.ts CHANGED
@@ -244,11 +244,25 @@ 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 [storagePrefix] - Local storage key prefix
263
+ */
264
+ static disposeObsoleteMediators(source:string, storagePrefix?:string): Promise<void>;
265
+
252
266
  /**
253
267
  * Create mediator account and deposit tokens to sell
254
268
  */
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
  /**
@@ -37,7 +38,9 @@ export class Mediator {
37
38
  console.error(e)
38
39
  throw new Error('Invalid selling amount')
39
40
  }
40
- this.authorization = new AuthorizationWrapper(authorization)
41
+ if (authorization) {
42
+ this.authorization = new AuthorizationWrapper(authorization)
43
+ }
41
44
  }
42
45
 
43
46
  /**
@@ -81,15 +84,25 @@ export class Mediator {
81
84
  * @type {string}
82
85
  * @private
83
86
  */
84
- storagePrefix = 'msb_'
87
+ storagePrefix = defaultStoragePrefix
85
88
 
86
89
  /**
87
90
  * Check if there are any non-disposed mediators that belong to lost swap sessions
88
91
  * @return {boolean}
89
92
  */
90
93
  get hasObsoleteMediators() {
94
+ return this.constructor.hasObsoleteMediators(this.source, this.storagePrefix)
95
+ }
96
+
97
+ /**
98
+ * Check if there are any non-disposed mediators that belong to lost swap sessions
99
+ * @param {string} source - Initiator account that created a mediator
100
+ * @param {string} [storagePrefix] - Local storage key prefix
101
+ * @return {boolean}
102
+ */
103
+ static hasObsoleteMediators(source, storagePrefix = defaultStoragePrefix) {
91
104
  return Object.entries(localStorage).some(([key, initiator]) =>
92
- initiator === this.source && key.startsWith(this.storagePrefix))
105
+ initiator === source && key.startsWith(storagePrefix))
93
106
  }
94
107
 
95
108
  /**
@@ -104,6 +117,17 @@ export class Mediator {
104
117
  }
105
118
  }
106
119
 
120
+ /**
121
+ * Retrieve funds from mediator accounts that belong to lost swap sessions
122
+ * @param {string} source - Initiator account that created a mediator
123
+ * @param {string} [storagePrefix] - Local storage key prefix
124
+ * @return {Promise}
125
+ */
126
+ static async disposeObsoleteMediators(source, storagePrefix = defaultStoragePrefix) {
127
+ const wrapper = new Mediator(source, 'XLM', 'XLM', '0', null)
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
  }