phpethereumsmartcontractsgetherc20

How to get ERC20 token name & symbol by contract address with PHP geth


I have the following situation:

Transaction: https://etherscan.io/tx/0xc7ee5bf1ea144b4e9e7dad32b574990c5e1b832226a626973929246577954fdf

I'm able to get the contract address in this transaction "0xf629cbd94d3791c9250152bd8dfbdf380e2a3b9c" with the following code:

$transaction_receipt = $geth->eth_getTransactionReceipt($transaction['hash']);
$erc20_address = $transaction_receipt['logs'][0]['topics'];

Now i need to get the name and the symbol of this ERC20 token through the contract address.

I tried my luck with "eth_call", since i saw someone mentioning this in a stackoverflow post. But unfortuanatly i'm stuck here with the syntax, and i'm not sure if this is the right way to go.

$geth->eth_call($transaction_receipt['logs'][0]['address'])

Error:

Too few arguments to function dappstatus\Geth\JsonRpc::eth_call(), 1 passed in /var/www/html/app/Http/Controllers/HomeController.php on line 2321 and exactly 2 expected

I tried with a second parameter after reading the API wiki https://eth.wiki/json-rpc/API

   $geth->eth_call($transaction_receipt['logs'][0]['address'],"'id':1")

Now i get this error (wrong syntaxt)

invalid argument 0: json: cannot unmarshal string into Go value of type ethapi.CallArgs

Maybe someone got a solution A for my syntax error and B to get the ERC20 name & symbol if my way of using eth_call is wrong.


Solution

  • You can simply use php-erc20 library.

    <?php
    
    require_once "vendor/autoload.php";
    
    $token = new \Lessmore92\Ethereum\Token("0xf629cbd94d3791c9250152bd8dfbdf380e2a3b9c", "https://mainnet.infura.io/v3/API_KEY");
    var_dump($token->name());
    var_dump($token->symbol());
    

    Output:

    string(10) "Enjin Coin"
    string(3) "ENJ"
    

    Upadate:
    If you are using your Geth node, you can use it as JSON-RPC server:

    $token = new \Lessmore92\Ethereum\Token(CONTRACT_ADDRESS, "http://127.0.0.1:8545");