From 89af761272c34ed0c385853aeebd7d36c1aeb5d8 Mon Sep 17 00:00:00 2001 From: lwYeo Date: Mon, 9 Aug 2021 16:08:49 +0800 Subject: [PATCH] Add next compound datetime in web API --- Crypto LP Compounder/Compounder.cs | 30 ++++++++++++++++++------------ DTO/ICompounder.cs | 2 ++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Crypto LP Compounder/Compounder.cs b/Crypto LP Compounder/Compounder.cs index a896c53..ed4d807 100644 --- a/Crypto LP Compounder/Compounder.cs +++ b/Crypto LP Compounder/Compounder.cs @@ -50,7 +50,7 @@ internal class Compounder : ICompounder private BigInteger _LastEstimateGasCostPerTxn; private uint _LastProcessTxnCount; - private DateTimeOffset _LastUpdate; + private DateTimeOffset _LastUpdate, _NextLoopTime; private readonly ValueSymbol _EstimateGasPerTxn; string ICompounder.Name => _Settings.Name; @@ -78,6 +78,10 @@ string[] ICompounder.Summary $" ({_Farm.CurrentDeposit.ChainValue.Value * (_Farm.OptimalAPY.Value / 100):n2} {_Farm.CurrentDeposit.ChainValue.Symbol})" + $" ({_Farm.OptimalCompoundsPerYear} compounds per year)" , + $"Next compound in {(_NextLoopTime - DateTimeOffset.Now).TotalDays:n0} d" + + $" {_NextLoopTime - DateTimeOffset.Now:hh' hr 'mm' min 'ss' sec'}" + + $" ({_NextLoopTime:yyyy-MM-ddTHH:mm:ssK})" + , $"Pending reward value: {_Farm.CurrentPendingReward.Value.Value:n9} {_Farm.CurrentPendingReward.Value.Symbol}" + $" ({_Farm.CurrentPendingReward.FiatValue.Value:n2} {_Farm.CurrentPendingReward.FiatValue.Symbol} /" + $" {_Farm.CurrentPendingReward.ChainValue.Value:n9} {_Farm.CurrentPendingReward.ChainValue.Symbol})" @@ -109,6 +113,8 @@ string[] ICompounder.Summary int ICompounder.OptimalCompoundsPerYear => _Farm.OptimalCompoundsPerYear; + DateTimeOffset ICompounder.NextCompoundDateTime => _NextLoopTime; + ValueSymbol ICompounder.EstimateGasPerTxn => _EstimateGasPerTxn; TokenValue ICompounder.CurrentDeposit => _Farm.CurrentDeposit; @@ -129,6 +135,7 @@ string[] ICompounder.Summary public Compounder(Settings.CompounderSettings settings) { + _NextLoopTime = DateTimeOffset.UnixEpoch; _Settings = settings; Log = new(settings); @@ -226,8 +233,7 @@ public Compounder(Settings.CompounderSettings settings) public async Task Start() { int intervalSecond; - TimeSpan intervalRemaining; - DateTimeOffset beginLoopTime, nextLoopTime; + DateTimeOffset beginLoopTime; DateTimeOffset lastUpdate = DateTimeOffset.Now; Stopwatch stopwatch = new(); @@ -240,7 +246,7 @@ public async Task Start() if (stateBytes?.Length.Equals(sizeof(long) * 2) ?? false) { long state = BitConverter.ToInt64(stateBytes.Take(sizeof(long)).ToArray()); - stateDateTime = DateTimeOffset.FromUnixTimeSeconds(state); + stateDateTime = DateTimeOffset.FromUnixTimeSeconds(state).ToOffset(DateTimeOffset.Now.Offset); state = BitConverter.ToInt64(stateBytes.Skip(sizeof(long)).ToArray()); processTimeTaken = TimeSpan.FromTicks(state); @@ -278,17 +284,17 @@ public async Task Start() Log.IsCompoundProcess = false; } - nextLoopTime = beginLoopTime; + _NextLoopTime = beginLoopTime; intervalSecond = 0; stopwatch.Restart(); - while ((nextLoopTime == beginLoopTime || DateTimeOffset.Now < nextLoopTime) && !Program.IsTerminate) + while ((_NextLoopTime == beginLoopTime || DateTimeOffset.Now < _NextLoopTime) && !Program.IsTerminate) { // Update interval every 5 minutes as gas fee fluctuates if (stopwatch.Elapsed.Seconds < 1 && stopwatch.Elapsed.Minutes % 5 == 0) { - if (nextLoopTime != beginLoopTime) + if (_NextLoopTime != beginLoopTime) { lastUpdate = DateTimeOffset.Now; @@ -318,13 +324,13 @@ public async Task Start() intervalSecond -= (int)Math.Floor(processTimeTaken.TotalSeconds); - nextLoopTime = beginLoopTime.AddSeconds(intervalSecond); - - intervalRemaining = nextLoopTime - DateTimeOffset.Now; + _NextLoopTime = beginLoopTime.AddSeconds(intervalSecond); Log.WriteLine(); - Log.WriteLine($"Next compound in {intervalRemaining.TotalDays:n0} d {intervalRemaining:hh' hr 'mm' min 'ss' sec'}" + - $" ({DateTimeOffset.Now.Add(intervalRemaining):yyyy-MM-dd T HH:mm:ss K})"); + Log.WriteLine( + $"Next compound in {(_NextLoopTime - DateTimeOffset.Now).TotalDays:n0} d" + + $" {_NextLoopTime - DateTimeOffset.Now:hh' hr 'mm' min 'ss' sec'}" + + $" ({_NextLoopTime:yyyy-MM-dd T HH:mm:ss K})"); } await Task.Delay(1000); diff --git a/DTO/ICompounder.cs b/DTO/ICompounder.cs index 6d528c7..37774e1 100644 --- a/DTO/ICompounder.cs +++ b/DTO/ICompounder.cs @@ -32,6 +32,8 @@ public interface ICompounder int OptimalCompoundsPerYear { get; } + DateTimeOffset NextCompoundDateTime { get; } + ValueSymbol EstimateGasPerTxn { get; } TokenValue CurrentDeposit { get; }