web3p / web3.php

A php interface for interacting with the Ethereum blockchain and ecosystem. Native ABI parsing and smart contract interactions.
MIT License
1.15k stars 540 forks source link

Failed to Transfer Token Between Wallet #321

Closed idevlinkcom closed 1 year ago

idevlinkcom commented 1 year ago

I need help to send token BSC (BEP20) to others wallet, I've successfully implement send BSC but not the token BEP20

I already created this

public function CreateAndSendTransBEP20($transactionParams) {
        $contractAddress = $transactionParams['contract'];
        $contract = self::EthContract($contractAddress);

        $address = isset($transactionParams['address']) ? $transactionParams['address'] : null;
        $addressprivate = isset($transactionParams['addressprivate']) ? $transactionParams['addressprivate'] : null;
        $toaddress = isset($transactionParams['toaddress']) ? $transactionParams['toaddress'] : null;
        $amount = isset($transactionParams['amount']) ? $transactionParams['amount'] : 0;
        $gas_limit = isset($transactionParams['gas_limit']) ? $transactionParams['gas_limit'] : 21001;
        $gas_price = isset($transactionParams['gas_price']) ? $transactionParams['gas_price'] : 5;

        $estimatedGas = "0x200b20";

        $contract->estimateGas('transfer', $toaddress, $amount, [
            'from' => $address,
        ], function ($err, $result) use (&$gas_limit) {
            if ($err !== null) {
                throw $err;
            }
            $gas_limit = $result->toString();
        });
        echo '<br>gas_limit:' . $gas_limit . '<br>';

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

        $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*2;
        $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' => '0x' . bcdechex($gas_limit),
            '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;
    }

And then i call the function with this code

$transactionparams = array(
          'contract'=>$contractaddress,
          'address' => $address,
          'addressprivate' => $addressprivate,
          'toaddress' => $toaddress,
          'gas_limit' => 21000, // gas price can be set or null
          'amount' => 10,
          'web3ChainId' => 97 // 56 bsc mainnet, 97 bsc testnet
        );
        $gettransactiontxbep20 = $bapi->CreateAndSendTransBEP20($transactionparams);

      echo "<pre>";
      print_r($gettransactiontxbep20);
      echo "</pre>";

What I got is an error showing

gas_limit:36043
Error: transaction underpriced
failed

Already try to double the gas price also give another +1 to nonce and the result is "Error: insufficient funds for gas * price + value"

Any one can help me with this error?

idevlinkcom commented 1 year ago

FOR an update if the value set to 0 the transaction will success, this is the example success using 0 value

$transactionparams = array(
         'contract'=>$contractaddress,
         'address' => $address,
         'addressprivate' => $addressprivate,
         'toaddress' => $toaddress,
         'gas_limit' => 21103, // gas price can be set or null
         'amount' => 0,
         'web3ChainId' => 97 // 56 bsc mainnet, 97 bsc testnet
       );
       $gettransactiontxbep20 = $bapi->CreateAndSendTransBEP20($transactionparams);

     echo "<pre>";
     print_r($gettransactiontxbep20);
     echo "</pre>";

https://testnet.bscscan.com/tx/0xc292e30c8a292e1aca1cca575c9f72856b121294346e4c4fe13388df67fdc8c1

idevlinkcom commented 1 year ago

here is my address sender 0x5454c66Eaa964D13B7d858D79E85c877Ccbf33Ab which contains 20 USDT Token

also trying this but no luck https://github.com/web3p/web3.php/issues/251#issuecomment-1063884215

numairawan commented 1 year ago

@idevlinkcom how you fixed this? getting the same error