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.")] [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"; 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 EthAddressFile { get; set; } = "/root/codex-testnet-starter/scripts/eth.address";
public string LogPath public string LogPath

View File

@ -7,37 +7,42 @@ namespace AutoClient.Modes.FolderStore
{ {
private readonly LogPrefixer log; private readonly LogPrefixer log;
private readonly GethConnector.GethConnector? connector; private readonly GethConnector.GethConnector? connector;
private readonly EthAddress? address; private readonly EthAddress[] addresses;
public BalanceChecker(App app) public BalanceChecker(App app)
{ {
log = new LogPrefixer(app.Log, "(Balance) "); log = new LogPrefixer(app.Log, "(Balance) ");
connector = GethConnector.GethConnector.Initialize(app.Log); connector = GethConnector.GethConnector.Initialize(app.Log);
address = LoadAddress(app); addresses = LoadAddresses(app);
} }
private EthAddress? LoadAddress(App app) private EthAddress[] LoadAddresses(App app)
{ {
try try
{ {
if (string.IsNullOrEmpty(app.Config.EthAddressFile)) return null; if (string.IsNullOrEmpty(app.Config.EthAddressFile)) return Array.Empty<EthAddress>();
if (!File.Exists(app.Config.EthAddressFile)) return null; if (!File.Exists(app.Config.EthAddressFile)) return Array.Empty<EthAddress>();
return new EthAddress( var tokens = app.Config.EthAddressFile.Split(";", StringSplitOptions.RemoveEmptyEntries);
File.ReadAllText(app.Config.EthAddressFile) return tokens.Select(ConvertToAddress).Where(a => a != null).ToArray();
.Trim()
.Replace("\n", "")
.Replace(Environment.NewLine, "")
);
} }
catch (Exception exc) catch (Exception exc)
{ {
log.Error($"Failed to load eth address from file: {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() public void Check()
{ {
if (connector == null) if (connector == null)
@ -45,35 +50,32 @@ namespace AutoClient.Modes.FolderStore
Log("Connector not configured. Can't check balances."); Log("Connector not configured. Can't check balances.");
return; return;
} }
if (address == null)
{
Log("EthAddress not found. Can't check balances.");
return;
}
try foreach (var address in addresses)
{ {
PerformCheck(); try
} {
catch (Exception exc) PerformCheck(address);
{ }
Log($"Exception while checking balances: {exc}"); catch (Exception exc)
{
Log($"Exception while checking balances: {exc}");
}
} }
} }
private void PerformCheck() private void PerformCheck(EthAddress address)
{ {
var geth = connector!.GethNode; var geth = connector!.GethNode;
var contracts = connector!.CodexContracts; var contracts = connector!.CodexContracts;
var addr = address!;
var eth = geth.GetEthBalance(addr); var eth = geth.GetEthBalance(address);
var tst = contracts.GetTestTokenBalance(addr); var tst = contracts.GetTestTokenBalance(address);
Log($"Balances: [{eth}] - [{tst}]"); Log($"Balances: [{eth}] - [{tst}]");
if (eth.Eth < 1) TryAddEth(geth, addr); if (eth.Eth < 1) TryAddEth(geth, address);
if (tst.Tst < 1) TryAddTst(contracts, addr); if (tst.Tst < 1) TryAddTst(contracts, address);
} }
private void TryAddEth(GethPlugin.IGethNode geth, EthAddress addr) private void TryAddEth(GethPlugin.IGethNode geth, EthAddress addr)

View File

@ -6,6 +6,7 @@ namespace AutoClient.Modes.FolderStore
{ {
private readonly App app; private readonly App app;
private readonly string filePath; private readonly string filePath;
private readonly object fileLock = new object();
public JsonFile(App app, string filePath) public JsonFile(App app, string filePath)
{ {
@ -15,35 +16,41 @@ namespace AutoClient.Modes.FolderStore
public T Load() public T Load()
{ {
try lock (fileLock)
{ {
if (!File.Exists(filePath)) try
{ {
var state = new T(); if (!File.Exists(filePath))
Save(state); {
return state; 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) public void Save(T state)
{ {
try lock (fileLock)
{ {
var json = JsonConvert.SerializeObject(state, Formatting.Indented); try
File.WriteAllText(filePath, json); {
} var json = JsonConvert.SerializeObject(state, Formatting.Indented);
catch (Exception exc) File.WriteAllText(filePath, json);
{ }
app.Log.Error("Failed to save state: " + exc); catch (Exception exc)
throw; {
app.Log.Error("Failed to save state: " + exc);
throw;
}
} }
} }
} }