简体   繁体   English

使用PHP将ERC20令牌从一个帐户转移到另一个帐户

[英]Transfer ERC20 token from one account to another using PHP

这个问题在PHP开发人员中引起了极大的痛苦,他们想出了一种使用ERC20合同/令牌的方法,即执行某些操作,例如检索合同的基本常量/信息(例如名称,符号,小数,totalSupply),检查地址余额,能够将这些ERC20令牌发送到其他以太坊地址等,而无需通过NodeJS或其他JS平台来与以太坊的web3 API配合使用。

How a ERC20 token transfer works? ERC20令牌转移如何工作?

Even though ERC20 contract's ABI comes with a transfer method built-in but that is not how you do a ERC20 token transfer. 即使ERC20合约的ABI内置了转移方法,但这也不是您进行ERC20令牌转移的方式。 The method to transfer tokens involve encoding of properly formatted contract's transfer method statement including all of the passed arguments using Keccak algorithm. 传递令牌的方法包括使用Keccak算法对格式正确的合同的传递方法语句(包括所有传递的参数)进行编码。 This is indeed a complicated process but what's the point of using a library when it does not make things easier for you as a developer? 这确实是一个复杂的过程,但是使用库并不能使您作为开发人员更轻松地有什么意义呢? So, here is a simple and savvy method to transfer ERC20 tokens from one Ethereum address to another… 因此,这是一种将ERC20令牌从一个以太坊地址转移到另一个以太坊的简单明了的方法…

Transaction Fee Note: Any transaction on Ethereum blockchain requires “gas” to be processed, therefore if an Ethereum address that you intend to transfer tokens from has sufficient amount of tokens but still has INSUFFICIENT amount of ETH, transaction will NOT go through! 交易费用说明:以太坊区块链上的任何交易都需要处理“ gas”,因此,如果您打算从中转移代币的以太坊地址具有足够数量的代币,但仍然有不足量的ETH,则交易将不会通过!

Library 图书馆

This answer uses erc20-php library, that can be installed using composer: 此答案使用erc20-php库,可以使用composer安装该库:

composer require furqansiddiqui/erc20-php

ERC20 Token Transfer ERC20代币转移

Let's start by instantiating necessary classes: 让我们从实例化必要的类开始:

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

Prepare your vars and grab instance of ERC20 token: 准备您的变量并获取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);

Encoding token transfer: 编码令牌传输:

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

Prepare Ethereum transaction: 准备以太坊交易:

Now that we have the required encoded transfer method hexadecimal string in as our $data var, next we will be preparing and dispatching this transaction, but here are the key notes: 现在,我们已经在$ data var中包含了所需的编码传输方法十六进制字符串,接下来,我们将准备并分派此事务,但是这里是关键说明:

Transaction Payee: ERC20 token transfer transactions are sent to ERC20 contract address, you have encoded original recipient's address in previous step so no need to be confused, the transaction has to be dispatched to the smart contract's address. 交易收款人: ERC20令牌转移交易被发送到ERC20合同地址,您已在上一步中对原始收件人的地址进行了编码,因此无需混淆,必须将交易分派到智能合约的地址。

Transaction Amount: Just like the payee, ERC20 token transfer amount is already encoded in our $data var therefore transaction's amount is ETH should be set to “0” 交易金额:就像收款人一样,ERC20代币转移金额已经编码在我们的$ data var中,因此交易金额为ETH应该设置为“ 0”

Preparing transaction: 准备交易:

$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

and that's about it! 就是这样! But seriously, don't forget to send this transaction: 但请注意,不要忘记发送此交易:

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

Congratulations, your ERC20 token transfer transaction has been dispatched to Ethereum P2P network. 恭喜,您的ERC20代币转移交易已发送到以太坊P2P网络。 You will receive transaction ID as return from send() method and you may use that transaction ID to check status of this transaction on any Ethereum blockchain explorer! 您将从send()方法中获得交易ID作为返回,您可以使用该交易ID在任何以太坊区块链浏览器上检查该交易的状态!

Thank you for reading! 感谢您的阅读! Let me know how it worked out for you, I have other similar topics covered on my blog as well: https://www.furqansiddiqui.com/ 让我知道如何为您解决问题,我的博客上也涵盖了其他类似的主题: https : //www.furqansiddiqui.com/

I have written a simple Ethereum adapter using Guzzle only which can handle any complexity of smart contract queries & transactions. 我仅使用Guzzle编写了一个简单的Ethereum适配器,它可以处理任何复杂的智能合约查询和交易。 Feel free to copy and modify for your own project: https://github.com/daikon-cqrs/ethereum-adapter . 随意复制和修改您自己的项目: https : //github.com/daikon-cqrs/ethereum-adapter Here is a token transfer example: 这是令牌转移的示例:

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' ]]); }

Bear in mind that because of the way Ethereum nodes handle nonces, synchronous transaction management can be a challenging problem to solve, and probably ultimately requires asynchronous processing on the PHP side. 请记住,由于以太坊节点处理随机数的方式,同步事务管理可能是一个具有挑战性的问题,可能最终需要在PHP端进行异步处理。

This is a quite difficult task, as you need a very deep understanding of how tokens work and you will need at least one node. 这是一项非常艰巨的任务,因为您需要对令牌的工作方式有非常深入的了解,并且至少需要一个节点。 If you are looking for a more easy method, that works with any programming language, you might check out Tokengateway . 如果您正在寻找一种适用于任何编程语言的更简单的方法,则可以查看Tokengateway It is a REST API that allows to send any ERC20 token, get info on any ERC20 token, get balances of any ERC20 token and more. 它是一个REST API,允许发送任何ERC20令牌,获取有关任何ERC20令牌的信息,获取任何ERC20令牌的余额等。 Also, there is no need to setup a node. 另外,无需设置节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM