Skip to content

Commit

Permalink
Added native resource details
Browse files Browse the repository at this point in the history
  • Loading branch information
krzlabrdx committed Aug 12, 2024
1 parent 18ce696 commit e7e527a
Show file tree
Hide file tree
Showing 39 changed files with 5,627 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,30 @@ public enum EntityRelationship
ValidatorPendingXrdWithdrawVault,
ValidatorLockedOwnerStakeUnitVault,
ValidatorPendingOwnerStakeUnitUnlockVault,
ValidatorStakeUnitValidator,
VaultResource,
VaultRoyalty,
VaultResourcePool,
AccountLockerLocker,
AccountLockerAccount,
ResourcePoolUnit,

/// <summary>
/// A Resource Pool to a Resource relationship.
/// </summary>
ResourcePoolResource,

/// <summary>
/// A Resource Pool to a Resource Vault relationship.
/// </summary>
ResourcePoolResourceVault,

/// <summary>
/// A Resource Pool to a Resource Pool Unit Resource relationship.
/// </summary>
ResourcePoolUnit,

/// <summary>
/// A Resource Pool Unit Resource to a Resource Pool relationship.
/// </summary>
ResourcePoolUnitResourcePool,
}
52 changes: 51 additions & 1 deletion src/RadixDlt.NetworkGateway.Abstractions/Numerics/TokenAmount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ 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 : Divide(a, b);

// ReSharper disable SimplifyConditionalTernaryExpression - As it's clearer as written
#pragma warning disable IDE0075
Expand Down Expand Up @@ -269,4 +269,54 @@ 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;
var doublePrecisionResult = doublePrecisionDividendSubUnits / divisorSubUnits;

var decimalIndexFromEnd = DecimalPrecision;
var digitCount = CountDigits(doublePrecisionResult);

while (decimalIndexFromEnd > 0 && (doublePrecisionResult % 10) == 0)
{
decimalIndexFromEnd--;
doublePrecisionResult /= 10;
digitCount--;
}

// TODO this is wrong! we must
return FromSubUnits(doublePrecisionResult);
}

// 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;
}
}
188 changes: 188 additions & 0 deletions src/RadixDlt.NetworkGateway.GatewayApi/gateway-api-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4094,6 +4094,8 @@ components:
$ref: "#/components/schemas/BigDecimal"
two_way_linked_dapps:
$ref: "#/components/schemas/TwoWayLinkedDappsCollection"
native_resource_details:
$ref: "#/components/schemas/NativeResourceDetails"
StateEntityDetailsResponseNonFungibleResourceDetails:
allOf:
- $ref: "#/components/schemas/StateEntityDetailsResponseItemDetails"
Expand Down Expand Up @@ -4122,6 +4124,8 @@ components:
type: string
two_way_linked_dapps:
$ref: "#/components/schemas/TwoWayLinkedDappsCollection"
native_resource_details:
$ref: "#/components/schemas/NativeResourceDetails"
StateEntityDetailsResponseFungibleVaultDetails:
allOf:
- $ref: "#/components/schemas/StateEntityDetailsResponseItemDetails"
Expand Down Expand Up @@ -4216,6 +4220,8 @@ components:
two_way_linked_dapp_details:
description: Two-way linked (resolved & verified) dApp details. Applicable to dApp definition accounts only and limited to on-ledger information only.
$ref: "#/components/schemas/TwoWayLinkedDappOnLedgerDetails"
native_resource_details:
$ref: "#/components/schemas/NativeResourceDetails"

StateAccountResourcePreferencesPageRequest:
allOf:
Expand Down Expand Up @@ -5017,6 +5023,188 @@ components:
items:
type: string

#
# Native Resources
#

NativeResourceUnitRedemptionValue:
type: array
items:
$ref: "#/components/schemas/NativeResourceRedemptionValueItem"
NativeResourceRedemptionValueItem:
type: object
required:
- resource_address
properties:
resource_address:
$ref: "#/components/schemas/Address"
amount:
$ref: "#/components/schemas/BigDecimal"

