Adds mint command

This commit is contained in:
benbierens 2023-10-20 10:14:56 +02:00
parent 991927b95f
commit 2b10f2ec58
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 81 additions and 16 deletions

View File

@ -1,15 +1,19 @@
using CodexPlugin;
using CodexContractsPlugin;
using Core;
using Discord.WebSocket;
using GethPlugin;
namespace BiblioTech
{
public abstract class BaseNetCommand : BaseCommand
{
private readonly DeploymentsFilesMonitor monitor;
private readonly CoreInterface ci;
public BaseNetCommand(DeploymentsFilesMonitor monitor)
public BaseNetCommand(DeploymentsFilesMonitor monitor, CoreInterface ci)
{
this.monitor = monitor;
this.ci = ci;
}
protected override async Task Invoke(SocketSlashCommand command)
@ -26,9 +30,16 @@ namespace BiblioTech
return;
}
await Execute(command, deployments.Single());
var codexDeployment = deployments.Single();
var gethDeployment = codexDeployment.GethDeployment;
var contractsDeployment = codexDeployment.CodexContractsDeployment;
var gethNode = ci.WrapGethDeployment(gethDeployment);
var contracts = ci.WrapCodexContractsDeployment(contractsDeployment);
await Execute(command, gethNode, contracts);
}
protected abstract Task Execute(SocketSlashCommand command, CodexDeployment codexDeployment);
protected abstract Task Execute(SocketSlashCommand command, IGethNode gethNode, ICodexContracts contracts);
}
}

View File

@ -33,15 +33,18 @@ namespace BiblioTech
ProjectPlugin.Load<CodexContractsPlugin.CodexContractsPlugin>();
var entryPoint = new EntryPoint(new ConsoleLog(), new KubernetesWorkflow.Configuration(
kubeConfigFile: null, // todo: readonly file
kubeConfigFile: null,
operationTimeout: TimeSpan.FromMinutes(5),
retryDelay: TimeSpan.FromSeconds(10),
kubernetesNamespace: "not-applicable"), "datafiles");
var monitor = new DeploymentsFilesMonitor();
var ci = entryPoint.CreateInterface();
var handler = new CommandHandler(client,
new GetBalanceCommand(monitor));
new GetBalanceCommand(monitor, ci),
new MintCommand(monitor, ci));
await client.LoginAsync(TokenType.Bot, Config.ApplicationToken);
await client.StartAsync();

View File

@ -9,29 +9,21 @@ namespace BiblioTech.TokenCommands
public class GetBalanceCommand : BaseNetCommand
{
private readonly EthAddressOption ethOption = new EthAddressOption();
private readonly CoreInterface ci;
public GetBalanceCommand(DeploymentsFilesMonitor monitor, CoreInterface ci)
: base(monitor)
: base(monitor, ci)
{
this.ci = ci;
}
public override string Name => "balance";
public override string Description => "Shows Eth and TestToken balance of an eth address.";
public override CommandOption[] Options => new[] { ethOption };
protected override async Task Execute(SocketSlashCommand command, CodexDeployment codexDeployment)
protected override async Task Execute(SocketSlashCommand command, IGethNode gethNode, ICodexContracts contracts)
{
var addr = await ethOption.Parse(command);
if (addr == null) return;
var gethDeployment = codexDeployment.GethDeployment;
var contractsDeployment = codexDeployment.CodexContractsDeployment;
var gethNode = ci.WrapGethDeployment(gethDeployment);
var contracts = ci.WrapCodexContractsDeployment(contractsDeployment);
var eth = gethNode.GetEthBalance(addr);
var testTokens = contracts.GetTestTokenBalance(gethNode, addr);

View File

@ -0,0 +1,59 @@
using CodexContractsPlugin;
using Core;
using Discord.WebSocket;
using GethPlugin;
namespace BiblioTech.TokenCommands
{
public class MintCommand : BaseNetCommand
{
private readonly string nl = Environment.NewLine;
private readonly Ether defaultEthToSend = 10.Eth();
private readonly TestToken defaultTestTokensToMint = 1024.TestTokens();
private readonly EthAddressOption ethOption = new EthAddressOption();
public MintCommand(DeploymentsFilesMonitor monitor, CoreInterface ci)
: base(monitor, ci)
{
}
public override string Name => "mint";
public override string Description => "Mint some TestTokens and send some Eth to the address if its balance is low.";
public override CommandOption[] Options => new[] { ethOption };
protected override async Task Execute(SocketSlashCommand command, IGethNode gethNode, ICodexContracts contracts)
{
var addr = await ethOption.Parse(command);
if (addr == null) return;
var report =
ProcessEth(gethNode, addr) +
ProcessTestTokens(gethNode, contracts, addr);
await command.RespondAsync(report);
}
private string ProcessTestTokens(IGethNode gethNode, ICodexContracts contracts, EthAddress addr)
{
var testTokens = contracts.GetTestTokenBalance(gethNode, addr);
if (testTokens.Amount < 64m)
{
contracts.MintTestTokens(gethNode, addr, defaultTestTokensToMint);
return $"Minted {defaultTestTokensToMint}." + nl;
}
return "TestToken balance over threshold." + nl;
}
private string ProcessEth(IGethNode gethNode, EthAddress addr)
{
var eth = gethNode.GetEthBalance(addr);
if (eth.Eth < 1.0m)
{
gethNode.SendEth(addr, defaultEthToSend);
return $"Sent {defaultEthToSend}." + nl;
}
return "Eth balance over threshold." + nl;
}
}
}