1

How to pass specific address in send method in Web3J?

For example, using web3 javascript library, we can pass from address like below.

contract.someMethod().send({ from: this.props.accounts[0] })

But in the Java wrapper, there are no parameters defined for send() method.

Is there a way to pass specific address in web3j?

1

If you want to send an unsigned transaction and let the client pick up the account from to use to sign the transaction. You can do the following:

Ether transaction

Transaction transaction = Transaction.createEtherTransaction(from, nonce, gasPrice, gasLimit, to, value);
web3.ethSendTransaction(transaction);

Smart contract transaction

Function function = new Function<>(
        "functionName",  // function we're calling
        Arrays.asList(new Type(value), ...),  // Parameters to pass as Solidity Types
        Arrays.asList(new TypeReference<Type>() {}, ...));

String encodedFunction = FunctionEncoder.encode(function)
Transaction transaction = Transaction.createContractTransaction(from, nonce, gasPrice, encodedFunction);
web3.ethSendTransaction(transaction);

Transaction Receipt:

String txHash = web3.ethSendTransaction(transaction).send().getTransactionHash();

EthGetTransactionReceipt transactionReceipt =
         web3j.ethGetTransactionReceipt(txHash).send();

if (transactionReceipt.getTransactionReceipt.isPresent()) {
    String contractAddress = transactionReceipt.get().getContractAddress();
} else {
    // try again until it's mined
}
  • Thanks, then where to pass this Transaction instance to get transaction receipt? – krupal.agile Jan 5 at 12:01
  • Just edited my answer, that should give you a hint – Greg Jeanmart Jan 5 at 12:05
  • How to generate nonce? – krupal.agile Jan 5 at 12:55
  • There is one last simple question: How to pass nonce? I didn't find the documentation for it. – krupal.agile Jan 5 at 13:13
  • Ok. I found this: EthGetTransactionCount ethGetTransactionCount = serverWeb3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send(); BigInteger nonce = ethGetTransactionCount.getTransactionCount(); – krupal.agile Jan 5 at 13:18
-2

Did you try this ?

contract.someMethod(parameter, {from: address, gas: 3000000},
function(err, res){

    if(!err){
        console.log("address sent ");    
    }
});
  • I am asking about web3j which is a java wrapper over web3 javascript library. – krupal.agile Jan 5 at 11:40

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.