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
|
|
|
|
|
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-12-15 10:09:15 +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
|
|
|
|
{
|
2024-03-29 10:24:11 +00:00
|
|
|
|
var tokens = Program.Config.MintTT.TestTokens();
|
|
|
|
|
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.)");
|
2023-12-15 10:09:15 +00:00
|
|
|
|
return null;
|
2023-10-20 08:14:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 10:09:15 +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
|
|
|
|
{
|
2024-03-29 10:24:11 +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.)");
|
2023-12-15 10:09:15 +00:00
|
|
|
|
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);
|
2024-03-29 10:24:11 +00:00
|
|
|
|
return testTokens.Amount < Program.Config.MintTT;
|
2023-10-22 09:10:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ShouldSendEth(IGethNode gethNode, EthAddress addr)
|
|
|
|
|
{
|
|
|
|
|
var eth = gethNode.GetEthBalance(addr);
|
2024-03-29 10:24:11 +00:00
|
|
|
|
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}";
|
2023-12-18 14:38:16 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|