@stellar-expert/tx-meta-effects-parser 7.0.0 → 8.1.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/README.MD CHANGED
@@ -108,7 +108,8 @@ parseTxOperationsMeta({
108
108
  source: 'GBKP…YDLI',
109
109
  asset: 'XLM',
110
110
  bid: '10000',
111
- charged: '100'
111
+ charged: '100',
112
+ balance: '99999999900'
112
113
  }
113
114
  ```
114
115
  </td>
@@ -152,7 +153,7 @@ parseTxOperationsMeta({
152
153
  <sub><code>accountDebited</code></sub>
153
154
  </td>
154
155
  <td><sub>CreateAccountOp, AccountMergeOp, PaymentOp, PathPaymentStrictReceiveOp,
155
- PathPaymentStrictSendOp, CreateClaimableBalanceOp, ClawbackOp</sub></td>
156
+ PathPaymentStrictSendOp, CreateClaimableBalanceOp, ClawbackOp, InvokeHostFunctionOp</sub></td>
156
157
  <td>
157
158
 
158
159
  ```js
@@ -160,7 +161,8 @@ PathPaymentStrictSendOp, CreateClaimableBalanceOp, ClawbackOp</sub></td>
160
161
  type: 'accountDebited',
161
162
  source: 'GBKP…YDLI',
162
163
  asset: 'XLM',
163
- amount: '1000000000'
164
+ amount: '1000000000',
165
+ balance: '199999999900'
164
166
  }
165
167
  ```
166
168
  </td>
@@ -172,7 +174,7 @@ PathPaymentStrictSendOp, CreateClaimableBalanceOp, ClawbackOp</sub></td>
172
174
  </td>
173
175
  <td><sub>CreateAccountOp, AccountMergeOp, PaymentOp, PathPaymentStrictReceiveOp,
174
176
  PathPaymentStrictSendOp, ClaimClaimableBalanceOp, ClawbackOp,
175
- ClawbackClaimableBalance, InflationOp</sub></td>
177
+ ClawbackClaimableBalance, InflationOp, InvokeHostFunctionOp</sub></td>
176
178
  <td>
177
179
 
178
180
  ```js
@@ -180,7 +182,8 @@ ClawbackClaimableBalance, InflationOp</sub></td>
180
182
  type: 'accountCredited',
181
183
  source: 'GBWC…DXHN',
182
184
  asset: 'XLM',
183
- amount: '1000000000'
185
+ amount: '1000000000',
186
+ balance: '299999999900'
184
187
  }
185
188
  ```
186
189
  </td>
@@ -412,7 +415,7 @@ ClawbackClaimableBalance, InflationOp</sub></td>
412
415
  <sub><code>assetMinted</code></sub>
413
416
  </td>
414
417
  <td><sub>PaymentOp, PathPaymentStrictReceiveOp, PathPaymentStrictSendOp, CreateClaimableBalanceOp,
415
- ManageBuyOfferOp, ManageSellOfferOp, CreatePassiveOfferOp, DepositLiquidityOp</sub></td>
418
+ ManageBuyOfferOp, ManageSellOfferOp, CreatePassiveOfferOp, DepositLiquidityOp, InvokeHostFunctionOp</sub></td>
416
419
  <td>
417
420
 
418
421
  ```js
@@ -432,8 +435,7 @@ ManageBuyOfferOp, ManageSellOfferOp, CreatePassiveOfferOp, DepositLiquidityOp</s
432
435
  <sub><code>assetBurned</code></sub>
433
436
  </td>
434
437
  <td><sub>PaymentOp, PathPaymentStrictReceiveOp, PathPaymentStrictSendOp, CreateClaimableBalanceOp, ManageBuyOfferOp, ManageSellOfferOp,
435
- CreatePassiveOfferOp, WithdrawLiquidityOp, ClawbackOp,
436
- ClaimClaimableBalanceOp</sub></td>
438
+ CreatePassiveOfferOp, WithdrawLiquidityOp, ClawbackOp, ClaimClaimableBalanceOp, InvokeHostFunctionOp</sub></td>
437
439
  <td>
