@stellar-broker/client 0.6.10 → 0.6.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stellar-broker/client",
3
- "version": "0.6.10",
3
+ "version": "0.6.12",
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
@@ -226,8 +226,9 @@ export class Mediator {
226
226
  * @param buyingAsset - Asset to buy
227
227
  * @param sellingAmount - Amount to sell
228
228
  * @param authorization - Authorization callback or secret key
229
+ * @param [reserveFeeAmount] - Amount reserved to cover tx fees (all unused funds will be refunded)
229
230
  */
230
- constructor(source: string, sellingAsset: string | Asset, buyingAsset: string | Asset, sellingAmount: string, authorization: ClientAuthorizationParams)
231
+ constructor(source: string, sellingAsset: string | Asset, buyingAsset: string | Asset, sellingAmount: string, authorization: ClientAuthorizationParams, reserveFeeAmount?: number)
231
232
 
232
233
  readonly source: string;
233
234
 
package/src/mediator.js CHANGED
@@ -3,8 +3,6 @@ import {fromStroops, toStroops} from './stroops.js'
3
3
  import {convertToStellarAsset} from './asset.js'
4
4
  import {AuthorizationWrapper} from './authorization.js'
5
5
 
6
- //additional XLM amount to cover tx fees
7
- const defaultFeesReserve = 3
8
6
  const defaultStoragePrefix = 'msb_'
9
7
 
10
8
  export class Mediator {
@@ -15,8 +13,9 @@ export class Mediator {
15
13
  * @param {string|Asset} buyingAsset - Identifier of the asset to buy
16
14
  * @param {string} sellingAmount - Asset amount to sell
17
15
  * @param {ClientAuthorizationParams} authorization - Authorization callback or secret key
16
+ * @param {number} [reserveFeeAmount] - Amount reserved to cover tx fees (all unused funds will be refunded)
18
17
  */
19
- constructor(source, sellingAsset, buyingAsset, sellingAmount, authorization) {
18
+ constructor(source, sellingAsset, buyingAsset, sellingAmount, authorization, reserveFeeAmount = 5) {
20
19
  if (!StrKey.isValidEd25519PublicKey(source))
21
20
  throw new Error('Invalid source account')
22
21
  this.source = source
@@ -39,6 +38,7 @@ export class Mediator {
39
38
  throw new Error('Invalid selling amount')
40
39
  }
41
40
  this.authorization = new AuthorizationWrapper(authorization)
41
+ this.reserveFeeAmount = reserveFeeAmount
42
42
  }
43
43
 
44
44
  /**
@@ -83,6 +83,11 @@ export class Mediator {
83
83
  * @private
84
84
  */
85
85
  storagePrefix = defaultStoragePrefix
86
+ /**
87
+ * @type {number}
88
+ * @private
89
+ */
90
+ reserveFeeAmount
86
91
 
87
92
  /**
88
93
  * Check if there are any non-disposed mediators that belong to lost swap sessions
@@ -137,8 +142,8 @@ export class Mediator {
137
142
  if (!sourceAccount)
138
143
  throw new Error('Mediator account doesn\'t exist on the ledger')
139
144
  //calculate fees reserve + account entries reserve
140
- const subentries = sourceAccount.signers.length - 1 + [this.sellingAsset, this.buyingAsset].filter(a => !a.isNative()).length
141
- const feesReserve = defaultFeesReserve + 0.5 * subentries
145
+ const subentries = 2 + sourceAccount.signers.length - 1 + [this.sellingAsset, this.buyingAsset].filter(a => !a.isNative()).length
146
+ const feesReserve = this.reserveFeeAmount + 0.5 * subentries
142
147
  const ops = []
143
148
  //create new random keypair for the trade
144
149
  this.mediator = Keypair.random()
@@ -187,6 +192,20 @@ export class Mediator {
187
192
  amount: fromStroops(this.sellingAmount)
188
193
  }))
189
194
  }
195
+ //create trustline for buying asset
196
+ if (!buyingAsset.isNative()) {
197
+ ops.push(Operation.changeTrust({
198
+ source: this.mediatorAddress,
199
+ asset: buyingAsset
200
+ }))
201
+ //if source account itself doesn't have trustline for buying asset
202
+ if (!findTrustline(sourceAccount, buyingAsset)) {
203
+ ops.push(Operation.changeTrust({
204
+ source: this.source,
205
+ asset: buyingAsset
206
+ }))
207
+ }
208
+ }
190
209
 
191
210
  if (sourceAccount.signers.length > 1) { //multisig or delegated schema
192
211
  let {thresholds, signers} = sourceAccount
@@ -230,13 +249,6 @@ export class Mediator {
230
249
  }))
231
250
  }
232
251
 
233
- //create trustline for buying asset
234
- if (!buyingAsset.isNative()) {
235
- ops.push(Operation.changeTrust({
236
- source: this.mediatorAddress,
237
- asset: buyingAsset
238
- }))
239
- }
240
252
  //store the record in the localStorage
241
253
  localStorage.setItem(this.storagePrefix + this.mediatorAddress, this.source)
242
254
  //confirm tx