3

I have a private key and address . Is it possible to make transaction with these parameter using web3j.

3 Answers 3

3
Web3j web3j = Web3j.build(new HttpService(""));
    Credentials credentials = Credentials.create("privateKey");
    BigInteger nonce = web3j.ethGetTransactionCount(credentials.getAddress(), DefaultBlockParameterName.PENDING).sendAsync().get().getTransactionCount();
    BigInteger gasPrice = web3j.ethGasPrice().sendAsync().get().getGasPrice();
    RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, Constants.ETH_GAS_LIMIT, "address", Convert.toWei("amount", Convert.Unit.ETHER).toBigIntegerExact());
    EthSendTransaction response = web3j.ethSendRawTransaction(Numeric.toHexString(TransactionEncoder.signMessage(rawTransaction, credentials))).send();
    String hash = response.getTransactionHash();
2
  • just use this method and put your parameters on the place of the strings.
    – Sanjay Rao
    Commented Oct 22, 2020 at 23:45
  • Web3j web3j = Web3j.build(new HttpService("")); TransactionReceipt receipt = Transfer.sendFunds(web3j, Credentials.create("privateKey"), "toAddress", new BigDecimal("amount"), Convert.Unit.ETHER).sendAsync().get(); System.err.println(receipt.getTransactionHash());
    – Sanjay Rao
    Commented Apr 1, 2021 at 15:36
1

If you want to send custom tokens as the contract owner, check out my answer here: Send Custom ERC20 Tokens from one Address to another

If you want to send tokens on behalf of another user, the idea is similiar.

First retrieve user's credentials from the privatekey , then do transaction.

Credentials getUserInfo (String privateKeyInHex){

    BigInteger privateKeyInBT = new BigInteger(privateKeyInHex, 16);

    ECKeyPair aPair = ECKeyPair.create(privateKeyInBT);
    Credentials aCredential = Credentials.create(aPair);

    return aCredential;
}

send tokens:

process(){
    ... 

    Credentials newUser =  getUserInfo(privateKeyInHex);

    TransactionReceiptProcessor transactionReceiptProcessor = new NoOpProcessor(web3);
    TransactionManager transactionManager = new RawTransactionManager(
                            web3, newUser, ChainId.MAINNET, transactionReceiptProcessor);
                    //if testing, use ChainId.ROPSTEN
    ...
0

Yes it's possible to send transaction. Use web3.eth.sendTransaction({to:'0xaddress', from:'0xaddress', value:web3.toWei(.033,'ether'),gas:30000})

3
  • Question is about web3j not web3js
    – Ajit Soman
    Commented Feb 28, 2018 at 8:21
  • are you using Ethereum wallet? Commented Feb 28, 2018 at 8:26
  • I have no wallet file. I have only ethereum private key and address
    – Ajit Soman
    Commented Feb 28, 2018 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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