【问题标题】:PHP web3 ERC20 token function callPHP web3 ERC20令牌函数调用
【发布时间】:2022-06-14 19:38:45
【问题描述】:

我有一个代表 ERC20 代币的智能合约。我已经使用 HardHat 在 Binance Testnet 上部署了智能合约。

我也有一个已部署的智能合约地址。

我已经使用以下链接在 PHP laravel 项目中集成了 Web3 库。

https://github.com/web3p/web3.php

我可以调用 web3 函数来获取 TOKEN Symbol。它工作正常。

我想使用智能合约的“转账”功能将我的代币转移到某个钱包地址。

我正在使用以下代码。

$timeout = 30; // set this time accordingly by default it is 1 sec
$web3 = new Web3(new HttpProvider(new HttpRequestManager('https://data-seed-prebsc-1- s1.binance.org:8545', $timeout)));
$ContractMeta = json_decode(file_get_contents(base_path('public/web3/Token.json')));
$contract = new Contract($web3->provider, $ContractMeta->abi);
$toAccount = 'WALLET_ADDRESS_OF_RECEIVER';
$fromAccount = 'PRIVATE_KEY_OF_SENDER';

$contract->at("DEPLOYED_WALLET_ADDRESS")->send('transfer', $toAccount, 18, [
        'from' => $fromAccount,
        'value' => '1000',
        'gas' => '0x200b20',
        'gasPrice' => '20000000000'
    ], function ($err, $result) use ($contract, $fromAccount, $toAccount) {
        if ($err !== null) {
            throw $err;
        }
        if ($result) {
            echo "\nTransaction has made:) id: " . $result . "\n";
        }
        $transactionId = $result;

        $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($fromAccount, $toAccount) {
            if ($err !== null) {
                throw $err;
            }
            if ($transaction) {
                echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\nTransaction dump:\n";
                var_dump($transaction);
            }
        });
    });

但我收到以下错误。

{
  "message": "Wrong type of eth_sendTransaction method argument 0.",
  "exception": "RuntimeException",
  "file": "/var/www/html/vendor/web3p/web3.php/src/Methods/EthMethod.php",
  "line": 125,
  "trace": [
    {
      "file": "/var/www/html/vendor/web3p/web3.php/src/Eth.php",
      "line": 102,
      "function": "validate",
      "class": "Web3\\Methods\\EthMethod",
      "type": "->"
    },
    {
      "file": "/var/www/html/vendor/web3p/web3.php/src/Contract.php",
      "line": 572,
      "function": "__call",
      "class": "Web3\\Eth",
      "type": "->"
    }
  ]
}

有人可以指导我解决这个问题吗?

这里是 Token.json -> ABI

ABI

【问题讨论】:

    标签: php laravel smartcontracts erc20 web3php


    【解决方案1】:

    此错误告诉您,您的合约响应中没有名称 = 转移和类型 = 函数的对象
    它应该是这样的:

       {
          "constant": false,
          "inputs": [
            {
              "name": "_to",
              "type": "address"
            },
            {
              "name": "_value",
              "type": "uint256"
            }
          ],
          "name": "transfer",
          "outputs": [],
          "payable": false,
          "stateMutability": "nonpayable",
          "type": "function"
        }
    

    【讨论】:

    • 我在 Token.json 中有这段代码 { "inputs": [ { "internalType": "address", "name": "to", "type": "address" }, { " internalType”:“uint256”,“name”:“amount”,“type”:“uint256”}],“name”:“transfer”,“outputs”:[{“internalType”:“bool”,“name” : "", "type": "bool" } ], "stateMutability": "nonpayable", "type": "function" }
    • 我已经更新了问题并把ABI也放了。
    • 我已经检查了您附加的 ABI 。就如我所告诉你的。你的 ABI 没有transfer 功能
    • 老兄,它有传输功能。我更新了问题并放了截图。
    • 我的错,这是因为转储浏览器搜索
    【解决方案2】:

    经过大量研究,在将数据写入区块链时,这是唯一适合我https://libraries.io/packagist/drlecks%2Fsimple-web3-php 的 PHP 库。

    【讨论】:

      猜你喜欢
      • 2018-06-19
      • 2018-10-01
      • 2019-12-01
      • 2023-03-31
      • 2018-11-18
      • 2018-06-17
      • 2021-10-08
      • 2022-01-17
      • 2018-08-01
      相关资源
      最近更新 更多