mirror of
https://github.com/logos-storage/logos-storage-nim-cs-dist-tests.git
synced 2026-01-02 13:33:07 +00:00
Mint command can now transfer tokens as well
This commit is contained in:
parent
f8ee2b8bdb
commit
7427c80d77
@ -20,7 +20,7 @@ namespace CodexContractsPlugin
|
||||
string MintTestTokens(EthAddress ethAddress, TestToken testTokens);
|
||||
TestToken GetTestTokenBalance(IHasEthAddress owner);
|
||||
TestToken GetTestTokenBalance(EthAddress ethAddress);
|
||||
void TransferTestTokens(EthAddress to, TestToken amount);
|
||||
string TransferTestTokens(EthAddress to, TestToken amount);
|
||||
|
||||
ICodexContractsEvents GetEvents(TimeRange timeRange);
|
||||
ICodexContractsEvents GetEvents(BlockInterval blockInterval);
|
||||
@ -96,9 +96,9 @@ namespace CodexContractsPlugin
|
||||
return balance.TstWei();
|
||||
}
|
||||
|
||||
public void TransferTestTokens(EthAddress to, TestToken amount)
|
||||
public string TransferTestTokens(EthAddress to, TestToken amount)
|
||||
{
|
||||
StartInteraction().TransferTestTokens(Deployment.TokenAddress, to.Address, amount.TstWei);
|
||||
return StartInteraction().TransferTestTokens(Deployment.TokenAddress, to.Address, amount.TstWei);
|
||||
}
|
||||
|
||||
public ICodexContractsEvents GetEvents(TimeRange timeRange)
|
||||
|
||||
@ -59,7 +59,7 @@ namespace CodexContractsPlugin
|
||||
return gethNode.Call<BalanceOfFunction, BigInteger>(tokenAddress, function).ToDecimal();
|
||||
}
|
||||
|
||||
public void TransferTestTokens(string tokenAddress, string toAccount, BigInteger amount)
|
||||
public string TransferTestTokens(string tokenAddress, string toAccount, BigInteger amount)
|
||||
{
|
||||
log.Debug($"({tokenAddress}) {toAccount} {amount}");
|
||||
var function = new TransferFunction
|
||||
@ -68,7 +68,7 @@ namespace CodexContractsPlugin
|
||||
Value = amount
|
||||
};
|
||||
|
||||
gethNode.SendTransaction(tokenAddress, function);
|
||||
return gethNode.SendTransaction(tokenAddress, function);
|
||||
}
|
||||
|
||||
public GetRequestOutputDTO GetRequest(string marketplaceAddress, byte[] requestId)
|
||||
|
||||
@ -38,10 +38,10 @@ namespace BiblioTech.Commands
|
||||
Transaction<Ether>? sentEth = null;
|
||||
Transaction<TestToken>? mintedTokens = null;
|
||||
|
||||
await Task.Run(() =>
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
sentEth = ProcessEth(gethNode, addr, report);
|
||||
mintedTokens = ProcessTokens(contracts, addr, report);
|
||||
mintedTokens = await ProcessTestTokens(contracts, addr, report);
|
||||
});
|
||||
|
||||
var reportLine = string.Join(Environment.NewLine, report);
|
||||
@ -51,49 +51,88 @@ namespace BiblioTech.Commands
|
||||
await context.Followup(reportLine);
|
||||
}
|
||||
|
||||
private string Format<T>(Transaction<T>? transaction)
|
||||
private async Task<Transaction<TestToken>?> ProcessTestTokens(ICodexContracts contracts, EthAddress addr, List<string> report)
|
||||
{
|
||||
if (transaction == null) return "-";
|
||||
return transaction.ToString();
|
||||
}
|
||||
|
||||
private Transaction<TestToken>? ProcessTokens(ICodexContracts contracts, EthAddress addr, List<string> report)
|
||||
{
|
||||
if (ShouldMintTestTokens(contracts, addr))
|
||||
if (IsTestTokenBalanceOverLimit(contracts, addr))
|
||||
{
|
||||
var tokens = Program.Config.MintTT.TstWei();
|
||||
var transaction = contracts.MintTestTokens(addr, tokens);
|
||||
report.Add($"Minted {tokens} {FormatTransactionLink(transaction)}");
|
||||
return new Transaction<TestToken>(tokens, transaction);
|
||||
report.Add("TestToken balance over threshold. (No TestTokens sent or minted.)");
|
||||
return null;
|
||||
}
|
||||
|
||||
report.Add("TestToken balance over threshold. (No TestTokens minted.)");
|
||||
return null;
|
||||
|
||||
var sent = await TransferTestTokens(contracts, addr, report);
|
||||
var minted = MintTestTokens(contracts, addr, report);
|
||||
|
||||
return new Transaction<TestToken>(sent.Item1, minted.Item1, $"{sent.Item2},{minted.Item2}");
|
||||
}
|
||||
|
||||
private Transaction<Ether>? ProcessEth(IGethNode gethNode, EthAddress addr, List<string> report)
|
||||
{
|
||||
if (ShouldSendEth(gethNode, addr))
|
||||
if (IsEthBalanceOverLimit(gethNode, addr))
|
||||
{
|
||||
var eth = Program.Config.SendEth.Eth();
|
||||
var transaction = gethNode.SendEth(addr, eth);
|
||||
report.Add($"Sent {eth} {FormatTransactionLink(transaction)}");
|
||||
return new Transaction<Ether>(eth, transaction);
|
||||
report.Add("Eth balance is over threshold. (No Eth sent.)");
|
||||
return null;
|
||||
}
|
||||
report.Add("Eth balance is over threshold. (No Eth sent.)");
|
||||
return null;
|
||||
var eth = Program.Config.SendEth.Eth();
|
||||
var transaction = gethNode.SendEth(addr, eth);
|
||||
report.Add($"Sent {eth} {FormatTransactionLink(transaction)}");
|
||||
return new Transaction<Ether>(eth, 0.Eth(), transaction);
|
||||
}
|
||||
|
||||
private bool ShouldMintTestTokens(ICodexContracts contracts, EthAddress addr)
|
||||
private (TestToken, string) MintTestTokens(ICodexContracts contracts, EthAddress addr, List<string> report)
|
||||
{
|
||||
var testTokens = contracts.GetTestTokenBalance(addr);
|
||||
return testTokens < Program.Config.MintTT.TstWei();
|
||||
if (Program.Config.MintTT < 1) return (0.TstWei(), string.Empty);
|
||||
var tokens = Program.Config.MintTT.TstWei();
|
||||
var transaction = contracts.MintTestTokens(addr, tokens);
|
||||
report.Add($"Minted {tokens} {FormatTransactionLink(transaction)}");
|
||||
return (tokens, transaction);
|
||||
}
|
||||
|
||||
private bool ShouldSendEth(IGethNode gethNode, EthAddress addr)
|
||||
private async Task<(TestToken, string)> TransferTestTokens(ICodexContracts contracts, EthAddress addr, List<string> report)
|
||||
{
|
||||
var nothing = (0.TstWei(), string.Empty);
|
||||
if (Program.Config.SendTT < 1) return nothing;
|
||||
if (Program.GethLink == null)
|
||||
{
|
||||
report.Add("Transaction operations are currently not available.");
|
||||
return nothing;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var amount = Program.Config.SendTT.TstWei();
|
||||
if (contracts.GetTestTokenBalance(Program.GethLink.Node.CurrentAddress).TstWei <= amount.TstWei)
|
||||
{
|
||||
report.Add("Unable to send TestTokens: Bot doesn't have enough! (Admins have been notified)");
|
||||
await Program.AdminChecker.SendInAdminChannel($"{nameof(MintCommand)} failed: Bot has insufficient tokens.");
|
||||
return nothing;
|
||||
}
|
||||
|
||||
var transaction = contracts.TransferTestTokens(addr, amount);
|
||||
report.Add($"Transferred {amount} {FormatTransactionLink(transaction)}");
|
||||
return (amount, transaction);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
report.Add("I'm sorry! Something went unexpectedly wrong. (Admins have been notified)");
|
||||
await Program.AdminChecker.SendInAdminChannel($"{nameof(MintCommand)} failed with: {ex}");
|
||||
}
|
||||
return nothing;
|
||||
}
|
||||
|
||||
private bool IsEthBalanceOverLimit(IGethNode gethNode, EthAddress addr)
|
||||
{
|
||||
var eth = gethNode.GetEthBalance(addr);
|
||||
return ((decimal)eth.Eth) < Program.Config.SendEth;
|
||||
return ((decimal)eth.Eth) > Program.Config.SendEth;
|
||||
}
|
||||
|
||||
private bool IsTestTokenBalanceOverLimit(ICodexContracts contracts, EthAddress addr)
|
||||
{
|
||||
var testTokens = contracts.GetTestTokenBalance(addr);
|
||||
|
||||
if (Program.Config.MintTT > 0 && testTokens > Program.Config.MintTT.TstWei()) return true;
|
||||
if (Program.Config.SendTT > 0 && testTokens > Program.Config.SendTT.TstWei()) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private string FormatTransactionLink(string transaction)
|
||||
|
||||
@ -35,6 +35,9 @@ namespace BiblioTech
|
||||
[Uniform("mint-tt", "mt", "MINTTT", true, "Amount of TSTWEI minted by the mint command.")]
|
||||
public BigInteger MintTT { get; set; } = 1073741824;
|
||||
|
||||
[Uniform("send-tt", "st", "SENDTT", true, "Amount of TSTWEI sent from the bot account by the mint command.")]
|
||||
public BigInteger SendTT { get; set; } = 1073741824;
|
||||
|
||||
[Uniform("no-discord", "nd", "NODISCORD", false, "For debugging: Bypasses all Discord API calls.")]
|
||||
public int NoDiscord { get; set; } = 0;
|
||||
|
||||
|
||||
@ -2,19 +2,25 @@
|
||||
{
|
||||
public class Transaction<T>
|
||||
{
|
||||
public Transaction(T tokenAmount, string transactionHash)
|
||||
public Transaction(T amountSent, T amountMinted, string transactionHash)
|
||||
{
|
||||
TokenAmount = tokenAmount;
|
||||
AmountSent = amountSent;
|
||||
AmountMinted = amountMinted;
|
||||
TransactionHash = transactionHash;
|
||||
}
|
||||
|
||||
public T TokenAmount { get; }
|
||||
public T AmountSent { get; }
|
||||
public T AmountMinted { get; }
|
||||
public string TransactionHash { get; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (TokenAmount == null) return "NULL";
|
||||
return TokenAmount.ToString()!;
|
||||
var result = "";
|
||||
if (AmountSent == null) result += "sent:null";
|
||||
else result += "send:" + AmountSent.ToString();
|
||||
if (AmountMinted == null) result += " minted:null";
|
||||
else result += " minted:" + AmountMinted.ToString();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,11 +91,11 @@ namespace BiblioTech
|
||||
{
|
||||
if (me.EthReceived != null)
|
||||
{
|
||||
result.Add($"{me.Utc.ToString("o")} - Sent {me.EthReceived.TokenAmount} to {me.UsedAddress}. ({me.EthReceived.TransactionHash})");
|
||||
result.Add($"{me.Utc.ToString("o")} - {me.EthReceived.ToString()} to {me.UsedAddress}. ({me.EthReceived.TransactionHash})");
|
||||
}
|
||||
if (me.TestTokensMinted != null)
|
||||
{
|
||||
result.Add($"{me.Utc.ToString("o")} - Minted {me.TestTokensMinted.TokenAmount} to {me.UsedAddress}. ({me.TestTokensMinted.TransactionHash})");
|
||||
result.Add($"{me.Utc.ToString("o")} - {me.TestTokensMinted.ToString()} to {me.UsedAddress}. ({me.TestTokensMinted.TransactionHash})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user