Skip to content

Commit

Permalink
code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelPawelec-RDX committed Aug 14, 2024
1 parent d6888ba commit 5bb2889
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ void AggregateEntityResourceVaultInternal(long entityId, long resourceEntityId,
}
}

var resourceOwnersToAdd = new Dictionary<(long EntityId, long ResourceEntityId), ResourceOwners>();
var resourceOwnersToAdd = new Dictionary<ResourceOwnersLookup, ResourceOwners>();

foreach (var x in entityResourceAggregatedVaultsHistoryToAdd)
{
Expand All @@ -1452,7 +1452,7 @@ void AggregateEntityResourceVaultInternal(long entityId, long resourceEntityId,
};

resourceOwnersToAdd.AddOrUpdate(
(x.EntityId, x.ResourceEntityId),
new ResourceOwnersLookup(x.EntityId, x.ResourceEntityId),
_ => new ResourceOwners
{
Id = sequences.ResourceOwnersSequence++,
Expand Down Expand Up @@ -1523,7 +1523,7 @@ void AggregateEntityResourceVaultInternal(long entityId, long resourceEntityId,
rowsInserted += await writeHelper.CopyResourceEntitySupplyHistory(resourceEntitySupplyHistoryToAdd, token);
rowsInserted += await writeHelper.CopyNonFungibleDataSchemaHistory(nonFungibleSchemaHistoryToAdd, token);
rowsInserted += await writeHelper.CopyKeyValueStoreSchemaHistory(keyValueStoreSchemaHistoryToAdd, token);
rowsInserted += await writeHelper.CopyResourceOwners(resourceOwnersToAdd.Select(x => x.Value).ToList(), token);
rowsInserted += await writeHelper.CopyResourceOwners(resourceOwnersToAdd.Values, token);

rowsInserted += await entityStateProcessor.SaveEntities();
rowsInserted += await entityMetadataProcessor.SaveEntities();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,5 @@ internal record struct EntityResourceVaultLookup(long EntityId, long ResourceEnt
internal record struct NonFungibleStoreLookup(long NonFungibleEntityId, long StateVersion);

internal record struct NonFungibleIdLookup(long ResourceEntityId, string NonFungibleId);

internal record struct ResourceOwnersLookup(long ResourceEntityId, long EntityId);

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ public ExtensionsQuerier(ReadOnlyDbContext dbContext, IDapperWrapper dapperWrapp

var totalCount = await _dbContext.ResourceOwners.CountAsync(x => x.ResourceEntityId == resourceEntity.Id, token);

var cd = new CommandDefinition(
commandText: @"
var entriesAndOneMore = await _dapperWrapper.ToList<ResourceOwnersViewModel>(
_dbContext,
@"
SELECT
ro.id as Id,
e.address AS EntityAddress,
Expand All @@ -123,30 +124,31 @@ FROM resource_owners ro
INNER JOIN entities e
ON ro.entity_id = e.id
WHERE ro.resource_entity_id = @resourceEntityId
AND (@balanceBoundary is null OR ((ro.balance, ro.id) < (Cast(@balanceBoundary AS numeric(1000,0)), @idBoundary)))
AND (@balanceBoundary is null OR ((ro.balance, ro.id) <= (Cast(@balanceBoundary AS numeric(1000,0)), @idBoundary)))
ORDER BY (ro.balance, ro.entity_id) DESC
LIMIT @limit",
parameters: new
new
{
resourceEntityId = resourceEntity.Id,
balanceBoundary = cursor?.BalanceBoundary,
idBoundary = cursor?.IdBoundary ?? long.MaxValue,
limit = limit,
limit = limit + 1,
},
cancellationToken: token);
token);

var result = (await _dapperWrapper.QueryAsync<ResourceOwnersViewModel>(_dbContext.Database.GetDbConnection(), cd)).ToList();
var lastElement = entriesAndOneMore.LastOrDefault();
var nextPageExists = entriesAndOneMore.Count == limit + 1 && lastElement != null;

var lastElement = result.LastOrDefault();
var nextCursor = lastElement != null
? new GatewayModel.ResourceOwnersCursor(lastElement.Id, TokenAmount.FromDecimalString(lastElement.Balance).ToString()).ToCursorString()
var nextCursor = nextPageExists
? new GatewayModel.ResourceOwnersCursor(lastElement!.Id, TokenAmount.FromDecimalString(lastElement.Balance).ToString()).ToCursorString()
: null;

switch (resourceEntity)
{
case GlobalFungibleResourceEntity:
{
var castedResult = result
var castedResult = entriesAndOneMore
.Take(limit)
.Select(
x => (GatewayModel.ResourceOwnersCollectionItem)new GatewayModel.ResourceOwnersCollectionFungibleResourceItem(
amount: TokenAmount.FromSubUnitsString(x.Balance).ToString(),
Expand All @@ -159,7 +161,8 @@ ORDER BY (ro.balance, ro.entity_id) DESC

case GlobalNonFungibleResourceEntity:
{
var castedResult = result
var castedResult = entriesAndOneMore
.Take(limit)
.Select(
x => (GatewayModel.ResourceOwnersCollectionItem)new GatewayModel.ResourceOwnersCollectionNonFungibleResourceItem(
nonFungibleIdsCount: long.Parse(TokenAmount.FromSubUnitsString(x.Balance).ToString()),
Expand Down

0 comments on commit 5bb2889

Please sign in to comment.