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
|
|
|
|
{
|
|
|
|
|
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-12-11 10:01:12 +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.");
|
2023-10-22 09:10:45 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var report = new List<string>();
|
|
|
|
|
|
|
|
|
|
var sentEth = ProcessEth(gethNode, addr, report);
|
2023-10-30 12:30:14 +00:00
|
|
|
|
var mintedTokens = ProcessTokens(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-25 08:54:26 +00:00
|
|
|
|
await context.Followup(string.Join(Environment.NewLine, report));
|
2023-10-20 08:14:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 12:30:14 +00:00
|
|
|
|
private 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
|
|
|
|
{
|
2023-10-30 12:30:14 +00:00
|
|
|
|
contracts.MintTestTokens(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-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);
|
2023-10-22 09:10:45 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|