Skip to content

Commit

Permalink
Add next compound datetime in web API
Browse files Browse the repository at this point in the history
  • Loading branch information
lwYeo committed Aug 9, 2021
1 parent 2f4361b commit 89af761
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 18 additions & 12 deletions Crypto LP Compounder/Compounder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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})"
Expand Down Expand Up @@ -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;
Expand All @@ -129,6 +135,7 @@ string[] ICompounder.Summary

public Compounder(Settings.CompounderSettings settings)
{
_NextLoopTime = DateTimeOffset.UnixEpoch;
_Settings = settings;

Log = new(settings);
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions DTO/ICompounder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public interface ICompounder

int OptimalCompoundsPerYear { get; }

DateTimeOffset NextCompoundDateTime { get; }

ValueSymbol EstimateGasPerTxn { get; }

TokenValue CurrentDeposit { get; }
Expand Down

0 comments on commit 89af761

Please sign in to comment.