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

92 lines
3.0 KiB
C#
Raw Normal View History

2023-09-21 08:56:48 +00:00
using Core;
using KubernetesWorkflow.Types;
2023-09-21 08:56:48 +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-21 08:56:48 +00:00
public interface IGethNode : IHasContainer
2023-09-15 13:52:02 +00:00
{
2023-09-20 08:13:29 +00:00
GethDeployment StartResult { get; }
2023-09-15 14:27:08 +00:00
2023-09-19 09:51:59 +00:00
Ether GetEthBalance();
Ether GetEthBalance(IHasEthAddress address);
2023-09-20 08:13:29 +00:00
Ether GetEthBalance(EthAddress address);
2023-09-19 09:51:59 +00:00
void SendEth(IHasEthAddress account, Ether eth);
2023-09-20 08:13:29 +00:00
void SendEth(EthAddress 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-19 11:58:45 +00:00
decimal? GetSyncedBlockNumber();
bool IsContractAvailable(string abi, string contractAddress);
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;
2023-09-20 08:13:29 +00:00
public GethNode(ILog log, GethDeployment startResult)
2023-09-15 13:52:02 +00:00
{
2023-09-19 09:51:59 +00:00
this.log = log;
StartResult = startResult;
2023-09-15 13:52:02 +00:00
}
2023-09-20 08:13:29 +00:00
public GethDeployment StartResult { get; }
2023-09-21 08:56:48 +00:00
public RunningContainer Container => StartResult.Container;
2023-09-15 13:52:02 +00:00
2023-09-19 09:51:59 +00:00
public Ether GetEthBalance()
{
return StartInteraction().GetEthBalance().Eth();
}
public Ether GetEthBalance(IHasEthAddress owner)
{
return GetEthBalance(owner.EthAddress);
}
2023-09-20 08:13:29 +00:00
public Ether GetEthBalance(EthAddress address)
2023-09-19 09:51:59 +00:00
{
return StartInteraction().GetEthBalance(address.Address).Eth();
}
public void SendEth(IHasEthAddress owner, Ether eth)
{
SendEth(owner.EthAddress, eth);
}
2023-09-20 08:13:29 +00:00
public void SendEth(EthAddress account, Ether eth)
2023-09-19 09:51:59 +00:00
{
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()
{
2023-11-07 11:02:17 +00:00
var address = StartResult.Container.GetAddress(log, GethContainerRecipe.HttpPortTag);
2023-10-23 11:36:20 +00:00
var account = StartResult.Account;
2023-09-19 11:39:24 +00:00
var creator = new NethereumInteractionCreator(log, address.Host, address.Port, account.PrivateKey);
return creator.CreateWorkflow();
2023-09-19 09:51:59 +00:00
}
2023-09-19 11:58:45 +00:00
public decimal? GetSyncedBlockNumber()
{
return StartInteraction().GetSyncedBlockNumber();
}
public bool IsContractAvailable(string abi, string contractAddress)
{
return StartInteraction().IsContractAvailable(abi, contractAddress);
}
2023-09-15 13:52:02 +00:00
}
}