Skip to content

Commit

Permalink
[WIP] CurrencyConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
zgrguric committed Mar 28, 2024
1 parent 010a522 commit f972484
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"brick/math": "^0.10.2|^0.11"
},
"require-dev": {
"phpunit/phpunit": "^10.1"
"phpunit/phpunit": "^10.1",
"symfony/var-dumper": "^6.1"
},
"scripts": {
"test": "./vendor/bin/phpunit --testdox"
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" stopOnFailure="true" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Test suite">
<directory suffix="Test.php">./tests/Unit</directory>
<directory suffix="Test.php">./tests/Feature</directory>
<directory suffix="UtilTest.php">./tests/Unit</directory>
<!--<directory suffix="Test.php">./tests/Feature</directory>-->
</testsuite>
</testsuites>
</phpunit>
77 changes: 77 additions & 0 deletions src/Utilities/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types=1);

namespace XRPLWin\XRPL\Utilities;

/**
* Utilities
*/
final class Util
{
/**
* Decode HEX currency to symbol.
* If already symbol returns that symbol (checked by length).
* Examples: USD,EUR,534F4C4F00000000000000000000000000000000,LP 031234...
* @see https://3v4l.org/Mp2Fu
* @return string
*/
public static function currencyToSymbol($currencycode, $malformedUtf8ReturnString = '?') : string
{
if( \strlen($currencycode) == 40 )
{
if(\substr($currencycode,0,2) == '03') {
//AMM LP token, 03 + 19 bytes of sha512
return 'LP '.$currencycode;
}

if(\substr($currencycode,0,2) == '01') {
//demurrage, convert it to utf8 for display


//let bytes = Buffer.from(demurrageCode, "hex")
$bytes = \array_values(unpack("C*", \hex2bin($currencycode)));
//let code = String.fromCharCode(bytes[1]) + String.fromCharCode(bytes[2]) + String.fromCharCode(bytes[3]);
$code = \chr($bytes[1]).\chr($bytes[2]).\chr($bytes[3]); //OK

//let interest_start = (bytes[4] << 24) + (bytes[5] << 16) + (bytes[6] << 8) + (bytes[7]);
//$interest_start = (int)(($bytes[4] << 24) . ($bytes[5] << 16) . ($bytes[6] << 8) . $bytes[7]);
//let interest_period = ieee754Float.fromBytes(bytes.slice(8, 16));
$interest_period = self::ieee754FloatFromBytes(\array_slice($bytes,8,8));
dd($interest_period);
//const year_seconds = 31536000; // By convention, the XRP Ledger's interest/demurrage rules use a fixed number of seconds per year (31536000), which is not adjusted for leap days or leap seconds
$year_seconds = 31536000;
//let interest_after_year = precision(Math.pow(Math.E, (interest_start+year_seconds - interest_start) / interest_period), 14);
$interest_after_year = $year_seconds / $interest_period;
dd($interest_start,$interest_after_year);
return 'DE '.$currencycode;
}

$r = \trim(\hex2bin($currencycode));
$r = preg_replace('/[\x00-\x1F\x7F]/', '', $r); //remove first 32 ascii characters and \x7F https://en.wikipedia.org/wiki/Control_character

if((bool)preg_match( '|[^\x20-\x7E]|', $r )) {
//binary eg: 80474F4C44000000000000000000000000000000
return preg_replace('/[[:^print:]]/', '', $r);
}

if(preg_match('//u', $r)) //This will will return 0 (with no additional information) if an invalid string is given.
return $r;
return $malformedUtf8ReturnString; //malformed UTF-8 string
}
return $currencycode;
}

/**
* IEEE 754 floating-point.
*
* Supports single- or double-precision
*/
public static function ieee754FloatFromBytes(array $bytes)
{
dd($bytes,pack('h', $bytes));
$data = unpack('f', pack('i', $bytes));
if($data === false)
throw new \Exception('Unable to extract ieee754FloatFromBytes');
return \array_values($data)[0];
}

}
32 changes: 32 additions & 0 deletions tests/Unit/UtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace XRPLWin\XRPL\Tests\Unit;

use PHPUnit\Framework\TestCase;
use XRPLWin\XRPL\Utilities\Util;

class XRPLParserUtilUtilTest extends TestCase
{
public function testConvertCurrencyToSymbolDemurrage()
{
$this->assertEquals('XAU (-0.5% pa)',Util::currencyToSymbol('0158415500000000C1F76FF6ECB0BAC600000000'));

}

public function testConvertCurrencyToSymbolISO()
{
$this->assertEquals('USD',Util::currencyToSymbol('USD'));
$this->assertEquals('EUR',Util::currencyToSymbol('EUR'));
$this->assertEquals('ABC',Util::currencyToSymbol('ABC'));
$this->assertEquals('000',Util::currencyToSymbol('000'));
$this->assertEquals('AB0',Util::currencyToSymbol('AB0'));
$this->assertEquals('123',Util::currencyToSymbol('123'));
}

public function testConvertCurrencyToSymbolLP()
{
$this->assertEquals('LP 03B20F3A7D26D33C6DA3503E5CCE3E67B102D4D2',Util::currencyToSymbol('03B20F3A7D26D33C6DA3503E5CCE3E67B102D4D2'));
}


}

0 comments on commit f972484

Please sign in to comment.