Skip to content

Commit

Permalink
Util currencyToSymbol
Browse files Browse the repository at this point in the history
  • Loading branch information
zgrguric committed Mar 28, 2024
1 parent f972484 commit ba28abe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
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="UtilTest.php">./tests/Unit</directory>
<!--<directory suffix="Test.php">./tests/Feature</directory>-->
<directory suffix="Test.php">./tests/Unit</directory>
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
</phpunit>
67 changes: 53 additions & 14 deletions src/Utilities/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,23 @@ public static function currencyToSymbol($currencycode, $malformedUtf8ReturnStrin

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);
if($interest_period === null || $interest_period === true) {
return $code.' (?% pa)';
}
//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;
$interest_after_year = \pow(\exp(1), $year_seconds / $interest_period);
$interest = ($interest_after_year*100) - 100;
return $code.' ('.$interest.'% pa)';
}

$r = \trim(\hex2bin($currencycode));
Expand All @@ -61,17 +60,57 @@ public static function currencyToSymbol($currencycode, $malformedUtf8ReturnStrin
}

/**
* IEEE 754 floating-point.
* IEEE754 floating-point.
*
* Supports single- or double-precision
*/
public static function ieee754FloatFromBytes(array $bytes)
public static function ieee754FloatFromBytes(array $bytes): float|null|bool
{
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];
$b = '';
$n = count($bytes);
for ($i = 0; $i < $n; $i++) {
$bits = base_convert((string)($bytes[$i] & 0xff),10,2);//.toString(2);
if(strlen($bits)<8) $bits = \str_pad($bits,8,'0',STR_PAD_LEFT);
$b = $b.$bits;
}

// Determine configuration. This could have all been precomputed but it is fast enough.
$exponentBits = count($bytes) === 4 ? 4 : 11;
$mantissaBits = (count($bytes) * 8) - $exponentBits - 1;
$bias = \pow(2,$exponentBits - 1) - 1;
$minExponent = 1 - $bias - $mantissaBits;
// Break up the binary representation into its pieces for easier processing.
$s = $b[0];
$e = \substr($b,1,$exponentBits);
$m = \substr($b,$exponentBits+1);
$value = 0;
$multiplier = ($s === '0' ? 1 : -1);

if(\preg_match('/^0+$/', $e)) { //regexp: allzeros
// Zero or denormalized
if(\preg_match('/^0+$/', $m)) { //regexp: allzeros
// Value is zero
} else {
$value = \intval($m, 2) * \pow(2, $minExponent);
}
} else if (\preg_match('/^1+$/', $e)) { //regexp: allones
// Infinity or NaN
if(\preg_match('/^0+$/', $m)) { //regexp: allones
$value = true; //Infinity
} else {
$value = null; //NaN
}
} else {
// Normalized
$exponent = \intval($e,2) - $bias;
$mantissa = \intval($m,2);
$value = (1+($mantissa * \pow(2,-$mantissaBits))) * \pow(2,$exponent);
}
if($value === false) return false;
if($value === null) return null;
return $value * $multiplier;


}

}
10 changes: 7 additions & 3 deletions tests/Unit/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class XRPLParserUtilUtilTest extends TestCase
public function testConvertCurrencyToSymbolDemurrage()
{
$this->assertEquals('XAU (-0.5% pa)',Util::currencyToSymbol('0158415500000000C1F76FF6ECB0BAC600000000'));

}

public function testConvertCurrencyToSymbolISO()
Expand All @@ -23,10 +22,15 @@ public function testConvertCurrencyToSymbolISO()
$this->assertEquals('123',Util::currencyToSymbol('123'));
}

public function testConvertCurrencyToSymbol()
{
$this->assertEquals('SOLO',Util::currencyToSymbol('534F4C4F00000000000000000000000000000000'));
$this->assertEquals('GOLD',Util::currencyToSymbol('80474F4C44000000000000000000000000000000')); //"GOLD" after cleanup
$this->assertEquals('ABCD',Util::currencyToSymbol('ABCD'));
}

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


}

0 comments on commit ba28abe

Please sign in to comment.