I am trying to get the balance of any ERC20 token of my wallet via a PHP code like:
public function getErc20TokenBalance(string $tokenAddress, callable $callback): void
{
// ERC20 ABI definition (balanceOf function)
$abi = '[{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"type":"function"}]';
// Encode the function call
$data = '0x70a08231000000000000000000000000' . substr($this->fromAddress, 2); // Method ID + address without '0x'
// Convert tokenAddress to lowercase and ensure it has '0x' prefix
$tokenAddress = strtolower($tokenAddress);
if (substr($tokenAddress, 0, 2) !== '0x') {
$tokenAddress = '0x' . $tokenAddress;
}
// Setup transaction
$transaction = new EthereumTx([
'to' => $tokenAddress, // Ensure $tokenAddress is passed as string
'data' => $data
]);
// Call contract using web3 instance with callback function
$this->web3->eth->call($transaction, function ($err, $result) use ($callback) {
if ($err !== null) {
throw new Exception('Error getting balance: ' . $err->getMessage());
}
// Parse result
$balance = new BigNumber($result->toString());
// Call the provided callback function with the balance
$callback($balance->getValue());
});
}
and I always get error: Wrong type of eth_call method argument 0.
on line $this->web3->eth->call($transaction, function ($err, $result) use ($callback) {
I am using the web3 library (https://github.com/web3p). I assume maybe the transaction object is wrong, but not sure why.