mayvenstudios / flow-php-sdk
Flow 区块链的 PHP SDK
0.3.3
2023-10-20 20:08 UTC
Requires
- php: >=7.0.0
- illuminate/support: ^10.0
- symfony/process: ^6.2
Requires (Dev)
- orchestra/testbench: ^5.0|^6.0
README
⚠️ 这是一个 alpha 版本;功能可能会改变。
这是一个 Laravel PHP 包,允许您通过使用 Flow CLI 与 Flow 区块链交互。
未来我们将实现与 GRPC 接口的完全集成,但到目前为止,它只是一个 Flow CLI 的包装器。
目前,SDK 包含以下功能
- 基本脚本、交易、事件和区块调用的 Flow CLI 包装器。
- 通过 gRPC 与 Flow Access API 通信(即将推出)
- 交易准备和签名(即将推出)
- 事件解析(即将推出)
安装
Composer
要使用 Composer 将此 SDK 添加到您的项目中,请使用以下命令
composer require mayvenstudios/flow-php-sdk
环境变量
之后,您应该在 .env 文件中添加以下变量。此示例显示如果您使用 Homebrew 在 macOS 上,则默认 Flow CLI 路径,但在 Linux 服务器上可能为 ~/.local/bin
FLOW_PATH=/opt/homebrew/bin/
FLOW_NETWORK=testnet
Cadence 文件夹
确保将所有 flow.json 配置文件和合约、脚本以及交易放入项目根目录下的 cadence 文件夹中非常重要。此文件夹将用作运行 flow 命令的基本路径,以便能够读取所有必要的 Flow 配置。
配置文件(可选)
如果您使用 Laravel,也可以运行此命令以发布配置文件,如果需要的话。
php artisan vendor:publish
安装 grpc 扩展(可选)
如果您想要帮助开发完整的 GRPC 版本,则需要此操作。
$ [sudo] pecl install grpc
示例用法
一旦您通过 Composer 添加了包并设置了环境变量,您可以使用以下方式使用 Flow Facade。
请记住,在您的 PHP 中使用 use Flow; 在开头导入类。
所有输出都将作为从 Flow CLI 返回的 JSON 解析的 PHP 对象返回。
获取最新区块
Flow::getLatestBlock();
通过 ID 获取最新区块
Flow::getBlock(123456);
获取一定范围内的区块中的事件(最多 250 个)
Flow::event('event_name_with_address_goes_here')->minBlock(1)->maxBlock(100)->run();
运行脚本
Flow::script('path_to_cadence_script_file')->run();
执行交易
Flow::transaction('path_to_cadence_transaction_file')->run();
参数
您可以通过以下方法轻松传递参数给脚本和交易
整数(UInt64)
Flow::transaction('path_to_cadence_transaction_file')->argInt(100)->run();
固定(UFix64)
Flow::transaction('path_to_cadence_transaction_file')->argFix(9.99)->run();
字符串
Flow::transaction('path_to_cadence_transaction_file')->argString('something')->run();
地址
Flow::transaction('path_to_cadence_transaction_file')->argAddress('0x711eba2a0d39d21a')->run();
布尔值
Flow::transaction('path_to_cadence_transaction_file')->argBool(true)->run();
字典字符串({String:String} 的字典)
Flow::transaction('path_to_cadence_transaction_file')->argDictionaryString(['key' => 'value'])->run();
贡献
项目处于非常早期阶段,欢迎所有贡献。
阅读贡献指南 以开始。
依赖项
Protobuf-PHP Illuminate-Support