79 lines
2.5 KiB
C#
Raw Normal View History

2023-09-19 11:51:59 +02:00
using Logging;
2023-09-19 13:39:24 +02:00
using Nethereum.Contracts;
2023-09-15 16:27:08 +02:00
using NethereumWorkflow;
2023-09-15 15:52:02 +02:00
namespace GethPlugin
{
2023-09-19 10:24:43 +02:00
public interface IGethNode
2023-09-15 15:52:02 +02:00
{
2023-09-19 11:51:59 +02:00
IGethStartResult StartResult { get; }
2023-09-15 16:27:08 +02:00
2023-09-19 11:51:59 +02: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 13:39:24 +02: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 15:52:02 +02:00
}
2023-09-19 10:24:43 +02:00
public class GethNode : IGethNode
2023-09-15 15:52:02 +02:00
{
2023-09-19 11:51:59 +02:00
private readonly ILog log;
public GethNode(ILog log, IGethStartResult startResult)
2023-09-15 15:52:02 +02:00
{
2023-09-19 11:51:59 +02:00
this.log = log;
StartResult = startResult;
Account = startResult.AllAccounts.Accounts.First();
2023-09-15 15:52:02 +02:00
}
2023-09-19 11:51:59 +02:00
public IGethStartResult StartResult { get; }
2023-09-15 15:52:02 +02:00
public GethAccount Account { get; }
2023-09-19 11:51:59 +02: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 13:39:24 +02: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 11:51:59 +02:00
}
2023-09-15 15:52:02 +02:00
}
}