438
440
 
439
441
  ```js
@@ -535,7 +537,7 @@ ClaimClaimableBalanceOp</sub></td>
535
537
  {
536
538
  type: 'liquidityPoolCreated',
537
539
  source: 'GBWC…DXHN',
538
- pool: '52a876f8',
540
+ pool: 'LA7FHJLT',
539
541
  reserves: [
540
542
  {
541
543
  asset: 'XLM',
@@ -565,7 +567,7 @@ ClaimClaimableBalanceOp</sub></td>
565
567
  {
566
568
  type: 'liquidityPoolUpdated',
567
569
  source: 'GBWC…DXHN',
568
- pool: '52a876f8',
570
+ pool: 'LA7FHJLT',
569
571
  reserves: [
570
572
  {
571
573
  asset: 'XLM',
@@ -595,7 +597,7 @@ ClaimClaimableBalanceOp</sub></td>
595
597
  {
596
598
  type: 'liquidityPoolDeposited',
597
599
  source: 'GBWC…DXHN',
598
- pool: '52a876f8',
600
+ pool: 'LA7FHJLT',
599
601
  assets: [
600
602
  {
601
603
  asset: 'XLM',
@@ -625,7 +627,7 @@ ClaimClaimableBalanceOp</sub></td>
625
627
  {
626
628
  type: 'liquidityPoolWithdrew',
627
629
  source: 'GBWC…DXHN',
628
- pool: '52a876f8',
630
+ pool: 'LA7FHJLT',
629
631
  assets: [
630
632
  {
631
633
  asset: 'XLM',
@@ -655,7 +657,7 @@ ClaimClaimableBalanceOp</sub></td>
655
657
  {
656
658
  type: 'liquidityPoolRemoved',
657
659
  source: 'GBWC…DXHN',
658
- pool: '52a876f8'
660
+ pool: 'LA7FHJLT'
659
661
  }
660
662
  ```
661
663
 
@@ -792,7 +794,7 @@ ClaimClaimableBalanceOp</sub></td>
792
794
  type: 'claimableBalanceCreated',
793
795
  source: 'GBWC…DXHN',
794
796
  sponsor: 'GBWC…DXHN',
795
- balance: '7aba6f9d',
797
+ balance: 'BDKDF75Y',
796
798
  asset: 'XLM',
797
799
  amount: '1000000000',
