mirror of
https://github.com/logos-storage/logos-storage-nim-cs-dist-tests.git
synced 2026-01-03 22:13:10 +00:00
pluralizes balance checker
This commit is contained in:
parent
2c9f9d1008
commit
d535977175
@ -41,7 +41,7 @@ namespace AutoClient
|
||||
[Uniform("folderToStore", "fts", "FOLDERTOSTORE", false, "When set, autoclient will attempt to upload and purchase storage for every non-JSON file in the provided folder.")]
|
||||
public string FolderToStore { get; set; } = "/data/EthereumMainnetPreMergeEraFiles";
|
||||
|
||||
[Uniform("ethAddressFile", "eaf", "ETHADDRESSFILE", false, "File with eth address used by codex node. Used for balance checking if geth/contracts information is provided.")]
|
||||
[Uniform("ethAddressFile", "eaf", "ETHADDRESSFILE", false, "File(s) with eth address used by codex node. Used for balance checking if geth/contracts information is provided. Semi-colon separated.")]
|
||||
public string EthAddressFile { get; set; } = "/root/codex-testnet-starter/scripts/eth.address";
|
||||
|
||||
public string LogPath
|
||||
|
||||
@ -7,37 +7,42 @@ namespace AutoClient.Modes.FolderStore
|
||||
{
|
||||
private readonly LogPrefixer log;
|
||||
private readonly GethConnector.GethConnector? connector;
|
||||
private readonly EthAddress? address;
|
||||
private readonly EthAddress[] addresses;
|
||||
|
||||
public BalanceChecker(App app)
|
||||
{
|
||||
log = new LogPrefixer(app.Log, "(Balance) ");
|
||||
|
||||
connector = GethConnector.GethConnector.Initialize(app.Log);
|
||||
address = LoadAddress(app);
|
||||
addresses = LoadAddresses(app);
|
||||
}
|
||||
|
||||
private EthAddress? LoadAddress(App app)
|
||||
private EthAddress[] LoadAddresses(App app)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(app.Config.EthAddressFile)) return null;
|
||||
if (!File.Exists(app.Config.EthAddressFile)) return null;
|
||||
if (string.IsNullOrEmpty(app.Config.EthAddressFile)) return Array.Empty<EthAddress>();
|
||||
if (!File.Exists(app.Config.EthAddressFile)) return Array.Empty<EthAddress>();
|
||||
|
||||
return new EthAddress(
|
||||
File.ReadAllText(app.Config.EthAddressFile)
|
||||
.Trim()
|
||||
.Replace("\n", "")
|
||||
.Replace(Environment.NewLine, "")
|
||||
);
|
||||
var tokens = app.Config.EthAddressFile.Split(";", StringSplitOptions.RemoveEmptyEntries);
|
||||
return tokens.Select(ConvertToAddress).Where(a => a != null).ToArray();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
log.Error($"Failed to load eth address from file: {exc}");
|
||||
return null;
|
||||
return Array.Empty<EthAddress>();
|
||||
}
|
||||
}
|
||||
|
||||
private EthAddress ConvertToAddress(string t)
|
||||
{
|
||||
return new EthAddress(
|
||||
File.ReadAllText(t)
|
||||
.Trim()
|
||||
.Replace("\n", "")
|
||||
.Replace(Environment.NewLine, ""));
|
||||
}
|
||||
|
||||
public void Check()
|
||||
{
|
||||
if (connector == null)
|
||||
@ -45,35 +50,32 @@ namespace AutoClient.Modes.FolderStore
|
||||
Log("Connector not configured. Can't check balances.");
|
||||
return;
|
||||
}
|
||||
if (address == null)
|
||||
{
|
||||
Log("EthAddress not found. Can't check balances.");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
foreach (var address in addresses)
|
||||
{
|
||||
PerformCheck();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log($"Exception while checking balances: {exc}");
|
||||
try
|
||||
{
|
||||
PerformCheck(address);
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log($"Exception while checking balances: {exc}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PerformCheck()
|
||||
private void PerformCheck(EthAddress address)
|
||||
{
|
||||
var geth = connector!.GethNode;
|
||||
var contracts = connector!.CodexContracts;
|
||||
var addr = address!;
|
||||
|
||||
var eth = geth.GetEthBalance(addr);
|
||||
var tst = contracts.GetTestTokenBalance(addr);
|
||||
var eth = geth.GetEthBalance(address);
|
||||
var tst = contracts.GetTestTokenBalance(address);
|
||||
|
||||
Log($"Balances: [{eth}] - [{tst}]");
|
||||
|
||||
if (eth.Eth < 1) TryAddEth(geth, addr);
|
||||
if (tst.Tst < 1) TryAddTst(contracts, addr);
|
||||
if (eth.Eth < 1) TryAddEth(geth, address);
|
||||
if (tst.Tst < 1) TryAddTst(contracts, address);
|
||||
}
|
||||
|
||||
private void TryAddEth(GethPlugin.IGethNode geth, EthAddress addr)
|
||||
|
||||
@ -6,6 +6,7 @@ namespace AutoClient.Modes.FolderStore
|
||||
{
|
||||
private readonly App app;
|
||||
private readonly string filePath;
|
||||
private readonly object fileLock = new object();
|
||||
|
||||
public JsonFile(App app, string filePath)
|
||||
{
|
||||
@ -15,35 +16,41 @@ namespace AutoClient.Modes.FolderStore
|
||||
|
||||
public T Load()
|
||||
{
|
||||
try
|
||||
lock (fileLock)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
try
|
||||
{
|
||||
var state = new T();
|
||||
Save(state);
|
||||
return state;
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
var state = new T();
|
||||
Save(state);
|
||||
return state;
|
||||
}
|
||||
var text = File.ReadAllText(filePath);
|
||||
return JsonConvert.DeserializeObject<T>(text)!;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
app.Log.Error("Failed to load state: " + exc);
|
||||
throw;
|
||||
}
|
||||
var text = File.ReadAllText(filePath);
|
||||
return JsonConvert.DeserializeObject<T>(text)!;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
app.Log.Error("Failed to load state: " + exc);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void Save(T state)
|
||||
{
|
||||
try
|
||||
lock (fileLock)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(state, Formatting.Indented);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
app.Log.Error("Failed to save state: " + exc);
|
||||
throw;
|
||||
try
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(state, Formatting.Indented);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
app.Log.Error("Failed to save state: " + exc);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user