From 9238c889a45aedbc7c294039debc7ad01c241df0 Mon Sep 17 00:00:00 2001 From: daniel <4954577+jaensen@users.noreply.github.com> Date: Sun, 19 May 2024 21:54:45 +0200 Subject: [PATCH] * Added more views: V_CrcV1_Transfers, V_Crc_Transfers * Added missing 'transactionHash' column to CrcV1_Transfer --- Circles.Index.CirclesV1/DatabaseSchema.cs | 48 +++++++++++++ Circles.Index.CirclesViews/DatabaseSchema.cs | 55 +++++++++++++++ Circles.Index.Common/IDatabaseQueryResult.cs | 12 ++-- Circles.Index.Rpc/CirclesRpcModule.cs | 37 ++++------ general-example-requests.md | 72 ++++++++++++++++++++ 5 files changed, 195 insertions(+), 29 deletions(-) diff --git a/Circles.Index.CirclesV1/DatabaseSchema.cs b/Circles.Index.CirclesV1/DatabaseSchema.cs index 7dbc5f5..dc23516 100644 --- a/Circles.Index.CirclesV1/DatabaseSchema.cs +++ b/Circles.Index.CirclesV1/DatabaseSchema.cs @@ -29,6 +29,7 @@ public class DatabaseSchema : IDatabaseSchema new("timestamp", ValueTypes.Int, true), new("transactionIndex", ValueTypes.Int, true), new("logIndex", ValueTypes.Int, true), + new("transactionHash", ValueTypes.String, true), new("tokenAddress", ValueTypes.Address, true), new("from", ValueTypes.Address, true), new("to", ValueTypes.Address, true), @@ -109,6 +110,53 @@ null as ""token"" ") }; + /// + /// All Circles v1 hub transfers + personal minting + /// + public static readonly EventSchema Transfers = new("V_CrcV1", "Transfers", + new byte[32], + [ + new("blockNumber", ValueTypes.Int, true), + new("timestamp", ValueTypes.Int, true), + new("transactionIndex", ValueTypes.Int, true), + new("logIndex", ValueTypes.Int, true), + new("transactionHash", ValueTypes.String, true), + new("from", ValueTypes.Address, true), + new("to", ValueTypes.Address, true), + new("amount", ValueTypes.BigInt, false) + ]) + { + SqlMigrationItem = new SqlMigrationItem(@" + create or replace view ""V_CrcV1_Transfers"" as + with ""allTransfers"" as ( + select ""blockNumber"", + ""timestamp"", + ""transactionIndex"", + ""logIndex"", + ""transactionHash"", + ""from"", + ""to"", + ""amount"" + from ""CrcV1_HubTransfer"" + union all + select t.""blockNumber"", + t.""timestamp"", + t.""transactionIndex"", + t.""logIndex"", + t.""transactionHash"", + t.""from"", + t.""to"", + t.""amount"" + from ""CrcV1_Transfer"" t + join public.""CrcV1_Signup"" s on s.""token"" = t.""tokenAddress"" and s.""user"" = t.""to"" + where ""from"" = '0x0000000000000000000000000000000000000000' + ) + select * + from ""allTransfers"" + order by ""blockNumber"" desc, ""transactionIndex"" desc, ""logIndex"" desc; + ") + }; + public IDictionary<(string Namespace, string Table), EventSchema> Tables { get; } = new Dictionary<(string Namespace, string Table), EventSchema> { diff --git a/Circles.Index.CirclesViews/DatabaseSchema.cs b/Circles.Index.CirclesViews/DatabaseSchema.cs index 5e250ce..e3ba099 100644 --- a/Circles.Index.CirclesViews/DatabaseSchema.cs +++ b/Circles.Index.CirclesViews/DatabaseSchema.cs @@ -96,6 +96,57 @@ null as ""cidV0Digest"" ") }; + public static readonly EventSchema Transfers = new("V_Crc", "Transfers", + new byte[32], + [ + new("blockNumber", ValueTypes.Int, true), + new("timestamp", ValueTypes.Int, true), + new("transactionIndex", ValueTypes.Int, true), + new("logIndex", ValueTypes.Int, true), + new("batchIndex", ValueTypes.Int, true, true), + new("transactionHash", ValueTypes.String, true), + new("version", ValueTypes.Int, false), + new("operator", ValueTypes.Address, true), + new("from", ValueTypes.Address, true), + new("to", ValueTypes.Address, true), + new("id", ValueTypes.BigInt, true), + new("value", ValueTypes.BigInt, false) + ]) + { + SqlMigrationItem = new SqlMigrationItem(@" + create or replace view ""V_Crc_Transfers"" as + with ""allTransfers"" as (select ""blockNumber"", + ""timestamp"", + ""transactionIndex"", + ""logIndex"", + 0 as ""batchIndex"", + ""transactionHash"", + 1 as ""version"", + null as ""operator"", + ""from"", + ""to"", + null as ""id"", + ""amount"" as ""value"" + from ""V_CrcV1_Transfers"" + union all + select ""blockNumber"", + ""timestamp"", + ""transactionIndex"", + ""logIndex"", + ""batchIndex"", + ""transactionHash"", + 2 as ""version"", + ""operator"", + ""from"", + ""to"", + ""id"", + ""value"" + from ""V_CrcV2_Transfers"") + select * + from ""allTransfers""; + ") + }; + public IDictionary<(string Namespace, string Table), EventSchema> Tables { get; } = new Dictionary<(string Namespace, string Table), EventSchema> { @@ -106,6 +157,10 @@ null as ""cidV0Digest"" { ("V_Crc", "TrustRelations"), TrustRelations + }, + { + ("V_Crc", "Transfers"), + Transfers } }; } \ No newline at end of file diff --git a/Circles.Index.Common/IDatabaseQueryResult.cs b/Circles.Index.Common/IDatabaseQueryResult.cs index ccb0d81..564af7c 100644 --- a/Circles.Index.Common/IDatabaseQueryResult.cs +++ b/Circles.Index.Common/IDatabaseQueryResult.cs @@ -6,8 +6,8 @@ namespace Circles.Index.Common; [JsonConverter(typeof(DatabaseQueryResultConverter))] public record DatabaseQueryResult( - string[] columns, - IEnumerable rows); + string[] Columns, + IEnumerable Rows); public class DatabaseQueryResultConverter : JsonConverter { @@ -21,12 +21,12 @@ public override void Write(Utf8JsonWriter writer, DatabaseQueryResult value, Jso { writer.WriteStartObject(); - writer.WritePropertyName("Columns"); - JsonSerializer.Serialize(writer, value.columns, options); + writer.WritePropertyName("columns"); + JsonSerializer.Serialize(writer, value.Columns, options); - writer.WritePropertyName("Rows"); + writer.WritePropertyName("rows"); writer.WriteStartArray(); - foreach (var row in value.rows) + foreach (var row in value.Rows) { writer.WriteStartArray(); foreach (var item in row) diff --git a/Circles.Index.Rpc/CirclesRpcModule.cs b/Circles.Index.Rpc/CirclesRpcModule.cs index f5b5003..41a5648 100644 --- a/Circles.Index.Rpc/CirclesRpcModule.cs +++ b/Circles.Index.Rpc/CirclesRpcModule.cs @@ -65,7 +65,7 @@ public Task> circles_getTrustRelations(Addr var incomingTrusts = new List(); var outgoingTrusts = new List(); - foreach (var resultRow in result.rows) + foreach (var resultRow in result.Rows) { var user = new Address(resultRow[0].ToString() ?? throw new Exception("A user in the result set is null")); var canSendTo = new Address(resultRow[1].ToString() ?? @@ -163,28 +163,19 @@ public ResultWrapper circles_query(SelectDto query) { Select select = query.ToModel(); var parameterizedSql = select.ToSql(_indexerContext.Database); - - Console.WriteLine("circles_query: parameterizedSql:"); - Console.WriteLine(parameterizedSql.Sql); - Console.WriteLine(string.Join(", ", - parameterizedSql.Parameters.Select(p => $" * {p.ParameterName}={p.Value}"))); - - - var result = _indexerContext.Database.Select(parameterizedSql); - - // Log the .net types of the columns of the first row of the result set: - foreach (var resultRow in result.rows) + + StringWriter stringWriter = new(); + stringWriter.WriteLine($"circles_query(SelectDto query):"); + stringWriter.WriteLine($" select: {parameterizedSql.Sql}"); + stringWriter.WriteLine($" parameters:"); + foreach (var parameter in parameterizedSql.Parameters) { - for (int colIdx = 0; colIdx < resultRow.Length; colIdx++) - { - var colName = result.columns[colIdx]; - var colValue = resultRow[colIdx]; - - _pluginLogger.Info($"Column '{colName}' is of type '{colValue?.GetType().Name ?? "null"}'"); - } - - break; + stringWriter.WriteLine($" {parameter.ParameterName}: {parameter.Value}"); } + + _pluginLogger.Info(stringWriter.ToString()); + + var result = _indexerContext.Database.Select(parameterizedSql); return ResultWrapper.Success(result); } @@ -208,7 +199,7 @@ private IEnumerable
TokenAddressesForAccount(Address circlesAccount) var sql = select.ToSql(_indexerContext.Database); return _indexerContext.Database .Select(sql) - .rows + .Rows .Select(o => new Address(o[0].ToString() ?? throw new Exception("A token address in the result set is null")) ); @@ -305,7 +296,7 @@ private IEnumerable V2TokenIdsForAccount(ILogger logger, Address addres return _indexerContext.Database .Select(sql) - .rows + .Rows .Select(o => UInt256.Parse(o[0]?.ToString() ?? throw new Exception("A token id in the result set is null")) ); diff --git a/general-example-requests.md b/general-example-requests.md index 9d3599f..46acc18 100644 --- a/general-example-requests.md +++ b/general-example-requests.md @@ -8,6 +8,78 @@ The examples in this file are general Circles RPC methods that can be used to qu ### circles_query +##### Get a paginated list of trust relations + +```shell +curl -X POST --data '{ + "jsonrpc": "2.0", + "id": 1, + "method": "circles_query", + "params": [ + { + "Namespace": "V_CrcV1", + "Table": "TrustRelations", + "Limit": 10, + "Columns": [], + "Filter": [{ + "Type": "Conjunction", + "ConjunctionType": "Or", + "Predicates": [ + { + "Type": "FilterPredicate", + "FilterType": "LessThan", + "Column": "blockNumber", + "Value": 9819862 + }, + { + "Type": "Conjunction", + "ConjunctionType": "And", + "Predicates": [ + { + "Type": "FilterPredicate", + "FilterType": "Equal", + "Column": "blockNumber", + "Value": 9819862 + }, + { + "Type": "FilterPredicate", + "FilterType": "LessThan", + "Column": "transactionIndex", + "Value": 0 + } + ] + }, + { + "Type": "Conjunction", + "ConjunctionType": "And", + "Predicates": [ + { + "Type": "FilterPredicate", + "FilterType": "Equal", + "Column": "blockNumber", + "Value": 9819862 + }, + { + "Type": "FilterPredicate", + "FilterType": "Equal", + "Column": "transactionIndex", + "Value": 0 + }, + { + "Type": "FilterPredicate", + "FilterType": "LessThan", + "Column": "logIndex", + "Value": 1 + } + ] + } + ] + }] + }] +}' -H "Content-Type: application/json" http://localhost:8545/ + +``` + ##### Get a list of Circles avatars This query returns v1 as well as v2 Circles users. The version of the user can be determined by the `version` column.