PHP Web3 ERC20令牌功能调用

发布于 2025-01-19 15:07:43 字数 2487 浏览 2 评论 0原文

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

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

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

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

我可以调用 web3 函数来获取 TOKEN 符号。它运行良好。

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

我正在使用以下代码。

$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": "eth_sendTransaction 方法参数 0 的类型错误。", “异常”:“运行时异常”, “文件”:“/var/www/html/vendor/web3p/web3.php/src/Methods/EthMethod.php”, “行”:125, “痕迹”: [ { “文件”:“/var/www/html/vendor/web3p/web3.php/src/Eth.php”, “行”:102, “功能”:“验证”, "class": "Web3\\Methods\\EthMethod", “类型”:“->” }, { “文件”:“/var/www/html/vendor/web3p/web3.php/src/Contract.php”, “行”:572, “函数”:“__call”, "class": "Web3\\Eth", “类型”:“->” } ] }

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

这是 Token.json -> ABI

ABI

传递函数

I have a Smart Contract that represent ERC20 token. I already deployed the smart contract on Binance Testnet using HardHat.

I have a deployed Smart Contract address as well.

I have integrated Web3 library in PHP laravel project using following link.

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

I can call web3 function to get TOKEN Symbol. It is working fine.

I want to transfer my tokens to some wallet address using 'transfer' function of the Smart contract.

I am using following code.

$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);
            }
        });
    });

But I am getting following error.

{
  "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": "->"
    }
  ]
}

Can someone please guide me on solving this?

Here is the Token.json -> ABI

ABI

Transfer Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文