using System.Threading.Tasks;
using Nethereum.Contracts.Extensions;
using Nethereum.Hex.HexTypes;
using Nethereum.RPC.TransactionManagers;
namespace Nethereum.Contracts.DeploymentHandlers
{
#if !DOTNET35
///
/// Signs a transaction estimating the gas if not set and retrieving the next nonce if not set
///
public class DeploymentSigner : DeploymentHandlerBase,
IDeploymentSigner where TContractDeploymentMessage : ContractDeploymentMessage, new()
{
private readonly IDeploymentEstimatorHandler _deploymentEstimatorHandler;
private ITransactionManager _transactionManager;
public DeploymentSigner(ITransactionManager transactionManager) : this(transactionManager,
new DeploymentEstimatorHandler(transactionManager))
{
}
public DeploymentSigner(ITransactionManager transactionManager,
IDeploymentEstimatorHandler deploymentEstimatorHandler) : base(transactionManager)
{
_deploymentEstimatorHandler = deploymentEstimatorHandler;
}
public async Task SignTransactionAsync(TContractDeploymentMessage deploymentMessage = null)
{
if (deploymentMessage == null) deploymentMessage = new TContractDeploymentMessage();
deploymentMessage.Gas = await GetOrEstimateMaximumGasAsync(deploymentMessage).ConfigureAwait(false);
var transactionInput = DeploymentMessageEncodingService.CreateTransactionInput(deploymentMessage);
return await TransactionManager.SignTransactionAsync(transactionInput).ConfigureAwait(false);
}
protected virtual async Task GetOrEstimateMaximumGasAsync(
TContractDeploymentMessage deploymentMessage)
{
return deploymentMessage.GetHexMaximumGas()
?? await _deploymentEstimatorHandler.EstimateGasAsync(deploymentMessage).ConfigureAwait(false);
}
}
#endif
}