phpmac/ethers-php
Composer 安装命令:
composer require phpmac/ethers-php
包简介
PHP SDK for Ethereum, inspired by ethers.js v6
README 文档
README
PHP SDK for Ethereum, inspired by ethers.js v6
Features
- Human-readable ABI support (fully compatible with ethers.js v6)
- Complete wallet functionality (creation, signing, sending transactions)
- Contract interaction (calling, deployment, event listening)
- Utility functions (unit conversion, address validation, hashing)
Installation
composer require phpmac/ethers-php
Quick Start
Provider
use Ethers\Ethers; use Ethers\Provider\JsonRpcProvider; // Create Provider $provider = new JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_KEY'); // Or use static method $provider = Ethers::getDefaultProvider('https://mainnet.infura.io/v3/YOUR_KEY'); // Get network info $network = $provider->getNetwork(); echo "Chain ID: " . $network['chainId']; // 1 echo "Name: " . $network['name']; // mainnet // Get current block number $blockNumber = $provider->getBlockNumber(); // Get account balance $balance = $provider->getBalance('0x...'); echo Ethers::formatEther($balance) . " ETH"; // Get gas price $gasPrice = $provider->getGasPrice(); // Get fee data (EIP-1559) $feeData = $provider->getFeeData();
Wallet
use Ethers\Signer\Wallet; // Create wallet from private key $wallet = new Wallet('0x...'); // Connect to Provider $wallet = $wallet->connect($provider); // Get address $address = $wallet->getAddress(); // Get balance $balance = $wallet->getBalance(); // Get nonce $nonce = $wallet->getNonce(); // Sign message $signature = $wallet->signMessage('Hello World'); // Send transaction $response = $wallet->sendTransaction([ 'to' => '0x...', 'value' => Ethers::parseEther('0.1'), ]); // Wait for confirmation $receipt = $response['wait'](1); // wait for 1 confirmation
Contract
Supports two ABI formats:
1. Human-readable ABI (recommended, same as ethers.js)
use Ethers\Ethers; $provider = Ethers::getDefaultProvider('https://mainnet.infura.io/v3/YOUR_KEY'); $contractAddress = '0x...'; // Human-readable ABI - same syntax as ethers.js $abi = [ 'function name() view returns (string)', 'function symbol() view returns (string)', 'function decimals() view returns (uint8)', 'function balanceOf(address owner) view returns (uint256)', 'function transfer(address to, uint256 amount) returns (bool)', 'event Transfer(address indexed from, address indexed to, uint256 value)', ]; $contract = Ethers::contract($contractAddress, $abi, $provider); // Call read-only methods - same as ethers.js $name = $contract->name(); $symbol = $contract->symbol(); $balance = $contract->balanceOf($userAddress); echo "$name ($symbol): $balance";
2. JSON ABI format
use Ethers\Contract\Contract; // Standard JSON ABI $erc20Abi = [ [ 'type' => 'function', 'name' => 'balanceOf', 'inputs' => [['name' => 'account', 'type' => 'address']], 'outputs' => [['name' => '', 'type' => 'uint256']], 'stateMutability' => 'view', ], [ 'type' => 'function', 'name' => 'transfer', 'inputs' => [ ['name' => 'to', 'type' => 'address'], ['name' => 'amount', 'type' => 'uint256'], ], 'outputs' => [['name' => '', 'type' => 'bool']], 'stateMutability' => 'nonpayable', ], ]; $contract = new Contract($tokenAddress, $erc20Abi, $provider); $balance = $contract->balanceOf($userAddress);
Write operations
// Connect Wallet for write operations $wallet = Ethers::wallet($privateKey, $provider); $contract = Ethers::contract($tokenAddress, $abi, $wallet); // Send transaction - same as ethers.js $response = $contract->transfer($toAddress, Ethers::parseUnits('100', 18)); $receipt = $response['wait'](); echo "Tx Hash: " . $response['hash']; // Estimate gas $gas = $contract->estimateGas('transfer', [$toAddress, Ethers::parseUnits('100', 18)]); // Static call $result = $contract->staticCall('transfer', [$toAddress, Ethers::parseUnits('100', 18)]);
ContractFunction style calls
// Get function object - similar to ethers.js contract.transfer $transferFunc = $contract->getFunction('transfer'); // staticCall $result = $transferFunc->staticCall([$to, $amount]); // estimateGas $gas = $transferFunc->estimateGas([$to, $amount]); // send $response = $transferFunc->send([$to, $amount]); // populateTransaction $tx = $transferFunc->populateTransaction([$to, $amount]);
IDE Support (Optional)
Contract uses PHP's __call magic method for dynamic function calls. This may cause IDE warnings like "Method not defined".
Solution 1: Use call() method
$name = $contract->call('name'); $balance = $contract->call('balanceOf', [$address]);
Solution 2: Create a typed subclass with PHPDoc
/** * @method string name() * @method string symbol() * @method string balanceOf(string $owner) * @method array transfer(string $to, string $amount) */ class TokenContract extends Contract {} $contract = new TokenContract($address, $abi, $provider); $name = $contract->name(); // IDE recognizes with full type hints
See CLAUDE.md for more details.
Multicall (Batch Requests)
Use JSON-RPC 2.0 batch requests to combine multiple contract calls into a single HTTP request.
use Ethers\Contract\Contract; use Ethers\Provider\JsonRpcProvider; use Ethers\Utils\Units; $provider = new JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_KEY'); $contract = new Contract($tokenAddress, $abi, $provider); // Prepare batch calls $calls = [ ['method' => 'name', 'args' => []], ['method' => 'symbol', 'args' => []], ['method' => 'decimals', 'args' => []], ['method' => 'totalSupply', 'args' => []], ]; // Execute batch - one HTTP request for all calls $results = $contract->multicall($calls); // Results are in the same order as calls echo "Name: " . $results[0][0]; echo "Symbol: " . $results[1][0]; echo "Decimals: " . $results[2][0]; echo "TotalSupply: " . Units::formatUnits($results[3][0], (int) $results[2][0]);
Key features:
- Single HTTP request for multiple data reads
- Results maintain call order
- Support for methods with arguments (e.g.,
balanceOf(address))
See examples/multicall_demo.php for full example.
Deploy Contract (ContractFactory)
use Ethers\Ethers; use Ethers\Contract\ContractFactory; // Human-readable ABI $abi = [ 'constructor(string name, string symbol)', 'function name() view returns (string)', 'function symbol() view returns (string)', 'function totalSupply() view returns (uint256)', ]; // Contract bytecode (from compiler) $bytecode = '0x608060405234801561001057600080fd5b50...'; // Create Factory $factory = Ethers::contractFactory($abi, $bytecode, $wallet); // Or instantiate directly $factory = new ContractFactory($abi, $bytecode, $wallet); // Deploy contract - pass constructor arguments $contract = $factory->deploy('My Token', 'MTK'); // Wait for deployment $contract->waitForDeployment(); echo "Deployed to: " . $contract->target; // Get deployment transaction $deployTx = $contract->deploymentTransaction(); echo "Tx Hash: " . $deployTx['hash']; // Call contract methods $name = $contract->name(); // "My Token"
Parse ABI (Interface)
use Ethers\Ethers; use Ethers\Contract\Interface_; // Create Interface from human-readable format $interface = Ethers::parseAbi([ 'function transfer(address to, uint256 amount) returns (bool)', 'event Transfer(address indexed from, address indexed to, uint256 value)', ]); // Or instantiate directly $interface = new Interface_([ 'function transfer(address to, uint256 amount) returns (bool)', ]); // Encode function call $data = $interface->encodeFunctionData('transfer', [$to, $amount]); // Decode function call $args = $interface->decodeFunctionData('transfer', $data); // Get function selector $func = $interface->getFunction('transfer'); echo $func['selector']; // 0xa9059cbb // Format to human-readable $fragments = $interface->format('minimal');
Utility Functions
use Ethers\Ethers; // Unit conversion $wei = Ethers::parseEther('1.5'); // "1500000000000000000" $ether = Ethers::formatEther($wei); // "1.5" $units = Ethers::parseUnits('100', 6); // USDT 6 decimals $formatted = Ethers::formatUnits($units, 6); // Hash $hash = Ethers::keccak256('Hello'); // Function selector $selector = Ethers::id('transfer(address,uint256)'); // "0xa9059cbb" // Address validation $isValid = Ethers::isAddress('0x...'); $checksumAddress = Ethers::getAddress('0x...'); // Constants $zero = Ethers::zeroAddress(); $zeroHash = Ethers::zeroHash();
API Reference
JsonRpcProvider
| Method | Description |
|---|---|
getChainId() |
Get chain ID |
getNetwork() |
Get network info |
getBlockNumber() |
Get current block number |
getBalance($address) |
Get account balance |
getTransactionCount($address) |
Get transaction count (nonce) |
getGasPrice() |
Get gas price |
getFeeData() |
Get fee data (EIP-1559) |
estimateGas($tx) |
Estimate gas |
call($tx) |
Read-only call |
sendRawTransaction($signedTx) |
Send signed transaction |
getTransaction($hash) |
Get transaction info |
getTransactionReceipt($hash) |
Get transaction receipt |
waitForTransaction($hash) |
Wait for transaction confirmation |
getBlock($blockHashOrNumber) |
Get block info |
getLogs($filter) |
Get event logs |
Wallet
| Method | Description |
|---|---|
getAddress() |
Get address |
getPrivateKey() |
Get private key |
connect($provider) |
Connect to Provider |
getBalance() |
Get balance |
getNonce() |
Get nonce |
signMessage($message) |
Sign message |
signTransaction($tx) |
Sign transaction |
sendTransaction($tx) |
Send transaction |
createRandom() |
Create random wallet |
Contract
| Method | Description |
|---|---|
call($method, $args) |
Read-only call |
send($method, $args) |
Send transaction |
staticCall($method, $args) |
Static call |
estimateGas($method, $args) |
Estimate gas |
encodeFunction($method, $args) |
Encode function call |
queryFilter($eventName, $filter) |
Query event logs |
multicall($calls) |
Batch requests (JSON-RPC 2.0) |
Comparison with ethers.js v6
| ethers.js v6 | ethers-php |
|---|---|
new ethers.JsonRpcProvider(url) |
new JsonRpcProvider($url) |
new ethers.Wallet(key, provider) |
new Wallet($key, $provider) |
new ethers.Contract(addr, abi, runner) |
new Contract($addr, $abi, $runner) |
new ethers.ContractFactory(abi, bytecode, signer) |
new ContractFactory($abi, $bytecode, $signer) |
ethers.parseEther('1.0') |
Ethers::parseEther('1.0') |
ethers.Interface.from(abi) |
Interface_::from($abi) |
contract.target |
$contract->target |
contract.balanceOf(addr) |
$contract->balanceOf($addr) |
contract.transfer.staticCall(to, amount) |
$contract->transfer->staticCall([$to, $amount]) |
contract.transfer.estimateGas(to, amount) |
$contract->transfer->estimateGas([$to, $amount]) |
contract.getFunction('transfer') |
$contract->getFunction('transfer') |
factory.deploy(arg1, arg2) |
$factory->deploy($arg1, $arg2) |
contract.waitForDeployment() |
$contract->waitForDeployment() |
ABI Format Comparison
// ethers.js v6 const abi = [ "function name() view returns (string)", "function transfer(address to, uint256 amount) returns (bool)", "event Transfer(address indexed from, address indexed to, uint256 value)", ]; const contract = new ethers.Contract(address, abi, provider);
// ethers-php - exactly the same syntax $abi = [ 'function name() view returns (string)', 'function transfer(address to, uint256 amount) returns (bool)', 'event Transfer(address indexed from, address indexed to, uint256 value)', ]; $contract = new Contract($address, $abi, $provider);
phpmac/ethers-php 适用场景与选型建议
phpmac/ethers-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 452 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「blockchain」 「ethereum」 「web3」 「evm」 「ethers」 「smart-contract」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phpmac/ethers-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phpmac/ethers-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phpmac/ethers-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The BlockTrail PHP SDK, for integration of Bitcoin functionality through the BlockTrail API
Bitcoin and Altcoin Payment Gateway for Magento 2 via CoinGate
Solana PHP SDK for interacting with the Solana blockchain
Binance Pay API for Laravel
Library for validation of cryptocoin wallets
A fast and dynamic Merkle tree implementation
统计信息
- 总下载量: 452
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-02