/// <summary> ///异步 发送交易,返回交易回执 /// </summary> /// <param name="abi">合约abi</param> /// <param name="contractAddress">合约地址</param> /// <param name="functionName">合约请求调用方法名称</param> /// <param name="inputsParameters">方法对应的 参数</param> /// <param name="value">请求参数值</param> /// <returns>交易回执</returns> public async Task <ReceiptResultDto> SendTranscationWithReceiptAsync(string abi, string contractAddress, string functionName, Parameter[] inputsParameters, params object[] value) { ReceiptResultDto receiptResult = new ReceiptResultDto(); var des = new ABIDeserialiser(); var contract = des.DeserialiseContract(abi); var function = contract.Functions.FirstOrDefault(x => x.Name == functionName); var sha3Signature = function.Sha3Signature;// "0x53ba0944"; var functionCallEncoder = new FunctionCallEncoder(); var result = functionCallEncoder.EncodeRequest(sha3Signature, inputsParameters, value); var blockNumber = await GetBlockNumberAsync(); var transDto = BuildTransactionParams(result, blockNumber, contractAddress); var tx = BuildRLPTranscation(transDto); tx.Sign(new EthECKey(this._privateKey.HexToByteArray(), true)); var txHash = await SendRequestAysnc <string>(tx.Data, tx.Signature); if (txHash != null) { receiptResult = await GetTranscationReceiptAsync(txHash); if (receiptResult == null) { throw new Exception("txHash != null 的时候报错了:" + receiptResult.ToJson()); } } return(receiptResult); }
/// <summary> ///同步 发送交易,返回交易回执 /// </summary> /// <param name="abi">合约abi</param> /// <param name="contractAddress">合约地址</param> /// <param name="functionName">合约请求调用方法名称</param> /// <param name="inputsParameters">方法对应的 参数</param> /// <param name="value">请求参数值</param> /// <returns>交易回执</returns> public ReceiptResultDto SendTranscationWithReceipt(string abi, string contractAddress, string functionName, Parameter[] inputsParameters, params object[] value) { ReceiptResultDto receiptResult = new ReceiptResultDto(); var des = new ABIDeserialiser(); var contract = des.DeserialiseContract(abi); var function = contract.Functions.FirstOrDefault(x => x.Name == functionName); var sha3Signature = function.Sha3Signature;// "0x53ba0944"; var functionCallEncoder = new FunctionCallEncoder(); var result = functionCallEncoder.EncodeRequest(sha3Signature, inputsParameters, value); var blockNumber = GetBlockNumber(); var transDto = BuildTransactionParams(result, blockNumber, contractAddress); var tx = BuildRLPTranscation(transDto); tx.Sign(new EthECKey(this._privateKey.HexToByteArray(), true)); var txHash = SendRequest <object>(tx.Data, tx.Signature); if (txHash != null) { receiptResult = GetTranscationReceipt(txHash.ToString()); } return(receiptResult); }
public void SendTranscationWithReceiptDecodeTest() { var contractService = new ContractService(BaseConfig.DefaultUrl, BaseConfig.DefaultRpcId, BaseConfig.DefaultChainId, BaseConfig.DefaultGroupId, privateKey); string contractAddress = "0x149d743274d91eeea8f646901fc8dd79551dccda";//上面测试部署合约得到合约地址 var inputsParameters = new[] { BuildParams.CreateParam("string", "n") }; var paramsValue = new object[] { "123" }; string functionName = "set";//调用合约方法 ReceiptResultDto receiptResultDto = contractService.SendTranscationWithReceipt(abi, contractAddress, functionName, inputsParameters, paramsValue); Assert.NotEmpty(receiptResultDto.Output); Assert.NotEmpty(receiptResultDto.Input); Assert.NotEmpty(receiptResultDto.Logs); var solidityAbi = new SolidityABI(abi); var inputList = solidityAbi.InputDecode(functionName, receiptResultDto.Input); Assert.True(inputList[0].Parameter.Name == "n" && inputList[0].Result.ToString() == "123"); string eventName = "SetEvent"; var eventList = solidityAbi.EventDecode(eventName, receiptResultDto.Logs); var eventpramas1 = eventList[0].Event.Find(x => x.Parameter.Name == "paramsStr"); var eventpramas2 = eventList[0].Event.Find(x => x.Parameter.Name == "operationTimeStamp"); Assert.True(eventpramas1.Result.ToString() == "123"); Assert.NotNull(eventpramas2.Result); }
/// <summary> /// 同步 获取交易回执 /// </summary> /// <param name="tanscationHash">交易Hash</param> /// <returns></returns> public ReceiptResultDto GetTranscationReceipt(string transcationHash) { var rpcRequest = new RpcRequestMessage(this._requestId, JsonRPCAPIConfig.GetTransactionReceipt, new object[] { this._requestObjectId, transcationHash }); ReceiptResultDto receiptResultDto = new ReceiptResultDto(); Stopwatch sw = new Stopwatch(); sw.Start(); long times = 0; while (true) { receiptResultDto = HttpUtils.RpcPost <ReceiptResultDto>(this._url, rpcRequest); times += sw.ElapsedMilliseconds; if (times > BaseConfig.DefaultExpirationTime) { sw.Stop(); throw new Exception("获取交易回执超时!"); } if (receiptResultDto != null) { break; } } return(receiptResultDto); }
/// <summary> /// 异步 获取交易回执 /// </summary> /// <param name="tanscationHash">交易Hash</param> /// <returns></returns> public async Task <ReceiptResultDto> GetTranscationReceiptAsync(string transcationHash) { var request = new RpcRequest(this._requestId, JsonRPCAPIConfig.GetTransactionReceipt, new object[] { this._requestObjectId, transcationHash }); var getRequest = new RpcRequestMessage(this._requestId, JsonRPCAPIConfig.GetTransactionReceipt, new object[] { this._requestObjectId, transcationHash }); ReceiptResultDto receiptResultDto = new ReceiptResultDto(); Stopwatch sw = new Stopwatch(); sw.Start(); long times = 0; while (true) { receiptResultDto = await this._rpcClient.SendRequestAsync <ReceiptResultDto>(request); times += sw.ElapsedMilliseconds; if (times > BaseConfig.DefaultExpirationTime) { sw.Stop(); throw new Exception("获取交易回执超时!"); } if (receiptResultDto != null) { break; } } return(receiptResultDto); }
public void GetContructorParamsTest() { //var data = "0x2dc9".HexToBigInteger(true); var contractService = new ContractService(BaseConfig.DefaultUrl, BaseConfig.DefaultRpcId, BaseConfig.DefaultChainId, BaseConfig.DefaultGroupId, privateKey); string contractAddress = "0x1be74c68f3fec43b2360dd7456748121e1216960";//上面测试部署合约得到合约地址 var funcInputsParameters = new[] { BuildParams.CreateParam("uint256", "temp") }; var funcParamsValue = new object[] { 1 }; string functionName = "get";//调用合约方法 ReceiptResultDto receiptResultDto = contractService.CallRequest(contractAddress, abi, functionName, funcInputsParameters, funcParamsValue); Assert.NotEmpty(receiptResultDto.Output); var solidityAbi = new SolidityABI(abi); var outputList = solidityAbi.OutputDecode(functionName, receiptResultDto.Output); Assert.Equal("123", outputList[0].Result); }