Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to send BEP20 token #362

Open
ExchangeAssets opened this issue Oct 26, 2024 · 0 comments
Open

Unable to send BEP20 token #362

ExchangeAssets opened this issue Oct 26, 2024 · 0 comments

Comments

@ExchangeAssets
Copy link

ExchangeAssets commented Oct 26, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant