2023-04-14 10:37:05 +00:00
|
|
|
|
using Logging;
|
2023-04-18 11:22:41 +00:00
|
|
|
|
using Nethereum.ABI.FunctionEncoding.Attributes;
|
|
|
|
|
using Nethereum.Contracts;
|
2023-04-14 10:37:05 +00:00
|
|
|
|
using Nethereum.Hex.HexTypes;
|
2023-04-14 08:51:35 +00:00
|
|
|
|
using Nethereum.Web3;
|
|
|
|
|
using System.Numerics;
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
namespace NethereumWorkflow
|
|
|
|
|
{
|
2023-04-14 10:37:05 +00:00
|
|
|
|
public class NethereumInteraction
|
2023-04-14 08:51:35 +00:00
|
|
|
|
{
|
2023-04-19 07:19:06 +00:00
|
|
|
|
private readonly List<Task> openTasks = new List<Task>();
|
2023-04-14 10:37:05 +00:00
|
|
|
|
private readonly TestLog log;
|
2023-04-14 08:51:35 +00:00
|
|
|
|
private readonly Web3 web3;
|
|
|
|
|
private readonly string rootAccount;
|
|
|
|
|
|
2023-04-14 10:37:05 +00:00
|
|
|
|
internal NethereumInteraction(TestLog log, Web3 web3, string rootAccount)
|
2023-04-14 08:51:35 +00:00
|
|
|
|
{
|
2023-04-14 10:37:05 +00:00
|
|
|
|
this.log = log;
|
2023-04-14 08:51:35 +00:00
|
|
|
|
this.web3 = web3;
|
|
|
|
|
this.rootAccount = rootAccount;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 11:22:41 +00:00
|
|
|
|
public string GetTokenAddress(string marketplaceAddress)
|
|
|
|
|
{
|
|
|
|
|
var function = new GetTokenFunction();
|
|
|
|
|
|
|
|
|
|
var handler = web3.Eth.GetContractQueryHandler<GetTokenFunction>();
|
2023-04-19 07:19:06 +00:00
|
|
|
|
return Time.Wait(handler.QueryAsync<string>(marketplaceAddress, function));
|
|
|
|
|
}
|
2023-04-18 11:22:41 +00:00
|
|
|
|
|
2023-04-19 07:19:06 +00:00
|
|
|
|
public void TransferWeiTo(string account, decimal amount)
|
|
|
|
|
{
|
|
|
|
|
if (amount < 1 || string.IsNullOrEmpty(account)) throw new ArgumentException("Invalid arguments for AddToBalance");
|
|
|
|
|
|
|
|
|
|
var value = ToHexBig(amount);
|
|
|
|
|
var transactionId = Time.Wait(web3.Eth.TransactionManager.SendTransactionAsync(rootAccount, account, value));
|
|
|
|
|
openTasks.Add(web3.Eth.TransactionManager.TransactionReceiptService.PollForReceiptAsync(transactionId));
|
2023-04-18 11:22:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void MintTestTokens(string account, decimal amount, string tokenAddress)
|
2023-04-18 08:22:11 +00:00
|
|
|
|
{
|
|
|
|
|
if (amount < 1 || string.IsNullOrEmpty(account)) throw new ArgumentException("Invalid arguments for MintTestTokens");
|
|
|
|
|
|
2023-04-18 11:22:41 +00:00
|
|
|
|
var function = new MintTokensFunction
|
|
|
|
|
{
|
|
|
|
|
Holder = account,
|
|
|
|
|
Amount = ToBig(amount)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var handler = web3.Eth.GetContractTransactionHandler<MintTokensFunction>();
|
2023-04-19 07:19:06 +00:00
|
|
|
|
openTasks.Add(handler.SendRequestAndWaitForReceiptAsync(tokenAddress, function));
|
2023-04-18 08:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 13:33:12 +00:00
|
|
|
|
public decimal GetBalance(string tokenAddress, string account)
|
2023-04-14 08:51:35 +00:00
|
|
|
|
{
|
2023-04-18 13:33:12 +00:00
|
|
|
|
var function = new GetTokenBalanceFunction
|
|
|
|
|
{
|
|
|
|
|
Owner = account
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var handler = web3.Eth.GetContractQueryHandler<GetTokenBalanceFunction>();
|
|
|
|
|
var result = ToDecimal(Time.Wait(handler.QueryAsync<BigInteger>(tokenAddress, function)));
|
|
|
|
|
|
2023-04-19 07:19:06 +00:00
|
|
|
|
Log($"Balance of {account} is {result} TestTokens.");
|
2023-04-14 10:37:05 +00:00
|
|
|
|
return result;
|
2023-04-14 08:51:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 07:19:06 +00:00
|
|
|
|
public void WaitForAllTransactions()
|
|
|
|
|
{
|
|
|
|
|
var tasks = openTasks.ToArray();
|
|
|
|
|
openTasks.Clear();
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(tasks);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 08:51:35 +00:00
|
|
|
|
private HexBigInteger ToHexBig(decimal amount)
|
|
|
|
|
{
|
2023-04-18 11:22:41 +00:00
|
|
|
|
var bigint = ToBig(amount);
|
2023-04-14 08:51:35 +00:00
|
|
|
|
var str = bigint.ToString("X");
|
|
|
|
|
return new HexBigInteger(str);
|
|
|
|
|
}
|
2023-04-14 10:37:05 +00:00
|
|
|
|
|
2023-04-18 11:22:41 +00:00
|
|
|
|
private BigInteger ToBig(decimal amount)
|
|
|
|
|
{
|
|
|
|
|
return new BigInteger(amount);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 10:37:05 +00:00
|
|
|
|
private decimal ToDecimal(HexBigInteger hexBigInteger)
|
|
|
|
|
{
|
|
|
|
|
return (decimal)hexBigInteger.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 13:33:12 +00:00
|
|
|
|
private decimal ToDecimal(BigInteger bigInteger)
|
|
|
|
|
{
|
|
|
|
|
return (decimal)bigInteger;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 10:37:05 +00:00
|
|
|
|
private void Log(string msg)
|
|
|
|
|
{
|
|
|
|
|
log.Log(msg);
|
|
|
|
|
}
|
2023-04-14 08:51:35 +00:00
|
|
|
|
}
|
2023-04-18 11:22:41 +00:00
|
|
|
|
|
|
|
|
|
[Function("token", "address")]
|
|
|
|
|
public class GetTokenFunction : FunctionMessage
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Function("mint")]
|
|
|
|
|
public class MintTokensFunction : FunctionMessage
|
|
|
|
|
{
|
|
|
|
|
[Parameter("address", "holder", 1)]
|
|
|
|
|
public string Holder { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter("uint256", "amount", 2)]
|
|
|
|
|
public BigInteger Amount { get; set; }
|
|
|
|
|
}
|
2023-04-18 13:33:12 +00:00
|
|
|
|
|
|
|
|
|
[Function("balanceOf", "uint256")]
|
|
|
|
|
public class GetTokenBalanceFunction :FunctionMessage
|
|
|
|
|
{
|
|
|
|
|
[Parameter("address", "owner", 1)]
|
|
|
|
|
public string Owner { get; set; }
|
|
|
|
|
}
|
2023-04-14 08:51:35 +00:00
|
|
|
|
}
|