Skip to content

Commit

Permalink
Added Ripple USD Stablecoin support
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderBuzz committed Dec 22, 2024
1 parent 97cd232 commit 81b0f8a
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 10 deletions.
2 changes: 0 additions & 2 deletions docker-compose.linux.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Use this as docker-compose.override.yml if you use Linux
version: '3.7'

services:
php:
volumes:
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.mac.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Use this as docker-compose.override.yml if you use Mac
version: '3.7'

services:
php:
volumes:
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.7'

services:
php:
build:
Expand Down
33 changes: 33 additions & 0 deletions examples/rlusd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

use Codedungeon\PHPCliColors\Color;
use Hardcastle\XRPL_PHP\Core\Stablecoin;
use Hardcastle\XRPL_PHP\Client\JsonRpcClient;

print_r(PHP_EOL . Color::GREEN);
print_r("┌───────────────────────┐" . PHP_EOL);
print_r("│ RLUSD example │" . PHP_EOL);
print_r("└───────────────────────┘" . PHP_EOL);
print_r(PHP_EOL . Color::RESET);

const NETWORK = 'testnet';

$client = new JsonRpcClient(NETWORK);

print_r(Color::YELLOW . "Funding wallet, please wait..." . PHP_EOL);
$wallet = $client->fundWallet();
print_r(Color::GREEN . "Created wallet - address: " . Color::WHITE . "{$wallet->getAddress()} " . Color::GREEN . "seed: " . Color::WHITE . "{$wallet->getSeed()}" . PHP_EOL);


print_r(Color::YELLOW . "Creating RLUSD trust line, please wait..." . PHP_EOL);
$trustSetTx = [
"TransactionType" => "TrustSet",
"Account" => $wallet->getAddress(),
"LimitAmount" => Stablecoin::getRLUSDAmount(NETWORK, '10000')
];
$trustSetPreparedTx = $client->autofill($trustSetTx);
$signedTx = $wallet->sign($trustSetPreparedTx);
$trustSetResponse = $client->submitAndWait($signedTx['tx_blob']);
print_r(Color::GREEN . "Trust line created! TxHash: " . Color::WHITE . "{$trustSetResponse->getResult()['hash']}" . PHP_EOL);
12 changes: 11 additions & 1 deletion examples/xrpBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Hardcastle\XRPL_PHP\Client\JsonRpcClient;
use Hardcastle\XRPL_PHP\Models\Account\AccountInfoRequest;
use Hardcastle\XRPL_PHP\Models\Account\AccountTxRequest;

/**
* This script can be used with the examples from
Expand All @@ -14,7 +15,7 @@
* by using the above link
*/

$testnetAccount = 'rN7T1bzCHSwQu6adkqPJAtvF4mdf1FMuG6'; //Address, not seed!
$testnetAccount = 'rNmDxPqCiPj65nXbvaxyqNn3JDqFYUEjd8'; //Address, not seed!

$client = new JsonRpcClient("https://s.altnet.rippletest.net:51234");

Expand All @@ -29,3 +30,12 @@
print_r("XRP Balance for Wallet {$testnetAccount} is {$json['result']['account_data']['Balance']} XRP");
print_r(PHP_EOL);

$req = new AccountTxRequest($testnetAccount);
$res = $client->syncRequest($req, true);

$content = $response->getBody()->getContents();
$json = json_decode($content, true);

print_r(PHP_EOL);
print_r($json);
print_r(PHP_EOL);
21 changes: 20 additions & 1 deletion src/Client/JsonRpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\ResponseInterface;
use Hardcastle\XRPL_PHP\Core\Networks;
use Hardcastle\XRPL_PHP\Models\BaseRequest;
use Hardcastle\XRPL_PHP\Models\BaseResponse;
use Hardcastle\XRPL_PHP\Models\ErrorResponse;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function __construct(
?string $maxFeeXrp = null,
?float $timeout = 3.0
) {
$this->connectionUrl = $connectionUrl;
$this->connectionUrl = $this->getNetworkUrl($connectionUrl);

$this->feeCushion = $feeCushion ?? self::DEFAULT_FEE_CUSHION;

Expand Down Expand Up @@ -353,6 +354,7 @@ public function submit(
* @param bool|null $autofill
* @param bool|null $failHard
* @param Wallet|null $wallet
*
* @return TxResponse
* @throws Exception
*/
Expand All @@ -366,6 +368,23 @@ public function submitAndWait(
return submitAndWait($this, $transaction, $autofill, $failHard, $wallet);
}

/**
*
*
* @param string $connection
*
* @return string
*/
private function getNetworkUrl(string $connection): string
{
try {
$network = Networks::getNetwork($connection);
return $network['jsonRpcUrl'];
} catch (Exception $e) {
return $connection;
}
}

/*
public function getBalances()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Networks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Networks
private const NETWORKS = [
'mainnet' => [
'label' => 'XRPL Mainnet',
'jsonRpcUrl' => 'https://s1.ripple.com:51234',
'wsUrl' => 'wss://s1.ripple.com',
'jsonRpcUrl' => 'https://xrplcluster.com',
'wsUrl' => 'wss://xrplcluster.com',
'networkId' => 0
],
'testnet' => [
Expand Down
46 changes: 46 additions & 0 deletions src/Core/Stablecoin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);
/**
* XRPL-PHP
*
* Copyright (c) Alexander Busse | Hardcastle Technologies
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Hardcastle\XRPL_PHP\Core;

use Exception;

class Stablecoin {
private const RLUSD = [
'mainnet' => [
'issuer' => 'rMxCKbEDwqr76QuheSUMdEGf4B9xJ8m5De',
'currency' => 'USD',
],
'testnet' => [
'issuer' => 'rQhWct2fv4Vc4KRjRgMrxa8xPN9Zx9iLKV',
'currency' => 'USD',
],
];

public static function getRLUSD(string $network): array
{
if (isset(self::RLUSD[$network])) {
return self::RLUSD[$network];
}

throw new Exception('RLUSD not available for network: ' . $network);
}

public static function getRLUSDAmount(string $network, string $value): array
{
$rlusd = self::getRLUSD($network);

return [
'currency' => $rlusd['currency'],
'issuer' => $rlusd['issuer'],
'value' => $value,
];
}
}

0 comments on commit 81b0f8a

Please sign in to comment.