【问题标题】:How to get ERC20 token name & symbol by contract address with PHP geth如何使用 PHP geth 通过合约地址获取 ERC20 代币名称和符号
【发布时间】:2023-04-09 21:23:01
【问题描述】:

我有以下情况:

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

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

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

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

我用“eth_call”试试运气,因为我看到有人在 stackoverflow 帖子中提到这一点。但不幸的是,我被语法困住了,我不确定这是否是正确的方法。

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

错误:

函数 dappstatus\Geth\JsonRpc::eth_call() 的参数太少,1 在 /var/www/html/app/Http/Controllers/HomeController.php 上传递 第 2321 行,正好是 2 行

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

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

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

无效参数 0:json:无法将字符串解组为 Go 值 输入 ethapi.CallArgs

如果我使用 eth_call 的方式错误,也许有人会为我的语法错误和 B 获得 ERC20 名称和符号的解决方案。

【问题讨论】:

    标签: php ethereum smartcontracts geth erc20


    【解决方案1】:

    您可以简单地使用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");
    

    【讨论】:

    • 我自己有一个geth全节点。所以额外使用 infura 是没有意义的。
    • @DappFuture 没问题,你可以用你的geth节点代替infura,例如$token = new \Lessmore92\Ethereum\Token("0xf629cbd94d3791c9250152bd8dfbdf380e2a3b9c", "http://127.0.0.1:8545");
    猜你喜欢
    • 1970-01-01
    • 2021-09-23
    • 2022-01-01
    • 2021-10-21
    • 2018-01-03
    • 2018-08-01
    • 2022-08-13
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多