@stellar-expert/tx-meta-effects-parser 8.1.0 → 8.2.0

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": "8.1.0",
3
+ "version": "8.2.0",
4
4
  "description": "Low-level effects parser for Stellar transaction results and meta XDR",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -6,8 +6,7 @@ const {
6
6
  xdrParseAsset,
7
7
  xdrParseAccountAddress,
8
8
  xdrParseScVal,
9
- xdrParseSacBalance,
10
- xdrParseTokenBalance
9
+ xdrParseSacBalanceChange
11
10
  } = require('./parser/tx-xdr-parser-utils')
12
11
  const {contractIdFromPreimage} = require('./parser/contract-preimage-encoder')
13
12
  const {generateContractCodeEntryHash} = require('./parser/ledger-key')
@@ -885,14 +884,12 @@ class EffectsAnalyzer {
885
884
  break
886
885
  }
887
886
  this.addEffect(effect)
888
- const tokenBalance = xdrParseTokenBalance(effect)
887
+ const tokenBalance = xdrParseSacBalanceChange(effect.type, key, after?.value, before?.value)
889
888
  if (tokenBalance) {
890
889
  const balanceEffects = this.effects.filter(e => e.source === tokenBalance.address &&
891
890
  (e.type === effectTypes.accountCredited || e.type === effectTypes.accountDebited) &&
892
891
  (e.asset === effect.owner || e.asset === this.sacMap?.get(effect.owner)))
893
- if (balanceEffects.length !== 1)
894
- return //we can set balance only when we found 1-1 mapping, if there are several balance changes, we can't establish balance relation
895
- balanceEffects[0].balance = tokenBalance.balance //set transfer effect balance
892
+ balanceEffects[balanceEffects.length - 1].balance = tokenBalance.balance //set latest transfer effect balance
896
893
  }
897
894
  }
898
895
 
@@ -293,7 +293,7 @@ function xdrParseScVal(value, treatBytesAsContractId = false) {
293
293
  }
294
294
  }
295
295
 
296
- function xdrParseTokenBalance({type, key, value}) {
296
+ function xdrParseSacBalanceChange(changeEventType, key, value) {
297
297
  const parsedKey = xdr.ScVal.fromXDR(key, 'base64')
298
298
  if (parsedKey._arm !== 'vec')
299
299
  return null
@@ -302,20 +302,25 @@ function xdrParseTokenBalance({type, key, value}) {
302
302
  return null
303
303
  if (keyParts[0]._arm !== 'sym' || keyParts[1]._arm !== 'address' || keyParts[0]._value.toString() !== 'Balance')
304
304
  return null
305
- let balance = '0'
306
- if (type !== effectTypes.contractDataRemoved) { //TODO: handle evicted entries separately
307
- const xdrVal = xdr.ScVal.fromXDR(value, 'base64')
308
- if (xdrVal._arm !== 'map')
309
- return null
310
- const parsedValue = xdrParseScVal(xdrVal)
311
- if (typeof parsedValue.amount !== 'string')
312
- return null
313
- balance = parsedValue.amount
314
- }
315
- return {
305
+ const res = {
316
306
  address: xdrParseScVal(keyParts[1]),
317
- balance
307
+ balance: changeEventType===effectTypes.contractDataRemoved?
308
+ '0':
309
+ retrieveBalanceFromStateData(value)
318
310
  }
311
+ if (res.balance === undefined)
312
+ return null
313
+ return res
314
+ }
315
+
316
+ function retrieveBalanceFromStateData(value) {
317
+ const xdrVal = xdr.ScVal.fromXDR(value, 'base64')
318
+ if (xdrVal._arm !== 'map')
319
+ return undefined
320
+ const parsedValue = xdrParseScVal(xdrVal)
321
+ if (typeof parsedValue.amount !== 'string')
322
+ return undefined
323
+ return parsedValue.amount
319
324
  }
320
325
 
321
326
  module.exports = {
@@ -329,5 +334,5 @@ module.exports = {
329
334
  xdrParseSignerKey,
330
335
  xdrParsePrice,
331
336
  xdrParseScVal,
332
- xdrParseTokenBalance
337
+ xdrParseSacBalanceChange
333
338
  }