mirror of
https://github.com/logos-storage/logos-storage-nim-cs-dist-tests.git
synced 2026-01-02 13:33:07 +00:00
Adds admin command to see bot balance
This commit is contained in:
parent
9499e53bcf
commit
f8ee2b8bdb
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
40
Tools/BiblioTech/GethLink.cs
Normal file
40
Tools/BiblioTech/GethLink.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user