2023-04-25 09:31:15 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
using Nethereum.ABI.FunctionEncoding.Attributes;
|
2023-04-18 11:22:41 +00:00
|
|
|
|
using Nethereum.Contracts;
|
2023-04-14 10:37:05 +00:00
|
|
|
|
using Nethereum.Hex.HexTypes;
|
2023-09-19 09:51:59 +00:00
|
|
|
|
using Nethereum.RPC.Eth.DTOs;
|
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-09-15 14:27:08 +00:00
|
|
|
|
private readonly ILog log;
|
2023-04-14 08:51:35 +00:00
|
|
|
|
private readonly Web3 web3;
|
|
|
|
|
|
2023-09-15 14:27:08 +00:00
|
|
|
|
internal NethereumInteraction(ILog log, Web3 web3)
|
2023-04-14 08:51:35 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
this.log = log;
|
2023-04-14 08:51:35 +00:00
|
|
|
|
this.web3 = web3;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 09:51:59 +00:00
|
|
|
|
public void SendEth(string toAddress, decimal ethAmount)
|
|
|
|
|
{
|
|
|
|
|
var receipt = Time.Wait(web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(toAddress, ethAmount));
|
|
|
|
|
if (!receipt.Succeeded()) throw new Exception("Unable to send Eth");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public decimal GetEthBalance()
|
|
|
|
|
{
|
|
|
|
|
return GetEthBalance(web3.TransactionManager.Account.Address);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public decimal GetEthBalance(string address)
|
|
|
|
|
{
|
|
|
|
|
var balance = Time.Wait(web3.Eth.GetBalance.SendRequestAsync(address));
|
|
|
|
|
return Web3.Convert.FromWei(balance.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 11:22:41 +00:00
|
|
|
|
public string GetTokenAddress(string marketplaceAddress)
|
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
log.Debug(marketplaceAddress);
|
2023-04-18 11:22:41 +00:00
|
|
|
|
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-05-03 08:21:15 +00:00
|
|
|
|
public void MintTestTokens(string[] accounts, decimal amount, string tokenAddress)
|
2023-04-19 07:19:06 +00:00
|
|
|
|
{
|
2023-05-03 08:21:15 +00:00
|
|
|
|
if (amount < 1 || accounts.Length < 1) throw new ArgumentException("Invalid arguments for MintTestTokens");
|
2023-04-19 07:19:06 +00:00
|
|
|
|
|
2023-05-03 08:21:15 +00:00
|
|
|
|
var tasks = accounts.Select(a => MintTokens(a, amount, tokenAddress));
|
2023-04-18 08:22:11 +00:00
|
|
|
|
|
2023-05-03 08:21:15 +00:00
|
|
|
|
Task.WaitAll(tasks.ToArray());
|
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-25 09:31:15 +00:00
|
|
|
|
log.Debug($"({tokenAddress}) {account}");
|
2023-04-18 13:33:12 +00:00
|
|
|
|
var function = new GetTokenBalanceFunction
|
|
|
|
|
{
|
|
|
|
|
Owner = account
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var handler = web3.Eth.GetContractQueryHandler<GetTokenBalanceFunction>();
|
2023-04-19 07:57:37 +00:00
|
|
|
|
return ToDecimal(Time.Wait(handler.QueryAsync<BigInteger>(tokenAddress, function)));
|
2023-04-14 08:51:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 08:21:15 +00:00
|
|
|
|
public bool IsSynced(string marketplaceAddress, string marketplaceAbi)
|
2023-04-19 07:19:06 +00:00
|
|
|
|
{
|
2023-05-03 08:21:15 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return IsBlockNumberOK() && IsContractAvailable(marketplaceAddress, marketplaceAbi);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-19 07:19:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 08:21:15 +00:00
|
|
|
|
private Task MintTokens(string account, decimal amount, string tokenAddress)
|
2023-04-25 05:46:09 +00:00
|
|
|
|
{
|
2023-05-03 08:21:15 +00:00
|
|
|
|
log.Debug($"({tokenAddress}) {amount} --> {account}");
|
|
|
|
|
if (string.IsNullOrEmpty(account)) throw new ArgumentException("Invalid arguments for MintTestTokens");
|
|
|
|
|
|
|
|
|
|
var function = new MintTokensFunction
|
|
|
|
|
{
|
|
|
|
|
Holder = account,
|
|
|
|
|
Amount = ToBig(amount)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var handler = web3.Eth.GetContractTransactionHandler<MintTokensFunction>();
|
|
|
|
|
return handler.SendRequestAndWaitForReceiptAsync(tokenAddress, function);
|
2023-04-25 05:46:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 08:21:15 +00:00
|
|
|
|
private bool IsBlockNumberOK()
|
2023-04-24 12:09:23 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
log.Debug();
|
2023-05-03 08:21:15 +00:00
|
|
|
|
var sync = Time.Wait(web3.Eth.Syncing.SendRequestAsync());
|
|
|
|
|
var number = Time.Wait(web3.Eth.Blocks.GetBlockNumber.SendRequestAsync());
|
|
|
|
|
var numberOfBlocks = ToDecimal(number);
|
|
|
|
|
return !sync.IsSyncing && numberOfBlocks > 256;
|
2023-04-25 05:46:09 +00:00
|
|
|
|
}
|
2023-04-24 12:09:23 +00:00
|
|
|
|
|
2023-05-03 08:21:15 +00:00
|
|
|
|
private bool IsContractAvailable(string marketplaceAddress, string marketplaceAbi)
|
2023-04-25 05:46:09 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
log.Debug();
|
2023-05-03 08:21:15 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var contract = web3.Eth.GetContract(marketplaceAbi, marketplaceAddress);
|
|
|
|
|
return contract != null;
|
|
|
|
|
}
|
|
|
|
|
catch
|
2023-04-24 12:09:23 +00:00
|
|
|
|
{
|
2023-05-03 08:21:15 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-24 12:09:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-25 05:46:09 +00:00
|
|
|
|
private decimal ToDecimal(HexBigInteger hexBigInteger)
|
|
|
|
|
{
|
|
|
|
|
return ToDecimal(hexBigInteger.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 13:33:12 +00:00
|
|
|
|
private decimal ToDecimal(BigInteger bigInteger)
|
|
|
|
|
{
|
|
|
|
|
return (decimal)bigInteger;
|
|
|
|
|
}
|
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)]
|
2023-06-01 07:35:18 +00:00
|
|
|
|
public string Holder { get; set; } = string.Empty;
|
2023-04-18 11:22:41 +00:00
|
|
|
|
|
|
|
|
|
[Parameter("uint256", "amount", 2)]
|
|
|
|
|
public BigInteger Amount { get; set; }
|
|
|
|
|
}
|
2023-04-18 13:33:12 +00:00
|
|
|
|
|
|
|
|
|
[Function("balanceOf", "uint256")]
|
2023-04-24 12:09:23 +00:00
|
|
|
|
public class GetTokenBalanceFunction : FunctionMessage
|
2023-04-18 13:33:12 +00:00
|
|
|
|
{
|
|
|
|
|
[Parameter("address", "owner", 1)]
|
2023-06-01 07:35:18 +00:00
|
|
|
|
public string Owner { get; set; } = string.Empty;
|
2023-04-18 13:33:12 +00:00
|
|
|
|
}
|
2023-04-14 08:51:35 +00:00
|
|
|
|
}
|