Skip to content

Commit

Permalink
Code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
krzlabrdx committed Aug 14, 2024
1 parent ffe0ee3 commit ab22a07
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public enum EntityRelationship
ValidatorToLockedOwnerStakeUnitVault,
ValidatorToPendingOwnerStakeUnitUnlockVault,

StakeVaultOfValidator,
StakeUnitOfValidator,
ClaimTokenOfValidator,

// Account Lockers (used on related Vaults and KeyValueStores)
Expand All @@ -97,7 +97,7 @@ public enum EntityRelationship
ResourcePoolToResource,
ResourcePoolToResourceVault,

UnitVaultOfResourcePool,
UnitResourceOfResourcePool,
ResourceVaultOfResourcePool,

// Access Controllers
Expand Down
29 changes: 1 addition & 28 deletions src/RadixDlt.NetworkGateway.Abstractions/Numerics/TokenAmount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ namespace RadixDlt.NetworkGateway.Abstractions.Numerics;

public static readonly TokenAmount Zero;
public static readonly TokenAmount NaN;
public static readonly TokenAmount OneFullUnit;

private const int DecimalPrecision = 18;

Expand All @@ -86,7 +85,6 @@ static TokenAmount()

Zero = new TokenAmount(0);
NaN = new TokenAmount(true);
OneFullUnit = new TokenAmount(_divisor);
}

private readonly BigInteger _subUnits;
Expand Down Expand Up @@ -270,41 +268,16 @@ public int CompareTo(TokenAmount other)
return isNaNComparison != 0 ? isNaNComparison : _subUnits.CompareTo(other._subUnits);
}

// Heavily inspired by https://www.codeproject.com/Articles/5366079/BigDecimal-in-Csharp
// Licensed under CPOL: https://en.wikipedia.org/wiki/Code_Project_Open_License
// Author: Paulo Francisco Zemek. August, 01, 2023.
private static TokenAmount Divide(TokenAmount dividend, TokenAmount divisor)
{
if (divisor == Zero)
{
// This rule might look odd, but when simplifying expressions, x/x (x divided by x) is 1.
// So, to keep the rule true, 0 divided by 0 is also 1.
if (dividend == Zero)
{
return OneFullUnit;
}

throw new DivideByZeroException($"{nameof(divisor)} can only be zero if {nameof(dividend)} is zero.");
return NaN;
}

var doublePrecisionDividendSubUnits = dividend._subUnits * _divisor;
var divisorSubUnits = divisor._subUnits;

return FromSubUnits(doublePrecisionDividendSubUnits / divisorSubUnits);
}

