PHP code example of furqansiddiqui / erc20-php

1. Go to this page and download the library: Download furqansiddiqui/erc20-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

furqansiddiqui / erc20-php example snippets


use FurqanSiddiqui\Ethereum\Rpc\GethRpc;
use FurqanSiddiqui\Ethereum\Erc20\Erc20;
use FurqanSiddiqui\Ethereum\Keypair\EthereumAddress;

$rpc = new GethRpc("https://mainnet.infura.io/v3/YOUR_PROJECT_ID");
$erc20 = new Erc20($rpc);

// USDC Token Address
$usdcAddress = new EthereumAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
$token = $erc20->deployedAt($usdcAddress);

echo "Name: " . $token->name() . PHP_EOL;
echo "Symbol: " . $token->symbol() . PHP_EOL;
echo "Decimals: " . $token->decimals() . PHP_EOL;
echo "Total Supply: " . $token->totalSupply() . PHP_EOL;

$userAddress = new EthereumAddress("0x...");

// Get raw balance (uint256)
$balance = $token->balanceOf($userAddress);
echo "Raw Balance: " . $balance . PHP_EOL;

// Get allowance
$spender = new EthereumAddress("0x...");
$allowance = $token->allowance($userAddress, $spender);

// Encode a transfer of 100 USDC (USDC has 6 decimals)
$toAddress = "0x...";
$amount = 100 * (10 ** 6);
$data = $token->encodeTransfer($toAddress, $amount);

// You can now use this $data in an 'eth_sendTransaction' or 'eth_sendRawTransaction' call

use FurqanSiddiqui\Ethereum\Erc20\Erc20ContractBase;
use FurqanSiddiqui\Ethereum\Evm\ContractMethod;
use FurqanSiddiqui\Ethereum\Evm\ContractMethodType;
use FurqanSiddiqui\Ethereum\Evm\AbiParam;

class MyCustomTokenAbi extends Erc20ContractBase
{
    public function __construct()
    {
        parent::__construct();
        
        // Add a custom method: burn(uint256)
        $burn = new ContractMethod(ContractMethodType::Function, "burn", false, false);
        $burn->appendInput(new AbiParam("uint256", null));
        $this->append($burn);
    }
}

// Usage
$erc20 = new Erc20($rpc, new MyCustomTokenAbi());
bash
composer