Skip to content

Commit

Permalink
Merge pull request #5 from aboutcircles/fix/argumentException-circles…
Browse files Browse the repository at this point in the history
…_getTokenBalances

fixed the parameter creation
  • Loading branch information
jaensen authored Jul 13, 2024
2 parents 96180fc + f482763 commit d4a7e4c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 0 additions & 2 deletions Circles.Index.CirclesV2.NameRegistry/LogParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public IEnumerable<IIndexEvent> ParseLog(Block block, TxReceipt receipt, LogEntr
yield break;
}

Console.WriteLine($"Event from NameRegistry: {log.Topics[0]}");

var topic = log.Topics[0];

if (topic == _registerShortNameTopic)
Expand Down
13 changes: 13 additions & 0 deletions Circles.Index.Query/FilterPredicate.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using System.Data;
using System.Text.Json;
using Circles.Index.Common;
Expand Down Expand Up @@ -40,6 +41,11 @@ private string FormatArrayParameter(object value, string parameterName)
return string.Join(", ", jsonElement.EnumerateArray().Select((_, index) => $"{parameterName}_{index}"));
}

if (value is IEnumerable e)
{
return string.Join(", ", e.Cast<object>().Select((_, index) => $"{parameterName}_{index}"));
}

throw new ArgumentException("Value must be an IEnumerable for In/NotIn filter types. Is: " +
value?.GetType().Name);
}
Expand All @@ -53,6 +59,13 @@ private IEnumerable<IDbDataParameter> CreateParameters(IDatabaseUtils database,
.ToList();
}

if (value is IEnumerable e && value is not string)
{
return e.Cast<object>()
.Select((v, index) => database.CreateParameter($"{parameterName}_{index}", v.ToString()))
.ToList();
}

return new List<IDbDataParameter> { database.CreateParameter(parameterName, value) };
}
}
7 changes: 4 additions & 3 deletions Circles.Index.Rpc/CirclesRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ private IDictionary<string, string> GetTokenOwners(string[] tokenAddresses)
{
// Construct a query for "V_Crc_Avatars" and select the "avatar" and "tokenId" columns.
// Use an IN clause to filter the results by the token addresses.
var inFilterValues = tokenAddresses.Select(o => o.ToLowerInvariant()).ToArray();
var select = new Select(
"V_Crc"
, "Avatars"
, new[] { "avatar", "tokenId" }
, new[]
{
new FilterPredicate("tokenId", FilterType.In,
tokenAddresses.Select(o => o.ToLowerInvariant()).ToArray())
new FilterPredicate("tokenId", FilterType.In, inFilterValues)
}
, Array.Empty<OrderBy>()
, null
Expand All @@ -248,7 +248,8 @@ private IDictionary<string, string> GetTokenOwners(string[] tokenAddresses)
private List<CirclesTokenBalance> CirclesTokenBalances(IEthRpcModule rpcModule, Address address, bool asTimeCircles)
{
IEnumerable<Address> tokens = TokenAddressesForAccount(address).ToArray();
IDictionary<string, string> tokenOwners = GetTokenOwners(tokens.Select(o => o.ToString(true, false)).ToArray());
var tokenAddressesOfAccount = tokens.Select(o => o.ToString(true, false)).ToArray();
IDictionary<string, string> tokenOwners = GetTokenOwners(tokenAddressesOfAccount);

// Call the erc20's balanceOf function for each token using _ethRpcModule.eth_call():
byte[] functionSelector = Keccak.Compute("balanceOf(address)").Bytes.Slice(0, 4).ToArray();
Expand Down

0 comments on commit d4a7e4c

Please sign in to comment.