Skip to content

Commit

Permalink
Add last compound info to web API + clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
lwYeo committed Aug 11, 2021
1 parent ec3ffcc commit 3895484
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 157 deletions.
149 changes: 73 additions & 76 deletions Crypto LP Compounder/Compounder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ limitations under the License.
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder
{
Expand All @@ -50,7 +46,9 @@ internal class Compounder : ICompounder
private BigInteger _LastEstimateGasCostPerTxn;
private uint _LastProcessTxnCount;

private DateTimeOffset _LastUpdate, _NextLoopTime;
private DateTimeOffset _LastCompoundDateTime, _LastUpdate, _NextLoopDateTime;
private TimeSpan _LastProcessTimeTaken;

private readonly ValueSymbol _EstimateGasPerTxn;

string ICompounder.Name => _Settings.Name;
Expand All @@ -64,63 +62,63 @@ internal class Compounder : ICompounder
_LastUpdate.Second,
_LastUpdate.Offset);

string[] ICompounder.Summary
{
get
string[] ICompounder.Summary =>
new[]
{
return new[] {
$"Current APR: {_Farm.CurrentAPR.Value.ToString(_Farm.CurrentAPR.Value < 1000 ? "n3" : "n0")} {_Farm.CurrentAPR.Symbol}" +
$" ({_Farm.CurrentDeposit.FiatValue.Value * (_Farm.CurrentAPR.Value / 100):n2} {_Farm.CurrentDeposit.FiatValue.Symbol} /" +
$" {_Farm.CurrentDeposit.ChainValue.Value * (_Farm.CurrentAPR.Value / 100):n2} {_Farm.CurrentDeposit.ChainValue.Symbol})"
,
$"Optimal APY: {_Farm.OptimalAPY.Value.ToString(_Farm.OptimalAPY.Value < 1000 ? "n3" : "n0")} {_Farm.OptimalAPY.Symbol}" +
$" ({_Farm.CurrentDeposit.FiatValue.Value * (_Farm.OptimalAPY.Value / 100):n2} {_Farm.CurrentDeposit.FiatValue.Symbol} /" +
$" {_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})"
,
$"Total liquidity:" +
$" {_Farm.CurrentDeposit.FiatValue.Value + _Farm.CurrentPendingReward.FiatValue.Value:n2} {_Farm.CurrentDeposit.FiatValue.Symbol} /" +
$" {_Farm.CurrentDeposit.ChainValue.Value + _Farm.CurrentPendingReward.ChainValue.Value:n9} {_Farm.CurrentDeposit.ChainValue.Symbol}"
,
$"Pending reward: {_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})"
,
$"Current deposit: {_Farm.CurrentDeposit.Value.Value:n9} {_Farm.CurrentDeposit.Value.Symbol}" +
$" ({_Farm.CurrentDeposit.FiatValue.Value:n2} {_Farm.CurrentDeposit.FiatValue.Symbol} /" +
$" {_Farm.CurrentDeposit.ChainValue.Value:n9} {_Farm.CurrentDeposit.ChainValue.Symbol})"
,
$"Underlying Token A deposit: {_Farm.UnderlyingTokenA_Deposit.Value.Value:n9} {_Farm.UnderlyingTokenA_Deposit.Value.Symbol}" +
$" ({_Farm.UnderlyingTokenA_Deposit.FiatValue.Value:n2} {_Farm.UnderlyingTokenA_Deposit.FiatValue.Symbol} /" +
$" {_Farm.UnderlyingTokenA_Deposit.ChainValue.Value:n9} {_Farm.UnderlyingTokenA_Deposit.ChainValue.Symbol})"
,
$"Underlying Token B deposit: {_Farm.UnderlyingTokenB_Deposit.Value.Value:n9} {_Farm.UnderlyingTokenB_Deposit.Value.Symbol}" +
$" ({_Farm.UnderlyingTokenB_Deposit.FiatValue.Value:n2} {_Farm.UnderlyingTokenB_Deposit.FiatValue.Symbol} /" +
$" {_Farm.UnderlyingTokenB_Deposit.ChainValue.Value:n9} {_Farm.UnderlyingTokenB_Deposit.ChainValue.Symbol})"
,
$"Reward value: {_Farm.Reward.FiatValue.Value:n2} {_Farm.Reward.FiatValue.Symbol} /" +
$" {_Farm.Reward.ChainValue.Value:n9} {_Farm.Reward.ChainValue.Symbol}"
,
$"Token A value: {_Farm.TokenA.FiatValue.Value:n2} {_Farm.TokenA.FiatValue.Symbol} /" +
$" {_Farm.TokenA.ChainValue.Value:n9} {_Farm.TokenA.ChainValue.Symbol}"
,
$"Token B value: {_Farm.TokenB.FiatValue.Value:n2} {_Farm.TokenB.FiatValue.Symbol} /" +
$" {_Farm.TokenB.ChainValue.Value:n9} {_Farm.TokenB.ChainValue.Symbol}"
};
}
}
$"Current APR: {_Farm.CurrentAPR.Value.ToString(_Farm.CurrentAPR.Value < 1000 ? "n3" : "n0")} {_Farm.CurrentAPR.Symbol}" +
$" ({_Farm.CurrentDeposit.FiatValue.Value * (_Farm.CurrentAPR.Value / 100):n2} {_Farm.CurrentDeposit.FiatValue.Symbol} /" +
$" {_Farm.CurrentDeposit.ChainValue.Value * (_Farm.CurrentAPR.Value / 100):n2} {_Farm.CurrentDeposit.ChainValue.Symbol})"
,
$"Optimal APY: {_Farm.OptimalAPY.Value.ToString(_Farm.OptimalAPY.Value < 1000 ? "n3" : "n0")} {_Farm.OptimalAPY.Symbol}" +
$" ({_Farm.CurrentDeposit.FiatValue.Value * (_Farm.OptimalAPY.Value / 100):n2} {_Farm.CurrentDeposit.FiatValue.Symbol} /" +
$" {_Farm.CurrentDeposit.ChainValue.Value * (_Farm.OptimalAPY.Value / 100):n2} {_Farm.CurrentDeposit.ChainValue.Symbol})" +
$" ({_Farm.OptimalCompoundsPerYear} compounds per year)"
,
$"Next estimated compound in {(_NextLoopDateTime - DateTimeOffset.Now).TotalDays:n0} d" +
$" {_NextLoopDateTime - DateTimeOffset.Now:hh' hr 'mm' min 'ss' sec'}" +
$" ({_NextLoopDateTime:yyyy-MM-ddTHH:mm:ssK})"
,
$"Total liquidity:" +
$" {_Farm.CurrentDeposit.FiatValue.Value + _Farm.CurrentPendingReward.FiatValue.Value:n2} {_Farm.CurrentDeposit.FiatValue.Symbol} /" +
$" {_Farm.CurrentDeposit.ChainValue.Value + _Farm.CurrentPendingReward.ChainValue.Value:n9} {_Farm.CurrentDeposit.ChainValue.Symbol}"
,
$"Pending reward: {_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})"
,
$"Current deposit: {_Farm.CurrentDeposit.Value.Value:n9} {_Farm.CurrentDeposit.Value.Symbol}" +
$" ({_Farm.CurrentDeposit.FiatValue.Value:n2} {_Farm.CurrentDeposit.FiatValue.Symbol} /" +
$" {_Farm.CurrentDeposit.ChainValue.Value:n9} {_Farm.CurrentDeposit.ChainValue.Symbol})"
,
$"Underlying Token A deposit: {_Farm.UnderlyingTokenA_Deposit.Value.Value:n9} {_Farm.UnderlyingTokenA_Deposit.Value.Symbol}" +
$" ({_Farm.UnderlyingTokenA_Deposit.FiatValue.Value:n2} {_Farm.UnderlyingTokenA_Deposit.FiatValue.Symbol} /" +
$" {_Farm.UnderlyingTokenA_Deposit.ChainValue.Value:n9} {_Farm.UnderlyingTokenA_Deposit.ChainValue.Symbol})"
,
$"Underlying Token B deposit: {_Farm.UnderlyingTokenB_Deposit.Value.Value:n9} {_Farm.UnderlyingTokenB_Deposit.Value.Symbol}" +
$" ({_Farm.UnderlyingTokenB_Deposit.FiatValue.Value:n2} {_Farm.UnderlyingTokenB_Deposit.FiatValue.Symbol} /" +
$" {_Farm.UnderlyingTokenB_Deposit.ChainValue.Value:n9} {_Farm.UnderlyingTokenB_Deposit.ChainValue.Symbol})"
,
$"Reward value: {_Farm.Reward.FiatValue.Value:n2} {_Farm.Reward.FiatValue.Symbol} /" +
$" {_Farm.Reward.ChainValue.Value:n9} {_Farm.Reward.ChainValue.Symbol}"
,
$"Token A value: {_Farm.TokenA.FiatValue.Value:n2} {_Farm.TokenA.FiatValue.Symbol} /" +
$" {_Farm.TokenA.ChainValue.Value:n9} {_Farm.TokenA.ChainValue.Symbol}"
,
$"Token B value: {_Farm.TokenB.FiatValue.Value:n2} {_Farm.TokenB.FiatValue.Symbol} /" +
$" {_Farm.TokenB.ChainValue.Value:n9} {_Farm.TokenB.ChainValue.Symbol}"
};

