web3j icon indicating copy to clipboard operation
web3j copied to clipboard

Function of smart contract always returns 0 from Web3j

Open dungtctin4 opened this issue 9 years ago • 1 comments

Hi every body, I have this problem and please help me to solve it .

I am successful to compile and deploy my smart contract using web3j on testrpc. But When I call the function that returns uint in this contract from web3j, I always receive 0. I present below my contract and my Java code using Web3j.

  1. My Smart contracts
pragma solidity ^0.4.6;
contract grandParentContract{

  address public myAddr = 0x1005...;

  modifier onlyFromMe() {
    if ((myAddr != address(0)) && (tx.origin != myAddr)) {
        Msg("Permission Error: this function is limited to the my address origin.");
        return;
    }
    _;
  }
}
pragma solidity ^0.4.6;
contract parentContract is grandParentContract {

/**************BEGIN: attributes or properties or dataobjectType**********/ 


 uint public maxIdValue=0;
 uint public minIdValue=0;

 event ElementAddition(uint _id);

 function parentContract() grandParentContract() {
   maxIdValue = 0;
   minIdValue = 0;
 }

}

contract MyContract is parentContract {
  struct MyElement {

    uint id; 
    string name;
    string description;
    uint[] currentForeignKeys;
  }
  mapping(uint => MyElement) public myElementMap; 

 
  function MyContract() parentContract() {

  }

 function addNewElement(string _name, string _description) public onlyFromMe() returns (uint) {
    uint _id = maxIdValue+1;


    myElementMap[_id].name = _name;
    myElementMap[_id].description = _description;

    maxIdValue = _id;
    ElementAddition(_id);
    return _id;
  }

}
  1. Compile and deploy Compile and Deploy successfully on testrpc using Web3 j in Java code

Web3j web3 = Web3j.build(new HttpService("testrpcIp:testrpcPort")); When I compile smart contracts, I obtains 3 *.bin files and 3 *.abi files MyContract.bin(abi), parentContract.bin(abi), grandParentContract.bin(abi) Of course, I only deploy the MyContract.bin smart contract file using web3j in Java code (Note that, when I deploy this smart contract, I only load the content of this MyContract.bin file, not add 0x before this content) The deployment is successful and I receive the contract address

  1. Call the addNewElement function on the MyContract smart contract from obtained contract address above, and I receive the 0 for its return
List<String> results= callMyFunction(web3,aCredential,contractAddress, "addNewElement", Arrays.<Type>asList(new Utf8String("name1"),new Utf8String("description of name1")), Arrays.asList(new TypeReference<Uint>() {}));
System.out.println("results = " + results);


public static synchronized List<String> callMyFunction(Web3j web3, Credentials creds, String contractAddress, String functionName, List inputArgs, List outputs) throws Exception {

    List<String> results = new ArrayList();
    Function function = new Function(functionName, inputArgs, outputs);
    String encodedFunction = FunctionEncoder.encode(function);
    EthCall response = web3.ethCall(Transaction.createEthCallTransaction(creds.getEcKeyPair().getPrivateKey().toString(16), contractAddress, encodedFunction), DefaultBlockParameterName.LATEST).sendAsync().get();
    if(response.hasError()){
        System.out.println("functionCall: " + response.getError().getMessage());
    }
    convertMyResult((response != null) && (response.getValue() != null) ? FunctionReturnDecoder.decode(response.getValue(), function.getOutputParameters()) : new ArrayList(), results);
    return results;
}

public static void convertMyResult(List<Type> returns, List<String> results) throws Exception {
    if (returns.size() > 0) {

        for(int i=0; i<returns.size(); i++){

                results.add(returns.get(i).getValue().toString());

        }
    }
}

I always receive the results : [0]. I tried to put maxIdValue in the MyContract instead of in ParentContract, but the result is the same. I even tried return 1 in this function, but the result is always 0. It seems that this function addNewElement is not called at all although I donot receive any errors at all in Java and I really received the zero result from this function. Please help me this problem. Thanks a lot

dungtctin4 avatar Jul 14 '17 18:07 dungtctin4

I use events to get returned value. Thanks

dungtctin4 avatar Jul 17 '17 21:07 dungtctin4