org.web3j.tx.exceptions.ContractCallException Java Examples

The following examples show how to use org.web3j.tx.exceptions.ContractCallException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReadonlyTransactionManagerTest.java    From web3j with Apache License 2.0 7 votes vote down vote up
@Test
public void sendCallRevertedTest() throws IOException {
    when(response.isReverted()).thenReturn(true);
    when(response.getRevertReason()).thenReturn(OWNER_REVERT_MSG_STR);
    when(service.send(any(), any())).thenReturn(response);

    ReadonlyTransactionManager readonlyTransactionManager =
            new ReadonlyTransactionManager(web3j, "");

    ContractCallException thrown =
            assertThrows(
                    ContractCallException.class,
                    () -> readonlyTransactionManager.sendCall("", "", defaultBlockParameter));

    assertEquals(String.format(REVERT_ERR_STR, OWNER_REVERT_MSG_STR), thrown.getMessage());
}
 
Example #2
Source File: PrivateContractPublicStateAcceptanceTest.java    From besu with Apache License 2.0 6 votes vote down vote up
@Test(expected = ContractCallException.class)
public void mustNotAllowAccessToPrivateStateFromPublicTx() throws Exception {
  final EventEmitter privateEventEmitter =
      transactionNode.execute(
          (privateContractTransactions.createSmartContract(
              EventEmitter.class,
              transactionNode.getTransactionSigningKey(),
              POW_CHAIN_ID,
              transactionNode.getEnclaveKey())));

  final TransactionReceipt receipt = privateEventEmitter.store(BigInteger.valueOf(12)).send();
  assertThat(receipt).isNotNull();

  final CrossContractReader publicReader =
      transactionNode.execute(
          contractTransactions.createSmartContract(CrossContractReader.class));

  publicReader.read(privateEventEmitter.getContractAddress()).send();
}
 
Example #3
Source File: Contract.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T extends Type, R> R executeCallSingleValueReturn(
        Function function, Class<R> returnType) throws IOException {
    T result = executeCallSingleValueReturn(function);
    if (result == null) {
        throw new ContractCallException("Empty value (0x) returned from contract");
    }

    Object value = result.getValue();
    if (returnType.isAssignableFrom(value.getClass())) {
        return (R) value;
    } else if (result.getClass().equals(Address.class) && returnType.equals(String.class)) {
        return (R) result.toString();  // cast isn't necessary
    } else {
        throw new ContractCallException(
                "Unable to convert response: " + value
                        + " to expected type: " + returnType.getSimpleName());
    }
}
 
Example #4
Source File: Contract.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T extends Type, R> R executeCallSingleValueReturn(
        Function function, Class<R> returnType) throws IOException {
    T result = executeCallSingleValueReturn(function);
    if (result == null) {
        throw new ContractCallException("Empty value (0x) returned from contract");
    }

    Object value = result.getValue();
    if (returnType.isAssignableFrom(value.getClass())) {
        return (R) value;
    } else if (result.getClass().equals(Address.class) && returnType.equals(String.class)) {
        return (R) result.toString();  // cast isn't necessary
    } else {
        throw new ContractCallException(
                "Unable to convert response: " + value
                        + " to expected type: " + returnType.getSimpleName());
    }
}
 
Example #5
Source File: PrivateTransactionManager.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Override
public String sendCall(
        final String to, final String data, final DefaultBlockParameter defaultBlockParameter)
        throws IOException {
    try {
        EthSendTransaction est =
                sendTransaction(
                        gasProvider.getGasPrice(),
                        gasProvider.getGasLimit(),
                        to,
                        data,
                        BigInteger.ZERO);
        final TransactionReceipt ptr = processResponse(est);

        if (!ptr.isStatusOK()) {
            throw new ContractCallException(
                    String.format(REVERT_ERR_STR, extractRevertReason(ptr, data, besu, false)));
        }
        return ((PrivateTransactionReceipt) ptr).getOutput();
    } catch (TransactionException e) {
        log.error("Failed to execute call", e);
        return null;
    }
}
 
