2
0
mirror of synced 2025-02-02 19:53:29 +00:00

All works

This commit is contained in:
benbierens 2023-10-22 11:10:45 +02:00
parent e16b1ce079
commit 8ef2e6023e
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
7 changed files with 54 additions and 24 deletions

View File

@ -11,6 +11,7 @@ namespace BiblioTech.Commands
public override string Name => "clear"; public override string Name => "clear";
public override string StartingMessage => "Hold on..."; public override string StartingMessage => "Hold on...";
public override string Description => "Admin only. Clears current Eth address for a user, allowing them to set a new one."; public override string Description => "Admin only. Clears current Eth address for a user, allowing them to set a new one.";
public override CommandOption[] Options => new[] { user };
protected override async Task Invoke(SocketSlashCommand command) protected override async Task Invoke(SocketSlashCommand command)
{ {

View File

@ -13,7 +13,7 @@ namespace BiblioTech.Commands
public override string Name => "deployments"; public override string Name => "deployments";
public override string StartingMessage => "Fetching deployments information..."; public override string StartingMessage => "Fetching deployments information...";
public override string Description => "Lists known deployments"; public override string Description => "Lists active TestNet deployments";
protected override async Task Invoke(SocketSlashCommand command) protected override async Task Invoke(SocketSlashCommand command)
{ {

View File

@ -7,16 +7,17 @@ namespace BiblioTech.Commands
{ {
public class MintCommand : BaseNetCommand public class MintCommand : BaseNetCommand
{ {
private readonly string nl = Environment.NewLine;
private readonly Ether defaultEthToSend = 10.Eth(); private readonly Ether defaultEthToSend = 10.Eth();
private readonly TestToken defaultTestTokensToMint = 1024.TestTokens(); private readonly TestToken defaultTestTokensToMint = 1024.TestTokens();
private readonly UserOption optionalUser = new UserOption( private readonly UserOption optionalUser = new UserOption(
description: "If set, mint tokens for this user. (Optional, admin-only)", description: "If set, mint tokens for this user. (Optional, admin-only)",
isRequired: true); isRequired: false);
private readonly UserAssociateCommand userAssociateCommand;
public MintCommand(DeploymentsFilesMonitor monitor, CoreInterface ci) public MintCommand(DeploymentsFilesMonitor monitor, CoreInterface ci, UserAssociateCommand userAssociateCommand)
: base(monitor, ci) : base(monitor, ci)
{ {
this.userAssociateCommand = userAssociateCommand;
} }
public override string Name => "mint"; public override string Name => "mint";
@ -28,36 +29,57 @@ namespace BiblioTech.Commands
{ {
var userId = GetUserId(optionalUser, command); var userId = GetUserId(optionalUser, command);
var addr = Program.UserRepo.GetCurrentAddressForUser(userId); var addr = Program.UserRepo.GetCurrentAddressForUser(userId);
if (addr == null) return; 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 = var report = new List<string>();
ProcessEth(gethNode, addr) +
ProcessTestTokens(gethNode, contracts, addr);
await command.FollowupAsync(report); var sentEth = ProcessEth(gethNode, addr, report);
var mintedTokens = ProcessTokens(gethNode, contracts, addr, report);
Program.UserRepo.AddMintEventForUser(userId, addr, sentEth, mintedTokens);
await command.FollowupAsync(string.Join(Environment.NewLine, report));
} }
private string ProcessTestTokens(IGethNode gethNode, ICodexContracts contracts, EthAddress addr) private TestToken ProcessTokens(IGethNode gethNode, ICodexContracts contracts, EthAddress addr, List<string> report)
{ {
var testTokens = contracts.GetTestTokenBalance(gethNode, addr); if (ShouldMintTestTokens(gethNode, contracts, addr))
if (testTokens.Amount < 64m)
{ {
contracts.MintTestTokens(gethNode, addr, defaultTestTokensToMint); contracts.MintTestTokens(gethNode, addr, defaultTestTokensToMint);
return $"Minted {defaultTestTokensToMint}." + nl; report.Add($"Minted {defaultTestTokensToMint}.");
return defaultTestTokensToMint;
} }
return "TestToken balance over threshold." + nl;
report.Add("TestToken balance over threshold.");
return 0.TestTokens();
} }
private string ProcessEth(IGethNode gethNode, EthAddress addr) private Ether ProcessEth(IGethNode gethNode, EthAddress addr, List<string> report)
{ {
var eth = gethNode.GetEthBalance(addr); if (ShouldSendEth(gethNode, addr))
if (eth.Eth < 1.0m)
{ {
gethNode.SendEth(addr, defaultEthToSend); gethNode.SendEth(addr, defaultEthToSend);
return $"Sent {defaultEthToSend}." + nl; report.Add($"Sent {defaultEthToSend}.");
return defaultEthToSend;
} }
report.Add("Eth balance is over threshold.");
return 0.Eth();
}
return "Eth balance over threshold." + nl; 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;
} }
} }
} }

View File

@ -11,6 +11,7 @@ namespace BiblioTech.Commands
public override string Name => "report"; public override string Name => "report";
public override string StartingMessage => "Getting that data..."; public override string StartingMessage => "Getting that data...";
public override string Description => "Admin only. Reports bot-interaction history for a user."; public override string Description => "Admin only. Reports bot-interaction history for a user.";
public override CommandOption[] Options => new[] { user };
protected override async Task Invoke(SocketSlashCommand command) protected override async Task Invoke(SocketSlashCommand command)
{ {

View File

@ -11,10 +11,8 @@ namespace BiblioTech.Commands
public override string Name => "set"; public override string Name => "set";
public override string StartingMessage => "hold on..."; public override string StartingMessage => "hold on...";
public override string Description => "Associates a Discord user with an Ethereum address in the TestNet. " + public override string Description => "Associates a Discord user with an Ethereum address.";
"Warning: You can set your Ethereum address only once! Double-check before hitting enter."; public override CommandOption[] Options => new CommandOption[] { ethOption, optionalUser };
public override CommandOption[] Options => new[] { ethOption };
protected override async Task Invoke(SocketSlashCommand command) protected override async Task Invoke(SocketSlashCommand command)
{ {

View File

@ -15,5 +15,8 @@ namespace BiblioTech
[Uniform("userdata", "u", "USERDATA", false, "Path where user data files will be saved.")] [Uniform("userdata", "u", "USERDATA", false, "Path where user data files will be saved.")]
public string UserDataPath { get; set; } = "userdata"; public string UserDataPath { get; set; } = "userdata";
[Uniform("admin-role", "a", "ADMINROLE", true, "Name of the Discord server admin role")]
public string AdminRoleName { get; set; } = string.Empty;
} }
} }

View File

@ -21,6 +21,11 @@ namespace BiblioTech
var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args); var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args);
Config = uniformArgs.Parse(); Config = uniformArgs.Parse();
if (!Directory.Exists(Config.UserDataPath))
{
Directory.CreateDirectory(Config.UserDataPath);
}
return new Program().MainAsync(); return new Program().MainAsync();
} }
@ -48,7 +53,7 @@ namespace BiblioTech
var handler = new CommandHandler(client, var handler = new CommandHandler(client,
new ClearUserAssociationCommand(), new ClearUserAssociationCommand(),
new GetBalanceCommand(monitor, ci, associateCommand), new GetBalanceCommand(monitor, ci, associateCommand),
new MintCommand(monitor, ci), new MintCommand(monitor, ci, associateCommand),
new ReportHistoryCommand(), new ReportHistoryCommand(),
associateCommand, associateCommand,
new DeploymentsCommand(monitor) new DeploymentsCommand(monitor)