extraton/php-ton-client
Composer 安装命令:
composer require extraton/php-ton-client
包简介
Extraton, PHP TON client
README 文档
README
Extraton, PHP TON Client
Extraton is a simple and powerful php-library to binding with the TON SDK. It allows to interact with FreeTON blockchain. It has the rich abilities:
- All methods of the TON SDK v1.0.0 are implemented
- Interaction with the TON SDK through an asynchronous calls
- The every method contains inline-doc
- The full autocomplete is available in a such IDE like the PHPStorm
- Simple installation by the Composer package manager
- The automatic download of the TON SDK library for the current environment
- The client auto configuration (out-of-the-box)
- Covered by the unit-tests
- Fully covered by the integration tests
- Tools to maintain code quality (static analyser and codestyle checker)
- Tools to the quick start to develop (see Dockerfile + Makefile)
- The error handling by the general exception interface (see src/Exception)
- Using generators to iterate the asynchronous events
- You can add your own client implementation based on FFIAdapter and Binding
- A simple interface to the graphql requests
- Temporary logs creation for the detailed analysis on integration tests running
Requirements
- php7.4+ or php8.0+
- ffi extension
- json extension
- zlib extension
- TON SDK Library 1.22.0 (download script included)
Installation
To install it via Composer simply run:
composer require extraton/php-ton-client
To automatically download the TON SDK library add the following lines to your project composer.json file:
... "scripts": { "download-ton-sdk-library": "Extraton\\TonClient\\Composer\\Scripts::downloadLibrary", "post-update-cmd": [ "@download-ton-sdk-library" ], "post-install-cmd": [ "@download-ton-sdk-library" ] } ...
The library will be downloaded after the commands composer install or composer update are called in your project root. To forced download the TON SDK library for your operating system, run the following command:
composer run download-ton-sdk-library
The TON SDK library will be installed to the directory YOUR_PROJECT_ROOT/vendor/extranton/php-ton-client/bin/.
Configuring
Simple TonClient instantiation:
$tonClient = TonClient::createDefault();
It uses the default configuration:
$config = [ "network" => [ 'server_address' => 'net.ton.dev', 'network_retries_count' => 5, 'message_retries_count' => 5, 'message_processing_timeout' => 60000, 'wait_for_timeout' => 60000, 'out_of_sync_threshold' => 30000, 'access_key' => '' ], 'abi' => [ 'workchain' => 0, 'message_expiration_timeout' => 60000, 'message_expiration_timeout_grow_factor' => 1.35 ], 'crypto' => [ 'mnemonic_dictionary' => 1, 'mnemonic_word_count' => 12, 'hdkey_derivation_path' => "m/44'/396'/0'/0/0", 'hdkey_compliant' => true ], ];
You can start using a simple configuration for TonClient:
$config = [ 'network' => [ 'server_address' => 'net.ton.dev' ] ];
All configuration options are available here. After instantiate the TonClient will automatically detect operating system and path to the TON SDK library:
// Create new instance with custom configuration and default path to TON SDK library $tonClient = new TonClient($config);
Default path: YOUR_PROJECT_ROOT/vendor/extraton/bin/tonclient.*. Also you can specify path by the following lines of code:
// Create new instance TonClient with custom path to TON SDK library $binding = new Binding('PATH_TO_TON_SDK_LIBRARY'); $tonClient = new TonClient($config, $binding);
Basic usage
// Get TON SDK version $result = $tonClient->version(); echo "TON SDK version: " , $result->getVersion() . PHP_EOL;
// Get instance of Crypto module $crypto = $tonClient->getCrypto(); // Generate random key pair $result = $crypto->generateRandomSignKeys(); $keyPair = $result->getKeyPair(); echo 'Public key: ' . $keyPair->getPublic() . PHP_EOL; echo 'Private key: ' . $keyPair->getSecret() . PHP_EOL;
// Get instance of Utils module $utils = $tonClient->getUtils(); // Convert address to hex format $result = $utils->convertAddressToHex('ee65d170830136253ad8bd2116a28fcbd4ac462c6f222f49a1505d2fa7f7f528'); echo 'Hex: ' . $result->getAddress() . PHP_EOL;
Building queries
Use special classes to easily build queries:
$query = (new ParamsOfWaitForCollection('accounts')) ->addResultField('id', 'last_paid') ->addFilter('last_paid', Filters::IN, [1601332024, 1601331924]) ->setTimeout(60_000); $net->waitForCollection($query);
$query = (new ParamsOfSubscribeCollection('transactions')) ->addResultField('id', 'block_id', 'balance_delta') ->addFilter('balance_delta', Filters::GT, '0x5f5e100'); $net->subscribeCollection($query);
$query = new ParamsOfQueryCollection('accounts'); $query->addResultField( 'acc_type', 'acc_type_name', 'balance', 'boc', 'id', 'last_paid', 'workchain_id', ); $query->addFilter( 'last_paid', Filters::IN, [ 1601332024, 1601331924, 1601332491, 1601332679 ] ); $query->addOrderBy('last_paid', OrderBy::DESC)->setLimit(2); $net->queryCollection($query);
You can add your own query class that implements the interface \Extraton\TonClient\Entity\Net\QueryInterface or extends the class \Extraton\TonClient\Entity\Net\AbstractQuery.
The following constants are available for filters and directions:
class Filters implements Params { public const EQ = 'eq'; public const GE = 'ge'; public const GT = 'gt'; public const IN = 'in'; public const LE = 'le'; public const LT = 'lt'; public const NE = 'ne'; public const NOT_IN = 'notIn'; ...
class OrderBy implements Params { public const ASC = 'ASC'; public const DESC = 'DESC'; ...
Advanced usage
Use the following example to build an application for monitoring events coming from the blockchain network:
// Build query $query = (new ParamsOfSubscribeCollection('transactions')) ->addResultField('id', 'block_id', 'balance_delta') ->addFilter('balance_delta', Filters::GT, '0x5f5e100'); // Get result with handle and start watching $result = $net->subscribeCollection($query); echo "Handle: {$result->getHandle()}." . PHP_EOL; $counter = 0; // Iterate generator foreach ($result->getIterator() as $event) { $counter++; echo "Event counter: {$counter}, event data:" . PHP_EOL; var_dump($event->getResult()); if ($counter > 25) { echo 'Manual stop watching.' . PHP_EOL; $result->stop(); // or call: $net->unsubscribe($result->getHandle()); } } echo 'Finished.' . PHP_EOL;
Examples
Please see Examples and Integration tests for more information on detailed usage.
⚠️ Warning
We use experemental PHP extension FFI. This extension allows the loading of shared libraries, calling of C functions and accessing of C data structures in pure PHP. This is the only one possible way to async integrate with the TON SDK library.
Please read the official warnings from the developers of php:
Warning This extension is EXPERIMENTAL. The behaviour of this extension including the names of its functions and any other documentation surrounding this extension may change without notice in a future release of PHP. This extension should be used at your own risk.
Although this works, this functionality is not supported on all libffi platforms, is not efficient and leaks resources by the end of request.
We have not detected memory leaks. But sometimes we caught segmentation faults during testing. 🙏 Hopefully the FFI extension will be stabilized in future versions of php.
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
Run the following command to run unit tests:
make test-unit
... and integration tests:
make test-integration
Some tests use TON SDK methods that listen for asynchronous events. Data from these events is saved to the directory /tests/Integration/artifacts/. This way you can analyze them in detail. For example, the test \Extraton\Tests\Integration\TonClient\ProcessingTest::testProcessMessageWithEvents uses the call of method \Extraton\TonClient\Processing::processMessage. Events received during generator iteration are saved in a file.
Code Quality
We use PHPStan and PHP Coding Standards Fixer to control code quality. Run the following commands to analyze the code and fix code style errors:
make analyze
make codestyle
make codestyle-fix
Contributing
Please see CONTRIBUTING for details.
Notice
If you have any issues, just feel free and open it in this repository, thx!
Credits
License
The Apache License Version 2.0. Please see License File for more information.
extraton/php-ton-client 适用场景与选型建议
extraton/php-ton-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 936 次下载、GitHub Stars 达 28, 最近一次更新时间为 2020 年 11 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dapp」 「extraton」 「freeton」 「ton」 「ton crystal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 extraton/php-ton-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 extraton/php-ton-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 extraton/php-ton-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 936
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2020-11-15