ValueSymbol ICompounder.CurrentAPR => _Farm.CurrentAPR;

ValueSymbol ICompounder.OptimalAPY => _Farm.OptimalAPY;

int ICompounder.OptimalCompoundsPerYear => _Farm.OptimalCompoundsPerYear;

DateTimeOffset ICompounder.NextCompoundDateTime => _NextLoopTime;
DateTimeOffset ICompounder.LastCompoundDateTime => _LastCompoundDateTime;

TimeSpan ICompounder.LastCompoundProcessDuration => TimeSpan.FromSeconds(Math.Round(_LastProcessTimeTaken.TotalSeconds));

DateTimeOffset ICompounder.NextEstimateCompoundDateTime => _NextLoopDateTime;

ValueSymbol ICompounder.EstimateGasPerTxn => _EstimateGasPerTxn;

Expand All @@ -142,7 +140,9 @@ string[] ICompounder.Summary

public Compounder(Settings.CompounderSettings settings)
{
_NextLoopTime = DateTimeOffset.UnixEpoch;
_LastProcessTimeTaken = TimeSpan.MinValue;
_LastCompoundDateTime = DateTimeOffset.UnixEpoch;
_NextLoopDateTime = DateTimeOffset.UnixEpoch;
_Settings = settings;

Log = new(settings);
Expand Down Expand Up @@ -240,68 +240,65 @@ public Compounder(Settings.CompounderSettings settings)
public async Task Start()
{
int intervalSecond;
DateTimeOffset beginLoopTime;
DateTimeOffset lastUpdate = DateTimeOffset.Now;
Stopwatch stopwatch = new();

DateTimeOffset stateDateTime = DateTimeOffset.UnixEpoch;
TimeSpan processTimeTaken = TimeSpan.MinValue;

string statePath = System.IO.Path.Combine(AppContext.BaseDirectory, "state_" + _Settings.Name);
byte[] stateBytes = System.IO.File.Exists(statePath) ? System.IO.File.ReadAllBytes(statePath) : null;
string statePath = Path.Combine(AppContext.BaseDirectory, "state_" + _Settings.Name);
byte[] stateBytes = File.Exists(statePath) ? File.ReadAllBytes(statePath) : null;

if (stateBytes?.Length.Equals(sizeof(long) * 2) ?? false)
{
long state = BitConverter.ToInt64(stateBytes.Take(sizeof(long)).ToArray());
stateDateTime = DateTimeOffset.FromUnixTimeSeconds(state).ToOffset(DateTimeOffset.Now.Offset);

state = BitConverter.ToInt64(stateBytes.Skip(sizeof(long)).ToArray());
processTimeTaken = TimeSpan.FromTicks(state);
_LastProcessTimeTaken = TimeSpan.FromTicks(state);
}

while (!Program.IsTerminate)
{
if (stateDateTime > DateTimeOffset.UnixEpoch)
{
beginLoopTime = stateDateTime;
_LastCompoundDateTime = stateDateTime;
stateDateTime = DateTimeOffset.UnixEpoch;
}
else
{
Log.IsCompoundProcess = true;

stopwatch.Restart();
_LastCompoundDateTime = DateTimeOffset.Now;

await ProcessCompound();

processTimeTaken = stopwatch.Elapsed;
_LastProcessTimeTaken = stopwatch.Elapsed;

Log.WriteLine($"Time taken: {processTimeTaken.TotalHours:n0} hr {processTimeTaken:mm' min 'ss' sec'}");
Log.WriteLine($"Time taken: {_LastProcessTimeTaken.TotalHours:n0} hr {_LastProcessTimeTaken:mm' min 'ss' sec'}");
Log.WriteLineBreak();

beginLoopTime = DateTimeOffset.Now;

stateBytes =
BitConverter.GetBytes(beginLoopTime.ToUnixTimeSeconds()).
Concat(BitConverter.GetBytes(processTimeTaken.Ticks)).
BitConverter.GetBytes(_LastCompoundDateTime.ToUnixTimeSeconds()).
Concat(BitConverter.GetBytes(_LastProcessTimeTaken.Ticks)).
ToArray();

_ = System.IO.File.WriteAllBytesAsync(statePath, stateBytes);
_ = File.WriteAllBytesAsync(statePath, stateBytes);

Log.IsCompoundProcess = false;
}

_NextLoopTime = beginLoopTime;
_NextLoopDateTime = _LastCompoundDateTime;
intervalSecond = 0;

stopwatch.Restart();

while ((_NextLoopTime == beginLoopTime || DateTimeOffset.Now < _NextLoopTime) && !Program.IsTerminate)
while ((_NextLoopDateTime == _LastCompoundDateTime || DateTimeOffset.Now < _NextLoopDateTime) && !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 (_NextLoopDateTime != _LastCompoundDateTime)
{
lastUpdate = DateTimeOffset.Now;

Expand Down Expand Up @@ -329,15 +326,15 @@ public async Task Start()
_EstimateGasPerTxn.Value =
(decimal)UnitConversion.Convert.FromWeiToBigDecimal(_LastEstimateGasCostPerTxn, UnitConversion.EthUnit.Ether);

intervalSecond -= (int)Math.Floor(processTimeTaken.TotalSeconds);
intervalSecond -= (int)Math.Floor(_LastProcessTimeTaken.TotalSeconds);

_NextLoopTime = beginLoopTime.AddSeconds(intervalSecond);
_NextLoopDateTime = _LastCompoundDateTime.AddSeconds(intervalSecond);

Log.WriteLine();
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})");
$"Next compound in {(_NextLoopDateTime - DateTimeOffset.Now).TotalDays:n0} d" +
$" {_NextLoopDateTime - DateTimeOffset.Now:hh' hr 'mm' min 'ss' sec'}" +
$" ({_NextLoopDateTime:yyyy-MM-dd T HH:mm:ss K})");
}

await Task.Delay(1000);
Expand Down
2 changes: 0 additions & 2 deletions Crypto LP Compounder/Contract/ERC20.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ limitations under the License.
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Util;
using Nethereum.Web3;
using System;
using System.Numerics;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder.Contract
{
Expand Down
1 change: 0 additions & 1 deletion Crypto LP Compounder/Contract/Farm/MOMA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Web3;
using System.Numerics;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder.Contract.Farm
{
Expand Down
8 changes: 2 additions & 6 deletions Crypto LP Compounder/Contract/Farm/MasterChef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ limitations under the License.
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Util;
using Nethereum.Web3;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder.Contract.Farm
{
Expand Down Expand Up @@ -596,8 +592,8 @@ public bool CalculateOptimalApy(BigInteger gasCostAmount, ref int compoundInterv
$" % ({compoundPerYear:n0} compounds / " +
(optimalUsdPerYr < 1000 ? $"{(decimal)optimalUsdPerYr:n2}" : $"{(decimal)optimalUsdPerYr:n0}") +
$" USD / " +
(optimalEthPerYr < 1000 ? $"{(decimal)optimalEthPerYr:n10}" : $"{_Settings.GasSymbol:n0}") +
$" {4} per year)");
(optimalEthPerYr < 1000 ? $"{(decimal)optimalEthPerYr:n10}" : $"{(decimal)optimalEthPerYr:n0}") +
$" {_Settings.GasSymbol} per year)");
}
else
{
Expand Down
1 change: 0 additions & 1 deletion Crypto LP Compounder/Contract/Farm/TombFinance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Web3;
using System.Numerics;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder.Contract.Farm
{
Expand Down
1 change: 0 additions & 1 deletion Crypto LP Compounder/Contract/Farm/YEL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Web3;
using System.Numerics;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder.Contract.Farm
{
Expand Down
2 changes: 0 additions & 2 deletions Crypto LP Compounder/Contract/UniswapV2/Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ limitations under the License.

using Nethereum.Contracts.ContractHandlers;
using Nethereum.Web3;
using System;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder.Contract.UniswapV2
{
Expand Down
4 changes: 0 additions & 4 deletions Crypto LP Compounder/Contract/UniswapV2/MlnlRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ limitations under the License.
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Util;
using Nethereum.Web3;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder.Contract.UniswapV2
{
Expand Down
4 changes: 0 additions & 4 deletions Crypto LP Compounder/Contract/UniswapV2/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ limitations under the License.
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Util;
using Nethereum.Web3;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder.Contract.UniswapV2
{
Expand Down
2 changes: 1 addition & 1 deletion Crypto LP Compounder/Crypto LP Compounder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Copyright>Copyright © lwYeo 2021</Copyright>
<Company>lwYeo@github</Company>
<Authors>lwYeo</Authors>
<Version>0.1.9</Version>
<Version>0.1.10</Version>
<RepositoryType>GitHub</RepositoryType>
<RepositoryUrl>https://github.com/lwYeo/CryptoLP_Compounder</RepositoryUrl>
<Product>Crypto LP Compounder</Product>
Expand Down
1 change: 0 additions & 1 deletion Crypto LP Compounder/DTO/Uniswap/V2Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.

using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts;
using System.Collections.Generic;
using System.Numerics;

namespace Crypto_LP_Compounder.DTO.Uniswap
Expand Down
5 changes: 0 additions & 5 deletions Crypto LP Compounder/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ limitations under the License.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;

namespace Crypto_LP_Compounder
Expand Down
5 changes: 0 additions & 5 deletions Crypto LP Compounder/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ You may obtain a copy of the License at
limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder
{
internal class Log
Expand Down
5 changes: 0 additions & 5 deletions Crypto LP Compounder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ limitations under the License.
*/

using DTO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

namespace Crypto_LP_Compounder
{
Expand Down
Loading

0 comments on commit 3895484

Please sign in to comment.