0

I'm trying to provide a customised gas price for faster transaction ,how can achieve that ?

Web3j web3 = Web3j.build(new HttpService("http://"));
    ERC20 javaToken = ERC20.load(contractAddress, web3, creds, new DefaultGasProvider());
    TransactionReceipt receipt = javaToken.transfer("0x6D594E3C913059e3AB2b3CEAc37b3050c689Da10", value).send();
  • That depends on a lot of things: Are you public network ? Is it POW or POA ? If not on public network then what are the miners' configuration ? – Florian Castelain Feb 11 at 7:50
  • I'm asking for java's Erc20 class functionality to provide gas fees for transaction., – Aniket Feb 11 at 8:41
  • @FlorianCastelain its on test network of Ganache ,I'm exploring this library for educational purpose. – Aniket Feb 11 at 8:42
1

you need a new class that implements the StaticGasProvider (the value of gaslimit is just an example, you don't usually need that much for a transfer transaction)

import org.web3j.tx.Contract;
import org.web3j.tx.gas.StaticGasProvider;

import java.math.BigInteger;

public class GasProvider extends StaticGasProvider {
    public static final BigInteger GAS_LIMIT = BigInteger.valueOf(3_500_000); 
    public GasProvider(BigInteger gas_price) {
        super(gas_price, GAS_LIMIT);
    }
}

And then

ERC20 javaToken = ERC20.load(contractAddress, web3, creds, new GasProvider(<your-gas-price-in-wei-as-biginteger>));
| improve this answer | |
  • How does this decrease "transaction time" ? – Florian Castelain Feb 11 at 10:47
  • you cannot decrease it, you can only give the miner more money so they pick it first and mine it before other transaction in their waiting-list – Majd TL Feb 11 at 11:06
  • Thanks @MajdTL ,but if i want to provide gas price through an api call or pass it as an parameter then ? any suggetion? – Aniket Feb 11 at 11:58
  • I edit the answer for you, it is pure java and has nothing to do with ethereum and web3j – Majd TL Feb 11 at 12:48
  • thanks alot @MajdTL – Aniket Feb 12 at 5:02

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.