From b92b2ab74bdf7f6def8ee1f44041be8f06c573ac Mon Sep 17 00:00:00 2001 From: daniel <4954577+jaensen@users.noreply.github.com> Date: Sat, 14 Dec 2024 05:00:48 +0100 Subject: [PATCH] fix: parsing GroupRedeemCollateralBurn and GroupRedeemCollateralReturn now works. --- .../LogParser.cs | 177 ++++++++++-------- Circles.Index/Circles.Index.csproj | 4 +- 2 files changed, 99 insertions(+), 82 deletions(-) diff --git a/Circles.Index.CirclesV2.StandardTreasury/LogParser.cs b/Circles.Index.CirclesV2.StandardTreasury/LogParser.cs index 9c253c0..fe074fb 100644 --- a/Circles.Index.CirclesV2.StandardTreasury/LogParser.cs +++ b/Circles.Index.CirclesV2.StandardTreasury/LogParser.cs @@ -1,11 +1,9 @@ using System.Numerics; -using Circles.Index.CirclesV2.StandardTreasury; using Circles.Index.Common; using Nethermind.Core; using Nethermind.Core.Crypto; using Nethermind.Core.Extensions; using Nethermind.Int256; -using DatabaseSchema = Circles.Index.CirclesV2.StandardTreasury.DatabaseSchema; namespace Circles.Index.CirclesV2.StandardTreasury; @@ -116,7 +114,7 @@ private IEnumerable CollateralLockedBatch(Block block, Tx string groupAddress = "0x" + log.Topics[1].ToString().Substring(Consts.AddressEmptyBytesPrefixLength); int offset = 0; - int idsLength = (int)new BigInteger(log.Data.Slice(offset, 32).ToArray()); + int idsLength = (int)new BigInteger(log.Data.Slice(offset, 32).ToArray(), true, true); offset += 32; List ids = new List(); @@ -126,7 +124,7 @@ private IEnumerable CollateralLockedBatch(Block block, Tx offset += 32; } - int valuesLength = (int)new BigInteger(log.Data.Slice(offset, 32).ToArray()); + int valuesLength = (int)new BigInteger(log.Data.Slice(offset, 32).ToArray(), true, true); offset += 32; List values = new List(); @@ -176,86 +174,105 @@ private GroupRedeem GroupRedeem(Block block, TxReceipt receipt, LogEntry log, in private IEnumerable GroupRedeemCollateralReturn(Block block, TxReceipt receipt, LogEntry log, int logIndex) { - // string groupAddress = "0x" + log.Topics[1].ToString().Substring(Consts.AddressEmptyBytesPrefixLength); - // string toAddress = "0x" + log.Topics[2].ToString().Substring(Consts.AddressEmptyBytesPrefixLength); - // - // int offset = 0; - // int idsLength = (int)new BigInteger(log.Data.Slice(offset, 32).ToArray()); - // offset += 32; - // - // List ids = new List(); - // for (int i = 0; i < idsLength; i++) - // { - // ids.Add(new UInt256(log.Data.Slice(offset, 32), true)); - // offset += 32; - // } - // - // int valuesLength = (int)new BigInteger(log.Data.Slice(offset, 32).ToArray()); - // offset += 32; - // - // List values = new List(); - // for (int i = 0; i < valuesLength; i++) - // { - // values.Add(new UInt256(log.Data.Slice(offset, 32), true)); - // offset += 32; - // } - // - // for (int i = 0; i < idsLength; i++) - // { - // yield return new GroupRedeemCollateralReturn( - // block.Number, - // (long)block.Timestamp, - // receipt.Index, - // logIndex, - // receipt.TxHash!.ToString(), - // i, - // groupAddress, - // toAddress, - // ids[i], - // values[i]); - // } - yield break; // Don't index right now. + // event GroupRedeemCollateralReturn(address indexed group, address indexed to, uint256[] ids, uint256[] values) + + // Extract addresses from topics: + string groupAddress = "0x" + log.Topics[1].ToString().Substring(Consts.AddressEmptyBytesPrefixLength); + string toAddress = "0x" + log.Topics[2].ToString().Substring(Consts.AddressEmptyBytesPrefixLength); + + var data = log.Data; + + // The first 32 bytes is the offset to the `ids` array + // The second 32 bytes is the offset to the `values` array + // Offsets are relative to the start of `log.Data`. + var idsOffset = (int)new BigInteger(data.Slice(0, 32).ToArray(), true, true); + var valuesOffset = (int)new BigInteger(data.Slice(32, 32).ToArray(), true, true); ; + + // Read ids array length and elements + int idsLength = (int)new BigInteger(data.Slice(idsOffset, 32).ToArray(), true, true); + int idsDataStart = idsOffset + 32; + + UInt256[] ids = new UInt256[idsLength]; + for (int i = 0; i < idsLength; i++) + { + ids[i] = new UInt256(data.Slice(idsDataStart + i * 32, 32), true); + } + + // Read values array length and elements + int valuesLength = (int)new BigInteger(data.Slice(valuesOffset, 32).ToArray(), true, true); + int valuesDataStart = valuesOffset + 32; + + UInt256[] values = new UInt256[valuesLength]; + for (int i = 0; i < valuesLength; i++) + { + values[i] = new UInt256(data.Slice(valuesDataStart + i * 32, 32), true); + } + + // Yield events + for (int i = 0; i < idsLength; i++) + { + yield return new GroupRedeemCollateralReturn( + block.Number, + (long)block.Timestamp, + receipt.Index, + logIndex, + receipt.TxHash!.ToString(), + i, + groupAddress, + toAddress, + ids[i], + values[i]); + } } private IEnumerable GroupRedeemCollateralBurn(Block block, TxReceipt receipt, LogEntry log, int logIndex) { - // string groupAddress = "0x" + log.Topics[1].ToString().Substring(Consts.AddressEmptyBytesPrefixLength); - // - // int offset = 0; - // int idsLength = (int)new BigInteger(log.Data.Slice(offset, 32).ToArray()); - // offset += 32; - // - // List ids = new List(); - // for (int i = 0; i < idsLength; i++) - // { - // ids.Add(new UInt256(log.Data.Slice(offset, 32), true)); - // offset += 32; - // } - // - // int valuesLength = (int)new BigInteger(log.Data.Slice(offset, 32).ToArray()); - // offset += 32; - // - // List values = new List(); - // for (int i = 0; i < valuesLength; i++) - // { - // values.Add(new UInt256(log.Data.Slice(offset, 32), true)); - // offset += 32; - // } - // - // for (int i = 0; i < idsLength; i++) - // { - // yield return new GroupRedeemCollateralBurn( - // block.Number, - // (long)block.Timestamp, - // receipt.Index, - // logIndex, - // receipt.TxHash!.ToString(), - // i, - // groupAddress, - // ids[i], - // values[i]); - // } - yield break; + // event GroupRedeemCollateralBurn(address indexed group, uint256[] ids, uint256[] values) + + // Extract address from topics: + string groupAddress = "0x" + log.Topics[1].ToString().Substring(Consts.AddressEmptyBytesPrefixLength); + + var data = log.Data; + + // The first 32 bytes is the offset to the `ids` array + // The second 32 bytes is the offset to the `values` array + var idsOffset = (int)new BigInteger(data.Slice(0, 32).ToArray(), true, true); + var valuesOffset = (int)new BigInteger(data.Slice(32, 32).ToArray(), true, true); + + // Read ids array + int idsLength = (int)new BigInteger(data.Slice(idsOffset, 32).ToArray(), true, true); + int idsDataStart = idsOffset + 32; + + UInt256[] ids = new UInt256[idsLength]; + for (int i = 0; i < idsLength; i++) + { + ids[i] = new UInt256(data.Slice(idsDataStart + i * 32, 32), true); + } + + // Read values array + int valuesLength = (int)new BigInteger(data.Slice(valuesOffset, 32).ToArray(), true, true); + int valuesDataStart = valuesOffset + 32; + + UInt256[] values = new UInt256[valuesLength]; + for (int i = 0; i < valuesLength; i++) + { + values[i] = new UInt256(data.Slice(valuesDataStart + i * 32, 32), true); + } + + // Yield events + for (int i = 0; i < idsLength; i++) + { + yield return new GroupRedeemCollateralBurn( + block.Number, + (long)block.Timestamp, + receipt.Index, + logIndex, + receipt.TxHash!.ToString(), + i, + groupAddress, + ids[i], + values[i]); + } } } \ No newline at end of file diff --git a/Circles.Index/Circles.Index.csproj b/Circles.Index/Circles.Index.csproj index 6f712bb..b3da0e6 100644 --- a/Circles.Index/Circles.Index.csproj +++ b/Circles.Index/Circles.Index.csproj @@ -8,8 +8,8 @@ Daniel Janz (Gnosis Service GmbH) Gnosis Service GmbH Circles - 1.11.4 - 1.11.4 + 1.11.5 + 1.11.5