@stellar-expert/tx-meta-effects-parser 7.0.0-rc.13 → 7.0.0-rc.14

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-expert/tx-meta-effects-parser",
3
- "version": "7.0.0-rc.13",
3
+ "version": "7.0.0-rc.14",
4
4
  "description": "Low-level effects parser for Stellar transaction results and meta XDR",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -159,29 +159,23 @@ class EventsAnalyzer {
159
159
  return null
160
160
  if (to === from) //self transfer - nothing happens
161
161
  return // TODO: need additional checks
162
- let classicAsset
163
- if (topics.length > 3) {
164
- classicAsset = xdrParseAsset(xdrParseScVal(topics[3]))
165
- if (!mapSacContract(this.effectsAnalyzer, contract, classicAsset)) {
166
- classicAsset = null //not an SAC event
167
- }
168
- }
169
- if (classicAsset) {
170
- if (classicAsset.includes(from)) { //SAC transfer by asset issuer
171
- this.effectsAnalyzer.mint(classicAsset, amount)
162
+ const asset = this.getAssetFromEventTopics(topics, contract)
163
+ if (!StrKey.isValidContract(asset)) {
164
+ if (asset.includes(from)) { //SAC transfer by asset issuer
165
+ this.effectsAnalyzer.mint(asset, amount)
172
166
  }
173
167
  if (isContractAddress(from)) {
174
- this.effectsAnalyzer.debit(amount, classicAsset, from)
168
+ this.effectsAnalyzer.debit(amount, asset, from)
175
169
  }
176
170
  if (isContractAddress(to)) {
177
- this.effectsAnalyzer.credit(amount, classicAsset, to)
171
+ this.effectsAnalyzer.credit(amount, asset, to)
178
172
  }
179
- if (classicAsset.includes(receiver)) { //SAC transfer by asset issuer
180
- this.effectsAnalyzer.burn(classicAsset, amount)
173
+ if (asset.includes(receiver)) { //SAC transfer by asset issuer
174
+ this.effectsAnalyzer.burn(asset, amount)
181
175
  }
182
176
  } else { //other cases
183
- this.effectsAnalyzer.debit(amount, this.effectsAnalyzer.resolveAsset(contract), from)
184
- this.effectsAnalyzer.credit(amount, this.effectsAnalyzer.resolveAsset(contract), to)
177
+ this.effectsAnalyzer.debit(amount, asset, from)
178
+ this.effectsAnalyzer.credit(amount, asset, to)
185
179
  }
186
180
  }
187
181
  break
@@ -198,11 +192,7 @@ class EventsAnalyzer {
198
192
  }
199
193
  if (typeof amount !== 'string')
200
194
  return null
201
- const last = topics[topics.length - 1]
202
- if (last._arm === 'str') {
203
- mapSacContract(this.effectsAnalyzer, contract, xdrParseAsset(xdrParseScVal(last)))
204
- }
205
- const asset = this.effectsAnalyzer.resolveAsset(contract)
195
+ const asset = this.getAssetFromEventTopics(topics, contract)
206
196
  this.effectsAnalyzer.mint(asset, amount)
207
197
  if (isContractAddress(asset) || isContractAddress(to)) {
208
198
  this.effectsAnalyzer.credit(amount, asset, to)
@@ -216,10 +206,7 @@ class EventsAnalyzer {
216
206
  const amount = processEventBodyValue(body.data())
217
207
  if (typeof amount !== 'string')
218
208
  return null
219
- if (topics.length > 2) {
220
- mapSacContract(this.effectsAnalyzer, contract, xdrParseAsset(xdrParseScVal(topics[2])))
221
- }
222
- const asset = this.effectsAnalyzer.resolveAsset(contract)
209
+ const asset = this.getAssetFromEventTopics(topics, contract)
223
210
  if (isContractAddress(asset) || isContractAddress(from)) {
224
211
  this.effectsAnalyzer.debit(amount, asset, from)
225
212
  }
@@ -233,12 +220,11 @@ class EventsAnalyzer {
233
220
  const amount = processEventBodyValue(body.data())
234
221
  if (typeof amount !== 'string')
235
222
  return null
236
- if (topics.length > 3) {
237
- mapSacContract(this.effectsAnalyzer, contract, xdrParseAsset(xdrParseScVal(topics[3])))
223
+ const asset = this.getAssetFromEventTopics(topics, contract)
224
+ if (StrKey.isValidContract(from)) { //transfer tokens from account only in case of contract assets to avoid double debits
225
+ this.effectsAnalyzer.debit(amount, asset, from)
226
+ this.effectsAnalyzer.burn(asset, amount)
238
227
  }
239
- const asset = this.effectsAnalyzer.resolveAsset(contract)
240
- this.effectsAnalyzer.debit(amount, asset, from)
241
- this.effectsAnalyzer.burn(asset, amount)
242
228
  }
243
229
  break
244
230
  case 'set_admin': {
@@ -246,9 +232,7 @@ class EventsAnalyzer {
246
232
  return //throw new Error('Non-standard event')
247
233
  const currentAdmin = xdrParseScVal(topics[1])
248
234
  const newAdmin = processEventBodyValue(body.data())
249
- if (topics.length > 2) {
250
- mapSacContract(this.effectsAnalyzer, contract, xdrParseAsset(xdrParseScVal(topics[2])))
251
- }
235
+ this.getAssetFromEventTopics(topics, contract)
252
236
  this.effectsAnalyzer.setAdmin(contract, newAdmin)
253
237
  }
254
238
  break
@@ -256,11 +240,7 @@ class EventsAnalyzer {
256
240
  if (!matchEventTopicsShape(topics, ['address', 'str?']))
257
241
  return //throw new Error('Non-standard event')
258
242
  const trustor = xdrParseScVal(topics[1])
259
- const encodedAsset = xdrParseScVal(topics[2])
260
- if (topics.length > 2) {
261
- mapSacContract(this.effectsAnalyzer, contract, xdrParseAsset(encodedAsset))
262
- }
263
- const asset = this.effectsAnalyzer.resolveAsset(contract)
243
+ const asset = this.getAssetFromEventTopics(topics, contract)
264
244
  const isAuthorized = processEventBodyValue(body.data())
265
245
  this.effectsAnalyzer.addEffect({
266
246
  type: effectTypes.trustlineAuthorizationUpdated,
@@ -284,6 +264,21 @@ class EventsAnalyzer {
284
264
  break*/
285
265
  }
286
266
  }
267
+
268
+ /**
269
+ * @param {ScVal[]} topics
270
+ * @param {string} contract
271
+ * @return {string|null}
272
+ * @private
273
+ */
274
+ getAssetFromEventTopics(topics, contract) {
275
+ const last = topics[topics.length - 1]
276
+ if (last._arm === 'str') {
277
+ const classicAsset = xdrParseAsset(xdrParseScVal(last))
278
+ mapSacContract(this.effectsAnalyzer, contract, classicAsset)
279
+ }
280
+ return this.effectsAnalyzer.resolveAsset(contract)
281
+ }
287
282
  }
288
283
 
289
284
  /**