Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code clean-up #781

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Release built: _not released yet_
- Brand-new `two_way_linked_*` properties on the `details` property of the Resources, Accounts, Packages and other global entities.
- See https://docs.radixdlt.com/docs/metadata-for-verification#metadata-standards-for-verification-of-onledger-entities for detailed specification.
- Added support for the Native Resource Details in the `/state/entity/details` endpoint, returned when the `native_resource_details` opt-in is enabled.
- Brand-new `native_resource_details` property on the `details` property.
- Introduced a new `native_resource_details` property on the `details` object when looking up fungible or non-fungible resource entities with the entity details endpoint. This property is present when the resource has a special meaning to native blueprints, and gives extra information about the resource. For example, it identifies pool units with their linked pool, and gives the redemption value for a single unit.
- Includes **unit** redemption value for the Validator LSU token and the unit tokens of various Pools.
- Added new endpoint `/extensions/resource-holders/page` which returns information about all holders of the queried resource.

Expand Down
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
45 changes: 3 additions & 42 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;
public static readonly TokenAmount MaxValue;

private const int DecimalPrecision = 18;
Expand All @@ -84,10 +83,10 @@ namespace RadixDlt.NetworkGateway.Abstractions.Numerics;
static TokenAmount()
{
_divisor = BigInteger.Pow(10, DecimalPrecision);

MaxValue = new TokenAmount(BigInteger.Pow(2, 192), 0);
Zero = new TokenAmount(0);
NaN = new TokenAmount(true);
OneFullUnit = new TokenAmount(_divisor);
}

private readonly BigInteger _subUnits;
Expand Down Expand Up @@ -155,9 +154,9 @@ public static TokenAmount FromDecimalString(string decimalString)

public static TokenAmount operator -(TokenAmount a, TokenAmount b) => (a.IsNaN() || b.IsNaN()) ? NaN : new TokenAmount(a._subUnits - b._subUnits);

public static TokenAmount operator *(TokenAmount a, TokenAmount b) => (a.IsNaN() || b.IsNaN()) ? NaN : new TokenAmount(a._subUnits * b._subUnits);
public static TokenAmount operator *(TokenAmount a, TokenAmount b) => (a.IsNaN() || b.IsNaN()) ? NaN : new TokenAmount((a._subUnits * b._subUnits) / _divisor);

public static TokenAmount operator /(TokenAmount a, TokenAmount b) => (a.IsNaN() || b.IsNaN()) ? NaN : Divide(a, b);
public static TokenAmount operator /(TokenAmount a, TokenAmount b) => (a.IsNaN() || b.IsNaN() || b == Zero) ? NaN : new TokenAmount((a._subUnits * _divisor) / b._subUnits);

// ReSharper disable SimplifyConditionalTernaryExpression - As it's clearer as written
#pragma warning disable IDE0075
Expand Down Expand Up @@ -270,42 +269,4 @@ public int CompareTo(TokenAmount other)
var isNaNComparison = _isNaN.CompareTo(other._isNaN);
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.");
}

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,7 +62,7 @@
* permissions under this License.
*/

using System;
using System;
using System.Collections.Generic;
using System.Numerics;
using Microsoft.EntityFrameworkCore.Migrations;
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
Loading