@stellar-broker/client 0.6.5 → 0.6.7

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.5",
3
+ "version": "0.6.7",
4
4
  "description": "Client library for StellarBroker service",
5
5
  "author": "team@stellar.broker",
6
6
  "homepage": "https://stellar.broker",
package/src/mediator.js CHANGED
@@ -1,10 +1,10 @@
1
- import {Asset, Keypair, Memo, Operation, TransactionBuilder, Horizon, Networks, StrKey} from '@stellar/stellar-sdk'
1
+ import {Asset, Keypair, Memo, Operation, TransactionBuilder, Horizon, Networks, StrKey, sign} from '@stellar/stellar-sdk'
2
2
  import {fromStroops, toStroops} from './stroops.js'
3
3
  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 = '3'
7
+ const defaultFeesReserve = 3
8
8
  const defaultStoragePrefix = 'msb_'
9
9
 
10
10
  export class Mediator {
@@ -136,10 +136,13 @@ export class Mediator {
136
136
  const sourceAccount = await this.loadAccount(this.source)
137
137
  if (!sourceAccount)
138
138
  throw new Error('Mediator account doesn\'t exist on the ledger')
139
+ //calculate fees reserve + account entries reserve
140
+ const feesReserve = defaultFeesReserve + 0.5 * (sourceAccount.signers.length - 1)
139
141
  const ops = []
140
142
  //create new random keypair for the trade
141
143
  this.mediator = Keypair.random()
142
144
  this.mediatorAddress = this.mediator.publicKey()
145
+ //create mediator account and deposit funds
143
146
  const {sellingAsset, buyingAsset} = this
144
147
  if (sellingAsset.isNative()) { //for XLM total amount should include fee reserves
145
148
  const amount = this.sellingAmount + BigInt(feesReserve * 10000000)
@@ -155,13 +158,13 @@ export class Mediator {
155
158
  } else {
156
159
  //check available XLM balance
157
160
  const xlmBalance = findTrustline(sourceAccount, Asset.native())
158
- if (xlmBalance.balance < parseInt(feesReserve))
161
+ if (parseFloat(xlmBalance.balance) < feesReserve)
159
162
  throw new Error('Insufficient XLM balance for potential trading fees')
160
163
  //create mediator account
161
164
  ops.push(Operation.createAccount({
162
165
  source: this.source,
163
166
  destination: this.mediatorAddress,
164
- startingBalance: feesReserve // for tx fees
167
+ startingBalance: feesReserve.toString() // for tx fees
165
168
  }))
166
169
  //check available XLM balance
167
170
  const sellingTrustline = findTrustline(sourceAccount, this.sellingAsset)
@@ -183,19 +186,49 @@ export class Mediator {
183
186
  amount: fromStroops(this.sellingAmount)
184
187
  }))
185
188
  }
186
- //add source account as a signer
187
- ops.push(Operation.setOptions({
188
- source: this.mediatorAddress,
189
- homeDomain: 'mediator.stellar.broker',
190
- masterWeight: 1,
191
- highThreshold: 1,
192
- medThreshold: 1,
193
- lowThreshold: 1,
194
- signer: {
195
- ed25519PublicKey: this.source,
196
- weight: 1
189
+
190
+ if (sourceAccount.signers.length > 1) { //multisig or delegated schema
191
+ let {thresholds, signers} = sourceAccount
192
+ signers = signers.filter(signer => signer.type === 'ed25519_public_key')
193
+ //add source account as a signer
194
+ ops.push(Operation.setOptions({
195
+ source: this.mediatorAddress,
196
+ inflationDest: this.source, //store source account to the inflation dest
197
+ homeDomain: 'mediator.stellar.broker',
198
+ masterWeight: Math.max(thresholds.high_threshold, thresholds.med_threshold), //own signer always has the highest weight
199
+ highThreshold: thresholds.high_threshold,
200
+ medThreshold: thresholds.med_threshold,
201
+ lowThreshold: thresholds.low_threshold,
202
+ signer: { //add first signer
203
+ ed25519PublicKey: signers[0].key,
204
+ weight: Math.max(1, signers[0].weight)
205
+ }
206
+ }))
207
+ //add all other signers
208
+ for (let i = 1; i < signers.length; i++) {
209
+ ops.push(Operation.setOptions({
210
+ source: this.mediatorAddress,
211
+ signer: {
212
+ ed25519PublicKey: signers[i].key,
213
+ weight: Math.max(1, signers[i].weight)
214
+ }
215
+ }))
197
216
  }
198
- }))
217
+ } else { //simple schema
218
+ ops.push(Operation.setOptions({
219
+ source: this.mediatorAddress,
220
+ homeDomain: 'mediator.stellar.broker',
221
+ masterWeight: 1,
222
+ highThreshold: 1,
223
+ medThreshold: 1,
224
+ lowThreshold: 1,
225
+ signer: { //add source account as a signer
226
+ ed25519PublicKey: this.source,
227
+ weight: 1
228
+ }
229
+ }))
230
+ }
231
+
199
232
  //create trustline for buying asset
200
233
  if (!buyingAsset.isNative()) {
201
234
  ops.push(Operation.changeTrust({
@@ -203,10 +236,10 @@ export class Mediator {
203
236
  asset: buyingAsset
204
237
  }))
205
238
  }
206
- //confirm tx
207
- await this.buildAndSend(sourceAccount, ops)
208
239
  //store the record in the localStorage
209
240
  localStorage.setItem(this.storagePrefix + this.mediatorAddress, this.source)
241
+ //confirm tx
242
+ await this.buildAndSend(sourceAccount, ops)
210
243
  //client will need mediator secret for trading
211
244
  return this.mediator.secret()
212
245
  }
@@ -221,15 +254,18 @@ export class Mediator {
221
254
  address = this.mediatorAddress
222
255
  }
223
256
  //load account
224
- let account = await this.loadAccount(address)
225
- if (!account)
257
+ let mediatorAccount = await this.loadAccount(address)
258
+ if (!mediatorAccount) {
259
+ //remove reference from local storage
260
+ localStorage.removeItem(this.storagePrefix + address)
226
261
  throw new Error(`Mediator account ${address} doesn't exist on the ledger`)
262
+ }
227
263
 
228
- if (!account.signers.find(s => s.key === this.source))
264
+ if (!mediatorAccount.signers.find(s => s.key === this.source))
229
265
  throw new Error(`${address} is not a mediator account for ${this.source}`)
230
266
  const ops = []
231
267
  //remove trustlines for each account balance
232
- for (const balance of account.balances) {
268
+ for (const balance of mediatorAccount.balances) {
233
269
  if (balance.asset_type === 'native')
234
270
  continue //skip XLM trustline - merge will handle the transfer
235
271
  const asset = convertToStellarAsset(balance)
@@ -255,9 +291,9 @@ export class Mediator {
255
291
  destination: this.source
256
292
  }))
257
293
  if (this.mediatorAddress !== address) {
258
- account = await this.loadAccount(this.source)
294
+ mediatorAccount = await this.loadAccount(this.source)
259
295
  }
260
- await this.buildAndSend(account, ops)
296
+ await this.buildAndSend(mediatorAccount, ops)
261
297
  //remove reference from local storage
262
298
  localStorage.removeItem(this.storagePrefix + address)
263
299
  }
package/jest.config.cjs DELETED
@@ -1,4 +0,0 @@
1
- module.exports = {
2
- clearMocks: true,
3
- maxWorkers: 1
4
- }