// Is there a faster approach that works with BigIntegers?
// It seems Log10 isn't faster at all.
private static int CountDigits(BigInteger value)
{
int count = 0;

while (value > 0)
{
count++;
value /= 10;
}

return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void ScanUpsert(CoreModel.Substate substateData, ReferencedEntity referen

_referencedEntities.Get((EntityAddress)validator.Value.StakeUnitResourceAddress).PostResolveConfigureLow((ResourceEntity ue) =>
{
ue.AddCorrelation(EntityRelationship.StakeVaultOfValidator, e.Id);
ue.AddCorrelation(EntityRelationship.StakeUnitOfValidator, e.Id);
});
});
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public void ScanUpsert(CoreModel.Substate substateData, ReferencedEntity referen

_referencedEntities.GetByDatabaseId(poolUnitResourceEntity.DatabaseId).PostResolveConfigureLow((ResourceEntity ue) =>
{
ue.AddCorrelation(EntityRelationship.UnitVaultOfResourcePool, e.Id);
ue.AddCorrelation(EntityRelationship.UnitResourceOfResourcePool, e.Id);
});

var vault = oneResourcePool.Value.Vault;
Expand All @@ -180,7 +180,7 @@ public void ScanUpsert(CoreModel.Substate substateData, ReferencedEntity referen

_referencedEntities.GetByDatabaseId(poolUnitResourceEntity.DatabaseId).PostResolveConfigureLow((ResourceEntity ue) =>
{
ue.AddCorrelation(EntityRelationship.UnitVaultOfResourcePool, e.Id);
ue.AddCorrelation(EntityRelationship.UnitResourceOfResourcePool, e.Id);
});

foreach (var poolVault in twoResourcePool.Value.Vaults)
Expand All @@ -206,7 +206,7 @@ public void ScanUpsert(CoreModel.Substate substateData, ReferencedEntity referen

_referencedEntities.GetByDatabaseId(poolUnitResourceEntity.DatabaseId).PostResolveConfigureLow((ResourceEntity ue) =>
{
ue.AddCorrelation(EntityRelationship.UnitVaultOfResourcePool, e.Id);
ue.AddCorrelation(EntityRelationship.UnitResourceOfResourcePool, e.Id);
});

foreach (var vault in multiResourcePool.Value.Vaults)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@
* permissions under this License.
*/

using System;
using System.Collections.Generic;
using System.Numerics;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using RadixDlt.NetworkGateway.Abstractions.Model;
using RadixDlt.NetworkGateway.Abstractions.StandardMetadata;
using RadixDlt.NetworkGateway.PostgresIntegration.Models;
using System;
using System.Collections.Generic;
using System.Numerics;

#nullable disable

Expand All @@ -85,7 +85,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
.Annotation("Npgsql:Enum:account_default_deposit_rule", "accept,reject,allow_existing")
.Annotation("Npgsql:Enum:account_resource_preference_rule", "allowed,disallowed")
.Annotation("Npgsql:Enum:authorized_depositor_badge_type", "resource,non_fungible")
.Annotation("Npgsql:Enum:entity_relationship", "component_to_instantiating_package,vault_to_resource,royalty_vault_of_component,validator_to_stake_vault,validator_to_pending_xrd_withdraw_vault,validator_to_locked_owner_stake_unit_vault,validator_to_pending_owner_stake_unit_unlock_vault,stake_vault_of_validator,claim_token_of_validator,account_locker_of_locker,account_locker_of_account,resource_pool_to_unit_resource,resource_pool_to_resource,resource_pool_to_resource_vault,unit_vault_of_resource_pool,resource_vault_of_resource_pool,access_controller_to_recovery_badge,recovery_badge_of_access_controller")
.Annotation("Npgsql:Enum:entity_relationship", "component_to_instantiating_package,vault_to_resource,royalty_vault_of_component,validator_to_stake_vault,validator_to_pending_xrd_withdraw_vault,validator_to_locked_owner_stake_unit_vault,validator_to_pending_owner_stake_unit_unlock_vault,stake_unit_of_validator,claim_token_of_validator,account_locker_of_locker,account_locker_of_account,resource_pool_to_unit_resource,resource_pool_to_resource,resource_pool_to_resource_vault,unit_resource_of_resource_pool,resource_vault_of_resource_pool,access_controller_to_recovery_badge,recovery_badge_of_access_controller")
.Annotation("Npgsql:Enum:entity_type", "global_consensus_manager,global_fungible_resource,global_non_fungible_resource,global_generic_component,internal_generic_component,global_account_component,global_package,internal_key_value_store,internal_fungible_vault,internal_non_fungible_vault,global_validator,global_access_controller,global_identity,global_one_resource_pool,global_two_resource_pool,global_multi_resource_pool,global_transaction_tracker,global_account_locker")
.Annotation("Npgsql:Enum:ledger_transaction_manifest_class", "general,transfer,validator_stake,validator_unstake,validator_claim,account_deposit_settings_update,pool_contribution,pool_redemption")
.Annotation("Npgsql:Enum:ledger_transaction_marker_event_type", "withdrawal,deposit")
Expand Down
Loading

0 comments on commit ab22a07

Please sign in to comment.