diff --git a/Tools/BiblioTech/BaseNetCommand.cs b/Tools/BiblioTech/BaseNetCommand.cs index 73f0d09..3036d5b 100644 --- a/Tools/BiblioTech/BaseNetCommand.cs +++ b/Tools/BiblioTech/BaseNetCommand.cs @@ -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); } } diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index fc4e693..cb8de54 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -33,15 +33,18 @@ namespace BiblioTech ProjectPlugin.Load(); 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(); diff --git a/Tools/BiblioTech/TokenCommands/GetBalanceCommand.cs b/Tools/BiblioTech/TokenCommands/GetBalanceCommand.cs index b1a2aaf..59bf4be 100644 --- a/Tools/BiblioTech/TokenCommands/GetBalanceCommand.cs +++ b/Tools/BiblioTech/TokenCommands/GetBalanceCommand.cs @@ -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); diff --git a/Tools/BiblioTech/TokenCommands/MintCommand.cs b/Tools/BiblioTech/TokenCommands/MintCommand.cs new file mode 100644 index 0000000..9d2bf95 --- /dev/null +++ b/Tools/BiblioTech/TokenCommands/MintCommand.cs @@ -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; + } + } +}