xrplwin/xrpl 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xrplwin/xrpl

Composer 安装命令:

composer require xrplwin/xrpl

包简介

A PHP package for communication with the XRP Ledger.

README 文档

README

main workflow GitHub license Total Downloads

PHP XRPL API Connector

Requirements

Installation

composer require xrplwin/xrpl

Usage sample

In sample below we will be using account_tx method.

Init Client

$client = new \XRPLWin\XRPL\Client([
    # Following values are defined by default, uncomment to override
    //'endpoint_reporting_uri' => 'http://s1.ripple.com:51234',
    //'endpoint_fullhistory_uri' => 'https://xrplcluster.com'
]);

Creating first request

# Create new 'account_tx' method instance
$account_tx = $client->api('account_tx')->params([
    'account' => 'rAccount...',
    'limit' => 10
]);

This will return an instance of XRPLWin\XRPL\Api\Methods\AccountTx.

Custom rate limit handling

If you wish to define custom behaviour when request is rate-limited you can via Closure function. Define this before executing send().

//(optional) Override default cooldown seconds (default is 5 seconds)
$account_tx->setCooldownSeconds(10);
//(optional) Override default number of tries when request is rate-limited (default is 3)
$account_tx->setTries(5);
//(optional) Set http timeout to 3 seconds (default is 0 - eg no timeout)
//           After 3 seconds method will throw \XRPLWin\XRPL\Exceptions\BadRequestException on timeout
$account_tx->setTimeout(3);

//(optional) Define custom cooldown callback
$account_tx->setCooldownHandler(
    /**
     * @param int $current_try Current try 1 to max
     * @param int $default_cooldown_seconds Predefined cooldown seconds
     * @return void|boolean return false to stop process
     */
    function(int $current_try, int $default_cooldown_seconds) {
        //Sample usage: calculate how much sleep() is needed depending on 
        $sec = $default_cooldown_seconds * $current_try;
        if($sec > 15) return false; //force stop
        sleep($sec);
    }
);

Fetching response

# Send request to Ledger
try {
    $account_tx->send();
} catch (\XRPLWin\XRPL\Exceptions\XWException $e) {
    // Handle errors
    throw $e;
}

if(!$account_tx->isSuccess()) {
    //XRPL response is returned but field result.status did not return 'success'
}

# Get fetched response as array
$response       = $account_tx->resultArray(); //array response from ledger
# Get fetched response as object
$response       = $account_tx->result();      //object response from ledger
# Get fetched final result from helper (varies method to method)
$transactions   = $account_tx->finalResult(); //array of transactions

Class method send() executes request against XRPLedger and response is stored into instance object. After that you can use one of provided class methods to retrieve result.

Promises

Alternative to fetch syncronous response as defined above, you can get Promise
Read more about Promises. Returned object is Promises/A+ implementation.

$promise = $account_tx->requestAsync();

$promises = [
    'rAcct1' => $promise,
    //...more promises
];

// Wait for the requests to complete
// Throws a ConnectException if any of the requests fail
$responses = \GuzzleHttp\Promise\Utils::unwrap($promises);

//Fill response data back into $account_tx instance
$account_tx->fill($responses['rAcct1']);

//...
$transactions = $account_tx->finalResult();

Notes: You will need to handle rate limiting and exception handling yourself when using Promises. Also each promise must be created from new instance of \XRPLWin\XRPL\Client this is to make sure each promise has its own seperated HttpClient instance.

Paginating result

# Check if there is next page
//$has_next_page = $account_tx->hasNextPage(); //bool

# Fetch next page of transactions if there is next page (next() does not return null)
if($next_account_tx = $account_tx->next()) {
    $next_result = $next_account_tx->send()->finalResult();
    // ...
}

To quick retrieve next instance to be executed (next page) you can use next(). This class method will return instance of XRPLWin\XRPL\Api\Methods\AccountTx with same parameters plus added marker from previous request. This helper function is not mandatory, you can always create new method instance manually like this $client->api('account_tx')->params([ ..., ['marker'] => [ ... ]].

See samples for more information.

Request workflow

  1. Prepare instance by setting params
  2. Use send() to execute request and handle errors by using try catch. (Request will be re-tried x amount of times if rate-limit is detected)
  3. XRPLedger response is stored in memory and it is available to read via
    ->result()
    ->resultArray()
    ->finalResult()

Methods

account_tx

$account_tx = $client->api('account_tx')->params([
    'account' => 'rAccount...',
    'limit' => 10
]);

tx

$account_tx = $client->api('tx')->params([
    'transaction' => 'DE80B0064677CEFFDE...',
    'binary' => false
]);

For other methods refer to https://xrpl.org/websocket-api-tool.html and src/Api/Methods

Utilities

There are few utilities available with this package:

  • Balance changes (with trading fees optional calculation)
  • Flags
  • UNLReport Flag Ledger
  • Currency code to readable currency code

Flags

use XRPLWin\XRPL\Utilities\Flags;

//Methods:
Flags::extract(int $flags, string $transactionType): array
Flags::description(string $transactiontype, string $flagname, bool $htmlFormat = false): string
Flags::hasFlag(int $flags, int $check): bool

UNLReportFlagLedger

Flag ledger is calculated using modulo formula LedgerIndex % 256.

use XRPLWin\XRPL\Utilities\UNLReportFlagLedger;

UNLReportFlagLedger::isFlag(256);   //for ledger sequence 256 - true
UNLReportFlagLedger::isFlag(257);   //for ledger sequence 257 - false
UNLReportFlagLedger::prev(6873600);          //6873344
UNLReportFlagLedger::prevOrCurrent(6873600); //6873600
UNLReportFlagLedger::next(6873600);          //6873856
UNLReportFlagLedger::nextOrCurrent(6873600); //6873600

Util class

Converts Currency Code ISO or HEX to human readable representation.

use XRPLWin\XRPL\Utilities\Util;
#Syntax: Util::currencyToSymbol(string $ISO_or_HEX, $malformedUtf8ReturnString = '?')

//ISO Currency Code
Util::currencyToSymbol('EUR') //EUR 
//Deprecated Demurrage Currency Code
Util::currencyToSymbol('0158415500000000C1F76FF6ECB0BAC600000000') //XAU (-0.5% pa)
//Nonstandard Currency Code
Util::currencyToSymbol('534F4C4F00000000000000000000000000000000') //SOLO

Read more:

Running tests

Run all tests in "tests" directory.

composer test

or

./vendor/bin/phpunit --testdox

xrplwin/xrpl 适用场景与选型建议

xrplwin/xrpl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.52k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「composer」 「ripple」 「xrp」 「xrpl」 「rippled」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 xrplwin/xrpl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 xrplwin/xrpl 我们能提供哪些服务?
定制开发 / 二次开发

基于 xrplwin/xrpl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.52k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 21
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-22