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

64 lines
2.3 KiB
C#
Raw Normal View History

2023-10-20 08:14:56 +00:00
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();
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)",
isRequired: true);
2023-10-20 08:14:56 +00:00
public MintCommand(DeploymentsFilesMonitor monitor, CoreInterface ci)
: base(monitor, ci)
{
}
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-20 08:14:56 +00:00
if (addr == null) return;
var report =
2023-10-20 08:14:56 +00:00
ProcessEth(gethNode, addr) +
ProcessTestTokens(gethNode, contracts, addr);
await command.FollowupAsync(report);
2023-10-20 08:14:56 +00:00
}
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;
}
}
}