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

86 lines
3.3 KiB
C#
Raw Normal View History

2023-10-20 08:14:56 +00:00
using CodexContractsPlugin;
using Core;
using Discord.WebSocket;
using GethPlugin;
2023-10-22 08:38:46 +00:00
namespace BiblioTech.Commands
2023-10-20 08:14:56 +00:00
{
public class MintCommand : BaseNetCommand
{
private readonly Ether defaultEthToSend = 10.Eth();
private readonly TestToken defaultTestTokensToMint = 1024.TestTokens();
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
2023-10-22 09:10:45 +00:00
public MintCommand(DeploymentsFilesMonitor monitor, CoreInterface ci, UserAssociateCommand userAssociateCommand)
2023-10-20 08:14:56 +00:00
: base(monitor, ci)
{
2023-10-22 09:10:45 +00:00
this.userAssociateCommand = userAssociateCommand;
2023-10-20 08:14:56 +00:00
}
public override string Name => "mint";
public override string StartingMessage => "Minting some tokens...";
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
protected override async Task Execute(SocketSlashCommand command, IGethNode gethNode, ICodexContracts contracts)
{
2023-10-22 08:10:52 +00:00
var userId = GetUserId(optionalUser, command);
var addr = Program.UserRepo.GetCurrentAddressForUser(userId);
2023-10-22 09:10:45 +00:00
if (addr == null)
{
await command.FollowupAsync($"No address has been set for this user. Please use '/{userAssociateCommand.Name}' to set it first.");
return;
}
var report = new List<string>();
var sentEth = ProcessEth(gethNode, addr, report);
var mintedTokens = ProcessTokens(gethNode, contracts, addr, report);
2023-10-20 08:14:56 +00:00
2023-10-22 09:10:45 +00:00
Program.UserRepo.AddMintEventForUser(userId, addr, sentEth, mintedTokens);
2023-10-20 08:14:56 +00:00
2023-10-22 09:10:45 +00:00
await command.FollowupAsync(string.Join(Environment.NewLine, report));
2023-10-20 08:14:56 +00:00
}
2023-10-22 09:10:45 +00:00
private TestToken ProcessTokens(IGethNode gethNode, ICodexContracts contracts, EthAddress addr, List<string> report)
2023-10-20 08:14:56 +00:00
{
2023-10-22 09:10:45 +00:00
if (ShouldMintTestTokens(gethNode, contracts, addr))
2023-10-20 08:14:56 +00:00
{
contracts.MintTestTokens(gethNode, addr, defaultTestTokensToMint);
2023-10-22 09:10:45 +00:00
report.Add($"Minted {defaultTestTokensToMint}.");
return defaultTestTokensToMint;
2023-10-20 08:14:56 +00:00
}
2023-10-22 09:10:45 +00:00
report.Add("TestToken balance over threshold.");
return 0.TestTokens();
2023-10-20 08:14:56 +00:00
}
2023-10-22 09:10:45 +00:00
private 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
{
gethNode.SendEth(addr, defaultEthToSend);
2023-10-22 09:10:45 +00:00
report.Add($"Sent {defaultEthToSend}.");
return defaultEthToSend;
2023-10-20 08:14:56 +00:00
}
2023-10-22 09:10:45 +00:00
report.Add("Eth balance is over threshold.");
return 0.Eth();
}
2023-10-20 08:14:56 +00:00
2023-10-22 09:10:45 +00:00
private bool ShouldMintTestTokens(IGethNode gethNode, ICodexContracts contracts, EthAddress addr)
{
var testTokens = contracts.GetTestTokenBalance(gethNode, addr);
return testTokens.Amount < 64m;
}
private bool ShouldSendEth(IGethNode gethNode, EthAddress addr)
{
var eth = gethNode.GetEthBalance(addr);
return eth.Eth < 1.0m;
2023-10-20 08:14:56 +00:00
}
}
}