2

I am using web3j java library to deploy a smart contract.

// I am encoding the constructor parameters here -- 2 & 3, 10 & 20
String encodedConstructor =
         FunctionEncoder.encodeConstructor(Arrays.asList(new Type(value), ...));

RawTransaction rawTransaction = RawTransaction.createContractTransaction(
    <nonce>,
    <gasPrice>,
    <gasLimit>,
    <value>,
    "0x <compiled smart contract code>" + encodedConstructor);

byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, <credentials>);

String hexValue = Numeric.toHexString(signedMessage);

I am using

web3.eth.sendRawTransaction(hexValue) 

to deploy the contract.

My smart contract has a constructor which accepts some parameters(a, b) and it has a function sum which returns sum of a and b. After deployment if I call the sum function, it always returns me 0.

Need help.

Reference Link: https://docs.web3j.io/transactions.html#creation-of-a-smart-contract

  • but the contract is deployed well or not? – Gawey Jun 1 '17 at 12:52
  • Contract got deployed. I got a pending transaction in txpool and after it was mined ,contract address was visible in the transaction receipt. – Karthik Manivasagam Jun 1 '17 at 13:17
  • So the problem is the code of the smart contract not the deployment of the contract no? you have the code? – Gawey Jun 1 '17 at 13:20
  • This is my contract bytecode, without encoded constructor parameters 6060604052341561000c57fe5b6040516040806100ea833981016040528080519060200190919080519060200190919050505b81600081905550806001819055505b50505b6098806100526000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063853255cc14603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b60006001546000540190505b905600a165627a7a7230582045d4154b165a91bbd45e384793e99fa7799715f3adb510b137f7451a94623fd90029 – Karthik Manivasagam Jun 1 '17 at 13:59
1

I have to pass the required parameters in Web3J datatypes, link on github.

A simple example is given in the a test file, FunctionEncoderTest.java.

 ArrayList<Type> dataParams = new ArrayList<>();    
 dataParams.add(new Utf8String("Name"));
 dataParams.add(new Address("Ethereum Address"));

 //This will be used for creating "RawTransaction" object
 Function function = new Function("updateDetails", dataParams, Collections.<TypeReference<?>>emptyList());
  • How to get deployed contract address? – Subhod I Mar 21 '18 at 12:19
0

you need to create a Function Object for your smart contract constructor then in data field were you put compiled solidity add the decode result of this function 'compiled smart contract'+'decoded function of the constructor'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.