Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将ERC20令牌从一个帐户转移到另一个帐户_Php_Ethereum_Erc20 - Fatal编程技术网

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

使用PHP将ERC20令牌从一个帐户转移到另一个帐户,php,ethereum,erc20,Php,Ethereum,Erc20,这个问题给PHP开发人员带来了很大的痛苦,他们想办法使用ERC20合同/令牌,即执行某些操作,如检索合同的基本常量/信息(例如名称、符号、小数、totalSupply)、检查地址余额、将这些ERC20令牌发送到其他以太坊地址的能力,等…无需通过NodeJS或其他JS平台来使用以太坊的web3 API。ERC20令牌传输是如何工作的? 尽管ERC20合同的ABI内置了传输方法,但这不是ERC20令牌传输的方式。传递令牌的方法涉及使用Keccak算法对格式正确的contract的传递方法语句进行编码

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

ERC20令牌传输是如何工作的?

尽管ERC20合同的ABI内置了传输方法,但这不是ERC20令牌传输的方式。传递令牌的方法涉及使用Keccak算法对格式正确的contract的传递方法语句进行编码,包括所有传递的参数。这确实是一个复杂的过程,但是当库不能使开发人员的工作变得更容易时,使用库又有什么意义呢?所以,这里有一个简单而精明的方法将ERC20令牌从一个以太坊地址转移到另一个以太坊地址

交易费用说明:以太坊区块链上的任何交易都需要处理“gas”,因此,如果您打算从中转移代币的以太坊地址有足够数量的代币,但仍然没有足够数量的ETH,则交易将无法进行

此答案使用
erc20 php
库,可使用composer安装该库:

composer需要furqansidqui/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);
编码令牌传输:

// 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变量,接下来我们将准备并分派此事务,但以下是关键注意事项:

交易收款人: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
就这样!但说真的,别忘了发送此交易:

$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,您可以使用该交易ID在任何以太坊区块链资源管理器上检查该交易的状态


谢谢你的阅读!让我知道它是如何为您工作的,我的博客上也有其他类似的主题:

我已经使用
Guzzle
编写了一个简单的
Ethereum
适配器,它只能处理任何复杂的智能合约查询和交易。请随意复制和修改您自己的项目:。下面是一个令牌传输示例:


公共函数transferToken(string$tokenContract、string$from、string$to、float$value):数组{
$signature=$this->getFunctionSignature('transfer(地址,uint256)】);
$to=str_pad(substr$to,2),64,'0',str_pad_左);
$value=str_pad($this->bcdechex($this->toWei($value)),64,'0',str_pad_LEFT);
返回$this->call('eth\u sendTransaction'[[
'from'=>$from,
'至'=>$tokenContract,
“数据”=>$signature.$to.$value,
“值”=>“0x0”
]]);
}

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