cs-codex-dist-tests/GethPlugin/GethNode.cs

79 lines
2.5 KiB
C#
Raw Normal View History

2023-09-19 09:51:59 +00:00
using Logging;
2023-09-19 11:39:24 +00:00
using Nethereum.Contracts;
2023-09-15 14:27:08 +00:00
using NethereumWorkflow;
2023-09-15 13:52:02 +00:00
namespace GethPlugin
{
2023-09-19 08:24:43 +00:00
public interface IGethNode
2023-09-15 13:52:02 +00:00
{
2023-09-19 09:51:59 +00:00
IGethStartResult StartResult { get; }
2023-09-15 14:27:08 +00:00
2023-09-19 09:51:59 +00:00
Ether GetEthBalance();
Ether GetEthBalance(IHasEthAddress address);
Ether GetEthBalance(IEthAddress address);
void SendEth(IHasEthAddress account, Ether eth);
void SendEth(IEthAddress account, Ether eth);
2023-09-19 11:39:24 +00:00
TResult Call<TFunction, TResult>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new();
void SendTransaction<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new();
2023-09-15 13:52:02 +00:00
}
2023-09-19 08:24:43 +00:00
public class GethNode : IGethNode
2023-09-15 13:52:02 +00:00
{
2023-09-19 09:51:59 +00:00
private readonly ILog log;
public GethNode(ILog log, IGethStartResult startResult)
2023-09-15 13:52:02 +00:00
{
2023-09-19 09:51:59 +00:00
this.log = log;
StartResult = startResult;
Account = startResult.AllAccounts.Accounts.First();
2023-09-15 13:52:02 +00:00
}
2023-09-19 09:51:59 +00:00
public IGethStartResult StartResult { get; }
2023-09-15 13:52:02 +00:00
public GethAccount Account { get; }
2023-09-19 09:51:59 +00:00
public Ether GetEthBalance()
{
return StartInteraction().GetEthBalance().Eth();
}
public Ether GetEthBalance(IHasEthAddress owner)
{
return GetEthBalance(owner.EthAddress);
}
public Ether GetEthBalance(IEthAddress address)
{
return StartInteraction().GetEthBalance(address.Address).Eth();
}
public void SendEth(IHasEthAddress owner, Ether eth)
{
SendEth(owner.EthAddress, eth);
}
public void SendEth(IEthAddress account, Ether eth)
{
2023-09-19 11:39:24 +00:00
StartInteraction().SendEth(account.Address, eth.Eth);
}
public TResult Call<TFunction, TResult>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
{
return StartInteraction().Call<TFunction, TResult>(contractAddress, function);
}
public void SendTransaction<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
{
StartInteraction().SendTransaction(contractAddress, function);
}
private NethereumInteraction StartInteraction()
{
var address = StartResult.RunningContainer.Address;
var account = Account;
var creator = new NethereumInteractionCreator(log, address.Host, address.Port, account.PrivateKey);
return creator.CreateWorkflow();
2023-09-19 09:51:59 +00:00
}
2023-09-15 13:52:02 +00:00
}
}