2023-10-24 13:25:45 +00:00
|
|
|
|
using BiblioTech.Options;
|
|
|
|
|
using CodexContractsPlugin;
|
2023-10-20 08:14:56 +00:00
|
|
|
|
using Core;
|
|
|
|
|
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";
|
2023-10-20 09:20:38 +00:00
|
|
|
|
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
|
|
|
|
|
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-24 13:25:45 +00:00
|
|
|
|
var userId = GetUserId(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-24 13:25:45 +00:00
|
|
|
|
await context.Command.FollowupAsync($"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);
|
|
|
|
|
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-24 13:25:45 +00:00
|
|
|
|
await context.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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|