@stellar-expert/tx-meta-effects-parser 8.1.0 → 8.3.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.3.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
 
package/src/index.js CHANGED
@@ -120,7 +120,10 @@ function parseTxOperationsMeta({
120
120
  } catch (e) {
121
121
  throw new TxMetaEffectParserError('Invalid transaction metadata XDR. ' + e.message)
122
122
  }
123
-
123
+ let feeSource = feeEffect.source
124
+ if (feeSource.startsWith('M')) {
125
+ feeSource = xdrParserUtils.retrieveBaseMuxedAddress(feeSource)
126
+ }
124
127
  //add tx-level effects
125
128
  for (const {before, after} of parseTxMetaChanges(meta)) {
126
129
  if (before.entry !== 'account')
@@ -129,7 +132,7 @@ function parseTxOperationsMeta({
129
132
  effect.source = (before || after).address
130
133
  res.effects.push(effect)
131
134
  }
132
- if (feeEffect.source === after.address) {
135
+ if (feeSource === after.address) {
133
136
  feeEffect.balance = after.balance
134
137
  }
135
138
  if (isFeeBump && protocol === 20 && before.balance !== after.balance) { //bump fee calculation bug in protocol v20
@@ -65,6 +65,15 @@ function xdrParsePrice(price) {
65
65
  return price.n() / price.d()
66
66
  }
67
67
 
68
+ /**
69
+ * @param {string} address
70
+ * @return {string}
71
+ */
72
+ function retrieveBaseMuxedAddress(address) {
73
+ const rawBytes = StrKey.decodeMed25519PublicKey(address)
74
+ return StrKey.encodeEd25519PublicKey(rawBytes.subarray(0, 32))
75
+ }
76
+
68
77
  /**
69
78
  * Parse account signer key XDR
70
79
  * @param {xdr.SignerKey} signer
@@ -293,7 +302,7 @@ function xdrParseScVal(value, treatBytesAsContractId = false) {
293
302
  }
294
303
  }
295
304
 
296
- function xdrParseTokenBalance({type, key, value}) {
305
+ function xdrParseSacBalanceChange(changeEventType, key, value) {
297
306
  const parsedKey = xdr.ScVal.fromXDR(key, 'base64')
298
307
  if (parsedKey._arm !== 'vec')
299
308
  return null
@@ -302,20 +311,25 @@ function xdrParseTokenBalance({type, key, value}) {
302
311
  return null
303
312
  if (keyParts[0]._arm !== 'sym' || keyParts[1]._arm !== 'address' || keyParts[0]._value.toString() !== 'Balance')
304
313
  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 {
314
+ const res = {
316
315
  address: xdrParseScVal(keyParts[1]),
317
- balance
316
+ balance: changeEventType===effectTypes.contractDataRemoved?
317
+ '0':
318
+ retrieveBalanceFromStateData(value)
318
319
  }
320
+ if (res.balance === undefined)
321
+ return null
322
+ return res
323
+ }
324
+
325
+ function retrieveBalanceFromStateData(value) {
326
+ const xdrVal = xdr.ScVal.fromXDR(value, 'base64')
327
+ if (xdrVal._arm !== 'map')
328
+ return undefined
329
+ const parsedValue = xdrParseScVal(xdrVal)
330
+ if (typeof parsedValue.amount !== 'string')
331
+ return undefined
332
+ return parsedValue.amount
319
333
  }
320
334
 
321
335
  module.exports = {
@@ -329,5 +343,6 @@ module.exports = {
329
343
  xdrParseSignerKey,
330
344
  xdrParsePrice,
331
345
  xdrParseScVal,
332
- xdrParseTokenBalance
346
+ xdrParseSacBalanceChange,
347
+ retrieveBaseMuxedAddress
333
348
  }