NativeResourceKind:
type: string
enum:
- Xrd
- PackageOwnerBadge
- AccountOwnerBadge
- IdentityOwnerBadge
- ValidatorOwnerBadge
- Secp256k1SignatureResource
- Ed25519SignatureResource
- GlobalCallerResource
- PackageOfDirectCallerResource
- SystemExecutionResource
- ValidatorLiquidStakeUnit
- ValidatorClaimNFT
- OneResourcePoolUnit
- TwoResourcePoolUnit
- MultiResourcePoolUnit
- AccessControllerRecoveryBadge
NativeResourceDetails:
type: object
discriminator:
propertyName: kind
mapping:
Xrd: "#/components/schemas/NativeResourceXrdValue"
PackageOwnerBadge: "#/components/schemas/NativeResourcePackageOwnerBadgeValue"
AccountOwnerBadge: "#/components/schemas/NativeResourceAccountOwnerBadgeValue"
IdentityOwnerBadge: "#/components/schemas/NativeResourceIdentityOwnerBadgeValue"
ValidatorOwnerBadge: "#/components/schemas/NativeResourceValidatorOwnerBadgeValue"
Secp256k1SignatureResource: "#/components/schemas/NativeResourceSecp256k1SignatureResourceValue"
Ed25519SignatureResource: "#/components/schemas/NativeResourceEd25519SignatureResourceValue"
GlobalCallerResource: "#/components/schemas/NativeResourceGlobalCallerResourceValue"
PackageOfDirectCallerResource: "#/components/schemas/NativeResourcePackageOfDirectCallerResourceValue"
SystemExecutionResource: "#/components/schemas/NativeResourceSystemExecutionResourceValue"
ValidatorLiquidStakeUnit: "#/components/schemas/NativeResourceValidatorLiquidStakeUnitValue"
ValidatorClaimNFT: "#/components/schemas/NativeResourceValidatorClaimNFTValue"
OneResourcePoolUnit: "#/components/schemas/NativeResourceOneResourcePoolUnitValue"
TwoResourcePoolUnit: "#/components/schemas/NativeResourceTwoResourcePoolUnitValue"
MultiResourcePoolUnit: "#/components/schemas/NativeResourceMultiResourcePoolUnitValue"
AccessControllerRecoveryBadge: "#/components/schemas/NativeResourceAccessControllerRecoveryBadgeValue"
required:
- kind
properties:
kind:
$ref: "#/components/schemas/NativeResourceKind"
NativeResourceXrdValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
NativeResourcePackageOwnerBadgeValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
NativeResourceAccountOwnerBadgeValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
NativeResourceIdentityOwnerBadgeValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
NativeResourceValidatorOwnerBadgeValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
NativeResourceSecp256k1SignatureResourceValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
NativeResourceEd25519SignatureResourceValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
NativeResourceGlobalCallerResourceValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
NativeResourcePackageOfDirectCallerResourceValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
NativeResourceSystemExecutionResourceValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
NativeResourceValidatorLiquidStakeUnitValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
required:
- validator_address
- redemption_resource_count
- current_redemption_value
properties:
validator_address:
$ref: "#/components/schemas/Address"
redemption_resource_count:
type: integer
unit_redemption_value:
$ref: "#/components/schemas/NativeResourceUnitRedemptionValue"
NativeResourceValidatorClaimNFTValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
required:
- validator_address
properties:
validator_address:
$ref: "#/components/schemas/Address"
NativeResourceOneResourcePoolUnitValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
required:
- pool_address
- redemption_resource_count
- current_redemption_value
properties:
pool_address:
$ref: "#/components/schemas/Address"
redemption_resource_count:
type: integer
unit_redemption_value:
$ref: "#/components/schemas/NativeResourceUnitRedemptionValue"
NativeResourceTwoResourcePoolUnitValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
required:
- pool_address
- redemption_resource_count
- current_redemption_value
properties:
pool_address:
$ref: "#/components/schemas/Address"
redemption_resource_count:
type: integer
unit_redemption_value:
$ref: "#/components/schemas/NativeResourceUnitRedemptionValue"
NativeResourceMultiResourcePoolUnitValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
required:
- pool_address
- redemption_resource_count
- unit_redemption_value
properties:
pool_address:
$ref: "#/components/schemas/Address"
redemption_resource_count:
type: integer
unit_redemption_value:
$ref: "#/components/schemas/NativeResourceUnitRedemptionValue"
NativeResourceAccessControllerRecoveryBadgeValue:
allOf:
- $ref: "#/components/schemas/NativeResourceDetails"
- type: object
required:
- access_controller_address
properties:
access_controller_address:
$ref: "#/components/schemas/Address"

#
# Metadata Typed Value
#
Expand Down
Loading

0 comments on commit e7e527a

Please sign in to comment.