sl叶finance/simple-web3-php
PHP的Web3库
v0.13
2024-06-03 18:15 UTC
Requires
- kornrunner/ethereum-offline-raw-tx: ^0.6.0
- kornrunner/keccak: ~1
- phpseclib/phpseclib: ~2.0.30
- simplito/elliptic-php: ^1.0
- web3p/rlp: 0.3.4
Requires (Dev)
- phpunit/phpunit: ^10
This package is auto-updated.
Last update: 2024-09-03 19:00:03 UTC
README
用于与以太坊区块链和生态系统交互的PHP接口。
特性
- PHP >= 7.4
- 可定制的curl调用
- 调用:获取网络状态
- 发送已签名的交易
- 批量调用请求和已签名的交易
- 地址和私钥创建
- 消息签名
- 完整的ABIv2编码/解码
- 合约创建
- 合约交互(调用/发送)
- 带过滤器的合约事件/日志
- 支持ERC20合约的非命名小数值
- 提供与简单类型、字符串、元组、数组、元组数组数组、多维数组等交互的示例
安装
最新稳定版
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表示的数字参数。
十六进制转BigNumber
use SWeb3\Utils; $res = $sweb3->call('eth_blockNumber', []); $bigNum = Utils::hexToBn($res->result);
数字转BigNumber
$bigNum = Utils::ToBn(123);
从BigNumber获取平均的人读字符串表示
$s_number = $bigNum->toString();
将1以太币格式化为wei(交易中以太币值所需的单位)
Utils::toWei('0.001', 'ether');
从值转换获取平均的人读字符串表示
$s_val = Utils::fromWeiToString('1001', 'kwei'); // "1.001" $s_val = Utils::toWeiString('1.001', 'kwei'); // "1001"
通用以太坊区块信息调用
$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 phpseclib\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网络上活跃,并有一些以太币可用于发送交易。
- 请务必使用测试端点,否则您将花费真实以太币发送交易
- 请务必保管好您的私钥!
//SIGNING define('SWP_ADDRESS', 'XXXXXXXXXX'); define('SWP_PRIVATE_KEY', 'XXXXXXXXXX');
示例合约
此示例中使用的solidity合约也位于同一文件夹中:swp_contract.sol
示例免责声明
不要基于此示例构建您的代码结构。此示例不代表干净/高效/高性能的方案来在生产环境中实现它们。它的唯一目的是展示Simple Web3 Php的一些特性。
模块
- 从web3p/web3.php分叉并扩展的Utils库
- 交易签名:kornrunner/ethereum-offline-raw-tx
- sha3编码:来自kornrunner/keccak
- BigNumber交互:phpseclib\Math
- 非对称密钥处理:simplito/elliptic-php
TODO
- 节点账户创建/交互
许可
MIT
捐款(ETH)
0x4a890A7AFB7B1a4d49550FA81D5cdca09DC8606b