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

Is it possible to send transaction in Binance Smart Chain using web3.php and web3p/ethereum-tx? #57

Open
VIKASKO opened this issue Sep 8, 2023 · 0 comments

Comments

@VIKASKO
Copy link

VIKASKO commented Sep 8, 2023

I am trying to send BEP-20 tokens from one address to another. I am getting the following error while attempting to sign the transaction with Privatekey. "Uncaught InvalidArgumentException: The input type didn't support" at this line of code $signedTransaction = $ethereumTx->sign($privateKey);

Here is the full code :

`
require 'web3.php/vendor/autoload.php';
require 'ethereum_tx/autoload.php';

use Web3\Web3;
use Web3\Contract;
use Web3\Utils;
use Web3p\EthereumTx\Transaction;

$web3 = new Web3('https://bsc-dataseed.binance.org/'); // Use the BSC node URL

// Replace with the actual contract address and ABI
$contractAddress = '0x55d398326f99059fF775485246999027B3197955';
$recipientAddress='0x7Affe1A120843E41b271E577900F0B5c43c2C2d9';

$tokenDecimals = 18; // Replace with the actual token's decimal places
$amount = 1; // Replace with the actual amount you want to transfer

$amountInBaseUnits = $amount * (10 ** $tokenDecimals);

$contractABI = "contract ABI";

// Set your wallet's private key
$privateKey = "myprivatekey";

$eth = $web3->eth;

// Create an instance of the contract using the ABI and address
$contract = new Contract($web3->provider, $contractABI, $contractAddress);

$transferFunctionCall = $contract->at($contractAddress)->getData(
'transfer', // Function name
$recipientAddress, // Parameter 1
$amountInBaseUnits // Parameter 2
);

// Fetch the nonce asynchronously
$web3->eth->getTransactionCount('0x02e6deD21C6c46CC4FFF2991988af13069C7c2A3', function ($err, $nonce) use ($web3, $contractAddress, $transferFunctionCall, $privateKey) {
if ($err !== null) {
echo "Error fetching nonce: " . $err->getMessage();
} else {
// Construct the transaction
$transaction = [
'from' => '0x02e6deD21C6c46CC4FFF2991988af13069C7c2A3',
'to' => $contractAddress,
'gas' => '0x' . dechex(100000), // Replace with appropriate gas value
'data' => $transferFunctionCall,
'nonce' => $nonce
];

	$ethereumTx = new Transaction($transaction);

$signedTransaction = $ethereumTx->sign($privateKey);

    // Send the transaction with a callback function
    $web3->eth->sendRawTransaction('0x' . $signedTransaction->getHex(), function ($err, $txHash) {
        if ($err !== null) {
            echo "Error sending transaction: " . $err->getMessage();
        } else {
            echo "Transaction Hash: " . $txHash;
        }
    });
}

});

var_dump($transactionHash);
`

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