Skip to content

Commit

Permalink
Merge pull request #14 from CirclesUBI/feature/circles-get-trust-rela…
Browse files Browse the repository at this point in the history
…tions

implemented circles_getTrustRelations
  • Loading branch information
jaensen authored May 18, 2024
2 parents 7eb817b + c4a30a6 commit 207d011
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
59 changes: 55 additions & 4 deletions Circles.Index.Rpc/CirclesRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ namespace Circles.Index.Rpc;
public class CirclesRpcModule : ICirclesRpcModule
{
private readonly ILogger _pluginLogger;

private readonly Context _indexerContext;
// private readonly IJsonRpcConfig? _jsonRpcConfig;
// private readonly TimeSpan _cancellationTokenTimeout;

public CirclesRpcModule(Context indexerContext)
{
ILogger baseLogger = indexerContext.NethermindApi.LogManager.GetClassLogger();
_pluginLogger = new LoggerWithPrefix("Circles.Index.Rpc:", baseLogger);
_indexerContext = indexerContext;
// _cancellationTokenTimeout = TimeSpan.FromMilliseconds(_jsonRpcConfig.Timeout);
}

public async Task<ResultWrapper<string>> circles_getTotalBalance(Address address)
Expand All @@ -38,6 +34,61 @@ public async Task<ResultWrapper<string>> circles_getTotalBalance(Address address
return ResultWrapper<string>.Success(totalBalance.ToString(CultureInfo.InvariantCulture));
}

public Task<ResultWrapper<CirclesTrustRelations>> circles_getTrustRelations(Address address)
{
var sql = @"
select ""user"",
""canSendTo"",
""limit""
from (
select ""blockNumber"",
""transactionIndex"",
""logIndex"",
""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 (""user"" = @address
or ""canSendTo"" = @address)
";

var parameterizedSql = new ParameterizedSql(sql, new[]
{
_indexerContext.Database.CreateParameter("address", address.ToString(true, false))
});

var result = _indexerContext.Database.Select(parameterizedSql);

var incomingTrusts = new List<CirclesTrustRelation>();
var outgoingTrusts = new List<CirclesTrustRelation>();

Console.WriteLine($"result.Rows.Count: {result.Rows.Count()}");

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() ??
throw new Exception("A canSendTo in the result set is null"));
var limit = int.Parse(resultRow[2].ToString() ?? throw new Exception("A limit in the result set is null"));

if (user == address)
{
// user is the sender
outgoingTrusts.Add(new CirclesTrustRelation(canSendTo, limit));
}
else
{
// user is the receiver
incomingTrusts.Add(new CirclesTrustRelation(user, limit));
}
}
var trustRelations = new CirclesTrustRelations(address, outgoingTrusts.ToArray(), incomingTrusts.ToArray());
return Task.FromResult(ResultWrapper<CirclesTrustRelations>.Success(trustRelations));
}

public async Task<ResultWrapper<CirclesTokenBalance[]>> circles_getTokenBalances(Address address)
{
using RentedEthRpcModule rentedEthRpcModule = new(_indexerContext.NethermindApi);
Expand Down
8 changes: 8 additions & 0 deletions Circles.Index.Rpc/ICirclesRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace Circles.Index.Rpc;

public record CirclesTokenBalance(Address Token, string Balance);

public record CirclesTrustRelation(Address User, int limit);

public record CirclesTrustRelations(Address User, CirclesTrustRelation[] Trusts, CirclesTrustRelation[] TrustedBy);

#endregion

[RpcModule("Circles")]
Expand All @@ -18,6 +22,10 @@ public interface ICirclesRpcModule : IRpcModule
[JsonRpcMethod(Description = "Gets the Circles balance of the specified address", IsImplemented = true)]
Task<ResultWrapper<string>> circles_getTotalBalance(Address address);

[JsonRpcMethod(Description = "This method allows you to query all (v1) trust relations of an address",
IsImplemented = true)]
Task<ResultWrapper<CirclesTrustRelations>> circles_getTrustRelations(Address address);

[JsonRpcMethod(Description = "Gets the balance of each Circles token the specified address holds",
IsImplemented = true)]
Task<ResultWrapper<CirclesTokenBalance[]>> circles_getTokenBalances(Address address);
Expand Down

0 comments on commit 207d011

Please sign in to comment.