drlecks / simple-web3-php
PHP的Web3库
v0.10
2022-12-12 17:26 UTC
Requires
- kornrunner/ethereum-offline-raw-tx: ^0.4.0
- kornrunner/keccak: ~1
- phpseclib/phpseclib: ~2.0.30
- simplito/elliptic-php: ^1.0
This package is auto-updated.
Last update: 2024-09-27 15:04:53 UTC
README
用于与以太坊区块链和生态系统交互的PHP接口。
特性
- PHP >= 7.4
- 可定制的curl调用
- 调用: 获取网络状态
- 发送已签名的交易
- 批处理调用请求和已签名的交易
- 地址和私钥创建
- 消息签名
- 完整的ABIv2编码/解码
- 合约创建
- 合约交互(调用/发送)
- 带有过滤器的合约事件/日志
- 支持带有非名义十进制值的ERC20合约
- 提供了与简单类型、字符串、元组、数组、元组的数组、多维数组的数组等交互的示例
- EIP712类型化结构化数据哈希
安装
最新稳定版本
composer require drlecks/simple-web3-php "^0.10.0"
或者您可以在composer.json中添加以下行
"drlecks/simple-web3-php": "^0.10.0"
开发(主分支)
composer require drlecks/simple-web3-php dev-master
或者您可以在composer.json中添加以下行
"drlecks/simple-web3-php": "dev-master"
使用方法
新建实例
use SWeb3\SWeb3; //initialize SWeb3 main object $sweb3 = new SWeb3('http://ethereum.node.provider:optional.node.port'); //optional if not sending transactions $from_address = '0x0000000000000000000000000000000000000000'; $from_address_private_key = '345346245645435....'; $sweb3->setPersonalData($from_address, $from_address_private_key);
转换值
- 大多数调用返回十六进制或BigNumber来表示数字。
- 大多数调用期望以BigNumber表示的数字参数。
十六进制转Big Number
use SWeb3\Utils; $res = $sweb3->call('eth_blockNumber', []); $bigNum = Utils::hexToBn($res->result);
数字转BigNumber
$bigNum = Utils::ToBn(123);
从Big Number获取平均人类可读的字符串表示形式
$s_number = $bigNum->toString();
将1以太坊格式化为wei(交易中ether值所需的单位)
Utils::toWei('0.001', 'ether');
从值转换获取平均人类可读的字符串表示形式
$s_val = Utils::fromWeiToString('1001', 'kwei'); // "1.001" $s_val = Utils::toWeiString('1.001', 'kwei'); // "1001"
ABI编码
手动编码参数
$abiEncoded = ABI::EncodeParameters_External(['address', 'uint256'], [$userAddress, 1]);
Keccak 256哈希
$hash = Utils::sha3($abiEncoded);
通用以太坊区块信息调用
$res = $sweb3->call('eth_blockNumber', []);
刷新燃气价格
$gasPrice = $sweb3->getGasPrice();
估算燃气价格(从发送参数中)
$sweb3->chainId = '0x3';//ropsten $sendParams = [ 'from' => $sweb3->personal->address, 'to' => '0x1111111111111111111111111111111111111111', 'value' => Utils::toWei('0.001', 'ether'), 'nonce' => $sweb3->personal->getNonce() ]; $gasEstimateResult = $sweb3->call('eth_estimateGas', [$sendParams]);
向地址发送0.001以太坊
//remember to set personal data first with a valid pair of address & private key $sendParams = [ 'from' => $sweb3->personal->address, 'to' => '0x1111111111111111111111111111111111111111', 'gasLimit' => 210000, 'value' => Utils::toWei('0.001', 'ether'), 'nonce' => $sweb3->personal->getNonce() ]; $result = $sweb3->send($sendParams);
批处理调用和交易
//enable batching $sweb3->batch(true); $sweb3->call('eth_blockNumber', []); $sweb3->call('eth_getBalance', [$sweb3->personal->address], 'latest'); //execute all batched calls in one request $res = $sweb3->executeBatch(); //batching has to be manually disabled $sweb3->batch(false);
账户
use SWeb3\Accounts; use SWeb3\Account; //create new account privateKey/address (returns Account) $account = Accounts::create(); //retrieve account (address) from private key $account2 = Accounts::privateKeyToAccount('...private_key...'); //sign message with account $res = $account2->sign('Some data');
合约交互
use SWeb3\SWeb3_Contract; $contract = new SWeb3_contract($sweb3, '0x2222222222222222222222222222222222222222', '[ABI...]'); //'0x2222...' is contract address // call contract function $res = $contract->call('autoinc_tuple_a'); // change function state //remember to set the sign values and chain id first: $sweb3->setPersonalData() & $sweb3->chainId $extra_data = ['nonce' => $sweb3->personal->getNonce()]; $result = $contract->send('Set_public_uint', 123, $extra_data);
合约事件(日志)
//optional parameters fromBlock, toBlock, topics //default values -> '0x0', 'latest', null (all events/logs from this contract) $res = $contract->getLogs();
合约创建(部署)
use SWeb3\SWeb3_Contract; $creation_abi = '[abi...]'; $contract = new SWeb3_contract($sweb3, '', $creation_abi); //set contract bytecode data $contract_bytecode = '123456789....'; $contract->setBytecode($contract_bytecode); //remember to set the sign values and chain id first: $sweb3->setPersonalData() & $sweb3->chainId $extra_params = ['nonce' => $sweb3->personal->getNonce()]; $result = $contract->deployContract( [123123], $extra_params);
通常所需的包含文件
use SWeb3\SWeb3; //always needed, to create the Web3 object use SWeb3\Utils; //sweb3 helper classes (for example, hex conversion operations) use SWeb3\SWeb3_Contract; //contract creation and interaction use SWeb3\Accounts; //account creation use SWeb3\Account; //single account management (signing) use phpseclib3\Math\BigInteger as BigNumber; //BigInt handling use stdClass; //for object interaction
提供的示例
在Examples/文件夹中有一些扩展示例,包括调用和发送示例
- example.call.php
- example.send.php
- example.batch.php
- example.account.php
- example.contract_creation.php
- example.erc20.php
示例配置
要执行示例,您需要在配置文件(example.config.php)中添加一些数据。
示例预先配置为与infura端点一起工作
define('INFURA_PROJECT_ID', 'XXXXXXXXX'); define('INFURA_PROJECT_SECRET', 'XXXXXXXXX'); define('ETHEREUM_NET_NAME', 'ropsten'); //ropsten , mainnet define('ETHEREUM_NET_ENDPOINT', 'https://'.ETHEREUM_NET_NAME.'.infura.io/v3/'.INFURA_PROJECT_ID);
只需添加您的infura项目密钥。如果您尚未配置API密钥要求,请忽略它。
如果您正在使用私有端点,请忽略所有infura定义
define('ETHEREUM_NET_ENDPOINT', 'https://123.123.40.123:1234');
要启用合约交互,设置合约数据(地址和ABI)。该文件预先配置为与我们的示例合约一起工作,已部署在ropsten上。
//swp_contract.sol is available on ropsten test net, address: 0x706986eEe8da42d9d85997b343834B38e34dc000 define('SWP_Contract_Address','0x706986eEe8da42d9d85997b343834B38e34dc000'); $SWP_Contract_ABI = '[...]'; define('SWP_Contract_ABI', $SWP_Contract_ABI);
要启用交易发送和签名,输入有效的地址和私钥对。在继续之前,请务必注意以下事项
- 该地址必须在ropsten网络上活跃,并且有足够的ether来发送交易。
- 请务必检查您正在使用测试端点,否则您将花费真实以太坊发送交易
- 请务必保管好您的私钥!
//SIGNING define('SWP_ADDRESS', 'XXXXXXXXXX'); define('SWP_PRIVATE_KEY', 'XXXXXXXXXX');
示例合约
在此示例中使用的Solidity合约也位于同一文件夹中:swp_contract.sol
示例免责声明
不要基于此示例构建您的代码结构。此示例不代表在生产环境中实现它们的清洁、高效或高性能方法。它的唯一目的是展示Simple Web3 Php的一些功能。
模块
- Utils库是从web3p/web3.php分叉和扩展的
- 交易签名:kornrunner/ethereum-offline-raw-tx
- SHA3 编码:来自 kornrunner/keccak
- BigNumber 交互:phpseclib3\Math
- 非对称密钥处理:simplito/elliptic-php
待办事项
- 节点账户创建/交互
许可证
MIT
捐款(ETH)
0x4a890A7AFB7B1a4d49550FA81D5cdca09DC8606b