I have a private key and address . Is it possible to make transaction with these parameter using web3j.
3 Answers
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();
-
just use this method and put your parameters on the place of the strings. 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()); Commented Apr 1, 2021 at 15:36
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
...
Yes it's possible to send transaction. Use web3.eth.sendTransaction({to:'0xaddress', from:'0xaddress', value:web3.toWei(.033,'ether'),gas:30000})
-
-
-
I have no wallet file. I have only ethereum private key and address Commented Feb 28, 2018 at 8:29