@stellar-broker/client 0.6.6 → 0.6.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/mediator.js +55 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stellar-broker/client",
3
- "version": "0.6.6",
3
+ "version": "0.6.8",
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(1, 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: { //cope signers and weights
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({
@@ -221,18 +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) {
226
259
  //remove reference from local storage
227
260
  localStorage.removeItem(this.storagePrefix + address)
228
261
  throw new Error(`Mediator account ${address} doesn't exist on the ledger`)
229
262
  }
230
263
 
231
- if (!account.signers.find(s => s.key === this.source))
264
+ if (!mediatorAccount.signers.find(s => s.key === this.source))
232
265
  throw new Error(`${address} is not a mediator account for ${this.source}`)
233
266
  const ops = []
234
267
  //remove trustlines for each account balance
235
- for (const balance of account.balances) {
268
+ for (const balance of mediatorAccount.balances) {
236
269
  if (balance.asset_type === 'native')
237
270
  continue //skip XLM trustline - merge will handle the transfer
238
271
  const asset = convertToStellarAsset(balance)
@@ -258,9 +291,9 @@ export class Mediator {
258
291
  destination: this.source
259
292
  }))
260
293
  if (this.mediatorAddress !== address) {
261
- account = await this.loadAccount(this.source)
294
+ mediatorAccount = await this.loadAccount(this.source)
262
295
  }
263
- await this.buildAndSend(account, ops)
296
+ await this.buildAndSend(mediatorAccount, ops)
264
297
  //remove reference from local storage
265
298
  localStorage.removeItem(this.storagePrefix + address)
266
299
  }