Skip to content

Unable to send BEP20 token #362

@ExchangeAssets

Description

@ExchangeAssets

I want to send BEP20 token but I get an error, here is my code:

function web3_send_token($from, $to, $amount, $contract_address, $private) {
    $web3 = new Web3('https://bsc-dataseed1.binance.org:443');
	
	$web3->eth->gasPrice(function ($error, $gasPrice) use (&$result) {
        if($error) {
            $result = [
                'error' => 'Ошибка получения gasPrice: ' . $error->getMessage()
            ];
            return;
        }
        $result['gasPrice'] = $gasPrice;
    });
	
	$web3->eth->getTransactionCount($from, 'pending', function ($error, $nonce) use (&$result) {
        if($error) {
            $result = [
                'error' => 'Ошибка получения nonce: ' . $error->getMessage()
            ];
            return;
        }
        $result['nonce'] = $nonce;
    });
	$abi = '[{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]';	
    
	$contract = new Contract($web3->provider, $abi);
	$amount = bcmul($amount, bcpow('10', 18), 0);
	
	$contract->at($contract_address)->estimateGas('transfer', $to, $amount, ['from' => $from], function ($error, $gas) use (&$result) {
		if($error) {
			$result = [
				'error' => 'Ошибка оценки газа: ' . $error->getMessage()
			];
			return;
		}
		$result['gas'] = $gas;
	});
	
	$data = $contract->at($contract_address)->getData('transfer', $to, $amount);		

	$transaction = new Transaction([
		'to' => $contract_address,
		'value' => '0x0',
		'gasPrice' => '0x' . dec_hex($result['gasPrice']->toString()),
		'gas' => '0x' . dec_hex($result['gas']->toString()),
		'nonce' => '0x' . dec_hex($result['nonce']->toString()),
		'data' => $data,
		'chainId' => 56
	]);

	$signedTransaction = '0x' . $transaction->sign($private);

	$web3->eth->sendRawTransaction($signedTransaction, function ($error, $txid) use (&$result) {
		if($error) {
			$result = [
				'error' => 'Ошибка отправки транзакции: ' . $error->getMessage()
			];
			return;
		}

		$result = [
			'txid' => $txid
		];
	});
	
    return $result;
}

Result of execution:

Status: Fail
https://bscscan.com/tx/0x4c127113b9cc15a5e6eec4f8522b758813474e51c0ec2746d199d71a6f8ae96e

What's wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions