From c344840019bb2a8c51d3d70accff9b022b79208b Mon Sep 17 00:00:00 2001 From: kyonRay Date: Thu, 28 Dec 2023 12:22:01 +0800 Subject: [PATCH] (transaction): refator transaction request, fix v2 assemble transaction service bug. --- .../manager/TransactionManagerTest.java | 20 +- .../AssembleEIP1559TransactionService.java | 174 ++++---- .../AssembleTransactionService.java | 377 +++++++++++------- .../transactionv2/dto/BasicDeployRequest.java | 48 +++ .../transactionv2/dto/BasicRequest.java | 65 +++ .../dto/DeployTransactionRequest.java | 33 +- ...loyTransactionRequestWithStringParams.java | 12 +- .../transactionv2/dto/TransactionRequest.java | 48 +-- .../TransactionRequestWithStringParams.java | 2 +- 9 files changed, 459 insertions(+), 320 deletions(-) create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicDeployRequest.java create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicRequest.java diff --git a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerTest.java b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerTest.java index 938947c60..6158b954b 100644 --- a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerTest.java +++ b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerTest.java @@ -310,7 +310,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { String abi = abiAndBinaryByContractName.getKey(); TransactionRequestBuilder requestBuilder = new TransactionRequestBuilder(abi, abiAndBinaryByContractName.getValue()); DeployTransactionRequestWithStringParams deployTransactionRequestWithStringParams = requestBuilder.buildDeployStringParamsRequest(deployParams); - TransactionResponse response = transactionService.deployContractWithStringParams(deployTransactionRequestWithStringParams); + TransactionResponse response = transactionService.deployContract(deployTransactionRequestWithStringParams); Assert.assertEquals(response.getTransactionReceipt().getStatus(), 0); String contractAddress = response.getContractAddress(); Assert.assertTrue(StringUtils.isNotBlank(response.getContractAddress())); @@ -320,7 +320,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { List callParams = new ArrayList<>(); // use no params method TransactionRequestWithStringParams request = requestBuilder.setMethod("getStructA").setTo(contractAddress).buildStringParamsRequest(callParams); - CallResponse callResponse = transactionService.sendCallWithStringParams(request); + CallResponse callResponse = transactionService.sendCall(request); List returnObject = callResponse.getReturnObject(); Assert.assertEquals(returnObject.size(), 1); Assert.assertEquals(callResponse.getReturnABIObject().size(), 1); @@ -329,7 +329,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { // use one params method callParams.add("[[\"test2312312312312\"],[\"ffffffff1234567890123456ffffffffffffffff1234567890123456ffffffff\"]]"); TransactionRequestWithStringParams request2 = requestBuilder.setMethod("getStructA").setTo(contractAddress).buildStringParamsRequest(callParams); - CallResponse callResponse2 = transactionService.sendCallWithStringParams(request2); + CallResponse callResponse2 = transactionService.sendCall(request2); returnObject = callResponse2.getReturnObject(); Assert.assertEquals(returnObject.size(), 1); Assert.assertEquals(callResponse2.getReturnABIObject().size(), 1); @@ -341,7 +341,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { List params = new ArrayList<>(); params.add("[[\"0xabcd\"],[\"0x1234\"]]"); TransactionRequestWithStringParams transactionRequestWithStringParams = requestBuilder.setMethod("setBytesArrayArray").setTo(contractAddress).buildStringParamsRequest(params); - TransactionResponse transactionResponse = transactionService.sendTransactionWithStringParams(transactionRequestWithStringParams); + TransactionResponse transactionResponse = transactionService.sendTransaction(transactionRequestWithStringParams); Assert.assertEquals(transactionResponse.getTransactionReceipt().getStatus(), 0); List results = transactionResponse.getResults(); List returnObject = transactionResponse.getReturnObject(); @@ -357,7 +357,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { List params = new ArrayList<>(); params.add("[[\"0xffffffff1234567890123456ffffffffffffffff1234567890123456ffffffff\",\"0xffffffff1234567890123456ffffffffffffffff1234567890123456ffffffff\"],[\"0xffffffff1234567890123456ffffffffffffffff1234567890123456ffffffff\"]]"); TransactionRequestWithStringParams transactionRequestWithStringParams = requestBuilder.setMethod("setBytes32ArrayArray").buildStringParamsRequest(params); - TransactionResponse transactionResponse = transactionService.sendTransactionWithStringParams(transactionRequestWithStringParams); + TransactionResponse transactionResponse = transactionService.sendTransaction(transactionRequestWithStringParams); Assert.assertEquals(transactionResponse.getTransactionReceipt().getStatus(), 0); List results = transactionResponse.getResults(); List returnObject = transactionResponse.getReturnObject(); @@ -375,7 +375,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { List params = new ArrayList<>(); params.add("[[\"0xabcdef\",\"0xffffffff1234567890123456ffffffffffffffff1234567890123456ffffffff\"],[\"0xffffffff1234567890123456ffffffffffffffff1234567890123456ffffffff\",\"0x1234\"]]"); TransactionRequestWithStringParams transactionRequestWithStringParams = requestBuilder.setMethod("setBytesStaticArrayArray").buildStringParamsRequest(params); - TransactionResponse transactionResponse = transactionService.sendTransactionWithStringParams(transactionRequestWithStringParams); + TransactionResponse transactionResponse = transactionService.sendTransaction(transactionRequestWithStringParams); Assert.assertEquals(transactionResponse.getTransactionReceipt().getStatus(), 0); List results = transactionResponse.getResults(); List returnObject = transactionResponse.getReturnObject(); @@ -393,7 +393,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { List params = new ArrayList<>(); params.add("[[\"0x1234567890123456789012345678901234567890123456789012345678901234\",\"0xffffffff1234567890123456ffffffffffffffff1234567890123456ffffffff\"],[\"0x1234567890123456789012345678901234567890123456789012345678901234\",\"0xffffffff1234567890123456ffffffffffffffff1234567890123456ffffffff\"],[\"0xffffffff1234567890123456ffffffffffffffff1234567890123456ffffffff\",\"0x1234567890123456789012345678901234567890123456789012345678901234\"]]"); TransactionRequestWithStringParams transactionRequestWithStringParams = requestBuilder.setMethod("setBytes32StaticArrayArray").buildStringParamsRequest(params); - TransactionResponse transactionResponse = transactionService.sendTransactionWithStringParams(transactionRequestWithStringParams); + TransactionResponse transactionResponse = transactionService.sendTransaction(transactionRequestWithStringParams); Assert.assertEquals(transactionResponse.getTransactionReceipt().getStatus(), 0); List results = transactionResponse.getResults(); List returnObject = transactionResponse.getReturnObject(); @@ -411,7 +411,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { List params = new ArrayList<>(); params.add("[[\"12312314565456345test\"],[\"ffffffff1234567890123456ffffffffffffffff1234567890123456ffffffff\"]]"); TransactionRequestWithStringParams transactionRequestWithStringParams = requestBuilder.setMethod("buildStructB").buildStringParamsRequest(params); - TransactionResponse transactionResponse = transactionService.sendTransactionWithStringParams(transactionRequestWithStringParams); + TransactionResponse transactionResponse = transactionService.sendTransaction(transactionRequestWithStringParams); Assert.assertEquals(transactionResponse.getTransactionReceipt().getStatus(), 0); List results = transactionResponse.getResults(); @@ -430,7 +430,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { params.add("[-128,129,[32]]"); // use static struct params, get single struct TransactionRequestWithStringParams transactionRequestWithStringParams = requestBuilder.setMethod("buildStaticStruct").buildStringParamsRequest(params); - TransactionResponse transactionResponse = transactionService.sendTransactionWithStringParams(transactionRequestWithStringParams); + TransactionResponse transactionResponse = transactionService.sendTransaction(transactionRequestWithStringParams); Assert.assertEquals(transactionResponse.getTransactionReceipt().getStatus(), 0); List results = transactionResponse.getResults(); @@ -447,7 +447,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { params2.add("-256"); params2.add("12321421"); TransactionRequestWithStringParams transactionRequestWithStringParams2 = requestBuilder.setMethod("buildStaticStruct").buildStringParamsRequest(params2); - TransactionResponse transactionResponse2 = transactionService.sendTransactionWithStringParams(transactionRequestWithStringParams2); + TransactionResponse transactionResponse2 = transactionService.sendTransaction(transactionRequestWithStringParams2); Assert.assertEquals(transactionResponse.getTransactionReceipt().getStatus(), 0); results = transactionResponse2.getResults(); returnObject = transactionResponse2.getReturnObject(); diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleEIP1559TransactionService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleEIP1559TransactionService.java index 13d875cc9..359d20a95 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleEIP1559TransactionService.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleEIP1559TransactionService.java @@ -6,6 +6,8 @@ import org.fisco.bcos.sdk.v3.codec.ContractCodecException; import org.fisco.bcos.sdk.v3.model.TransactionReceipt; import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.BasicDeployRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.BasicRequest; import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequest; import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequestWithStringParams; import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequest; @@ -13,45 +15,34 @@ import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse; import org.fisco.bcos.sdk.v3.utils.Hex; -/** - * AssembleTransactionService - * - *

codec(abi, method, params) -> inputData sendTx(to, inputData) -> receipt decode(abi, method, - * receipt.output, ) -> result - */ public class AssembleEIP1559TransactionService extends AssembleTransactionService { AssembleEIP1559TransactionService(Client client) { super(client); } - public TransactionResponse sendEIP1559Transaction(TransactionRequest request) + public TransactionResponse sendEIP1559Transaction(BasicRequest request) throws ContractCodecException, JniException { - byte[] encodeMethod = - contractCodec.encodeMethod( - request.getAbi(), request.getMethod(), request.getParams()); - TransactionReceipt receipt = - transactionManager.sendTransactionEIP1559( - request.getTo(), - Hex.toHexString(encodeMethod), - request.getValue(), - request.getEip1559Struct(), - request.getAbi(), - false); - if (Objects.nonNull(receipt) - && (Objects.isNull(receipt.getInput()) || receipt.getInput().isEmpty())) { - receipt.setInput(Hex.toHexStringWithPrefix(encodeMethod)); + if (!request.isTransactionEssentialSatisfy()) { + throw new ContractCodecException("Request is not satisfy, please check."); + } + byte[] encodeMethod = null; + if (request instanceof TransactionRequest) { + encodeMethod = + contractCodec.encodeMethod( + request.getAbi(), + request.getMethod(), + ((TransactionRequest) request).getParams()); + } else if (request instanceof TransactionRequestWithStringParams) { + encodeMethod = + contractCodec.encodeMethodFromString( + request.getAbi(), + request.getMethod(), + ((TransactionRequestWithStringParams) request).getStringParams()); + } else { + throw new ContractCodecException("Request type error, please check."); } - return this.transactionDecoder.decodeReceiptWithValues( - request.getAbi(), request.getMethod(), receipt); - } - public TransactionResponse sendEIP1559TransactionWithStringParams( - TransactionRequestWithStringParams request) - throws ContractCodecException, JniException { - byte[] encodeMethod = - contractCodec.encodeMethodFromString( - request.getAbi(), request.getMethod(), request.getStringParams()); TransactionReceipt receipt = transactionManager.sendTransactionEIP1559( request.getTo(), @@ -68,32 +59,29 @@ public TransactionResponse sendEIP1559TransactionWithStringParams( request.getAbi(), request.getMethod(), receipt); } - public TransactionResponse deployContractEIP1559(DeployTransactionRequest request) + public TransactionResponse deployContractEIP1559(BasicDeployRequest request) throws ContractCodecException, JniException { - byte[] encodeConstructor = - contractCodec.encodeConstructor( - request.getAbi(), request.getBin(), request.getParams()); - TransactionReceipt receipt = - transactionManager.sendTransactionEIP1559( - request.getTo(), - Hex.toHexString(encodeConstructor), - request.getValue(), - request.getEip1559Struct(), - request.getAbi(), - true); - if (Objects.nonNull(receipt) - && (Objects.isNull(receipt.getInput()) || receipt.getInput().isEmpty())) { - receipt.setInput(Hex.toHexStringWithPrefix(encodeConstructor)); + if (!request.isTransactionEssentialSatisfy()) { + throw new ContractCodecException("DeployRequest is not satisfy, please check."); + } + + byte[] encodeConstructor = null; + if (request instanceof DeployTransactionRequest) { + encodeConstructor = + contractCodec.encodeConstructor( + request.getAbi(), + request.getBin(), + ((DeployTransactionRequest) request).getParams()); + } else if (request instanceof DeployTransactionRequestWithStringParams) { + encodeConstructor = + contractCodec.encodeConstructorFromString( + request.getAbi(), + request.getBin(), + ((DeployTransactionRequestWithStringParams) request).getStringParams()); + } else { + throw new ContractCodecException("DeployRequest type error, please check."); } - return this.transactionDecoder.decodeReceiptWithValues(request.getAbi(), "", receipt); - } - public TransactionResponse deployContractEIP1559WithStringParams( - DeployTransactionRequestWithStringParams request) - throws ContractCodecException, JniException { - byte[] encodeConstructor = - contractCodec.encodeConstructorFromString( - request.getAbi(), request.getBin(), request.getStringParams()); TransactionReceipt receipt = transactionManager.sendTransactionEIP1559( request.getTo(), @@ -109,28 +97,28 @@ public TransactionResponse deployContractEIP1559WithStringParams( return this.transactionDecoder.decodeReceiptWithValues(request.getAbi(), "", receipt); } - public String asyncSendEIP1559Transaction( - TransactionRequest request, TransactionCallback callback) + public String asyncSendEIP1559Transaction(BasicRequest request, TransactionCallback callback) throws ContractCodecException, JniException { - byte[] encodeMethod = - contractCodec.encodeMethod( - request.getAbi(), request.getMethod(), request.getParams()); - return transactionManager.asyncSendTransactionEIP1559( - request.getTo(), - Hex.toHexString(encodeMethod), - request.getValue(), - request.getEip1559Struct(), - request.getAbi(), - false, - callback); - } + if (!request.isTransactionEssentialSatisfy()) { + throw new ContractCodecException("Request is not satisfy, please check."); + } + byte[] encodeMethod = null; + if (request instanceof TransactionRequest) { + encodeMethod = + contractCodec.encodeMethod( + request.getAbi(), + request.getMethod(), + ((TransactionRequest) request).getParams()); + } else if (request instanceof TransactionRequestWithStringParams) { + encodeMethod = + contractCodec.encodeMethodFromString( + request.getAbi(), + request.getMethod(), + ((TransactionRequestWithStringParams) request).getStringParams()); + } else { + throw new ContractCodecException("Request type error, please check."); + } - public String asyncSendEIP1559TransactionWithStringParams( - TransactionRequestWithStringParams request, TransactionCallback callback) - throws ContractCodecException, JniException { - byte[] encodeMethod = - contractCodec.encodeMethodFromString( - request.getAbi(), request.getMethod(), request.getStringParams()); return transactionManager.asyncSendTransactionEIP1559( request.getTo(), Hex.toHexString(encodeMethod), @@ -142,27 +130,29 @@ public String asyncSendEIP1559TransactionWithStringParams( } public String asyncDeployContractEIP1559( - DeployTransactionRequest request, TransactionCallback callback) + BasicDeployRequest request, TransactionCallback callback) throws ContractCodecException, JniException { - byte[] encodeConstructor = - contractCodec.encodeConstructor( - request.getAbi(), request.getBin(), request.getParams()); - return transactionManager.asyncSendTransactionEIP1559( - request.getTo(), - Hex.toHexString(encodeConstructor), - request.getValue(), - request.getEip1559Struct(), - request.getAbi(), - true, - callback); - } + if (!request.isTransactionEssentialSatisfy()) { + throw new ContractCodecException("DeployRequest is not satisfy, please check."); + } + + byte[] encodeConstructor = null; + if (request instanceof DeployTransactionRequest) { + encodeConstructor = + contractCodec.encodeConstructor( + request.getAbi(), + request.getBin(), + ((DeployTransactionRequest) request).getParams()); + } else if (request instanceof DeployTransactionRequestWithStringParams) { + encodeConstructor = + contractCodec.encodeConstructorFromString( + request.getAbi(), + request.getBin(), + ((DeployTransactionRequestWithStringParams) request).getStringParams()); + } else { + throw new ContractCodecException("DeployRequest type error, please check."); + } - public String asyncDeployContractEIP1559WithStringParams( - DeployTransactionRequestWithStringParams request, TransactionCallback callback) - throws ContractCodecException, JniException { - byte[] encodeConstructor = - contractCodec.encodeConstructorFromString( - request.getAbi(), request.getBin(), request.getStringParams()); return transactionManager.asyncSendTransactionEIP1559( request.getTo(), Hex.toHexString(encodeConstructor), diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleTransactionService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleTransactionService.java index 6273c3df7..8476edd60 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleTransactionService.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleTransactionService.java @@ -17,6 +17,8 @@ import org.fisco.bcos.sdk.v3.transaction.codec.decode.ReceiptParser; import org.fisco.bcos.sdk.v3.transaction.codec.decode.TransactionDecoderInterface; import org.fisco.bcos.sdk.v3.transaction.codec.decode.TransactionDecoderService; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.BasicDeployRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.BasicRequest; import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequest; import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequestWithStringParams; import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequest; @@ -47,15 +49,57 @@ public AssembleTransactionService(Client client) { client.getCryptoSuite().getHashImpl(), client.isWASM()); } + /** + * Sets the TransactionManager for this service. DefaultTransactionManager is used by default. + * + *

ProxySignTransactionManager can be used to sign transactions with a proxy account, you can + * change account easily. + * + * @param transactionManager the TransactionManager to be set + */ public void setTransactionManager(TransactionManager transactionManager) { this.transactionManager = transactionManager; } - public TransactionResponse sendTransaction(TransactionRequest request) + /** + * This method is used to send a transaction. + * + *

If the request is an instance of TransactionRequest, it encodes the method with the + * parameters from the request. + * + *

If the request is an instance of TransactionRequestWithStringParams, it encodes the method + * with the string parameters from the request. + * + *

If the request is not an instance of either, it throws a ContractCodecException. + * + * @param request the request containing the necessary information to send the transaction + * @return TransactionResponse the response of the transaction + * @throws ContractCodecException if there is an error with the contract codec or the request is + * not an instance of TransactionRequest or TransactionRequestWithStringParams + * @throws JniException if there is an error with the JNI + */ + public TransactionResponse sendTransaction(BasicRequest request) throws ContractCodecException, JniException { - byte[] encodeMethod = - contractCodec.encodeMethod( - request.getAbi(), request.getMethod(), request.getParams()); + if (!request.isTransactionEssentialSatisfy()) { + throw new ContractCodecException("Request is not satisfy, please check."); + } + byte[] encodeMethod = null; + if (request instanceof TransactionRequest) { + encodeMethod = + contractCodec.encodeMethod( + request.getAbi(), + request.getMethod(), + ((TransactionRequest) request).getParams()); + } else if (request instanceof TransactionRequestWithStringParams) { + encodeMethod = + contractCodec.encodeMethodFromString( + request.getAbi(), + request.getMethod(), + ((TransactionRequestWithStringParams) request).getStringParams()); + } else { + throw new ContractCodecException("Request type error, please check."); + } + TransactionReceipt receipt = transactionManager.sendTransaction( request.getTo(), @@ -73,34 +117,44 @@ public TransactionResponse sendTransaction(TransactionRequest request) request.getAbi(), request.getMethod(), receipt); } - public TransactionResponse sendTransactionWithStringParams( - TransactionRequestWithStringParams request) + /** + * This method is used to deploy a contract. + * + *

If the request is an instance of DeployTransactionRequest, it encodes the constructor with + * the parameters from the request. + * + *

If the request is an instance of DeployTransactionRequestWithStringParams, it encodes the + * constructor with the string parameters from the request. + * + *

If the request is not an instance of either, it throws a ContractCodecException. + * + * @param request the request containing the necessary information to deploy the contract + * @return TransactionResponse the response of the transaction + * @throws ContractCodecException if there is an error with the contract codec or the request is + * not an instance of DeployTransactionRequest or DeployTransactionRequestWithStringParams + * @throws JniException if there is an error with the JNI + */ + public TransactionResponse deployContract(BasicDeployRequest request) throws ContractCodecException, JniException { - byte[] transactionData = - contractCodec.encodeMethodFromString( - request.getAbi(), request.getMethod(), request.getStringParams()); - TransactionReceipt receipt = - transactionManager.sendTransaction( - request.getTo(), - Hex.toHexString(transactionData), - request.getValue(), - request.getGasPrice(), - request.getGasLimit(), - request.getAbi(), - false); - if (Objects.nonNull(receipt) - && (Objects.isNull(receipt.getInput()) || receipt.getInput().isEmpty())) { - receipt.setInput(Hex.toHexStringWithPrefix(transactionData)); + if (!request.isTransactionEssentialSatisfy()) { + throw new ContractCodecException("DeployRequest is not satisfy, please check."); + } + byte[] encodeConstructor = null; + if (request instanceof DeployTransactionRequest) { + encodeConstructor = + contractCodec.encodeConstructor( + request.getAbi(), + request.getBin(), + ((DeployTransactionRequest) request).getParams()); + } else if (request instanceof DeployTransactionRequestWithStringParams) { + encodeConstructor = + contractCodec.encodeConstructorFromString( + request.getAbi(), + request.getBin(), + ((DeployTransactionRequestWithStringParams) request).getStringParams()); + } else { + throw new ContractCodecException("DeployRequest type error, please check."); } - return this.transactionDecoder.decodeReceiptWithValues( - request.getAbi(), request.getMethod(), receipt); - } - - public TransactionResponse deployContract(DeployTransactionRequest request) - throws ContractCodecException, JniException { - byte[] encodeConstructor = - contractCodec.encodeConstructor( - request.getAbi(), request.getBin(), request.getParams()); TransactionReceipt receipt = transactionManager.sendTransaction( request.getTo(), @@ -117,33 +171,45 @@ public TransactionResponse deployContract(DeployTransactionRequest request) return this.transactionDecoder.decodeReceiptWithoutValues(request.getAbi(), receipt); } - public TransactionResponse deployContractWithStringParams( - DeployTransactionRequestWithStringParams request) + /** + * This method is used to send a transaction asynchronously. + * + *

If the request is an instance of TransactionRequest, it encodes the method with the + * parameters from the request. + * + *

If the request is an instance of TransactionRequestWithStringParams, it encodes the method + * with the string parameters from the request. + * + *

If the request is not an instance of either, it throws a ContractCodecException. + * + * @param request the request containing the necessary information to send the transaction + * @param callback the callback to be called when the transaction is sent + * @return String the transaction hash + * @throws ContractCodecException if there is an error with the contract codec or the request is + * not an instance of TransactionRequest or TransactionRequestWithStringParams + * @throws JniException if there is an error with the JNI + */ + public String asyncSendTransaction(BasicRequest request, TransactionCallback callback) throws ContractCodecException, JniException { - byte[] encodedConstructor = - contractCodec.encodeConstructorFromString( - request.getAbi(), request.getBin(), request.getStringParams()); - TransactionReceipt receipt = - transactionManager.sendTransaction( - request.getTo(), - Hex.toHexString(encodedConstructor), - request.getValue(), - request.getGasPrice(), - request.getGasLimit(), - request.getAbi(), - true); - if (Objects.nonNull(receipt) - && (Objects.isNull(receipt.getInput()) || receipt.getInput().isEmpty())) { - receipt.setInput(Hex.toHexStringWithPrefix(encodedConstructor)); + if (!request.isTransactionEssentialSatisfy()) { + throw new ContractCodecException("Request is not satisfy, please check."); + } + byte[] encodeMethod = null; + if (request instanceof TransactionRequest) { + encodeMethod = + contractCodec.encodeMethod( + request.getAbi(), + request.getMethod(), + ((TransactionRequest) request).getParams()); + } else if (request instanceof TransactionRequestWithStringParams) { + encodeMethod = + contractCodec.encodeMethodFromString( + request.getAbi(), + request.getMethod(), + ((TransactionRequestWithStringParams) request).getStringParams()); + } else { + throw new ContractCodecException("Request type error, please check."); } - return this.transactionDecoder.decodeReceiptWithoutValues(request.getAbi(), receipt); - } - - public String asyncSendTransaction(TransactionRequest request, TransactionCallback callback) - throws ContractCodecException, JniException { - byte[] encodeMethod = - contractCodec.encodeMethod( - request.getAbi(), request.getMethod(), request.getParams()); return transactionManager.asyncSendTransaction( request.getTo(), Hex.toHexString(encodeMethod), @@ -155,29 +221,45 @@ public String asyncSendTransaction(TransactionRequest request, TransactionCallba callback); } - public String asyncSendTransactionWithStringParams( - TransactionRequestWithStringParams request, TransactionCallback callback) - throws ContractCodecException, JniException { - byte[] encodeMethodFromString = - contractCodec.encodeMethodFromString( - request.getAbi(), request.getMethod(), request.getStringParams()); - return transactionManager.asyncSendTransaction( - request.getTo(), - Hex.toHexString(encodeMethodFromString), - request.getValue(), - request.getGasPrice(), - request.getGasLimit(), - request.getAbi(), - false, - callback); - } - - public String asyncDeployContract( - DeployTransactionRequest request, TransactionCallback callback) + /** + * This method is used to deploy a contract asynchronously. + * + *

If the request is an instance of DeployTransactionRequest, it encodes the constructor with + * the parameters from the request. + * + *

If the request is an instance of DeployTransactionRequestWithStringParams, it encodes the + * constructor with the string parameters from the request. + * + *

If the request is not an instance of either, it throws a ContractCodecException. + * + * @param request the request containing the necessary information to deploy the contract + * @param callback the callback to be called when the transaction is sent + * @return String the transaction hash + * @throws ContractCodecException if there is an error with the contract codec or the request is + * not an instance of DeployTransactionRequest or DeployTransactionRequestWithStringParams + * @throws JniException if there is an error with the JNI + */ + public String asyncDeployContract(BasicDeployRequest request, TransactionCallback callback) throws ContractCodecException, JniException { - byte[] encodeConstructor = - contractCodec.encodeConstructor( - request.getAbi(), request.getBin(), request.getParams()); + if (!request.isTransactionEssentialSatisfy()) { + throw new ContractCodecException("DeployRequest is not satisfy, please check."); + } + byte[] encodeConstructor = null; + if (request instanceof DeployTransactionRequest) { + encodeConstructor = + contractCodec.encodeConstructor( + request.getAbi(), + request.getBin(), + ((DeployTransactionRequest) request).getParams()); + } else if (request instanceof DeployTransactionRequestWithStringParams) { + encodeConstructor = + contractCodec.encodeConstructorFromString( + request.getAbi(), + request.getBin(), + ((DeployTransactionRequestWithStringParams) request).getStringParams()); + } else { + throw new ContractCodecException("DeployRequest type error, please check."); + } return transactionManager.asyncSendTransaction( request.getTo(), Hex.toHexString(encodeConstructor), @@ -189,48 +271,86 @@ public String asyncDeployContract( callback); } - public String asyncDeployContractWithStringParams( - DeployTransactionRequestWithStringParams request, TransactionCallback callback) - throws JniException, ContractCodecException { - byte[] encodeConstructorFromString = - contractCodec.encodeConstructorFromString( - request.getAbi(), request.getBin(), request.getStringParams()); - return transactionManager.asyncSendTransaction( - request.getTo(), - Hex.toHexString(encodeConstructorFromString), - request.getValue(), - request.getGasPrice(), - request.getGasLimit(), - request.getAbi(), - true, - callback); - } - - public CallResponse sendCall(TransactionRequest request) - throws ContractCodecException, JniException { - byte[] encodeMethod = - contractCodec.encodeMethod( - request.getAbi(), request.getMethod(), request.getParams()); + /** + * This method is used to send a call. + * + *

If the request is an instance of TransactionRequest, it encodes the method with the + * parameters from the request. + * + *

If the request is an instance of TransactionRequestWithStringParams, it encodes the method + * with the string parameters from the request. + * + *

If the request is not an instance of either, it throws a ContractCodecException. + * + * @param request the request containing the necessary information to send the call + * @return CallResponse the response of the call + * @throws ContractCodecException if there is an error with the contract codec or the request is + * not an instance of TransactionRequest or TransactionRequestWithStringParams + * @throws JniException if there is an error with the JNI + */ + public CallResponse sendCall(BasicRequest request) throws ContractCodecException, JniException { + if (!request.isTransactionEssentialSatisfy()) { + throw new ContractCodecException("Request is not satisfy, please check."); + } + byte[] encodeMethod = null; + if (request instanceof TransactionRequest) { + encodeMethod = + contractCodec.encodeMethod( + request.getAbi(), + request.getMethod(), + ((TransactionRequest) request).getParams()); + } else if (request instanceof TransactionRequestWithStringParams) { + encodeMethod = + contractCodec.encodeMethodFromString( + request.getAbi(), + request.getMethod(), + ((TransactionRequestWithStringParams) request).getStringParams()); + } else { + throw new ContractCodecException("Request type error, please check."); + } Call call = transactionManager.sendCall(request.getTo(), Hex.toHexString(encodeMethod)); return parseCallResponse(request, call); } - public CallResponse sendCallWithStringParams(TransactionRequestWithStringParams request) + /** + * This method is used to send a call asynchronously. + * + *

If the request is an instance of TransactionRequest, it encodes the method with the + * parameters from the request. + * + *

If the request is an instance of TransactionRequestWithStringParams, it encodes the method + * with the string parameters from the request. + * + *

If the request is not an instance of either, it throws a ContractCodecException. + * + * @param request the request containing the necessary information to send the call + * @param callback the callback to be called when the call is sent + * @throws ContractCodecException if there is an error with the contract codec or the request is + * not an instance of TransactionRequest or TransactionRequestWithStringParams + * @throws JniException if there is an error with the JNI + */ + public void asyncSendCall(BasicRequest request, RespCallback callback) throws ContractCodecException, JniException { - byte[] encodeMethodFromString = - contractCodec.encodeMethodFromString( - request.getAbi(), request.getMethod(), request.getStringParams()); - Call call = - transactionManager.sendCall( - request.getTo(), Hex.toHexString(encodeMethodFromString)); - return parseCallResponse(request, call); - } + if (!request.isTransactionEssentialSatisfy()) { + throw new ContractCodecException("Request is not satisfy, please check."); + } + byte[] encodeMethod = null; + if (request instanceof TransactionRequest) { + encodeMethod = + contractCodec.encodeMethod( + request.getAbi(), + request.getMethod(), + ((TransactionRequest) request).getParams()); + } else if (request instanceof TransactionRequestWithStringParams) { + encodeMethod = + contractCodec.encodeMethodFromString( + request.getAbi(), + request.getMethod(), + ((TransactionRequestWithStringParams) request).getStringParams()); + } else { + throw new ContractCodecException("Request type error, please check."); + } - public void asyncSendCall(TransactionRequest request, RespCallback callback) - throws ContractCodecException, JniException { - byte[] encodeMethod = - contractCodec.encodeMethod( - request.getAbi(), request.getMethod(), request.getParams()); transactionManager.asyncSendCall( request.getTo(), Hex.toHexString(encodeMethod), @@ -254,36 +374,7 @@ public void onError(Response errorResponse) { }); } - public void asyncSendCallWithStringParams( - TransactionRequestWithStringParams request, RespCallback callback) - throws ContractCodecException, JniException { - byte[] encodeMethodFromString = - contractCodec.encodeMethodFromString( - request.getAbi(), request.getMethod(), request.getStringParams()); - transactionManager.asyncSendCall( - request.getTo(), - Hex.toHexString(encodeMethodFromString), - new RespCallback() { - @Override - public void onResponse(Call call) { - try { - callback.onResponse(parseCallResponse(request, call)); - } catch (ContractCodecException e) { - Response response = new Response(); - response.setErrorCode(-1); - response.setErrorMessage(e.getMessage()); - callback.onError(response); - } - } - - @Override - public void onError(Response errorResponse) { - callback.onError(errorResponse); - } - }); - } - - private CallResponse parseCallResponse(TransactionRequest request, Call call) + private CallResponse parseCallResponse(BasicRequest request, Call call) throws ContractCodecException { CallResponse callResponse = new CallResponse(); RetCode retCode = ReceiptParser.parseCallOutput(call.getCallResult(), ""); diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicDeployRequest.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicDeployRequest.java new file mode 100644 index 000000000..729af6761 --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicDeployRequest.java @@ -0,0 +1,48 @@ +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto; + +import java.math.BigInteger; +import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; + +public class BasicDeployRequest extends BasicRequest { + private String bin; + + public BasicDeployRequest( + String abi, + String bin, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + EIP1559Struct eip1559Struct) { + super(abi, "", "", value, gasPrice, gasLimit, eip1559Struct); + this.bin = bin; + } + + public String getBin() { + return bin; + } + + public void setBin(String bin) { + this.bin = bin; + } + + public void setTo(String to) { + this.to = to; + } + + @Override + public boolean isTransactionEssentialSatisfy() { + return super.isTransactionEssentialSatisfy() && bin != null; + } + + @Override + public String toString() { + return "BasicDeployRequest{" + + "base='" + + super.toString() + + '\'' + + "bin='" + + bin + + '\'' + + "} "; + } +} diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicRequest.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicRequest.java new file mode 100644 index 000000000..5f011635f --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicRequest.java @@ -0,0 +1,65 @@ +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto; + +import java.math.BigInteger; +import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; + +public class BasicRequest { + + protected String abi; + protected String method; + + protected String to; + protected BigInteger value; + protected BigInteger gasPrice; + protected BigInteger gasLimit; + protected EIP1559Struct eip1559Struct; + + public BasicRequest( + String abi, + String method, + String to, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + EIP1559Struct eip1559Struct) { + this.abi = abi; + this.method = method; + this.to = to; + this.value = value; + this.gasPrice = gasPrice; + this.gasLimit = gasLimit; + this.eip1559Struct = eip1559Struct; + } + + public String getAbi() { + return abi; + } + + public String getMethod() { + return method; + } + + public String getTo() { + return to; + } + + public BigInteger getValue() { + return value; + } + + public BigInteger getGasPrice() { + return gasPrice; + } + + public BigInteger getGasLimit() { + return gasLimit; + } + + public EIP1559Struct getEip1559Struct() { + return eip1559Struct; + } + + public boolean isTransactionEssentialSatisfy() { + return abi != null && method != null && to != null; + } +} diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequest.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequest.java index 4206e6080..13a513813 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequest.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequest.java @@ -1,11 +1,12 @@ package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto; import java.math.BigInteger; +import java.util.List; import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; -public class DeployTransactionRequest extends TransactionRequest { +public class DeployTransactionRequest extends BasicDeployRequest { - private String bin; + private List params; public DeployTransactionRequest( String abi, @@ -14,35 +15,19 @@ public DeployTransactionRequest( BigInteger gasPrice, BigInteger gasLimit, EIP1559Struct eip1559Struct) { - super(abi, null, null, value, gasPrice, gasLimit, eip1559Struct); - this.bin = bin; + super(abi, bin, value, gasPrice, gasLimit, eip1559Struct); } - public String getBin() { - return bin; + public void setParams(List params) { + this.params = params; } - public void setBin(String bin) { - this.bin = bin; - } - - public void setTo(String to) { - this.to = to; + public List getParams() { + return params; } @Override public boolean isTransactionEssentialSatisfy() { - return super.isTransactionEssentialSatisfy() && bin != null; - } - - @Override - public String toString() { - return "DeployTransactionRequest{" - + "base=" - + super.toString() - + ", bin='" - + bin - + '\'' - + "} "; + return super.isTransactionEssentialSatisfy() && params != null; } } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequestWithStringParams.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequestWithStringParams.java index bad844ef9..3c5444083 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequestWithStringParams.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequestWithStringParams.java @@ -4,7 +4,7 @@ import java.util.List; import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; -public class DeployTransactionRequestWithStringParams extends DeployTransactionRequest { +public class DeployTransactionRequestWithStringParams extends BasicDeployRequest { private List stringParams; @@ -34,10 +34,10 @@ public boolean isTransactionEssentialSatisfy() { @Override public String toString() { return "DeployTransactionRequestWithStringParams{" - + "to='" - + to - + '\'' - + "} " - + super.toString(); + + "base=" + + super.toString() + + ", params=" + + stringParams + + "} "; } } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequest.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequest.java index 727e219c7..0e9b28cbb 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequest.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequest.java @@ -4,15 +4,8 @@ import java.util.List; import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; -public class TransactionRequest { - private String abi; - private String method; - protected String to; +public class TransactionRequest extends BasicRequest { private List params; - private BigInteger value; - private BigInteger gasPrice; - private BigInteger gasLimit; - private EIP1559Struct eip1559Struct; public TransactionRequest( String abi, @@ -22,53 +15,20 @@ public TransactionRequest( BigInteger gasPrice, BigInteger gasLimit, EIP1559Struct eip1559Struct) { - this.abi = abi; - this.method = method; - this.to = to; - this.value = value; - this.gasPrice = gasPrice; - this.gasLimit = gasLimit; - this.eip1559Struct = eip1559Struct; + super(abi, method, to, value, gasPrice, gasLimit, eip1559Struct); } public void setParams(List params) { this.params = params; } - public String getAbi() { - return abi; - } - - public String getMethod() { - return method; - } - - public String getTo() { - return to; - } - public List getParams() { return params; } - public BigInteger getValue() { - return value; - } - - public BigInteger getGasPrice() { - return gasPrice; - } - - public BigInteger getGasLimit() { - return gasLimit; - } - - public EIP1559Struct getEip1559Struct() { - return eip1559Struct; - } - + @Override public boolean isTransactionEssentialSatisfy() { - return abi != null && method != null && to != null; + return super.isTransactionEssentialSatisfy() && params != null; } @Override diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequestWithStringParams.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequestWithStringParams.java index 8cd89c2a7..8266fe0d9 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequestWithStringParams.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequestWithStringParams.java @@ -4,7 +4,7 @@ import java.util.List; import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; -public class TransactionRequestWithStringParams extends TransactionRequest { +public class TransactionRequestWithStringParams extends BasicRequest { private List stringParams;