From 06813ed2b90eb4ecef4443aa614c433dd42a993f Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sun, 9 Jun 2024 18:17:33 +0200 Subject: [PATCH] Refactor balance conversion and calculation methods --- src/BeeNet/Models/BzzBalance.cs | 26 ++++++++++++++------------ src/BeeNet/Models/GnosisChain.cs | 23 +++++++++++++++++++++++ src/BeeNet/Models/PostageBatch.cs | 16 +++++++++++++--- src/BeeNet/Models/XDaiBalance.cs | 26 ++++++++++++++------------ 4 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 src/BeeNet/Models/GnosisChain.cs diff --git a/src/BeeNet/Models/BzzBalance.cs b/src/BeeNet/Models/BzzBalance.cs index 9b488b0..5e38129 100644 --- a/src/BeeNet/Models/BzzBalance.cs +++ b/src/BeeNet/Models/BzzBalance.cs @@ -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; @@ -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)); @@ -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); diff --git a/src/BeeNet/Models/GnosisChain.cs b/src/BeeNet/Models/GnosisChain.cs new file mode 100644 index 0000000..6dadbab --- /dev/null +++ b/src/BeeNet/Models/GnosisChain.cs @@ -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); + } +} \ No newline at end of file diff --git a/src/BeeNet/Models/PostageBatch.cs b/src/BeeNet/Models/PostageBatch.cs index 29bc547..b077a68 100644 --- a/src/BeeNet/Models/PostageBatch.cs +++ b/src/BeeNet/Models/PostageBatch.cs @@ -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 { @@ -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) @@ -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) @@ -146,5 +146,15 @@ internal PostageBatch(Clients.Response52 batch) /// The count of the fullest bucket /// 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); } } diff --git a/src/BeeNet/Models/XDaiBalance.cs b/src/BeeNet/Models/XDaiBalance.cs index 757558e..fc36256 100644 --- a/src/BeeNet/Models/XDaiBalance.cs +++ b/src/BeeNet/Models/XDaiBalance.cs @@ -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; @@ -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)); @@ -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);