I read from many questions (example: How to estimate the cost to call a Smart Contract method?) that web3.eth has a method estimateGas that can provide an estimate of the gas usage of a transaction/function call.
I am trying to get an estimate for calling a function using my Java wrapper class generated by the web3j CLI.
Web3j web3 = Web3j.build(new HttpService(endpoint));
Credentials creds = Credentials.create(privateKey);
DefaultGasProvider gasProvider = new DefaultGasProvider();
MyNFT contract = MyNFT.load(contractAddress, web3, creds, gasProvider);
// I am interested in estimating how much gas the following call will use
TransactionReceipt receipt = contract.mintNFT(recipient, tokenUri).send();
How should I go about doing this in Java? Is there a Java equivalent for estimateGas?
estimateGascalculates how much gas a function call requires. You are calculating the cost, which is another thing. – Ismael Nov 03 '22 at 06:25web3.eth.estimateGas. It links to an existing answer that usesweb3.eth.estimateGasto determine the cost of calling a contract function. – Ismael Nov 03 '22 at 18:17web3.eth.estimateGas, which simulates the function call and it returns the exact gas limit required. – Ismael Nov 04 '22 at 14:03