gri3li / ethereum-smart-contract
Ethereum 智能合约包装类
v1.0
2019-05-11 16:30 UTC
Requires
- php: >=7.0
- ext-gmp: *
- sc0vu/web3.php: ^0.1.4
- sop/crypto-types: ^0.2.1
- web3p/ethereum-tx: ^0.3.4
This package is auto-updated.
Last update: 2024-09-28 18:32:35 UTC
README
Ethereum 智能合约包装
安装
composer require gri3li/ethereum-smart-contract
用法
创建合约实例
use gri3li\EthereumSmartContract; $instance = EthereumSmartContract::createByHost( 'https://:8545', '1', // mainnet '0xB8c77482e45F1F44dE1745F52C74426C631bDD52', // contract address file_get_contents('path_to_contract_abi_file.json') // abi string );
读取,例如,erc20 代币获取余额
$response = $instance->read('balanceOf', ['0x227390eeba512120c16C239B6556C0992022E961']); var_dump($response); /* array(1) { 'balance' => class phpseclib\Math\BigInteger#47 (2) { public $value => string(24) "0x01c3ca8bcdc38115a80020" public $engine => string(3) "gmp" } } */ /** @var \BI\BigInteger $balance */ $balance = $response['balance']; var_dump($balance->toString());
写入,例如,erc20 代币发送转账
$fromPrivateKey = '4ffe6b52e5f649794dd4f75ed91276ad0dd417ec24cd24ba22802ea50e9d34fd'; $toAddress = '0x227390eeba512120c16C239B6556C0992022E962'; $amount = '1000000000000000000'; $gasLimit = '800000'; $gasPrice = '12000000000'; $txHash = $instance->write('transfer', [$toAddress, $amount], $fromPrivateKey, $gasPrice, $gasLimit);