Skip to content

Commit

Permalink
* Added more views: V_CrcV1_Transfers, V_Crc_Transfers
Browse files Browse the repository at this point in the history
* Added missing 'transactionHash' column to CrcV1_Transfer
  • Loading branch information
jaensen committed May 19, 2024
1 parent 9587947 commit 9238c88
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 29 deletions.
48 changes: 48 additions & 0 deletions Circles.Index.CirclesV1/DatabaseSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -109,6 +110,53 @@ null as ""token""
")
};

/// <summary>
/// All Circles v1 hub transfers + personal minting
/// </summary>
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>
{
Expand Down
55 changes: 55 additions & 0 deletions Circles.Index.CirclesViews/DatabaseSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
{
Expand All @@ -106,6 +157,10 @@ null as ""cidV0Digest""
{
("V_Crc", "TrustRelations"),
TrustRelations
},
{
("V_Crc", "Transfers"),
Transfers
}
};
}
12 changes: 6 additions & 6 deletions Circles.Index.Common/IDatabaseQueryResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Circles.Index.Common;

[JsonConverter(typeof(DatabaseQueryResultConverter))]
public record DatabaseQueryResult(
string[] columns,
IEnumerable<object?[]> rows);
string[] Columns,
IEnumerable<object?[]> Rows);

public class DatabaseQueryResultConverter : JsonConverter<DatabaseQueryResult>
{
Expand All @@ -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)
Expand Down
37 changes: 14 additions & 23 deletions Circles.Index.Rpc/CirclesRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Task<ResultWrapper<CirclesTrustRelations>> circles_getTrustRelations(Addr
var incomingTrusts = new List<CirclesTrustRelation>();
var outgoingTrusts = new List<CirclesTrustRelation>();

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() ??
Expand Down Expand Up @@ -163,28 +163,19 @@ public ResultWrapper<DatabaseQueryResult> 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<DatabaseQueryResult>.Success(result);
}
Expand All @@ -208,7 +199,7 @@ private IEnumerable<Address> 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"))
);
Expand Down Expand Up @@ -305,7 +296,7 @@ private IEnumerable<UInt256> 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"))
);
Expand Down
72 changes: 72 additions & 0 deletions general-example-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9238c88

Please sign in to comment.