/// <summary>
        ///
        /// This method will use Nethereum to deploy a Wonka contract to the chain.
        ///
        /// <param name="poDeployMsg">The deployment message with the bytecodes that can create the target contract</param>
        /// <param name="psWonkaAbi">The ABI of the target contract</param>
        /// <param name="psSenderAddress">The ABI of the target contract</param>
        /// <param name="psWeb3HttpUrl">The client node to which we will deploy the contract</param>
        /// <returns>The address of the new instance of the target contract</returns>
        /// </summary>
        public static async Task <string> DeployContractAsync(this ContractDeploymentMessage poDeployMsg, Web3 poWeb3, string psWonkaAbi, string psSenderAddress, string psWeb3HttpUrl = "")
        {
            var transactionHash =
                await poWeb3.Eth.DeployContract.SendRequestAsync(psWonkaAbi, poDeployMsg.ByteCode, psSenderAddress).ConfigureAwait(false);

            var receipt = await poWeb3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionHash).ConfigureAwait(false);

            while (receipt == null)
            {
                Thread.Sleep(5000);
                receipt = await poWeb3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionHash).ConfigureAwait(false);
            }

            var contractAddress = receipt.ContractAddress;

            return(contractAddress);
        }
        /// <summary>
        ///
        /// This method will use Nethereum to deploy a Wonka contract to the chain.
        ///
        /// <param name="poDeployMsg">The deployment message with the bytecodes that can create the target contract</param>
        /// <param name="psWonkaAbi">The ABI of the target contract</param>
        /// <param name="psSenderAddress">The ABI of the target contract</param>
        /// <param name="pnGas">The amount of gas supplied for the deployment of the contract</param>
        /// <param name="psWeb3HttpUrl">The client node to which we will deploy the contract</param>
        /// <returns>The address of the new instance of the target contract</returns>
        /// </summary>
        public static string DeployContract(this ContractDeploymentMessage poDeployMsg, Web3 poWeb3, string psWonkaAbi, string psSenderAddress, Nethereum.Hex.HexTypes.HexBigInteger pnGas, string psWeb3HttpUrl = "")
        {
            var transactionHash =
                poWeb3.Eth.DeployContract.SendRequestAsync(psWonkaAbi, poDeployMsg.ByteCode, psSenderAddress, pnGas).Result;

            // NOTE: Should we start/stop mining by default?
            // var mineResult = poWeb3.Miner.Start.SendRequestAsync(6).Result;

            var receipt = poWeb3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionHash).Result;

            while (receipt == null)
            {
                Thread.Sleep(5000);
                receipt = poWeb3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionHash).Result;
            }

            // NOTE: Should we start/stop mining by default?
            // mineResult = poWeb3.Miner.Stop.SendRequestAsync().Result;

            var contractAddress = receipt.ContractAddress;

            return(contractAddress);
        }
        public async Task <IActionResult> Post([FromBody] ContractDeploymentRequest model)
        {
            // non-forced-to-disposal
            ContractDeploymentResponse result = new ContractDeploymentResponse
            {
                IsSucceded = true,
                ResultId   = (int)ContractDeploymentResponseEnum.Success
            };

            // forced-to-disposal
            KeyVaultConnectionInfo keyVaultConnectionInfo = null;

            try
            {
                if (string.IsNullOrEmpty(model.Name))
                {
                    throw new BusinessException((int)ContractDeploymentResponseEnum.FailedEmptyName);
                }

                if (string.IsNullOrEmpty(model.Description))
                {
                    throw new BusinessException((int)ContractDeploymentResponseEnum.FailedEmptyDescription);
                }

                keyVaultConnectionInfo = new KeyVaultConnectionInfo()
                {
                    CertificateName    = ApplicationSettings.KeyVaultCertificateName,
                    ClientId           = ApplicationSettings.KeyVaultClientId,
                    ClientSecret       = ApplicationSettings.KeyVaultClientSecret,
                    KeyVaultIdentifier = ApplicationSettings.KeyVaultIdentifier
                };

                using (MessageQueueHelper messageQueueHelper = new MessageQueueHelper())
                {
                    ContractDeploymentMessage contractDeploymentMessage = new ContractDeploymentMessage()
                    {
                        name        = model.Name,
                        description = model.Description
                    };

                    await messageQueueHelper.QueueMessageAsync(contractDeploymentMessage, ApplicationSettings.ContractDeploymentQueueName, keyVaultConnectionInfo);
                }
            }
            catch (Exception ex)
            {
                result.IsSucceded = false;

                if (ex is BusinessException)
                {
                    result.ResultId = ((BusinessException)ex).ResultId;
                }
                else
                {
                    result.ResultId = (int)ContractDeploymentResponseEnum.Failed;

                    this.logger.LogError($">> Exception: {ex.Message}, StackTrace: {ex.StackTrace}");

                    if (ex.InnerException != null)
                    {
                        this.logger.LogError($">> Inner Exception Message: {ex.InnerException.Message}, Inner Exception StackTrace: {ex.InnerException.StackTrace}");
                    }
                }
            }
            finally
            {
                keyVaultConnectionInfo = null;

                GC.Collect();
            }

            string message = EnumDescription.GetEnumDescription((ContractDeploymentResponseEnum)result.ResultId);

            this.logger.LogInformation($">> Message information: {message}");

            return((result.IsSucceded) ? (ActionResult) new OkObjectResult(new { message = message }) : (ActionResult) new BadRequestObjectResult(new { message = message }));
        }
 public void LogContractDeployment(ContractDeploymentMessage contractDeploymentMessage)
 {
     _writer.WriteLine(Stateprinter.PrintObject(contractDeploymentMessage));
 }
 public void LogGivenContractDeployment(ContractDeploymentMessage contractDeploymentMessage)
 {
     _writer.WriteLine("Given I Deploy Contract:");
     LogContractDeployment(contractDeploymentMessage);
 }