contract Calculator{
int result;
function Calculator(int initialResult){
result=initialResult;
}
}
If above is the contract source code I have created then how to call Calculator function using ethereumj API. I have already used
contract.callFunction("Calculator", 10)
But this did not work. Also called
contract.callConstFunction("Calculator", 10)
But this too didn't not work.
And when saw the ABI it had the function type ="constructor" but couldn't find any method that represents calling constructor.
Please suggest if there is any way to call constructor functions?
Best Answer
I finally found a solution. As discussed in the other answer, you need to parse and append the init parameters to the contract code before sending the transaction. I did't found actually an elegant and generic way to parse arguments directly in Java (but it should not hard to do it) so I decoded the ABI syntax into byte[].
First of all copy the StandaloneBlockchain.java and add one method (sorry, I can't find an easy way to do it via extension):
Then in the StandaloneBlockchainSample.java, refer the modified class, modify the sample contract to have 2 parameters in the constructor (I didn't have time to check all types, but it should work without problems) and assign the parameters init values:
After that, check the correct value of the parameters with:
Note that you need to modify the private assertEqual method inside the class to assert other types aside from the already defined BigInteger. I tested this solution with EthereumJ 1.2.0: maybe next releases will include a better way to pass init values to the constructor, so update the answer if you will be aware of that.