Skip to content

Commit

Permalink
Added multi-instances process
Browse files Browse the repository at this point in the history
  • Loading branch information
lwYeo committed Aug 9, 2021
1 parent 6cf67e6 commit 2f4361b
Show file tree
Hide file tree
Showing 19 changed files with 232 additions and 117 deletions.
13 changes: 7 additions & 6 deletions Crypto LP Compounder/Compounder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class Compounder : ICompounder
private const string BurnAddress = "0x0000000000000000000000000000000000000000";
private const uint MaxRetries = 20;

private readonly Settings _Settings;
private readonly Settings.CompounderSettings _Settings;
private readonly Web3 _Web3;

private readonly Contract.UniswapV2.Factory _Factory;
Expand Down Expand Up @@ -127,7 +127,7 @@ string[] ICompounder.Summary

public Log Log { get; }

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

Expand All @@ -143,6 +143,7 @@ public Compounder(Settings settings)
_LastProcessTxnCount = DefaultTxnCountPerProcess;
_LastEstimateGasCostPerTxn = BigInteger.Zero;

Log.WriteLine();
Log.WriteLineBreak();
Log.WriteLine("Autocompounder settings");
Log.WriteLineBreak();
Expand All @@ -156,7 +157,7 @@ public Compounder(Settings settings)
string.IsNullOrWhiteSpace(_Settings.LiquidityPool.TaxFreeContract) ? "disabled" : _Settings.LiquidityPool.TaxFreeContract;

Log.WriteLine("Name: " + _Settings.Name);
Log.WriteLine("WebApiURL: " + _Settings.WebApiURL);
Log.WriteLine("IsLogAll: " + _Settings.IsLogAll.ToString());
Log.WriteLine("RPC URL: " + _Settings.RPC_URL);
Log.WriteLine("RPC_Timeout: " + _Settings.RPC_Timeout.ToString() + " s");
Log.WriteLine("GasPriceOffsetGwei:" + _Settings.GasPriceOffsetGwei.ToString() + " Gwei");
Expand Down Expand Up @@ -207,9 +208,9 @@ public Compounder(Settings settings)

_Farm = _Settings.Farm.FarmType switch
{
"WFTM-TOMB:TSHARE" => new Contract.Farm.TombFinance(Log, _Settings, _Web3, _Router, _RewardToken),
"WFTM-YEL:YEL" => new Contract.Farm.YEL(Log, _Settings, _Web3, _Router, _RewardToken),
"WBNB-MOMA:MOMA" => new Contract.Farm.MOMA(Log, _Settings, _Web3, _Router, _RewardToken),
"WFTM-TOMB_TSHARE" => new Contract.Farm.TombFinance(Log, _Settings, _Web3, _Router, _RewardToken),
"WFTM-YEL_YEL" => new Contract.Farm.YEL(Log, _Settings, _Web3, _Router, _RewardToken),
"WBNB-MOMA_MOMA" => new Contract.Farm.MOMA(Log, _Settings, _Web3, _Router, _RewardToken),
_ => null
};

