wula / web3
Web3代理
Installs: 30
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 0
Type:wula-extension
Requires
- sc0vu/web3.php: ^0.1
- wula/wula-installer: ^2.0
This package is auto-updated.
Last update: 2025-02-26 17:31:50 UTC
README
为sc0vu/web3.php中的Web3
提供一个简单的封装以适用于wula/wulaphp.
安装
composer require wula/web3
配置
使用之前请配置文件: conf/web3_config.php
:
return [ 'etherscanKey'=>'your apiKey issued by https://etherscan.io/', 'nodes'=>[ 'default' => [ 'url'=>'http://localhost:8545', 'timeout'=>5, 'startBlockId'=>10000, 'fullTxData'=>true ], 'otherServer'=>[ 'url'=>'http://www.nidefuwuqi.com:8545', 'timeout'=>5 ] ] ]
说明:
- etherscanKey: API KEY
- nodes: 节点
url
: RPC服务器type
: 可选proxy
或空,当为proxy
时,使用etherscan代理.timeout
: 连接超时,单位秒startBlockId
: 从哪个区块开始同步数据,如果不设置则不会同步区块数据fullTxData
: 是否获取事务的详细数据
使用
获取web3实例并使用:
//连接默认(default)服务器 $web3 = Web3Factory::newWeb3(); //或者连接指定服务器 $web3 = Web3Factory::newWeb3('otherServer'); //1. 获取最新区块 $block = $web3->getBlock(); //2. 获取1000区块并包括详细信息 $block = $web3->getBlock(QuantityFormatter::format(1000),true);
开启同步:
使用artisan cron
命令执行sync.php
:
# php artisan cron -i3 extensions/wula/web3/sync.php
每3秒同步一次.
事件
每同步到一个区块数据时触发ethereum\onBlockSynced
事件:
参数说明
Web3 $web3
Web3 实例.blockNumber
区块ID.stdClass $block
区块数据或null,详见eth_getBlockByHash文档.
感谢
本扩展使用了sc0vu/web3.php库,特此感谢.