@stellar-broker/client 0.6.2 → 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 +5 -0
- package/package.json +1 -1
- package/src/index.d.ts +14 -0
- package/src/mediator.js +37 -11
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
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
|
@@ -4,7 +4,8 @@ import {convertToStellarAsset} from './asset.js'
|
|
|
4
4
|
import {AuthorizationWrapper} from './authorization.js'
|
|
5
5
|
|
|
6
6
|
//additional XLM amount to cover tx fees
|
|
7
|
-
const feesReserve = '
|
|
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
|
-
|
|
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 =
|
|
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 ===
|
|
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
|
|
@@ -173,10 +197,12 @@ export class Mediator {
|
|
|
173
197
|
}
|
|
174
198
|
}))
|
|
175
199
|
//create trustline for buying asset
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
200
|
+
if (!buyingAsset.isNative()) {
|
|
201
|
+
ops.push(Operation.changeTrust({
|
|
202
|
+
source: this.mediatorAddress,
|
|
203
|
+
asset: buyingAsset
|
|
204
|
+
}))
|
|
205
|
+
}
|
|
180
206
|
//confirm tx
|
|
181
207
|
await this.buildAndSend(sourceAccount, ops)
|
|
182
208
|
//store the record in the localStorage
|
|
@@ -268,7 +294,7 @@ export class Mediator {
|
|
|
268
294
|
tx = await this.authorization.authorize(tx)
|
|
269
295
|
}
|
|
270
296
|
//execute the tx
|
|
271
|
-
const res = await this.createHorizon().submitTransaction(tx, {skipMemoRequiredCheck: true})
|
|
297
|
+
const res = await this.constructor.createHorizon().submitTransaction(tx, {skipMemoRequiredCheck: true})
|
|
272
298
|
if (!res.successful)
|
|
273
299
|
throw new Error('Failed to create mediator account')
|
|
274
300
|
}
|
|
@@ -280,7 +306,7 @@ export class Mediator {
|
|
|
280
306
|
*/
|
|
281
307
|
async loadAccount(address) {
|
|
282
308
|
try {
|
|
283
|
-
return this.createHorizon().loadAccount(address)
|
|
309
|
+
return this.constructor.createHorizon().loadAccount(address)
|
|
284
310
|
} catch (e) {
|
|
285
311
|
console.error(e)
|
|
286
312
|
}
|
|
@@ -290,7 +316,7 @@ export class Mediator {
|
|
|
290
316
|
* @return {HorizonServer}
|
|
291
317
|
* @private
|
|
292
318
|
*/
|
|
293
|
-
createHorizon() {
|
|
319
|
+
static createHorizon() {
|
|
294
320
|
return new Horizon.Server('https://horizon.stellar.org')
|
|
295
321
|
}
|
|
296
322
|
}
|