Adds admin command to see bot balance

This commit is contained in:
thatben 2025-07-31 11:05:11 +02:00
parent 9499e53bcf
commit f8ee2b8bdb
No known key found for this signature in database
GPG Key ID: 62C543548433D43E
4 changed files with 80 additions and 21 deletions

View File

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

View File

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

View File

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

View File

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