Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/Utils/UnitConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ArkEcosystem\Crypto\Utils;

use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode;
use InvalidArgumentException;

class UnitConverter
Expand All @@ -23,6 +24,7 @@ class UnitConverter
*
* @param float|int|string $value
* @param string $unit
*
* @return BigDecimal
*/
public static function parseUnits($value, string $unit = 'ark'): BigDecimal
Expand All @@ -44,17 +46,24 @@ public static function parseUnits($value, string $unit = 'ark'): BigDecimal
*
* @param string $value
* @param string $unit
* @return float
*
* @return BigDecimal
*/
public static function formatUnits(string $value, string $unit = 'ark'): float
public static function formatUnits(string $value, string $unit = 'ark'): BigDecimal
{
switch (strtolower($unit)) {
case 'wei':
return (float) bcdiv($value, (string) self::WEI_MULTIPLIER, 18);
return BigDecimal::of($value)
->dividedBy(self::WEI_MULTIPLIER, 0, RoundingMode::HALF_UP)
->stripTrailingZeros();
case 'gwei':
return (float) bcdiv($value, (string) self::GWEI_MULTIPLIER, 18);
return BigDecimal::of($value)
->dividedBy(self::GWEI_MULTIPLIER, 9, RoundingMode::HALF_UP)
->stripTrailingZeros();
case 'ark':
return (float) bcdiv($value, (string) self::ARK_MULTIPLIER, 18);
return BigDecimal::of($value)
->dividedBy(self::ARK_MULTIPLIER, 18, RoundingMode::HALF_UP)
->stripTrailingZeros();
default:
throw new InvalidArgumentException("Unsupported unit: {$unit}. Supported units are 'wei', 'gwei', and 'ark'.");
}
Expand Down
49 changes: 37 additions & 12 deletions tests/Unit/Utils/UnitConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,45 @@
expect((string) $arkValueDecimal)->toBe('100000000000000000');
});

test('it should format units from wei', function () {
$formattedValue = UnitConverter::formatUnits('1', 'wei');
expect($formattedValue)->toBe(1.0);
});
test('it should format units from wei', function ($formattedAmount, $amount) {
$formattedValue = UnitConverter::formatUnits($formattedAmount, 'wei');

test('it should format units from gwei', function () {
$formattedValue = UnitConverter::formatUnits('1000000000', 'gwei');
expect($formattedValue)->toBe(1.0);
});
expect((string) $formattedValue)->toEqual($amount);
})->with([
['1', '1'],
['10', '10'],
['100', '100'],
['1000', '1000'],
['10000', '10000'],
]);

test('it should format units from ark', function () {
$formattedValue = UnitConverter::formatUnits('1000000000000000000', 'ark');
expect($formattedValue)->toBe(1.0);
});
test('it should format units from gwei', function ($formattedAmount, $amount) {
$formattedValue = UnitConverter::formatUnits($formattedAmount, 'gwei');

expect((string) $formattedValue)->toEqual($amount);
})->with([
['100000001', '0.100000001'],
['100000000', '0.1'],
['1000000000', '1'],
['10000000000', '10'],
['100000000000', '100'],
['1000000000000', '1000'],
['10000000000000', '10000'],
]);

test('it should format units from ark', function ($formattedAmount, $amount) {
$formattedValue = UnitConverter::formatUnits($formattedAmount, 'ark');

expect((string) $formattedValue)->toEqual($amount);
})->with([
['100000000000000001', '0.100000000000000001'],
['100000000000000000', '0.1'],
['1000000000000000000', '1'],
['10000000000000000000', '10'],
['100000000000000000000', '100'],
['1000000000000000000000', '1000'],
['10000000000000000000000', '10000'],
]);

test('it should throw exception for unsupported unit in parse', function () {
expect(fn () => UnitConverter::parseUnits(1, 'unsupported'))
Expand Down
Loading