Expand Down
4 changes: 2 additions & 2 deletions Crypto LP Compounder/Contract/ERC20.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ namespace Crypto_LP_Compounder.Contract
internal class ERC20
{
private readonly Log _Log;
private readonly Settings _Settings;
private readonly Settings.CompounderSettings _Settings;
private readonly Web3 _Web3;
private readonly ContractHandler _ContractHandler;

public string Address { get; }

public ERC20(Log log, Settings settings, Web3 web3, string tokenAddress)
public ERC20(Log log, Settings.CompounderSettings settings, Web3 web3, string tokenAddress)
{
_Log = log;
Address = tokenAddress;
Expand Down
2 changes: 1 addition & 1 deletion Crypto LP Compounder/Contract/Farm/MOMA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Crypto_LP_Compounder.Contract.Farm
{
internal class MOMA : MasterChef
{
public MOMA(Log log, Settings settings, Web3 web3, UniswapV2.Router router, ERC20 rewardToken) :
public MOMA(Log log, Settings.CompounderSettings settings, Web3 web3, UniswapV2.Router router, ERC20 rewardToken) :
base(log, settings, web3, router, rewardToken)
{
}
Expand Down
16 changes: 8 additions & 8 deletions Crypto LP Compounder/Contract/Farm/MasterChef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Crypto_LP_Compounder.Contract.Farm
internal abstract class MasterChef
{
protected readonly Log _Log;
protected readonly Settings _Settings;
protected readonly Settings.CompounderSettings _Settings;
protected readonly Web3 _Web3;
protected readonly ERC20 _RewardToken;
protected readonly ERC20 _TokenA;
Expand All @@ -40,7 +40,7 @@ internal abstract class MasterChef
protected readonly ContractHandler _ContractHandler;
protected readonly UniswapV2.Router _Router;

public MasterChef(Log log, Settings settings, Web3 web3, UniswapV2.Router router, ERC20 rewardToken)
public MasterChef(Log log, Settings.CompounderSettings settings, Web3 web3, UniswapV2.Router router, ERC20 rewardToken)
{
_Log = log;
_Settings = settings;
Expand All @@ -59,25 +59,25 @@ public MasterChef(Log log, Settings settings, Web3 web3, UniswapV2.Router router
SetTokenSymbol(CurrentDeposit, "LP");

UnderlyingTokenA_Deposit = new();
SetTokenSymbol(UnderlyingTokenA_Deposit, _Settings.Farm.FarmType.Split(':')[0].Split('-')[0]);
SetTokenSymbol(UnderlyingTokenA_Deposit, _Settings.Farm.FarmType.Split('_')[0].Split('-')[0]);

UnderlyingTokenB_Deposit = new();
SetTokenSymbol(UnderlyingTokenB_Deposit, _Settings.Farm.FarmType.Split(':')[0].Split('-')[1]);
SetTokenSymbol(UnderlyingTokenB_Deposit, _Settings.Farm.FarmType.Split('_')[0].Split('-')[1]);

CurrentPendingReward = new();
SetTokenSymbol(CurrentPendingReward, _Settings.Farm.FarmType.Split(':')[1]);
SetTokenSymbol(CurrentPendingReward, _Settings.Farm.FarmType.Split('_')[1]);

TokenA = new();
TokenA.Value.Value = 1;
SetTokenSymbol(TokenA, _Settings.Farm.FarmType.Split(':')[0].Split('-')[0]);
SetTokenSymbol(TokenA, _Settings.Farm.FarmType.Split('_')[0].Split('-')[0]);

TokenB = new();
TokenB.Value.Value = 1;
SetTokenSymbol(TokenB, _Settings.Farm.FarmType.Split(':')[0].Split('-')[1]);
SetTokenSymbol(TokenB, _Settings.Farm.FarmType.Split('_')[0].Split('-')[1]);

Reward = new();
Reward.Value.Value = 1;
SetTokenSymbol(Reward, _Settings.Farm.FarmType.Split(':')[1]);
SetTokenSymbol(Reward, _Settings.Farm.FarmType.Split('_')[1]);
}

public ValueSymbol CurrentAPR { get; private set; }
Expand Down
2 changes: 1 addition & 1 deletion Crypto LP Compounder/Contract/Farm/TombFinance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Crypto_LP_Compounder.Contract.Farm
{
internal class TombFinance : MasterChef
{
public TombFinance(Log log, Settings settings, Web3 web3, UniswapV2.Router router, ERC20 rewardToken) :
public TombFinance(Log log, Settings.CompounderSettings settings, Web3 web3, UniswapV2.Router router, ERC20 rewardToken) :
base(log, settings, web3, router, rewardToken)
{
}
Expand Down
2 changes: 1 addition & 1 deletion Crypto LP Compounder/Contract/Farm/YEL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Crypto_LP_Compounder.Contract.Farm
{
internal class YEL : MasterChef
{
public YEL(Log log, Settings settings, Web3 web3, UniswapV2.Router router, ERC20 rewardToken) :
public YEL(Log log, Settings.CompounderSettings settings, Web3 web3, UniswapV2.Router router, ERC20 rewardToken) :
base(log, settings, web3, router, rewardToken)
{
}
Expand Down
4 changes: 2 additions & 2 deletions Crypto LP Compounder/Contract/UniswapV2/Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ namespace Crypto_LP_Compounder.Contract.UniswapV2
internal class Factory
{
private readonly Log _Log;
private readonly Settings _Settings;
private readonly Settings.CompounderSettings _Settings;
private readonly Web3 _Web3;
private readonly ContractHandler _ContractHandler;

public Factory(Log log, Settings settings, Web3 web3)
public Factory(Log log, Settings.CompounderSettings settings, Web3 web3)
{
_Log = log;
_Settings = settings;
Expand Down
2 changes: 1 addition & 1 deletion Crypto LP Compounder/Contract/UniswapV2/MlnlRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class MlnlRouter : Router
{
private readonly ContractHandler _MlnlContractHandler;

public MlnlRouter(Log log, Settings settings, Web3 web3) : base(log, settings, web3)
public MlnlRouter(Log log, Settings.CompounderSettings settings, Web3 web3) : base(log, settings, web3)
{
if (!string.IsNullOrWhiteSpace(_LpSettings.ZapContract))
_MlnlContractHandler = _Web3.Eth.GetContractHandler(_LpSettings.ZapContract);
Expand Down
6 changes: 3 additions & 3 deletions Crypto LP Compounder/Contract/UniswapV2/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace Crypto_LP_Compounder.Contract.UniswapV2
internal class Router
{
protected readonly Log _Log;
protected readonly Settings _Settings;
protected readonly Settings.LiquidityPoolParams _LpSettings;
protected readonly Settings.CompounderSettings _Settings;
protected readonly Settings.CompounderSettings.LiquidityPoolParams _LpSettings;

protected readonly Web3 _Web3;
protected readonly ContractHandler _ContractHandler;
Expand All @@ -40,7 +40,7 @@ internal class Router
private readonly ERC20 _TokenA;
private readonly ERC20 _TokenB;

public Router(Log log, Settings settings, Web3 web3)
public Router(Log log, Settings.CompounderSettings settings, Web3 web3)
{
_Log = log;
_Settings = settings;
Expand Down
4 changes: 2 additions & 2 deletions Crypto LP Compounder/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class Log

public bool IsCompoundProcess { get; set; }

public Log(Settings settings)
public Log(Settings.CompounderSettings settings)
{
_IsLogAll = settings.IsLogAll;
_InstanceName = settings.Name;
Expand All @@ -47,7 +47,7 @@ public Log(Settings settings)

public async Task FlushLogsAsync()
{
while (!string.IsNullOrWhiteSpace(_LogQueue.Peek())) await Task.Delay(100);
while (_LogQueue.TryPeek(out _)) await Task.Delay(100);
}

public void Write(string log)
Expand Down
83 changes: 63 additions & 20 deletions Crypto LP Compounder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ internal class Program

private static readonly ManualResetEvent _FlushCompleteEvent = new(false);

private static Compounder _AutoCompounder;
private static Compounder[] _Compounders;

private static readonly Func<ICompounder[]> _GetAllCompounders = () =>
{
return new ICompounder[] { _AutoCompounder };
};
private static readonly Func<ICompounder[]> _GetAllCompounders = () => _Compounders?.OfType<ICompounder>().ToArray();

private static volatile bool _IsPause;

private static volatile bool _IsTerminate;

private static Settings _Settings;
private static Settings.MainSettings _Settings;

internal static bool IsTerminate => _IsTerminate;

Expand All @@ -61,11 +60,11 @@ internal class Program

internal static string GetLineBreak() => string.Concat(Enumerable.Repeat("=", 100));

internal static void LogLineBreakConsole() => LogConsole(GetLineBreak());
internal static void LogLineBreakConsole() => LogLineConsole(GetLineBreak());

internal static async Task FlushConsoleLogsAsync()
{
while (!string.IsNullOrWhiteSpace(_ConsoleLogQueue.Peek())) await Task.Delay(100);
while (_ConsoleLogQueue.TryPeek(out _)) await Task.Delay(100);
}

internal static async Task StartConsoleLog()
Expand All @@ -81,15 +80,26 @@ await Task.Factory.StartNew(() =>

internal static void ExitWithErrorCode(int errorCode)
{
_AutoCompounder.Log.FlushLogsAsync().Wait();
ExitWithErrorCodeAsync(errorCode).Wait();
}

private static async Task ExitWithErrorCodeAsync(int errorCode)
{
_IsTerminate = true;
_IsPause = true;

await FlushConsoleLogsAsync();

LogLineConsole();
LogConsole("Press any key to continue...");

FlushConsoleLogsAsync().Wait();
await FlushConsoleLogsAsync();

Console.ReadKey();

Environment.Exit(errorCode);

_IsPause = false;
}

private static async Task Main(string[] args)
Expand All @@ -110,22 +120,55 @@ private static async Task Main(string[] args)

SetConsoleCtrlHandler();

_Settings = Settings.LoadSettings();
_Settings = Settings.MainSettings.LoadSettings();

WebApi.Program.Start(_Settings.WebApiURL, _GetAllCompounders, args);
if (_Settings.Compounders.Select(c => c.Name).Distinct().Count() < _Settings.Compounders.Count)
{
LogLineConsole();
LogLineConsole("Instance name(s) in all settings file must be unique!");
await ExitWithErrorCodeAsync(13);
}

_AutoCompounder = new(_Settings);
LogLineConsole();
LogLineConsole("Starting Web API...");
_ = WebApi.Program
.Start(_Settings.WebApiURL, _GetAllCompounders, args)
.ContinueWith(async t =>
{
if (t.IsFaulted)
{
_IsTerminate = true;
_IsPause = true;

LogLineConsole();
LogLineConsole(t.Exception.Message);

await ExitWithErrorCodeAsync(20);
}
});

await Task.Delay(1000); // Wait for web API to initialize

if (!_IsTerminate)
{
_Compounders = _Settings.Compounders.Select(settings => new Compounder(settings)).ToArray();

await _AutoCompounder.Start();
LogLineConsole();
LogLineConsole("Starting autocompounders...");

await _AutoCompounder.Log.FlushLogsAsync();
await Task.WhenAll(_Compounders.Select(c => c.Start()));

LogLineConsole();
LogLineBreakConsole();
LogLineConsole("Autocompounder stopped");
LogLineBreakConsole();
await Task.WhenAll(_Compounders.Select(c => c.Log.FlushLogsAsync()));

LogLineConsole();
LogLineBreakConsole();
LogLineConsole("Autocompounder stopped");
LogLineBreakConsole();
}

await FlushConsoleLogsAsync();

FlushConsoleLogsAsync().Wait();
while (_IsPause) await Task.Delay(100);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), originalConsoleMode);
Expand Down
Loading

0 comments on commit 2f4361b

Please sign in to comment.