cs-codex-dist-tests/Tools/BiblioTech/Commands/MintCommand.cs

105 lines
4.2 KiB
C#
Raw Normal View History

2023-10-24 13:25:45 +00:00
using BiblioTech.Options;
using CodexContractsPlugin;
2023-10-20 08:14:56 +00:00
using GethPlugin;
2023-10-22 08:38:46 +00:00
namespace BiblioTech.Commands
2023-10-20 08:14:56 +00:00
{
2023-11-02 11:30:48 +00:00
public class MintCommand : BaseGethCommand
2023-10-20 08:14:56 +00:00
{
2023-10-22 08:10:52 +00:00
private readonly UserOption optionalUser = new UserOption(
description: "If set, mint tokens for this user. (Optional, admin-only)",
2023-10-22 09:10:45 +00:00
isRequired: false);
private readonly UserAssociateCommand userAssociateCommand;
2023-10-20 08:14:56 +00:00
public MintCommand(UserAssociateCommand userAssociateCommand)
2023-10-20 08:14:56 +00:00
{
2023-10-22 09:10:45 +00:00
this.userAssociateCommand = userAssociateCommand;
2023-10-20 08:14:56 +00:00
}
public override string Name => "mint";
2023-11-02 11:30:48 +00:00
public override string StartingMessage => RandomBusyMessage.Get();
2023-10-22 08:10:52 +00:00
public override string Description => "Mint some TestTokens and send some Eth to the user if their balance is low.";
public override CommandOption[] Options => new[] { optionalUser };
2023-10-20 08:14:56 +00:00
2023-10-24 13:25:45 +00:00
protected override async Task Execute(CommandContext context, IGethNode gethNode, ICodexContracts contracts)
2023-10-20 08:14:56 +00:00
{
2023-10-25 09:25:27 +00:00
var userId = GetUserFromCommand(optionalUser, context);
2023-10-22 08:10:52 +00:00
var addr = Program.UserRepo.GetCurrentAddressForUser(userId);
2023-10-22 09:10:45 +00:00
if (addr == null)
{
2023-10-25 08:54:26 +00:00
await context.Followup($"No address has been set for this user. Please use '/{userAssociateCommand.Name}' to set it first.");
await Program.AdminChecker.SendInAdminChannel($"User {Mention(userId)} used '/{Name}' but address has not been set.");
2023-10-22 09:10:45 +00:00
return;
}
var report = new List<string>();
Transaction<Ether>? sentEth = null;
Transaction<TestToken>? mintedTokens = null;
await Task.Run(() =>
{
sentEth = ProcessEth(gethNode, addr, report);
mintedTokens = ProcessTokens(contracts, addr, report);
});
2023-10-20 08:14:56 +00:00
var reportLine = string.Join(Environment.NewLine, report);
2023-10-22 09:10:45 +00:00
Program.UserRepo.AddMintEventForUser(userId, addr, sentEth, mintedTokens);
await Program.AdminChecker.SendInAdminChannel($"User {Mention(userId)} used '/{Name}' successfully. ({reportLine})");
2023-10-20 08:14:56 +00:00
await context.Followup(reportLine);
}
private string Format<T>(Transaction<T>? transaction)
{
if (transaction == null) return "-";
return transaction.ToString();
2023-10-20 08:14:56 +00:00
}
private Transaction<TestToken>? ProcessTokens(ICodexContracts contracts, EthAddress addr, List<string> report)
2023-10-20 08:14:56 +00:00
{
2023-10-30 12:30:14 +00:00
if (ShouldMintTestTokens(contracts, addr))
2023-10-20 08:14:56 +00:00
{
var tokens = Program.Config.MintTT.TstWei();
var transaction = contracts.MintTestTokens(addr, tokens);
report.Add($"Minted {tokens} {FormatTransactionLink(transaction)}");
return new Transaction<TestToken>(tokens, transaction);
2023-10-20 08:14:56 +00:00
}
2023-10-22 09:10:45 +00:00
2023-12-15 10:02:06 +00:00
report.Add("TestToken balance over threshold. (No TestTokens minted.)");
return null;
2023-10-20 08:14:56 +00:00
}
private Transaction<Ether>? ProcessEth(IGethNode gethNode, EthAddress addr, List<string> report)
2023-10-20 08:14:56 +00:00
{
2023-10-22 09:10:45 +00:00
if (ShouldSendEth(gethNode, addr))
2023-10-20 08:14:56 +00:00
{
var eth = Program.Config.SendEth.Eth();
var transaction = gethNode.SendEth(addr, eth);
report.Add($"Sent {eth} {FormatTransactionLink(transaction)}");
return new Transaction<Ether>(eth, transaction);
2023-10-20 08:14:56 +00:00
}
2023-12-15 10:02:06 +00:00
report.Add("Eth balance is over threshold. (No Eth sent.)");
return null;
2023-10-22 09:10:45 +00:00
}
2023-10-20 08:14:56 +00:00
2023-10-30 12:30:14 +00:00
private bool ShouldMintTestTokens(ICodexContracts contracts, EthAddress addr)
2023-10-22 09:10:45 +00:00
{
2023-10-30 12:30:14 +00:00
var testTokens = contracts.GetTestTokenBalance(addr);
return testTokens < Program.Config.MintTT.TstWei();
2023-10-22 09:10:45 +00:00
}
private bool ShouldSendEth(IGethNode gethNode, EthAddress addr)
{
var eth = gethNode.GetEthBalance(addr);
return eth.Eth < Program.Config.SendEth;
2023-10-20 08:14:56 +00:00
}
2023-12-15 10:02:06 +00:00
private string FormatTransactionLink(string transaction)
{
2023-12-18 10:27:28 +00:00
var url = $"https://explorer.testnet.codex.storage/tx/{transaction}";
return $"- [View on block explorer](<{url}>){Environment.NewLine}Transaction ID - `{transaction}`";
2023-12-15 10:02:06 +00:00
}
2023-10-20 08:14:56 +00:00
}
}