private async Task DeployAsync(byte[] compilation, IConsole console) { console.WriteLine("Deploying to node ", this.Node); var model = new BuildCreateContractTransactionRequest(); model.ContractCode = compilation.ToHexString(); model.AccountName = this.AccountName; model.FeeAmount = this.FeeAmount; model.Amount = this.Amount; model.GasPrice = this.GasPrice; model.GasLimit = this.GasLimit; model.Password = this.Password; model.WalletName = this.WalletName; model.Sender = this.Sender; model.Parameters = this.Params; var deployer = new HttpContractDeployer(client, DeploymentResource); DeploymentResult response = await deployer.DeployAsync(this.Node, model); console.WriteLine(string.Empty); if (response.Success) { console.WriteLine("Contract creation transaction successful!"); console.WriteLine($"Transaction Id: {response.TransactionId}"); console.WriteLine($"Contract Address: {response.ContractAddress}"); return; } console.WriteLine(string.Format("Deployment Error: {0}", response.Message)); foreach (string error in response.Errors) { console.WriteLine(error); } }
/// <summary> /// Deploys a contract's bytecode to the provided node via HTTP /// </summary> public async Task <DeploymentResult> DeployAsync(string nodeUrl, BuildCreateContractTransactionRequest model) { string json = NetJSON.NetJSON.Serialize(model); HttpResponseMessage response; try { response = await this.client.PostAsync( GetDeploymentUri(nodeUrl, this.deploymentResource), new StringContent(json, Encoding.UTF8, "application/json")); } catch (HttpRequestException e) { var errors = new List <string> { e.Message, }; if (e.InnerException != null) { errors.Add(e.InnerException.Message); } errors.Add("Please ensure that an instance of your full node is running before trying to deploy a smart contract."); return(DeploymentResult.DeploymentFailure(errors)); } if (response.IsSuccessStatusCode) { string successJson = await response.Content.ReadAsStringAsync(); BuildCreateContractTransactionResponse successResponse = NetJSON.NetJSON.Deserialize <BuildCreateContractTransactionResponse>(successJson); if (successResponse.Success) { return(DeploymentResult.DeploymentSuccess(successResponse)); } else { return(DeploymentResult.DeploymentFailure(successResponse)); } } if (response.Content == null) { return(DeploymentResult.DeploymentFailure(response.StatusCode.ToString())); } string responseBody = await response.Content.ReadAsStringAsync(); try { ErrorResponse resp = NetJSON.NetJSON.Deserialize <ErrorResponse>(responseBody); return(DeploymentResult.DeploymentFailure(resp.Errors.Select(err => err.Message))); } catch (Exception) { return(DeploymentResult.DeploymentFailure(responseBody)); } }