Search code examples
phpethereumerc20

Transfer ERC20 token from one account to another using PHP


This issue has caused a good pain among PHP developers to figure out a way to work with ERC20 contracts/token, i.e. perform certain actions like retrieving contract’s basic constants/information (e.g. name, symbol, decimals, totalSupply), checking balance of an address, ability to send these ERC20 tokens to other Ethereum addresses, etc… without going through NodeJS or other JS platforms to work with Ethereum’s web3 API.


Solution

  • How a ERC20 token transfer works?

    Even though ERC20 contract’s ABI comes with a transfer method built-in but that is not how you do a ERC20 token transfer. The method to transfer tokens involve encoding of properly formatted contract’s transfer method statement including all of the passed arguments using Keccak algorithm. 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…

    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!

    Library

    This answer uses erc20-php library, that can be installed using composer:

    composer require furqansiddiqui/erc20-php

    ERC20 Token Transfer

    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:

    $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:

    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.

    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”

    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. 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!

    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/