如何使用 PHP geth 通过合约地址获取 ERC20 代币名称和符号

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

我有以下情况:

交易:https://etherscan.io/tx/0xc7ee5bf1ea144b4e9e7dad32b574990c5e1b832226a626973929246577954fdf

我可以使用以下代码获取此交易“0xf629cbd94d3791c9250152bd8dfbdf380e2a3b9c”中的合约地址:

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

现在需要通过合约地址获取这个ERC20代币的名称和符号

我用“eth_call”试试运气,因为我看到有人在 Whosebug 中提到这个 post。但不幸的是,我被语法困在这里,我不确定这是否是正确的方法。

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

错误:

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

我在阅读 API wiki 后尝试使用第二个参数 https://eth.wiki/json-rpc/API

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

现在我收到这个错误(语法错误)

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

如果我使用 eth_call 的方式有误,也许有人为我的语法错误提供了解决方案 A,并为获取 ERC20 名称和符号提供了 B。

您可以简单地使用 php-erc20 库。

<?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());

输出:

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

更新:
如果您使用的是 Geth 节点,则可以将其用作 JSON-RPC 服务器:

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