0

i'm trying to do a simple transaction for a token but i keep getting following error for estimate gas limit : "Request have return error: gas required exceeds allowance or always failing transaction" here is the code for estimate gas limit:

 $eth_balance = $this->ethBallance($from);
    $erc20_balance = $this->tokenBallance($from, $token);

    $rpcUrl = env('ETHEREUM_RPC_URL');
    $jsonRpc = new JsonRPCClient($rpcUrl);
    $utility = new Utility();
    $amountHex = $utility->convertHex($amount, $this::ERC20_DECIMALS[$token]);
    $data = $this::ERC20_TRANSFER_HEX . str_pad(substr($to, 2), 64, "0", STR_PAD_LEFT) . str_pad($amountHex, 64, "0", STR_PAD_LEFT);

    $estimateGas = 0;
    $gasPrice = 0;
   try {
        $gasPrice = $jsonRpc->eth_gasPrice();

        $estimateGas = $jsonRpc->eth_estimateGas(array(
            'from' => $from,
            'to' => env($token . '_CONTRACT_ADDRESS'),
            'value' => '0x0',
            'data' => $data,
            'gasPrice' => '0x'.dechex(hexdec($gasPrice) * 2),
        ));
    } catch (\Exception $e) {
        // $tran->status = TransactionStatus::PENDING;
        // $tran->save();

        return response()->json([
            'success' => false,
            'error' => $e->getMessage()
        ]);
    }

    $hexEstimate = $estimateGas;
    $gasPrice = $jsonRpc->eth_gasPrice();

    $hexGas = $gasPrice;

    $estimateGas = hexdec($estimateGas);
    $gasPrice = hexdec($gasPrice) * $this::ETH_UNIT;

    if ($eth_balance > $estimateGas * $gasPrice * 2 && $erc20_balance->last_balance_token >= $amount) {
        try {
            $result = $jsonRpc->personal_sendTransaction(array(
                'from' => $from,
                'to' => env($token . '_CONTRACT_ADDRESS'),
                'value' => '0x0',
                'gas' => $hexEstimate,
                'gasPrice' => '0x'.dechex(hexdec($hexGas) * 1),
                'data' => $data
            ), $privateKey);
        } catch (\Exception $e) {
            //$tran->status = TransactionStatus::PENDING;
            //$tran->save();

            return response()->json([
                'success' => false,
                'error' => $e->getMessage()
            ]);
        }
    } else {
        $tran->status = TransactionStatus::PENDING;
        $tran->save();

        return response()->json([
            'success' => false,
            'error' => 'Balance is not enough'
        ]);
    }

    dd(respose());

    return response()->json([
        'success' => true,
    ]); 
}

and here is the error that returns:

{"success":false,"error":"Request have return error: gas required exceeds allowance or always failing transaction; \nRequest: {\"jsonrpc\":\"2.0\",\"method\":\"eth_estimateGas\",\"params\":[{\"from\":\"0x19abe73861ec71fcb5d069a361bf5f96801a793e\",\"to\":\"0x0e8d6b471e332f140e7d9dbb99e5e3822f728da6\",\"value\":\"0x0\",\"data\":\"0xa9059cbb00000000000000000000000010D307Bfc6939B8857B93393456fa02E8D07d8c000000000000000000000000000000000000000000000001b1ae4d6e2ef500000\",\"gasPrice\":\"0x77359400\"}],\"id\":3}; "}

and here are the constants :

const ERC20_BALANCEOF_HEX = '0x70a08231';
const ERC20_DECIMALS = [
    'ABYSS' => 18,
    'B2BCoin' => 18,
    'TTENS' => 18,
    'CoinCrowd' => 18
];
const ERC20_TRANSFER_HEX = '0xa9059cbb';
const ETH_DECIMALS = 18;
const ETH_UNIT = 1 / 1000000000000000000;
  • I assume the $to address points to a contract? What's the code for the contract? This error usually means the transaction is going to fail. In this case likely culprits would be: no payable fallback function, or the payable fallback function is reverting. Without seeing the code, it's hard to speculate further. – smarx Mar 16 at 16:14
  • i have updated the code section in the question here is the full code for the function , im using the test run on ABYSS contract – Arian Mar 16 at 16:17
  • What's $this::ERC20_TRANSFER_HEX? What's env($token . '_CONTRACT_ADDRESS')? Etc.? Which of the two transactions is giving you the problem? Preferably, just write a short bit of code that sends one transaction and has hardcoded values in it so people can reproduce the issue. – smarx Mar 16 at 16:26
  • i asked a developer to write this function for me and he set these constants in the function : const ERC20_BALANCEOF_HEX = '0x70a08231'; const ERC20_DECIMALS = [ 'ABYSS' => 18, 'B2BCoin' => 18, 'TTENS' => 18, 'CoinCrowd' => 18 ]; const ERC20_TRANSFER_HEX = '0xa9059cbb'; const ETH_DECIMALS = 18; const ETH_UNIT = 1 / 1000000000000000000; – Arian Mar 16 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.