1

I use web3 php to connnect to BSC and it is work, unless one things that is to send BEP20 token to others wallet. I created some function like Get Balance of BNB, get Balance of BEP20 also transfer BNB to others wallet all is working.

but this is not working

public function CreateAndSendTransBEP20($transactionParams) {
        $contractAddress = $transactionParams['contract'];
        $contract = self::EthContract($contractAddress);
        
        $amount = isset($transactionParams['amount']) ? $transactionParams['amount'] : 0;
        $address = isset($transactionParams['address']) ? $transactionParams['address'] : null;
        $addressprivate = isset($transactionParams['addressprivate']) ? $transactionParams['addressprivate'] : null;
        $toaddress = isset($transactionParams['toaddress']) ? $transactionParams['toaddress'] : null;
        $gas_limit = isset($transactionParams['gas_limit']) ? $transactionParams['gas_limit'] : 21000;
        $gas_price = isset($transactionParams['gas_price']) ? $transactionParams['gas_price'] : 5;
        
// $contract->estimateGas('transfer', $toaddress, $amount, [
//     'from' => $address,
// ], function ($err, $result) use (&$gas_limit) {
//     if ($err !== null) {
//         throw $err;
//     }
//     $gas_limit = $result->toString();
// });

        $web3ChainId = isset($transactionParams['web3ChainId']) ? $transactionParams['web3ChainId'] : "0x0";
        $data = $contract->getData('transfer', $toaddress, $amount);

        $wallet_balance = 0;
        $wallet_balance = $this->GetBalanceBEP20($address,$contractAddress);
        $payout_tx = 'failed';
        
        $nonce = 0;
        $this->Eth()->getTransactionCount($address, function ($err, $result) use (&$nonce) {
            $nonce = gmp_intval($result->value);
        });
        $amount_wei = eth_to_wei($amount);
        $gas_wei = gwei_to_wei($gas_price);
        $gas_limit = $gas_limit;
        $transaction_fee = bcmul($gas_wei, $gas_limit);
        $value_wei = bcsub($amount_wei, $transaction_fee);
        
        $transaction = [
            'nonce' => '0x' . dechex($nonce),
            'from' => strtolower($address),
            'to' => strtolower($contractAddress),
            'gas' => '0x' . bcdechex($gas_limit),
            'gasLimit' => "0x2fefd8",
            'gasPrice' => '0x' . bcdechex($gas_wei),
            'value' => '0x' . bcdechex($value_wei),
            'chainId' => strval($web3ChainId),
            'data' => '0x' . $data
        ];
        $transaction = new Transaction($transaction);
        $signedTx = $transaction->sign($addressprivate);
        
        $this->Eth()->sendRawTransaction('0x' . $signedTx, function ($err, $tx) use (&$payout_tx) {
            if ($err !== null) {
                echo 'Error: ' . $err->getMessage();
                $payout_tx = 'failed';
            } else {
                $payout_tx = $tx;
            }
        });
        return $payout_tx;
    }

This always return "Error: transaction underpriced". I already try to change NONCE +1 or +100 and the result is same

Also already to increase the gas limit and price but both will return "Error: insufficient funds for gas * price + value"

any help would be good, I'm using https://github.com/web3p/web3.php

1 Answer 1

1

The error message shows that gas price or gas limit must be greater.

You should increase these parameters so that it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.