798
800
  claimants: [
@@ -821,7 +823,7 @@ ClaimClaimableBalanceOp</sub></td>
821
823
  type: 'claimableBalanceRemoved',
822
824
  source: 'GBKP…YDLI',
823
825
  sponsor: 'GBWC…DXHN',
824
- balance: '7aba6f9d',
826
+ balance: 'BDKDF75Y',
825
827
  asset: 'XLM',
826
828
  amount: '1000000000',
827
829
  claimants: [
@@ -1198,61 +1200,6 @@ ClaimClaimableBalanceOp</sub></td>
1198
1200
  </td>
1199
1201
  </tr>
1200
1202
 
1201
- <tr>
1202
- <td>
1203
- <sub><code>liquidityPoolSponsorshipCreated</code></sub>
1204
- </td>
1205
- <td><sub>ChangeThrustOp</sub></td>
1206
- <td>
1207
-
1208
- ```js
1209
- {
1210
- type: 'liquidityPoolSponsorshipCreated',
1211
- source: 'GBWC…DXHN',
1212
- sponsor: 'GBWC…DXHN'
1213
- }
1214
- ```
1215
-
1216
- </td>
1217
- </tr>
1218
-
1219
- <tr>
1220
- <td>
1221
- <sub><code>liquidityPoolSponsorshipUpdated</code></sub>
1222
- </td>
1223
- <td><sub>RevokeSponsorshipOp</sub></td>
1224
- <td>
1225
-
1226
- ```js
1227
- {
1228
- type: 'liquidityPoolSponsorshipUpdated',
1229
- source: 'GBWC…DXHN',
1230
- sponsor: 'GDNP…CMXH',
1231
- prevSponsor: 'GBWC…DXHN'
1232
- }
1233
- ```
1234
-
1235
- </td>
1236
- </tr>
1237
-
1238
- <tr>
1239
- <td>
1240
- <sub><code>liquidityPoolSponsorshipRemoved</code></sub>
1241
- </td>
1242
- <td><sub>RevokeSponsorshipOp</sub></td>
1243
- <td>
1244
-
1245
- ```js
1246
- {
1247
- type: 'liquidityPoolSponsorshipRemoved',
1248
- source: 'GBWC…DXHN',
1249
- prevSponsor: 'GBWC…DXHN'
1250
- }
1251
- ```
1252
-
1253
- </td>
1254
- </tr>
1255
-
1256
1203
  <tr>
1257
1204
  <td>
1258
1205
  <sub><code>contractCodeUploaded</code></sub>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stellar-expert/tx-meta-effects-parser",
3
- "version": "7.0.0",
3
+ "version": "8.1.0",
4
4
  "description": "Low-level effects parser for Stellar transaction results and meta XDR",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -2,7 +2,13 @@ const {StrKey, hash, xdr, nativeToScVal} = require('@stellar/stellar-base')
2
2
  const effectTypes = require('./effect-types')
3
3
  const {validateAmount, normalizeAddress, parseLargeInt} = require('./parser/normalization')
4
4
  const {parseLedgerEntryChanges} = require('./parser/ledger-entry-changes-parser')
5
- const {xdrParseAsset, xdrParseAccountAddress, xdrParseScVal} = require('./parser/tx-xdr-parser-utils')
5
+ const {
6
+ xdrParseAsset,
7
+ xdrParseAccountAddress,
8
+ xdrParseScVal,
9
+ xdrParseSacBalance,
10
+ xdrParseTokenBalance
11
+ } = require('./parser/tx-xdr-parser-utils')
6
12
  const {contractIdFromPreimage} = require('./parser/contract-preimage-encoder')
7
13
  const {generateContractCodeEntryHash} = require('./parser/ledger-key')
8
14
  const {analyzeSignerChanges} = require('./aggregation/signer-changes-analyzer')
@@ -344,14 +350,14 @@ class EffectsAnalyzer {
344
350
  }
345
351
 
346
352
  liquidityPoolDeposit() {
347
- const {liquidityPoolId} = this.operation
348
- const change = this.changes.find(ch => ch.type === 'liquidityPool' && ch.action === 'updated' && ch.after.pool === liquidityPoolId)
353
+ const pool = StrKey.encodeLiquidityPool(Buffer.from(this.operation.liquidityPoolId, 'hex'))
354
+ const change = this.changes.find(ch => ch.type === 'liquidityPool' && ch.action === 'updated' && ch.after.pool === pool)
349
355
  if (!change) //tx failed
350
356
  return
351
357
  const {before, after} = change
352
358
  this.addEffect({
353
359
  type: effectTypes.liquidityPoolDeposited,
354
- pool: this.operation.liquidityPoolId,
360
+ pool,
355
361
  assets: after.asset.map((asset, i) => ({
356
362
  asset,
357
363
  amount: (after.amount[i] - before.amount[i]).toString()
@@ -362,7 +368,7 @@ class EffectsAnalyzer {
362
368
  }
363
369
 
364
370
  liquidityPoolWithdraw() {
365
- const pool = this.operation.liquidityPoolId
371
+ const pool = StrKey.encodeLiquidityPool(Buffer.from(this.operation.liquidityPoolId, 'hex'))
366
372
  const change = this.changes.find(ch => ch.type === 'liquidityPool' && ch.action === 'updated' && ch.before.pool === pool)
367
373
  if (!change) //tx failed
368
374
  return
@@ -481,7 +487,7 @@ class EffectsAnalyzer {
481
487
  asset: claimedOffer.asset
482
488
  }
483
489
  if (claimedOffer.poolId) {
484
- trade.pool = claimedOffer.poolId.toString('hex')
490
+ trade.pool = StrKey.encodeLiquidityPool(claimedOffer.poolId)
485
491
  } else {
486
492
  trade.offer = claimedOffer.offerId
487
493
  trade.seller = claimedOffer.account
@@ -803,33 +809,6 @@ class EffectsAnalyzer {
803
809
  this.addEffect(effect)
804
810
  }
805
811
 
806
- processContractBalance(effect) {
807
- const parsedKey = xdr.ScVal.fromXDR(effect.key, 'base64')
808
- if (parsedKey._arm !== 'vec')
809
- return
810
- const keyParts = parsedKey._value
811
- if (!(keyParts instanceof Array) || keyParts.length !== 2)
812
- return
813
- if (keyParts[0]._arm !== 'sym' || keyParts[1]._arm !== 'address' || keyParts[0]._value.toString() !== 'Balance')
814
- return
815
- const account = xdrParseScVal(keyParts[1])
816
- const balanceEffects = this.effects.filter(e => (e.type === effectTypes.accountCredited || e.type === effectTypes.accountDebited) && e.source === account && e.asset === effect.owner)
817
- if (balanceEffects.length !== 1) //we can set balance only when we found 1-1 mapping, if there are several balance changes, we can't establish balance relation
818
- return
819
- if (effect.type === effectTypes.contractDataRemoved) { //balance completely removed - this may be a reversible operation if the balance simply expired
820
- balanceEffects[0].balance = '0'
821
- return
822
- }
823
- const value = xdr.ScVal.fromXDR(effect.value, 'base64')
824
- if (value._arm !== 'map')
825
- return
826
- const parsedValue = xdrParseScVal(value)
827
- if (typeof parsedValue.clawback !== 'boolean' || typeof parsedValue.authorized !== 'boolean' || typeof parsedValue.amount !== 'string')
828
- return
829
- //set transfer effect balance
830
- balanceEffects[0].balance = parsedValue.amount
831
- }
832
-
833
812
  processContractChanges({action, before, after}) {
834
813
  const {kind, owner: contract, keyHash} = after
835
814
  let effect = {
@@ -906,7 +885,15 @@ class EffectsAnalyzer {
906
885
  break
907
886
  }
908
887
  this.addEffect(effect)
909
- this.processContractBalance(effect)
888
+ const tokenBalance = xdrParseTokenBalance(effect)
889
+ if (tokenBalance) {
890
+ const balanceEffects = this.effects.filter(e => e.source === tokenBalance.address &&
891
+ (e.type === effectTypes.accountCredited || e.type === effectTypes.accountDebited) &&
892
+ (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
896
+ }
910
897
  }
911
898
 
912
899
  processContractCodeChanges({type, action, before, after}) {
package/src/index.js CHANGED
@@ -129,6 +129,9 @@ function parseTxOperationsMeta({
129
129
  effect.source = (before || after).address
130
130
  res.effects.push(effect)
131
131
  }
132
+ if (feeEffect.source === after.address) {
133
+ feeEffect.balance = after.balance
134
+ }
132
135
  if (isFeeBump && protocol === 20 && before.balance !== after.balance) { //bump fee calculation bug in protocol v20
133
136
  const currentFee = BigInt(feeEffect.charged)
134
137
  const diff = BigInt(after.balance) - BigInt(before.balance)
@@ -1,6 +1,12 @@
1
1
  const {StrKey} = require('@stellar/stellar-base')
2
2
  const {TxMetaEffectParserError, UnexpectedTxMetaChangeError} = require('../errors')
3
- const {xdrParseAsset, xdrParseAccountAddress, xdrParseClaimant, xdrParsePrice, xdrParseSignerKey} = require('./tx-xdr-parser-utils')
3
+ const {
4
+ xdrParseAsset,
5
+ xdrParseAccountAddress,
6
+ xdrParseClaimant,
7
+ xdrParsePrice,
8
+ xdrParseSignerKey
9
+ } = require('./tx-xdr-parser-utils')
4
10
  const {generateContractStateEntryHash, generateContractCodeEntryHash} = require('./ledger-key')
5
11
 
6
12
  /**
@@ -176,8 +182,7 @@ function parseTrustlineEntry(value) {
176
182
  asset = xdrParseAsset(trustlineAsset)
177
183
  break
178
184
  case 3:
179
- asset = trustlineEntryXdr.asset().liquidityPoolId().toString('hex')
180
- //data.liquidityPoolUseCount = trustlineEntryXdr.liquidityPoolUseCount()
185
+ asset = StrKey.encodeLiquidityPool(trustlineEntryXdr.asset().liquidityPoolId())
181
186
  break
182
187
  default:
183
188
  throw new TxMetaEffectParserError(`Unsupported trustline type ` + trustlineType)
@@ -219,7 +224,7 @@ function parseLiquidityPoolEntry(value) {
219
224
  const params = body.params()
220
225
  return {
221
226
  entry: 'liquidityPool',
222
- pool: liquidityPoolEntryXdr.liquidityPoolId().toString('hex'),
227
+ pool: StrKey.encodeLiquidityPool(liquidityPoolEntryXdr.liquidityPoolId()),
223
228
  asset: [xdrParseAsset(params.assetA()), xdrParseAsset(params.assetB())],
224
229
  fee: params.fee(),
225
230
  amount: [body.reserveA().toString(), body.reserveB().toString()],
@@ -247,7 +252,7 @@ function parseOfferEntry(value) {
247
252
  function parseClaimableBalanceEntry(value) {
248
253
  const claimableBalanceXdr = value.value()
249
254
  const data = {
250
- balanceId: Buffer.from(claimableBalanceXdr.balanceId().value()).toString('hex'),
255
+ balanceId: StrKey.encodeClaimableBalance(claimableBalanceXdr.balanceId().value()),
251
256
  entry: 'claimableBalance',
252
257
  asset: xdrParseAsset(claimableBalanceXdr.asset()),
253
258
  amount: claimableBalanceXdr.amount().toString(),
@@ -1,5 +1,13 @@
1
- const {xdr, StrKey, LiquidityPoolId, scValToBigInt, encodeMuxedAccount, encodeMuxedAccountToAddress} = require('@stellar/stellar-base')
1
+ const {
2
+ xdr,
3
+ StrKey,
4
+ LiquidityPoolId,
5
+ scValToBigInt,
6
+ encodeMuxedAccount,
7
+ encodeMuxedAccountToAddress
8
+ } = require('@stellar/stellar-base')
2
9
  const {TxMetaEffectParserError} = require('../errors')
10
+ const effectTypes = require('../effect-types')
3
11
 
4
12
  /**
5
13
  * Parse account address from XDR representation
@@ -285,6 +293,31 @@ function xdrParseScVal(value, treatBytesAsContractId = false) {
285
293
  }
286
294
  }
287
295
 
296
+ function xdrParseTokenBalance({type, key, value}) {
297
+ const parsedKey = xdr.ScVal.fromXDR(key, 'base64')
298
+ if (parsedKey._arm !== 'vec')
299
+ return null
300
+ const keyParts = parsedKey._value
301
+ if (!(keyParts instanceof Array) || keyParts.length !== 2)
302
+ return null
303
+ if (keyParts[0]._arm !== 'sym' || keyParts[1]._arm !== 'address' || keyParts[0]._value.toString() !== 'Balance')
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 {
316
+ address: xdrParseScVal(keyParts[1]),
317
+ balance
318
+ }
319
+ }
320
+
288
321
  module.exports = {
289
322
  xdrParseAsset,
290
323
  xdrParseAccountAddress,
@@ -295,5 +328,6 @@ module.exports = {
295
328
  xdrParseTradeAtom,
296
329
  xdrParseSignerKey,
297
330
  xdrParsePrice,
298
- xdrParseScVal
331
+ xdrParseScVal,
332
+ xdrParseTokenBalance
299
333
  }