cs-codex-dist-tests/Nethereum/NethereumInteraction.cs

59 lines
1.7 KiB
C#
Raw Normal View History

2023-04-14 10:37:05 +00:00
using Logging;
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-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-14 10:37:05 +00:00
public void TransferTo(string account, decimal amount)
2023-04-14 08:51:35 +00:00
{
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));
Time.Wait(web3.Eth.TransactionManager.TransactionReceiptService.PollForReceiptAsync(transactionId));
2023-04-14 10:37:05 +00:00
Log($"Transferred {amount} to {account}");
2023-04-14 08:51:35 +00:00
}
public decimal GetBalance(string account)
{
var bigInt = Time.Wait(web3.Eth.GetBalance.SendRequestAsync(account));
2023-04-14 10:37:05 +00:00
var result = ToDecimal(bigInt);
Log($"Balance of {account} is {result}");
return result;
2023-04-14 08:51:35 +00:00
}
private HexBigInteger ToHexBig(decimal amount)
{
var bigint = new BigInteger(amount);
var str = bigint.ToString("X");
return new HexBigInteger(str);
}
2023-04-14 10:37:05 +00:00
private decimal ToDecimal(HexBigInteger hexBigInteger)
{
return (decimal)hexBigInteger.Value;
}
private void Log(string msg)
{
log.Log(msg);
}
2023-04-14 08:51:35 +00:00
}
}