Skip to content

Commit

Permalink
Add common view for v1 and v2 avatars and their trust connections
Browse files Browse the repository at this point in the history
  • Loading branch information
jaensen committed May 19, 2024
1 parent 0ce2f94 commit 2068419
Show file tree
Hide file tree
Showing 16 changed files with 481 additions and 16 deletions.
82 changes: 82 additions & 0 deletions Circles.Index.CirclesV1/DatabaseSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,80 @@ public class DatabaseSchema : IDatabaseSchema
new("amount", ValueTypes.BigInt, false)
]);

public static readonly EventSchema TrustRelations = new("V_CrcV1", "TrustRelations", 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("user", ValueTypes.Address, true),
new("canSendTo", ValueTypes.Address, true),
new("limit", ValueTypes.Int, false),
])
{
SqlMigrationItem = new SqlMigrationItem(@"
create or replace view ""V_CrcV1_TrustRelations"" as
select ""blockNumber"",
""timestamp"",
""transactionIndex"",
""logIndex"",
""transactionHash"",
""user"",
""canSendTo"",
""limit""
from (
select ""blockNumber"",
""timestamp"",
""transactionIndex"",
""logIndex"",
""transactionHash"",
""user"",
""canSendTo"",
""limit"",
row_number() over (partition by ""user"", ""canSendTo"" order by ""blockNumber"" desc, ""transactionIndex"" desc, ""logIndex"" desc) as ""rn""
from ""CrcV1_Trust""
) t
where ""rn"" = 1
and ""limit"" > 0
order by ""blockNumber"" desc, ""transactionIndex"" desc, ""logIndex"" desc;
")
};

public static readonly EventSchema Avatars = new("V_CrcV1", "Avatars", 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("user", ValueTypes.Address, true),
new("token", ValueTypes.Address, true),
])
{
SqlMigrationItem = new SqlMigrationItem(@"
create or replace view ""V_CrcV1_Avatars"" as
select ""blockNumber"",
""timestamp"",
""transactionIndex"",
""logIndex"",
""transactionHash"",
'human' as ""type"",
""user"",
""token""
from ""CrcV1_Signup""
union all
select ""blockNumber"",
""timestamp"",
""transactionIndex"",
""logIndex"",
""transactionHash"",
'organization' as ""type"",
""organization"",
null as ""token""
from ""CrcV1_OrganizationSignup"";
")
};

public IDictionary<(string Namespace, string Table), EventSchema> Tables { get; } =
new Dictionary<(string Namespace, string Table), EventSchema>
{
Expand All @@ -57,6 +131,14 @@ public class DatabaseSchema : IDatabaseSchema
{
("CrcV1", "Transfer"),
Transfer
},
{
("V_CrcV1", "TrustRelations"),
TrustRelations
},
{
("V_CrcV1", "Avatars"),
Avatars
}
};

Expand Down
4 changes: 2 additions & 2 deletions Circles.Index.CirclesV2/DatabaseSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Circles.Index.CirclesV2;

/*
TODO: It looks like the new Hub doesn't have an equivalent to the 'HubTransfer' event
hub/
Hub.sol:
event PersonalMint(address indexed human, uint256 amount, uint256 startPeriod, uint256 endPeriod);
Expand All @@ -22,7 +22,7 @@ namespace Circles.Index.CirclesV2;
Manual events:
event TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values);
TODO:
TODO: Add the 'Lift' contract events
lift/
DemurrageCircles.sol:
event DepositDemurraged(address indexed account, uint256 amount, uint256 inflationaryAmount);
Expand Down
13 changes: 13 additions & 0 deletions Circles.Index.CirclesViews/Circles.Index.CirclesViews.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Circles.Index.Common\Circles.Index.Common.csproj" />
</ItemGroup>

</Project>
111 changes: 111 additions & 0 deletions Circles.Index.CirclesViews/DatabaseSchema.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Circles.Index.Common;

namespace Circles.Index.CirclesViews;

public class DatabaseSchema : IDatabaseSchema
{
public ISchemaPropertyMap SchemaPropertyMap { get; } = new SchemaPropertyMap();

public IEventDtoTableMap EventDtoTableMap { get; } = new EventDtoTableMap();

public static readonly EventSchema TrustRelations = new("V_Crc", "TrustRelations", 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("version", ValueTypes.Int, false),
new("trustee", ValueTypes.String, false),
new("truster", ValueTypes.String, false),
new("expiryTime", ValueTypes.Int, false),
new("limit", ValueTypes.Int, false)
])
{
SqlMigrationItem = new SqlMigrationItem(@"
create or replace view ""V_Crc_TrustRelations"" as
select ""blockNumber"",
""timestamp"",
""transactionIndex"",
""logIndex"",
""transactionHash"",
2 as ""version"",
""trustee"",
""truster"",
""expiryTime"",
null as ""limit""
from ""V_CrcV2_TrustRelations""
union all
select ""blockNumber"",
""timestamp"",
""transactionIndex"",
""logIndex"",
""transactionHash"",
1 as ""version"",
""user"",
""canSendTo"",
null as ""expiryTime"",
""limit""
from ""V_CrcV1_TrustRelations"";
")
};

public static readonly EventSchema Avatars = new("V_Crc", "Avatars", 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("version", ValueTypes.Int, false),
new("type", ValueTypes.String, false),
new("invitedBy", ValueTypes.String, false),
new("avatar", ValueTypes.String, false),
new("tokenId", ValueTypes.String, false),
new("name", ValueTypes.String, false),
new("cidV0Digest", ValueTypes.Bytes, false),
])
{
SqlMigrationItem = new SqlMigrationItem(@"
create or replace view ""V_Crc_Avatars"" as
select ""blockNumber"",
""timestamp"",
""transactionIndex"",
""logIndex"",
""transactionHash"",
2 as ""version"",
""type"",
""invitedBy"",
""avatar"",
""tokenId"",
""name"",
""cidV0Digest""
from ""V_CrcV2_Avatars""
union all
select ""blockNumber"",
""timestamp"",
""transactionIndex"",
""logIndex"",
""transactionHash"",
1 as ""version"",
""type"",
null as ""invitedBy"",
""user"" as ""avatar"",
""token"" as ""tokenId"",
null as ""name"",
null as ""cidV0Digest""
from ""V_CrcV1_Avatars"";
")
};

public IDictionary<(string Namespace, string Table), EventSchema> Tables { get; } =
new Dictionary<(string Namespace, string Table), EventSchema>
{
{
("V_Crc", "Avatars"),
Avatars
},
{
("V_Crc", "TrustRelations"),
TrustRelations
}
};
}
11 changes: 6 additions & 5 deletions Circles.Index.Postgres/PostgresDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,16 @@ public DatabaseQueryResult Select(ParameterizedSql select)
{
if (resultSchema[i].NpgsqlDbType == NpgsqlDbType.Numeric)
{
row[i] = reader.GetFieldValue<BigInteger>(i);
row[i] = reader.GetFieldValue<BigInteger?>(i);
}
else
{
row[i] = reader.GetValue(i);
if (row[i] is DBNull)
{
row[i] = null;
}
}

if (row[i] is DBNull)
{
row[i] = null;
}
}

Expand Down
1 change: 1 addition & 0 deletions Circles.Index/Circles.Index.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<ItemGroup>
<ProjectReference Include="..\Circles.Index.CirclesV2.NameRegistry\Circles.Index.CirclesV2.NameRegistry.csproj" />
<ProjectReference Include="..\Circles.Index.CirclesViews\Circles.Index.CirclesViews.csproj" />
<ProjectReference Include="..\Circles.Index.Common\Circles.Index.Common.csproj" />
<ProjectReference Include="..\Circles.Index.Postgres\Circles.Index.Postgres.csproj" />
<ProjectReference Include="..\Circles.Index.CirclesV1\Circles.Index.CirclesV1.csproj" />
Expand Down
3 changes: 2 additions & 1 deletion Circles.Index/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public async Task Init(INethermindApi nethermindApi)
IDatabaseSchema v1 = new CirclesV1.DatabaseSchema();
IDatabaseSchema v2 = new CirclesV2.DatabaseSchema();
IDatabaseSchema v2NameRegistry = new CirclesV2.NameRegistry.DatabaseSchema();
IDatabaseSchema databaseSchema = new CompositeDatabaseSchema([common, v1, v2, v2NameRegistry]);
IDatabaseSchema circlesViews = new CirclesViews.DatabaseSchema();
IDatabaseSchema databaseSchema = new CompositeDatabaseSchema([common, v1, v2, v2NameRegistry, circlesViews]);

ILogger baseLogger = nethermindApi.LogManager.GetClassLogger();
ILogger pluginLogger = new LoggerWithPrefix($"{Name}: ", baseLogger);
Expand Down
15 changes: 15 additions & 0 deletions Circles.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docker", "docker", "{B30CB7
add-deploy-script-tp-v2-repo.sh = add-deploy-script-tp-v2-repo.sh
v2-example-requests.md = v2-example-requests.md
v1-example-requests.md = v1-example-requests.md
general-example-requests.md = general-example-requests.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Circles.Index.Postgres", "Circles.Index.Postgres\Circles.Index.Postgres.csproj", "{8ED5FCD0-0995-44E7-9A11-E0EB03BB0832}"
Expand All @@ -51,6 +52,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Circles.Index.Query", "Circ
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Circles.Index.Query.Tests", "Circles.Index.Query.Tests\Circles.Index.Query.Tests.csproj", "{A4BFF176-178E-4C8B-A7C1-DC7240F91E0D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Schemas", "Schemas", "{64189094-62E5-48CA-BD66-1A7B82263BA8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Circles.Index.CirclesViews", "Circles.Index.CirclesViews\Circles.Index.CirclesViews.csproj", "{265A4516-DC59-4D8B-B097-36902108B143}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -93,5 +98,15 @@ Global
{A4BFF176-178E-4C8B-A7C1-DC7240F91E0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4BFF176-178E-4C8B-A7C1-DC7240F91E0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4BFF176-178E-4C8B-A7C1-DC7240F91E0D}.Release|Any CPU.Build.0 = Release|Any CPU
{265A4516-DC59-4D8B-B097-36902108B143}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{265A4516-DC59-4D8B-B097-36902108B143}.Debug|Any CPU.Build.0 = Debug|Any CPU
{265A4516-DC59-4D8B-B097-36902108B143}.Release|Any CPU.ActiveCfg = Release|Any CPU
{265A4516-DC59-4D8B-B097-36902108B143}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2B7D2126-6D6F-4A5B-81A1-3E5F1A9645F6} = {64189094-62E5-48CA-BD66-1A7B82263BA8}
{FF8E96F7-2903-4C0B-A443-19EA8D7FBB67} = {64189094-62E5-48CA-BD66-1A7B82263BA8}
{33D651AB-1C61-4A06-9C02-A144E7342317} = {64189094-62E5-48CA-BD66-1A7B82263BA8}
{265A4516-DC59-4D8B-B097-36902108B143} = {64189094-62E5-48CA-BD66-1A7B82263BA8}
EndGlobalSection
EndGlobal
17 changes: 13 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ query [Circles](https://www.aboutcircles.com/) protocol events.

**Query a node**

If you're just looking for a way to query Circles events, you can check out the query examples in
the [v1-example-requests.md](v1-example-requests.md) and [v2-example-requests.md](v2-example-requests.md) files.
If you're just looking for a way to query Circles events, you can check out the query examples:

* [General examples](general-example-requests.md)
* [Circles v1 examples](v1-example-requests.md)
* [Circles v2 examples](v2-example-requests.md)

For a detailed description of the available RPC methods, see the [Circles RPC methods](#circles-rpc-methods) section.

Expand Down Expand Up @@ -216,7 +219,7 @@ docker compose -f docker-compose.spaceneth.yml up

## Circles RPC methods

The plugin extends the Nethermind JSON-RPC API with additional methods to query Circles events and aggregate values.
The plugin extends the Nethermind JSON-RPC API with additional methods to query Circles events and aggregate values.

You can find concrete examples for all rpc-methods in the [v1-example-requests.md](v1-example-requests.md)
and [v2-example-requests.md](v2-example-requests.md) files.
Expand Down Expand Up @@ -282,10 +285,16 @@ Namespaces and tables:
* `UpdateMetadataDigest`
* `URI`
* `CidV0` (predecessor of `URI` and `UpdateMetadataDigest`)
* `V_CrcV1`
* `Avatars` (view combining `Signup` and `OrganizationSignup`)
* `TrustRelations` (view filtered to represent all current `Trust` relations)
* `V_CrcV2`
* `Transfers` (view combining `TransferBatch` and `TransferSingle`)
* `Avatars` (view combining `RegisterHuman`, `InviteHuman`, `RegisterGroup` and `RegisterOrganization`)
* `TrustRelations` (view filtered to represent all current `Trust` relations)
* `Transfers` (view combining `TransferBatch` and `TransferSingle`)
* `V_Crc`
* `Avatars` (view combining `V_CrcV1_Avatars` and `V_CrcV2_Avatars`)
* `TrustRelations` (view combining `V_CrcV1_TrustRelations` and `V_CrcV2_TrustRelations`)

#### Available filter types

Expand Down
1 change: 1 addition & 0 deletions arm64.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ COPY --from=build /circles-nethermind-plugin/Circles.Index.Common.dll /nethermin
COPY --from=build /circles-nethermind-plugin/Circles.Index.CirclesV1.dll /nethermind/plugins
COPY --from=build /circles-nethermind-plugin/Circles.Index.CirclesV2.dll /nethermind/plugins
COPY --from=build /circles-nethermind-plugin/Circles.Index.CirclesV2.NameRegistry.dll /nethermind/plugins
COPY --from=build /circles-nethermind-plugin/Circles.Index.CirclesViews.dll /nethermind/plugins
COPY --from=build /circles-nethermind-plugin/Circles.Index.Rpc.dll /nethermind/plugins
COPY --from=build /circles-nethermind-plugin/Circles.Index.Query.dll /nethermind/plugins
COPY --from=build /circles-nethermind-plugin/Nethermind.Int256.dll /nethermind/plugins
Expand Down
Loading

0 comments on commit 2068419

Please sign in to comment.