From 1c297b5f746b1c46ec1e97f2001ec744f33568bc Mon Sep 17 00:00:00 2001 From: fullkomnun Date: Sun, 29 Mar 2020 15:35:57 +0300 Subject: [PATCH 1/2] handle contract call reverts properly by throwing a ContractCallException --- .../web3j/quorum/tx/ClientTransactionManager.java | 13 ++++++++----- src/main/kotlin/org/web3j/tx/ContractErrorUtil.kt | 7 +++++++ 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/org/web3j/tx/ContractErrorUtil.kt diff --git a/src/main/java/org/web3j/quorum/tx/ClientTransactionManager.java b/src/main/java/org/web3j/quorum/tx/ClientTransactionManager.java index 40ff296..2a19198 100644 --- a/src/main/java/org/web3j/quorum/tx/ClientTransactionManager.java +++ b/src/main/java/org/web3j/quorum/tx/ClientTransactionManager.java @@ -19,11 +19,13 @@ import org.web3j.protocol.Web3j; import org.web3j.protocol.core.DefaultBlockParameter; import org.web3j.protocol.core.methods.request.Transaction; +import org.web3j.protocol.core.methods.response.EthCall; import org.web3j.protocol.core.methods.response.EthGetCode; import org.web3j.protocol.core.methods.response.EthSendTransaction; import org.web3j.quorum.Quorum; import org.web3j.quorum.methods.request.PrivateTransaction; import org.web3j.tx.TransactionManager; +import org.web3j.tx.ContractErrorUtil; /** TransactionManager implementation for using a Quorum node to transact. */ public class ClientTransactionManager extends TransactionManager { @@ -112,11 +114,12 @@ public EthSendTransaction sendTransaction( @Override public String sendCall(String to, String data, DefaultBlockParameter defaultBlockParameter) throws IOException { - return quorum.ethCall( - Transaction.createEthCallTransaction(getFromAddress(), to, data), - defaultBlockParameter) - .send() - .getValue(); + EthCall ethCall = quorum.ethCall( + Transaction.createEthCallTransaction(getFromAddress(), to, data), + defaultBlockParameter) + .send(); + ContractErrorUtil.assertCallNotReverted(ethCall); + return ethCall.getValue(); } @Override diff --git a/src/main/kotlin/org/web3j/tx/ContractErrorUtil.kt b/src/main/kotlin/org/web3j/tx/ContractErrorUtil.kt new file mode 100644 index 0000000..9da5e0e --- /dev/null +++ b/src/main/kotlin/org/web3j/tx/ContractErrorUtil.kt @@ -0,0 +1,7 @@ +@file:JvmName("ContractErrorUtil") + +package org.web3j.tx + +import org.web3j.protocol.core.methods.response.EthCall + +fun assertCallNotReverted(ethCall: EthCall) = TransactionManager.assertCallNotReverted(ethCall) \ No newline at end of file From 07c528b99d94b225f4b24136e5bc909341246f6d Mon Sep 17 00:00:00 2001 From: fullkomnun Date: Mon, 30 Mar 2020 09:45:33 +0300 Subject: [PATCH 2/2] fixed format violations --- .../web3j/quorum/tx/ClientTransactionManager.java | 11 ++++++----- src/main/kotlin/org/web3j/tx/ContractErrorUtil.kt | 14 +++++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/web3j/quorum/tx/ClientTransactionManager.java b/src/main/java/org/web3j/quorum/tx/ClientTransactionManager.java index 2a19198..c50562e 100644 --- a/src/main/java/org/web3j/quorum/tx/ClientTransactionManager.java +++ b/src/main/java/org/web3j/quorum/tx/ClientTransactionManager.java @@ -24,8 +24,8 @@ import org.web3j.protocol.core.methods.response.EthSendTransaction; import org.web3j.quorum.Quorum; import org.web3j.quorum.methods.request.PrivateTransaction; -import org.web3j.tx.TransactionManager; import org.web3j.tx.ContractErrorUtil; +import org.web3j.tx.TransactionManager; /** TransactionManager implementation for using a Quorum node to transact. */ public class ClientTransactionManager extends TransactionManager { @@ -114,10 +114,11 @@ public EthSendTransaction sendTransaction( @Override public String sendCall(String to, String data, DefaultBlockParameter defaultBlockParameter) throws IOException { - EthCall ethCall = quorum.ethCall( - Transaction.createEthCallTransaction(getFromAddress(), to, data), - defaultBlockParameter) - .send(); + EthCall ethCall = + quorum.ethCall( + Transaction.createEthCallTransaction(getFromAddress(), to, data), + defaultBlockParameter) + .send(); ContractErrorUtil.assertCallNotReverted(ethCall); return ethCall.getValue(); } diff --git a/src/main/kotlin/org/web3j/tx/ContractErrorUtil.kt b/src/main/kotlin/org/web3j/tx/ContractErrorUtil.kt index 9da5e0e..3046ecc 100644 --- a/src/main/kotlin/org/web3j/tx/ContractErrorUtil.kt +++ b/src/main/kotlin/org/web3j/tx/ContractErrorUtil.kt @@ -1,7 +1,19 @@ +/* + * Copyright 2020 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ @file:JvmName("ContractErrorUtil") package org.web3j.tx import org.web3j.protocol.core.methods.response.EthCall -fun assertCallNotReverted(ethCall: EthCall) = TransactionManager.assertCallNotReverted(ethCall) \ No newline at end of file +fun assertCallNotReverted(ethCall: EthCall) = TransactionManager.assertCallNotReverted(ethCall)