Hot answers tagged

12

Below is my approach, verified by importing the result privatekey into MetaMask and get the same address as expected. private static JSONObject process(String seed){ JSONObject processJson = new JSONObject(); try { ECKeyPair ecKeyPair = Keys.createEcKeyPair(); BigInteger privateKeyInDec = ecKeyPair.getPrivateKey()...


11

Recently, there are so many people confused about miner.start(1) returning null, but it's actually not a problem, just a reasonable return value misunderstanding. If you aren't sure that your node is mining, please execute eth.hashrate to check, if the return value is greater than zero, it is mining, otherwise, it is not. Now let's analyze why miner.start(...


9

The "ethGetBalance" method will return the balance of any given account. Pls see example below: // connect to node Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/ // send asynchronous requests to get balance EthGetBalance ethGetBalance = web3 .ethGetBalance("0xAccountAddress", DefaultBlockParameterName.LATEST) ....


9

Here is my suggestions: ERC20 token balance Just to clarify, all ERC20 tokens have the same set of methods (ERC20 protocol). Now, there are two ways to accomplish your task: use ERC20 balanceOf(address _owner) method // Get the account balance of another account with address _owner function balanceOf(address _owner) public constant returns (uint256 balance)...


7

I know this question is old, but providing an answer here as I came across this issue and couldn't find a solution elsewhere... This appears to be a bug in Web3j that occurs when you pass in the contract address as part of your filter. EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract....


7

SOLVED!!! LONG/Detailed METHOD!!! (Shorter Version at bottom) The main problem was Bytes32(byte[]) only supports 32 length byte[]. Also keep in mind Numeric.hexStringToByteArray(strValueInHex) converts any HexString to byte[]. This is the process: String => Hex => 32 length Hex (ie. 64 length HexString) => byte[] => Bytes32 Note: "00" = 1 Hex ...


7

@Luiz Soares Thank you for giving me the clue of the value being in WEI but solution provided didn't worked me.After some digging the documentation again got the solution Convert.fromWei(ethGetBalance.getBalance().toString(), Unit.ETHER);


7

You can send and confirm transaction without waiting using CompletableFuture and callback function. rc-> Instance that calls contract function through remote call. CompletableFuture<TransactionReceipt> transactionReceiptCompletableFuture = rc.sendAsync(); transactionReceiptCompletableFuture.thenAccept(transactionReceipt -> { // then accept ...


6

try setting miner.setEtherbase(eth.coinbase) or step by step: start Mist browser (let the node synchronize completely) open console and type: geth attach miner.setEtherbase(eth.accounts[0]) miner.start() Even if it returns null you can see in the buttom left corner of Mist broser, that mining has startet. It takes a while till the hashrate incrases. You ...


6

This is the easiest way Web3j web3j = Web3j.build(new HttpService("https://rinkeby.infura.io/v3/.......")); Block block = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send().getBlock(); System.out.println(block.getNumber().toString());


6

Under the hood, the Web3J SmartContract Java Wrapper calculates the nonce by getting the number of transactions getTransactionCount for the account (credential). EthGetTransactionCount ethGetTransactionCount = web3.ethGetTransactionCount(credentials.getAddress(), DefaultBlockParameterName.PENDING).send(); BigInteger nonce = ethGetTransactionCount....


6

If you are using the smart contact wrapper then you usually need to give one of the available transaction managers as a parameter and then give the chain Id when you initialize it Web3j web3 = Web3j.build(new HttpService("https://bsc-dataseed.binance.org/")); long chainId = 56; FastRawTransactionManager txMananger = new FastRawTransactionManager(...


5

Love that your using Web3J, I think with a little practice you will find that it's a great API for dealing with Ethereum :-) So regarding your question there are two things: I think your problem is the third line in your code. The list "outputParameters" should contain all return variables of the method. So if you are expecting a String you should do ...


5

The problem was actually double hashing. Looking at the Web3j's signMessage() method Sign.SignatureData sig =Sign.signMessage(messageBytes, ecKeyPair); and signedMessageToKey() method String pubKey = Sign.signedMessageToKey(messageBytes, sig).toString(16); These methods internally hash(sha3) the input messageBytes before signing and on verification. This ...


5

I found the solution, please refer, #296 Use FastRawTransactionManager to speed up your transactions. Use RawTransactionManager to shorten the polling interval. If you need both, use the following code, pollingInterval = 3000; // 3 seconds FastRawTransactionManager fastRawTxMgr = new FastRawTransactionManager(web3, credentials, new ...


5

It is the BigNumber representation of the wallet balance that is output by your script. Indeed, BigNumbers need to be transformed through toString(int) or toNumber() in order to become compatible with javascript int or string. You can read more about this here and here. To understand the object output, I found on BigNumbers's git that: The value of a ...


5

In solidity 0.4.22 the require and revert reason were added. As can be seen here, they are abi-encoded as if it were a call to a function "Error(string)". You'll need to make an eth_call to your contract. This blog post gives an example: an eth_call to a function function myFunction(uint256 input) public view returns (uint256) { require(input >= 5, ...


5

If you are using web3 v0.x then you can send transaction by following: // creation of contract object var MyContract = web3.eth.contract(abi); // initiate contract for an address var myContractInstance = MyContract.at(contractAddress); // send a transaction to a function myContractInstance.myStateChangingMethod('someParam1', {value: xxx, gas: xxxx}); If ...


4

To generate the Credentials if you have a plain public key and private key, you need to convert them in to the hex representation and then pass them in to the constructor for Credentials String hexPrivateKey = String.format("%040x", new BigInteger(1, privateKey.getBytes())); String hexPublicKey = String.format("%040x", new BigInteger(1, publicKey.getBytes())...


4

Remix provide you with three environments to run your smart contract code. As well explained in the docs, JavaScript VM: All the transactions will be executed in a sandbox blockchain in the browser. This means nothing will be persisted and a page reload will restart a new blockchain from scratch, the old one will not be saved. Injected Provider: ...


4

Yes, you can do it using following code import org.web3j.crypto.Credentials; import org.web3j.crypto.ECKeyPair; public static String getPublicKeyInHex(String privateKeyInHex) { BigInteger privateKeyInBT = new BigInteger(privateKeyInHex, 16); ECKeyPair aPair = ECKeyPair.create(privateKeyInBT); BigInteger publicKeyInBT = aPair.getPublicKey(); ...


4

This is my implementation of getting the latest block number. I set up the project using https://docs.web3j.io/getting_started/#gradle import org.web3j.protocol.Web3j; import org.web3j.protocol.core.methods.response.EthBlockNumber; import org.web3j.protocol.http.HttpService; import java.util.concurrent.ExecutionException; public class App { public ...


4

Opcode 0x1c is SHR or right shift, which was introduced in the recent update (more here). It was not available prior to February, 2019. It is likely that your Java tool (web3j?) is out dated and does not recognize this opcode. You may be able to upgrade to a newer version which has Constantinople/St. Petersburg support.


4

eth_newFilter is used to receive and filter Ethereum logs. Logs are produced when a transaction is included in a block, hits a smart-contract and generates a log, which is not what you want. You may want to look at web3js pending transaction subscriptions: https://web3js.readthedocs.io/en/1.0/web3-eth-subscribe.html#subscribe-pendingtransactions const ...


4

Let's take for example the following simple storage contract: pragma solidity ^0.5.6; contract SimpleStorage { uint storedData; function set(uint x) public { storedData = x; } function get() public view returns (uint) { return storedData; } } You can send a transaction to a (already deployed) smart contract with the ...


3

The result variable in console.log(result) is an object which contains these fields (see the docs): Object - An event object as follows: address: String, 32 Bytes - address from which this log originated. args: Object - The arguments coming from the event. blockHash: String, 32 Bytes - hash of the block where this log was in. null when its pending. ...


3

I figured out what was wrong. My GAS_PRICE and GAS_LIMIT values were not proper. I later figured out that Web3j has default GAS_PRICE and GAS_LIMIT, see updated code below BigInteger GAS = Contract.GAS_LIMIT; BigInteger GAS_PRICE = Contract.GAS_PRICE; Contract.GAS_LIMIT and Contract.GAS_PRICE are deprecated. Hence, use the below constants ...


3

You can use CURL command or use request module inside your code to call create account API - CURL command for your reference: curl -X POST --data '{"jsonrpc":"2.0","method":"personal_newAccount","params":["pass"],"id":74}' http://localhost:8545 Also, you must run the geth node with the "personal" api enabled to make this method available: geth --rpc --...


3

The null is expected. If you are mining you will see lots of messages in your console like this: INFO [07-25|18:46:25] Successfully sealed new block number=72 hash=d0316e…36554e INFO [07-25|18:46:25] 🔗 block reached canonical chain number=67 hash=a89be4…5a73be INFO [07-25|18:46:25] 🔨 mined potential block number=72 ...


3

I am not sure about how did you configure your Blockchain on Azure, i assume that you have created a mining node(s) while you are creating your private blockchain, if so, we need to connect to that node with SSH,, to do so, lookup for the mining node address,, you can find it in : Deployments > Microsoft-azure-blockchain.azure-multi.... Now, copy the ...


Only top voted, non community-wiki answers of a minimum length are eligible