hyperledger-web3j / web3j

Lightweight Java and Android library for integration with Ethereum clients
https://www.web3labs.com/web3j-sdk
Other
5.12k stars 1.69k forks source link

Estimating gas fee of an ERC20 token transfer returns "gas required exceeds allowance or always failing transaction" #1095

Closed chess-equality closed 3 years ago

chess-equality commented 5 years ago

Bug title

As with the title, I'm getting the error "gas required exceeds allowance or always failing transaction".

Steps To Reproduce

I'm trying to use Transaction.createContractTransaction along with web3j.ethEstimateGas to estimate the gas fee (I'm using Kotlin by the way):

val function = Function(MyToken.FUNC_TRANSFER, listOf(Address(destination), Uint256(amount)), emptyList())
val txData: String = FunctionEncoder.encode(function)

logger.info(">>>>>>>>>> txData = $txData")

val ethGasPrice: EthGasPrice = web3j.ethGasPrice().sendAsync().get()
val transaction: Transaction = Transaction.createContractTransaction(
        credentials.address,
        nonce,
        ethGasPrice.gasPrice,
        DefaultGasProvider.GAS_LIMIT,
        amount,
        txData)

val ethEstimateGas = web3j.ethEstimateGas(transaction).sendAsync().get()

Expected behavior

I should get the estimated gas fee.

Environment

Describe the environment in which the issue occurs

rach-id commented 4 years ago

Hello, Instead of using the createContractTransaction, use the createFunctionCallTransaction. The code should look something like that

    val transaction = Transaction.createFunctionCallTransaction(credentials.address,
        BigInteger.ONE,  ethGasPrice.gasPrice, DefaultGasProvider.GAS_LIMIT, contractAddress, BigInteger.valueOf(10000000000), txData)
AlexandrouR commented 4 years ago

@chess-equality is this still an issue?