diff --git a/Tools/BiblioTech/BaseGethCommand.cs b/Tools/BiblioTech/BaseGethCommand.cs index 104784cc..d3e0995d 100644 --- a/Tools/BiblioTech/BaseGethCommand.cs +++ b/Tools/BiblioTech/BaseGethCommand.cs @@ -8,15 +8,14 @@ namespace BiblioTech { protected override async Task Invoke(CommandContext context) { - var gethConnector = GetGeth(); - if (gethConnector == null) + if (Program.GethLink == null) { await context.Followup("Blockchain operations are (temporarily) unavailable."); return; } - var gethNode = gethConnector.GethNode; - var contracts = gethConnector.CodexContracts; + var gethNode = Program.GethLink.Node; + var contracts = Program.GethLink.Contracts; if (!contracts.IsDeployed()) { @@ -27,19 +26,6 @@ namespace BiblioTech await Execute(context, gethNode, contracts); } - private GethConnector.GethConnector? GetGeth() - { - try - { - return GethConnector.GethConnector.Initialize(Program.Log); - } - catch (Exception ex) - { - Program.Log.Error("Failed to initialize geth connector: " + ex); - return null; - } - } - protected abstract Task Execute(CommandContext context, IGethNode gethNode, ICodexContracts contracts); } } diff --git a/Tools/BiblioTech/Commands/AdminCommand.cs b/Tools/BiblioTech/Commands/AdminCommand.cs index f9877181..4a440bf4 100644 --- a/Tools/BiblioTech/Commands/AdminCommand.cs +++ b/Tools/BiblioTech/Commands/AdminCommand.cs @@ -9,6 +9,7 @@ namespace BiblioTech.Commands private readonly ReportCommand reportCommand = new ReportCommand(); private readonly WhoIsCommand whoIsCommand = new WhoIsCommand(); private readonly LogReplaceCommand logReplaceCommand; + private readonly BalanceCommand balanceCommand = new BalanceCommand(); public AdminCommand(CustomReplacement replacement) { @@ -19,13 +20,14 @@ namespace BiblioTech.Commands public override string StartingMessage => "..."; public override string Description => "Admins only."; - public override CommandOption[] Options => new CommandOption[] - { + public override CommandOption[] Options => + [ clearCommand, reportCommand, whoIsCommand, - logReplaceCommand - }; + logReplaceCommand, + balanceCommand + ]; protected override async Task Invoke(CommandContext context) { @@ -45,6 +47,7 @@ namespace BiblioTech.Commands await reportCommand.CommandHandler(context); await whoIsCommand.CommandHandler(context); await logReplaceCommand.CommandHandler(context); + await balanceCommand.CommandHandler(context); } public class ClearUserAssociationCommand : SubCommandOption @@ -182,5 +185,32 @@ namespace BiblioTech.Commands } } } + + public class BalanceCommand : SubCommandOption + { + public BalanceCommand() + : base(name: "balance", + description: "Shows ETH and TST balance of the bot.") + { + } + + protected override async Task onSubCommand(CommandContext context) + { + if (Program.GethLink == null) + { + await context.Followup("Geth connection not available."); + return; + } + + var node = Program.GethLink.Node; + var contracts = Program.GethLink.Contracts; + + var address = node.CurrentAddress; + var eth = node.GetEthBalance(); + var tst = contracts.GetTestTokenBalance(address); + + await context.Followup($"Bot account ({address}) has {eth} and {tst}"); + } + } } } diff --git a/Tools/BiblioTech/GethLink.cs b/Tools/BiblioTech/GethLink.cs new file mode 100644 index 00000000..a619f3ef --- /dev/null +++ b/Tools/BiblioTech/GethLink.cs @@ -0,0 +1,40 @@ +using CodexContractsPlugin; +using GethPlugin; + +namespace BiblioTech +{ + public class GethLink + { + private GethLink(IGethNode node, ICodexContracts contracts) + { + Node = node; + Contracts = contracts; + } + + public IGethNode Node { get; } + public ICodexContracts Contracts { get; } + + public static GethLink? Create() + { + var gethConnector = GetGeth(); + if (gethConnector == null) return null; + + var gethNode = gethConnector.GethNode; + var contracts = gethConnector.CodexContracts; + return new GethLink(gethNode, contracts); + } + + private static GethConnector.GethConnector? GetGeth() + { + try + { + return GethConnector.GethConnector.Initialize(Program.Log); + } + catch (Exception ex) + { + Program.Log.Error("Failed to initialize geth connector: " + ex); + return null; + } + } + } +} diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index 28662388..d6147561 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -21,6 +21,7 @@ namespace BiblioTech public static ChainActivityHandler ChainActivityHandler { get; set; } = null!; public static ChainEventsSender EventsSender { get; set; } = null!; public static ILog Log { get; private set; } = null!; + public static GethLink? GethLink { get; private set; } = null; public static Task Main(string[] args) { @@ -32,6 +33,8 @@ namespace BiblioTech new ConsoleLog() ); + GethLink = GethLink.Create(); + Dispatcher = new CallDispatcher(Log); EnsurePath(Config.DataPath);