Skip to content

Commit

Permalink
Refactor balance conversion and calculation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Jun 9, 2024
1 parent bef4424 commit 06813ed
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 27 deletions.
26 changes: 14 additions & 12 deletions src/BeeNet/Models/BzzBalance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ namespace Etherna.BeeNet.Models
{
// Consts.
public const int DecimalPrecision = 16;
private static readonly decimal TenPowerDecimalPrecision =
(decimal)Math.Pow(10, DecimalPrecision);
public static readonly decimal PlursInBzz = (decimal)Math.Pow(10, DecimalPrecision);

// Fields.
private readonly decimal balance;
Expand All @@ -38,8 +37,7 @@ public BzzBalance(decimal balance)
public static BzzBalance FromDecimal(decimal value) => new(value);
public static BzzBalance FromDouble(double value) => new((decimal)value);
public static BzzBalance FromInt32(int value) => new(value);
public static BzzBalance FromPlurLong(long plurValue) =>
decimal.Divide(plurValue, TenPowerDecimalPrecision);
public static BzzBalance FromPlurLong(long plurValue) => decimal.Divide(plurValue, PlursInBzz);
public static BzzBalance FromPlurString(string plurValue) =>
FromPlurLong(long.Parse(plurValue, CultureInfo.InvariantCulture));

Expand All @@ -51,24 +49,28 @@ obj is BzzBalance xDaiObj &&
public bool Equals(BzzBalance other) => balance == other.balance;
public override int GetHashCode() => balance.GetHashCode();
public decimal ToDecimal() => balance;
public long ToPlurLong() => (long)decimal.Multiply(balance, TenPowerDecimalPrecision);
public string ToPlurString() => decimal.Multiply(balance, TenPowerDecimalPrecision).ToString(CultureInfo.InvariantCulture);
public long ToPlurLong() => (long)decimal.Multiply(balance, PlursInBzz);
public string ToPlurString() => decimal.Multiply(balance, PlursInBzz).ToString(CultureInfo.InvariantCulture);
public override string ToString() => balance.ToString(CultureInfo.InvariantCulture);

// Static methods.
public static BzzBalance Add(BzzBalance left, BzzBalance right) => left + right;
public static BzzBalance Decrement(BzzBalance balance) => --balance;
public static BzzBalance Divide(BzzBalance left, BzzBalance right) => left / right;
public static decimal Divide(BzzBalance left, BzzBalance right) => left.balance / right.balance;
public static BzzBalance Divide(BzzBalance left, decimal right) => left.balance / right;
public static BzzBalance Increment(BzzBalance balance) => ++balance;
public static BzzBalance Multiply(BzzBalance left, BzzBalance right) => left * right;
public static BzzBalance Multiply(BzzBalance left, decimal right) => left.balance * right;
public static BzzBalance Multiply(decimal left, BzzBalance right) => left * right.balance;
public static BzzBalance Negate(BzzBalance balance) => -balance;
public static BzzBalance Subtract(BzzBalance left, BzzBalance right) => left - right;

// Operator methods.
public static BzzBalance operator +(BzzBalance left, BzzBalance right) => new(left.balance + right.balance);
public static BzzBalance operator -(BzzBalance left, BzzBalance right) => new(left.balance - right.balance);
public static BzzBalance operator *(BzzBalance left, BzzBalance right) => new(left.balance * right.balance);
public static BzzBalance operator /(BzzBalance left, BzzBalance right) => new(left.balance / right.balance);
public static BzzBalance operator +(BzzBalance left, BzzBalance right) => left.balance + right.balance;
public static BzzBalance operator -(BzzBalance left, BzzBalance right) => left.balance - right.balance;
public static BzzBalance operator *(BzzBalance left, decimal right) => left.balance * right;
public static BzzBalance operator *(decimal left, BzzBalance right) => left * right.balance;
public static decimal operator /(BzzBalance left, BzzBalance right) => left.balance / right.balance;
public static BzzBalance operator /(BzzBalance left, decimal right) => left.balance / right;

public static bool operator ==(BzzBalance left, BzzBalance right) => left.Equals(right);
public static bool operator !=(BzzBalance left, BzzBalance right) => !(left == right);
Expand Down
23 changes: 23 additions & 0 deletions src/BeeNet/Models/GnosisChain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2021-present Etherna SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;

namespace Etherna.BeeNet.Models
{
public static class GnosisChain
{
public static readonly TimeSpan BlockTime = TimeSpan.FromSeconds(5);
}
}
16 changes: 13 additions & 3 deletions src/BeeNet/Models/PostageBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.BeeNet.Clients;
using System;
using System.Globalization;

namespace Etherna.BeeNet.Models
{
Expand Down Expand Up @@ -53,7 +53,7 @@ public PostageBatch(
}

// Internal constructors.
internal PostageBatch(Clients.Stamps batch)
internal PostageBatch(Stamps batch)
{
ArgumentNullException.ThrowIfNull(batch, nameof(batch));
if (batch.Depth is < MinDepth or > MaxDepth)
Expand All @@ -71,7 +71,7 @@ internal PostageBatch(Clients.Stamps batch)
Utilization = batch.Utilization;
}

internal PostageBatch(Clients.Response52 batch)
internal PostageBatch(Response52 batch)
{
ArgumentNullException.ThrowIfNull(batch, nameof(batch));
if (batch.Depth is < MinDepth or > MaxDepth)
Expand Down Expand Up @@ -146,5 +146,15 @@ internal PostageBatch(Clients.Response52 batch)
/// The count of the fullest bucket
/// </summary>
public uint Utilization { get; }

// Static methods.
public static BzzBalance CalculateAmount(BzzBalance chainPrice, TimeSpan ttl) =>
(decimal)(ttl / GnosisChain.BlockTime) * chainPrice;

public static BzzBalance CalculatePrice(BzzBalance amount, int depth) =>
amount * (decimal)Math.Pow(2, depth);

public static BzzBalance CalculatePrice(BzzBalance chainPrice, TimeSpan ttl, int depth) =>
CalculatePrice(CalculateAmount(chainPrice, ttl), depth);
}
}
26 changes: 14 additions & 12 deletions src/BeeNet/Models/XDaiBalance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ namespace Etherna.BeeNet.Models
{
// Consts.
public const int DecimalPrecision = 18;
private static readonly decimal TenPowerDecimalPrecision =
(decimal)Math.Pow(10, DecimalPrecision);
public static readonly decimal WeisInXDai = (decimal)Math.Pow(10, DecimalPrecision);

// Fields.
private readonly decimal balance;
Expand All @@ -38,8 +37,7 @@ public XDaiBalance(decimal balance)
public static XDaiBalance FromDecimal(decimal value) => new(value);
public static XDaiBalance FromDouble(double value) => new((decimal)value);
public static XDaiBalance FromInt32(int value) => new(value);
public static XDaiBalance FromWeiLong(long weiValue) =>
decimal.Divide(weiValue, TenPowerDecimalPrecision);
public static XDaiBalance FromWeiLong(long weiValue) => decimal.Divide(weiValue, WeisInXDai);
public static XDaiBalance FromWeiString(string weiValue) =>
FromWeiLong(long.Parse(weiValue, CultureInfo.InvariantCulture));

Expand All @@ -52,23 +50,27 @@ obj is XDaiBalance xDaiObj &&
public override int GetHashCode() => balance.GetHashCode();
public decimal ToDecimal() => balance;
public override string ToString() => balance.ToString(CultureInfo.InvariantCulture);
public long ToWeiLong()=> (long)decimal.Multiply(balance, TenPowerDecimalPrecision);
public string ToWeiString() => decimal.Multiply(balance, TenPowerDecimalPrecision).ToString(CultureInfo.InvariantCulture);
public long ToWeiLong()=> (long)decimal.Multiply(balance, WeisInXDai);
public string ToWeiString() => decimal.Multiply(balance, WeisInXDai).ToString(CultureInfo.InvariantCulture);

// Static methods.
public static XDaiBalance Add(XDaiBalance left, XDaiBalance right) => left + right;
public static XDaiBalance Decrement(XDaiBalance balance) => --balance;
public static XDaiBalance Divide(XDaiBalance left, XDaiBalance right) => left / right;
public static decimal Divide(XDaiBalance left, XDaiBalance right) => left.balance / right.balance;
public static XDaiBalance Divide(XDaiBalance left, decimal right) => left.balance / right;
public static XDaiBalance Increment(XDaiBalance balance) => ++balance;
public static XDaiBalance Multiply(XDaiBalance left, XDaiBalance right) => left * right;
public static XDaiBalance Multiply(XDaiBalance left, decimal right) => left.balance * right;
public static XDaiBalance Multiply(decimal left, XDaiBalance right) => left * right.balance;
public static XDaiBalance Negate(XDaiBalance balance) => -balance;
public static XDaiBalance Subtract(XDaiBalance left, XDaiBalance right) => left - right;

// Operator methods.
public static XDaiBalance operator +(XDaiBalance left, XDaiBalance right) => new(left.balance + right.balance);
public static XDaiBalance operator -(XDaiBalance left, XDaiBalance right) => new(left.balance - right.balance);
public static XDaiBalance operator *(XDaiBalance left, XDaiBalance right) => new(left.balance * right.balance);
public static XDaiBalance operator /(XDaiBalance left, XDaiBalance right) => new(left.balance / right.balance);
public static XDaiBalance operator +(XDaiBalance left, XDaiBalance right) => left.balance + right.balance;
public static XDaiBalance operator -(XDaiBalance left, XDaiBalance right) => left.balance - right.balance;
public static XDaiBalance operator *(XDaiBalance left, decimal right) => left.balance * right;
public static XDaiBalance operator *(decimal left, XDaiBalance right) => left * right.balance;
public static decimal operator /(XDaiBalance left, XDaiBalance right) => left.balance / right.balance;
public static XDaiBalance operator /(XDaiBalance left, decimal right) => left.balance / right;

public static bool operator ==(XDaiBalance left, XDaiBalance right) => left.Equals(right);
public static bool operator !=(XDaiBalance left, XDaiBalance right) => !(left == right);
Expand Down

0 comments on commit 06813ed

Please sign in to comment.