pluralizes balance checker

This commit is contained in:
ThatBen 2025-04-03 14:42:34 +02:00
parent 2c9f9d1008
commit d535977175
No known key found for this signature in database
GPG Key ID: 62C543548433D43E
3 changed files with 59 additions and 50 deletions

View File

@ -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

View File

@ -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)

View File

@ -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;
}
}
}
}