public async Task <string> ExecuteReadFunction(SmartContractExecutionRequestPayload smartContractExecutionRequestPayload) { try { if (smartContractExecutionRequestPayload == null) { throw new ArgumentNullException(nameof(smartContractExecutionRequestPayload) + " is null"); } if (smartContractExecutionRequestPayload.SmartContractDeployedInstanceId == 0) { throw new ArgumentException(nameof(smartContractExecutionRequestPayload.SmartContractDeployedInstanceId) + " is not valid"); } var smartContractDeployedInstance = await this.smartContractDb.GetSmartContractDeployedInstance(smartContractExecutionRequestPayload.SmartContractDeployedInstanceId); if (smartContractDeployedInstance != null) { var smartContract = await this.smartContractDb.GetSmartContract(smartContractDeployedInstance.SmartContractId); if (smartContract != null) { var web3 = new Web3Geth(); var contract = web3.Eth.GetContract(smartContract.Abi, smartContractDeployedInstance.DeployedAddress); var contractFunction = contract.GetFunction(smartContractExecutionRequestPayload.Function); var param = smartContractExecutionRequestPayload.Parameters; Object functionResult = null; if (param.Count > 0) { functionResult = await contractFunction.CallAsync <Object>(param.ToArray()); } else { functionResult = await contractFunction.CallAsync <Object>(); } return(functionResult.ToString()); } else { throw new Exception("Unable to find Smart Contract"); } } else { throw new Exception("Unable to find Smart Contract deployed instance"); } } catch (Exception ex) { this.logger.LogException(ex, "An error occured in method SmartContractManager ==> ExecuteReadFunction"); throw; } }
public async Task <IHttpActionResult> ExecuteReadFunction([FromBody] SmartContractExecutionRequestPayload smartContractExecutionRequestPayload) { try { var smartContractDeployedInstanceItem = await this.smartContractManager.ExecuteReadFunction(smartContractExecutionRequestPayload); return(Ok(smartContractDeployedInstanceItem)); } catch (Exception ex) { return(InternalServerError(ex)); } }
public async Task <SmartContractTransaction> ExecuteWriteFunction(SmartContractExecutionRequestPayload smartContractExecutionRequestPayload) { if (smartContractExecutionRequestPayload == null) { throw new ArgumentNullException(nameof(smartContractExecutionRequestPayload) + " is null"); } if (smartContractExecutionRequestPayload.SmartContractDeployedInstanceId == 0) { throw new ArgumentException(nameof(smartContractExecutionRequestPayload.SmartContractDeployedInstanceId) + " is not valid"); } if (string.IsNullOrEmpty(smartContractExecutionRequestPayload.TransactionUser)) { throw new ArgumentException(nameof(smartContractExecutionRequestPayload.TransactionUser) + " is not valid"); } try { var userDltAccount = await this.accountDb.GetUserDltAccountByLoginId(smartContractExecutionRequestPayload.TransactionUser); var smartContractDeployedInstance = await this.smartContractDb.GetSmartContractDeployedInstance(smartContractExecutionRequestPayload.SmartContractDeployedInstanceId); if (userDltAccount != null && smartContractDeployedInstance != null) { var smartContract = await this.smartContractDb.GetSmartContract(smartContractDeployedInstance.SmartContractId); if (smartContract != null) { var web3 = new Web3Geth(); var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(userDltAccount.Address, userDltAccount.PassPhrase, 120); if (unlockResult) { var contract = web3.Eth.GetContract(smartContract.Abi, smartContractDeployedInstance.DeployedAddress); var contractFunction = contract.GetFunction(smartContractExecutionRequestPayload.Function); var param = smartContractExecutionRequestPayload.Parameters; var functionDataParam = param.ToArray(); var gas = await contractFunction.EstimateGasAsync(userDltAccount.Address, new HexBigInteger(9000), null, functionDataParam); string transactionHash = await contractFunction.SendTransactionAsync(userDltAccount.Address, gas, null, functionDataParam); if (!string.IsNullOrEmpty(transactionHash)) { SmartContractTransaction smartContractTransaction = new SmartContractTransaction() { SmartContractDeployedInstanceId = smartContractExecutionRequestPayload.SmartContractDeployedInstanceId, SmartContractFunction = smartContractExecutionRequestPayload.Function, TransactionHash = transactionHash, TransactionUser = smartContractExecutionRequestPayload.TransactionUser, SmartContractFunctionParameters = Newtonsoft.Json.JsonConvert.SerializeObject(smartContractExecutionRequestPayload.Parameters) }; var updatedSmartContractTransaction = await this.smartContractDb.CreateSmartContractTransaction(smartContractTransaction); return(updatedSmartContractTransaction); } else { throw new Exception("Generate transaction hash is null"); } } else { throw new Exception("Unable to unlock the transaction user DLT account for performing the DLT transaction"); } } else { throw new Exception("Unabel to unlock the account"); } } else { throw new Exception("Unable to find Smart Contract"); } } catch (Exception ex) { this.logger.LogException(ex, "An error occured in method SmartContractManager ==> ExecuteWriteFunction"); throw; } }
public Task <SmartContractDeployedInstanceItem> ExecuteReadFunction(SmartContractExecutionRequestPayload smartContractExecutionRequestPayload) { throw new NotImplementedException(); }