Example #6
Source File: Contract.java    From web3j with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T extends Type, R> R executeCallSingleValueReturn(
        Function function, Class<R> returnType) throws IOException {
    T result = executeCallSingleValueReturn(function);
    if (result == null) {
        throw new ContractCallException("Empty value (0x) returned from contract");
    }

    Object value = result.getValue();
    if (returnType.isAssignableFrom(result.getClass())) {
        return (R) result;
    } else if (returnType.isAssignableFrom(value.getClass())) {
        return (R) value;
    } else if (result.getClass().equals(Address.class) && returnType.equals(String.class)) {
        return (R) result.toString(); // cast isn't necessary
    } else {
        throw new ContractCallException(
                "Unable to convert response: "
                        + value
                        + " to expected type: "
                        + returnType.getSimpleName());
    }
}
 
Example #7
Source File: BaseContract.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private <T> CallResponse<T> executeCallObjectValueReturn(Function function, Class<T> returnType) throws IOException {
 	PlatonCall ethCall = web3j.platonCall(
             Transaction.createEthCallTransaction(
             		transactionManager.getFromAddress(), contractAddress, EncoderUtils.functionEncoder(function)),
             DefaultBlockParameterName.LATEST)
             .send();
 	
 	String result = Numeric.cleanHexPrefix(ethCall.getValue());
 	if(result==null || "".equals(result)){
 		  throw new ContractCallException("Empty value (0x) returned from contract");
 	}

 	CallRet callRet = JSONUtil.parseObject(new String(Hex.decode(result)), CallRet.class);
     if (callRet == null) {
     	throw new ContractCallException("Unable to convert response: " + result);
     }
     
     CallResponse<T> callResponse = new CallResponse<T>();
     if (callRet.isStatusOk()) {
     	callResponse.setCode(callRet.getCode());
     	if(BigInteger.class.isAssignableFrom(returnType) ) {
     		callResponse.setData((T)numberDecoder(callRet.getRet()));
     	}else {
     		callResponse.setData(JSONUtil.parseObject(JSONUtil.toJSONString(callRet.getRet()), returnType));
}
     } else {
     	callResponse.setCode(callRet.getCode());
     	callResponse.setErrMsg(callRet.getRet().toString());
     }
     return callResponse;
 }
 
Example #8
Source File: BaseContract.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private <T> CallResponse<List<T>> executeCallListValueReturn(Function function, Class<T> returnType) throws IOException {
	PlatonCall ethCall = web3j.platonCall(
            Transaction.createEthCallTransaction(
            		transactionManager.getFromAddress(), contractAddress, EncoderUtils.functionEncoder(function)),
            DefaultBlockParameterName.LATEST)
            .send();
	
	String result = Numeric.cleanHexPrefix(ethCall.getValue());
	if(result==null || "".equals(result)){
		  throw new ContractCallException("Empty value (0x) returned from contract");
	}
	
	CallRet callRet = JSONUtil.parseObject(new String(Hex.decode(result)), CallRet.class);
    if (callRet == null) {
    	throw new ContractCallException("Unable to convert response: " + result);
    }
    
    CallResponse<List<T>> callResponse = new CallResponse<List<T>>();
    if (callRet.isStatusOk()) {
    	callResponse.setCode(callRet.getCode());
    	callResponse.setData(JSONUtil.parseArray(JSONUtil.toJSONString(callRet.getRet()), returnType));
    } else {
    	callResponse.setCode(callRet.getCode());
    	callResponse.setErrMsg(callRet.getRet().toString());
    }

    if(callRet.getCode() == ErrorCode.OBJECT_NOT_FOUND){
        callResponse.setCode(ErrorCode.SUCCESS);
        callResponse.setData(Collections.emptyList());
    }

    return callResponse;
}
 
Example #9
Source File: ContractTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCallSingleValueReverted() throws Exception {
    prepareCall(OWNER_REVERT_MSG_HASH);
    ContractCallException thrown =
            assertThrows(ContractCallException.class, () -> contract.callSingleValue().send());

    assertEquals(
            String.format(TransactionManager.REVERT_ERR_STR, OWNER_REVERT_MSG_STR),
            thrown.getMessage());
}
 
Example #10
Source File: TransactionManager.java    From web3j with Apache License 2.0 4 votes vote down vote up
static void assertCallNotReverted(EthCall ethCall) {
    if (ethCall.isReverted()) {
        throw new ContractCallException(
                String.format(REVERT_ERR_STR, ethCall.getRevertReason()));
    }
}