使用 PHP 将 ERC20 代币从一个账户转移到另一个账户

Transfer ERC20 token from one account to another using PHP

这个问题已经让 PHP 开发人员想出一种使用 ERC20 contracts/token 的方法,即执行某些操作,例如检索合约的基本 constants/information(例如名称, symbol, decimals, totalSupply), 检查一个地址的余额,将这些 ERC20 代币发送到其他以太坊地址的能力,等等​​......无需通过 NodeJS 或其他 JS 平台与以太坊的 web3 一起工作 API.

ERC20 代币转账如何运作?

尽管 ERC20 合约的 ABI 带有内置的转移方法,但这不是你进行 ERC20 代币转移的方式。转移代币的方法涉及使用 Keccak 算法对格式正确的合约转移方法语句进行编码,包括所有传递的参数。这确实是一个复杂的过程,但是当它不能让开发人员的工作变得更轻松时,使用库有什么意义呢?因此,这是一种将 ERC20 代币从一个以太坊地址转移到另一个地址的简单而精明的方法……

交易费用注意:以太坊区块链上的任何交易都需要处理“gas”,因此如果您打算从中转移代币的以太坊地址有足够数量的代币但仍然有足够数量的 ETH,交易将不会通过!

图书馆

这个答案使用 erc20-php 库,可以使用 composer 安装:

composer require furqansiddiqui/erc20-php

ERC20代币转账

让我们从实例化必要的开始类:

<?php
declare(strict_types=1);

use EthereumRPC\EthereumRPC;
use ERC20\ERC20;

// Instantiate Ethereum RPC lib with your server credentials (i.e. Ethereum-Go)
// This example assumes Ethereum RPC server running on standard port 8545 on localhost
$geth = new EthereumRPC('127.0.0.1', 8545);

// Instantiate ERC20 lib by passing Instance of EthereumRPC lib as constructor argument
$erc20 = new ERC20($geth);

准备您的变量并获取 ERC20 令牌实例:

$contract = "0x...contract-address"; // ERC20 contract address
$payer = "0x...payer-address"; // Sender's Ethereum account
$payee = "0x...payee-address"; // Recipient's Ethereum account
$amount = "1.2345"; // Amount of tokens to transfer

// Grab instance of ERC20_Token class
$token = $erc20->token($contract);

编码令牌传输:

// First argument is payee/recipient of this transfer
// Second argument is the amount of tokens that will be sent
$data = $token->encodedTransferData($payee, $amount);

准备以太坊交易:

现在我们已经将所需的编码传输方法十六进制字符串作为我们的 $data var,接下来我们将准备和调度此交易,但这里是关键注意事项:

交易收款人:ERC20代币转账交易发送到ERC20合约地址,您在上一步已经编码了原始收款人地址所以不需要混淆,交易必须是发送到智能合约的地址。

交易金额: 与收款人一样,ERC20 代币转账金额已经编码在我们的 $data var 中,因此交易金额为 ETH 应设置为“0”

正在准备交易:

$transaction = $geth->personal()->transaction($payer, $contract) // from $payer to $contract address
  ->amount("0") // Amount should be ZERO
  ->data($data); // Our encoded ERC20 token transfer data from previous step

仅此而已!但说真的,别忘了发送这笔交易:

// Send transaction with ETH account passphrase
$txId = $transaction->send("secret"); // Replace "secret" with actual passphrase of SENDER's ethereum account

恭喜,您的 ERC20 代币转账交易已发送至以太坊 P2P 网络。您将从 send() 方法收到交易 ID return,您可以使用该交易 ID 在任何以太坊区块链浏览器上检查此交易的状态!

感谢阅读!让我知道你的结果如何,我的博客上也有其他类似的主题:https://www.furqansiddiqui.com/

我只使用 Guzzle 编写了一个简单的 Ethereum 适配器,它可以处理任何复杂的智能合约查询和交易。随意为您自己的项目复制和修改:https://github.com/daikon-cqrs/ethereum-adapter。这是一个令牌传输示例:

public function transferToken(string $tokenContract, string $from, string $to, float $value): array { $signature = $this->getFunctionSignature('transfer(address,uint256)'); $to = str_pad(substr($to, 2), 64, '0', STR_PAD_LEFT); $value = str_pad($this->bcdechex($this->toWei($value)), 64, '0', STR_PAD_LEFT); return $this->call('eth_sendTransaction', [[ 'from' => $from, 'to' => $tokenContract, 'data' => $signature.$to.$value, 'value' => '0x0' ]]); }

请记住,由于以太坊节点处理随机数的方式,同步事务管理可能是一个具有挑战性的问题,可能最终需要在 PHP 端进行异步处理。