Skip to content

Commit 37b98db

Browse files
committed
adds balance checker to autoclient foldersaver
1 parent 288ad81 commit 37b98db

File tree

5 files changed

+122
-1
lines changed

5 files changed

+122
-1
lines changed

ProjectPlugins/CodexClient/StoragePurchaseContract.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private void WaitForStorageContractState(TimeSpan timeout, string desiredState,
143143
private void LogSubmittedDuration()
144144
{
145145
Log($"Pending to Submitted in {Time.FormatDuration(PendingToSubmitted)} " +
146-
$"( < {Time.FormatDuration(gracePeriod)})");
146+
$"( < {Time.FormatDuration(Purchase.Expiry + gracePeriod)})");
147147
}
148148

149149
private void LogStartedDuration()

Tools/AutoClient/AutoClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<ProjectReference Include="..\..\Framework\ArgsUniform\ArgsUniform.csproj" />
12+
<ProjectReference Include="..\..\Framework\GethConnector\GethConnector.csproj" />
1213
<ProjectReference Include="..\..\Framework\Logging\Logging.csproj" />
1314
<ProjectReference Include="..\..\ProjectPlugins\CodexPlugin\CodexPlugin.csproj" />
1415
</ItemGroup>

Tools/AutoClient/Configuration.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class Configuration
4141
[Uniform("folderToStore", "fts", "FOLDERTOSTORE", false, "When set, autoclient will attempt to upload and purchase storage for every non-JSON file in the provided folder.")]
4242
public string FolderToStore { get; set; } = "/data/EthereumMainnetPreMergeEraFiles";
4343

44+
[Uniform("ethAddressFile", "eaf", "ETHADDRESSFILE", false, "File with eth address used by codex node. Used for balance checking if geth/contracts information is provided.")]
45+
public string EthAddressFile { get; set; } = "/root/codex-testnet-starter/scripts/eth.address";
46+
4447
public string LogPath
4548
{
4649
get
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using GethConnector;
2+
using Logging;
3+
using Utils;
4+
5+
namespace AutoClient.Modes.FolderStore
6+
{
7+
public class BalanceChecker
8+
{
9+
private readonly LogPrefixer log;
10+
private readonly GethConnector.GethConnector? connector;
11+
private readonly EthAddress? address;
12+
13+
public BalanceChecker(App app)
14+
{
15+
log = new LogPrefixer(app.Log, "(Balance) ");
16+
17+
connector = GethConnector.GethConnector.Initialize(app.Log);
18+
address = LoadAddress(app);
19+
}
20+
21+
private EthAddress? LoadAddress(App app)
22+
{
23+
try
24+
{
25+
if (string.IsNullOrEmpty(app.Config.EthAddressFile)) return null;
26+
if (!File.Exists(app.Config.EthAddressFile)) return null;
27+
28+
return new EthAddress(
29+
File.ReadAllText(app.Config.EthAddressFile)
30+
.Trim()
31+
.Replace("\n", "")
32+
.Replace(Environment.NewLine, "")
33+
);
34+
}
35+
catch (Exception exc)
36+
{
37+
log.Error($"Failed to load eth address from file: {exc}");
38+
return null;
39+
}
40+
}
41+
42+
public void Check()
43+
{
44+
if (connector == null)
45+
{
46+
Log("Connector not configured. Can't check balances.");
47+
return;
48+
}
49+
if (address == null)
50+
{
51+
Log("EthAddress not found. Can't check balances.");
52+
return;
53+
}
54+
55+
try
56+
{
57+
PerformCheck();
58+
}
59+
catch (Exception exc)
60+
{
61+
Log($"Exception while checking balances: {exc}");
62+
}
63+
}
64+
65+
private void PerformCheck()
66+
{
67+
var geth = connector!.GethNode;
68+
var contracts = connector!.CodexContracts;
69+
var addr = address!;
70+
71+
var eth = geth.GetEthBalance(addr);
72+
var tst = contracts.GetTestTokenBalance(addr);
73+
74+
Log($"Balances: [{eth}] - [{tst}]");
75+
76+
if (eth.Eth < 1) TryAddEth(geth, addr);
77+
if (tst.Tst < 1) TryAddTst(contracts, addr);
78+
}
79+
80+
private void TryAddEth(GethPlugin.IGethNode geth, EthAddress addr)
81+
{
82+
try
83+
{
84+
var amount = 100.Eth();
85+
var result = geth.SendEth(addr, amount);
86+
Log($"Successfull added {amount} - {result}");
87+
}
88+
catch (Exception exc)
89+
{
90+
Log("Failed to add eth: " + exc);
91+
}
92+
}
93+
94+
private void TryAddTst(CodexContractsPlugin.ICodexContracts contracts, EthAddress addr)
95+
{
96+
try
97+
{
98+
var amount = 100.Tst();
99+
var result = contracts.MintTestTokens(addr, amount);
100+
Log($"Successfull added {amount} - {result}");
101+
}
102+
catch (Exception exc)
103+
{
104+
Log("Failed to add testtokens: " + exc);
105+
}
106+
}
107+
108+
private void Log(string msg)
109+
{
110+
log.Log(msg);
111+
}
112+
}
113+
}

Tools/AutoClient/Modes/FolderStore/FolderSaver.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ public class FolderSaver
99
private readonly CodexWrapper instance;
1010
private readonly JsonFile<FolderStatus> statusFile;
1111
private readonly FolderStatus status;
12+
private readonly BalanceChecker balanceChecker;
1213
private int changeCounter = 0;
1314
private int failureCount = 0;
1415

1516
public FolderSaver(App app, CodexWrapper instance)
1617
{
1718
this.app = app;
1819
this.instance = instance;
20+
balanceChecker = new BalanceChecker(app);
1921

2022
statusFile = new JsonFile<FolderStatus>(app, Path.Combine(app.Config.FolderToStore, FolderSaverFilename));
2123
status = statusFile.Load();
@@ -27,6 +29,7 @@ public void Run(CancellationTokenSource cts)
2729
if (!folderFiles.Any()) throw new Exception("No files found in " + app.Config.FolderToStore);
2830

2931
changeCounter = 0;
32+
balanceChecker.Check();
3033
foreach (var folderFile in folderFiles)
3134
{
3235
if (cts.IsCancellationRequested) return;
@@ -46,6 +49,7 @@ public void Run(CancellationTokenSource cts)
4649
if (changeCounter > 5)
4750
{
4851
changeCounter = 0;
52+
balanceChecker.Check();
4953
SaveFolderSaverJsonFile();
5054
}
5155

0 commit comments

Comments
 (0)