Skip to content

Commit

Permalink
Merge pull request #86 from sc0Vu/fix-83
Browse files Browse the repository at this point in the history
Fix #83
  • Loading branch information
sc0Vu authored May 27, 2018
2 parents 4eaebd4 + 4fd5d1a commit 5d04cec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,7 @@ public static function toWei($number, $unit)

if (is_array($bn)) {
// fraction number
list($whole, $fraction, $negative1) = $bn;

$fractionLength = strlen($fraction->toString());
list($whole, $fraction, $fractionLength, $negative1) = $bn;

if ($fractionLength > strlen(self::UNITS[$unit])) {
throw new InvalidArgumentException('toWei fraction part is out of limit.');
Expand Down Expand Up @@ -504,6 +502,7 @@ public static function toBn($number)
return [
new BigNumber($whole),
new BigNumber($fraction),
strlen($comps[1]),
isset($negative1) ? $negative1 : false
];
} else {
Expand Down
40 changes: 29 additions & 11 deletions test/unit/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,24 @@ public function testToWei()
$bn = Utils::toWei('1.69', 'ether');
$this->assertEquals($bn->toString(), '1690000000000000000');

$bn = Utils::toWei('0.01', 'ether');
$this->assertEquals($bn->toString(), '10000000000000000');

$bn = Utils::toWei('0.002', 'ether');
$this->assertEquals($bn->toString(), '2000000000000000');

$bn = Utils::toWei(0.1, 'ether');
$this->assertEquals($bn->toString(), '100000000000000000');

$bn = Utils::toWei(1.69, 'ether');
$this->assertEquals($bn->toString(), '1690000000000000000');

$bn = Utils::toWei(0.01, 'ether');
$this->assertEquals($bn->toString(), '10000000000000000');

$bn = Utils::toWei(0.002, 'ether');
$this->assertEquals($bn->toString(), '2000000000000000');

$bn = Utils::toWei('-0.1', 'ether');
$this->assertEquals($bn->toString(), '-100000000000000000');

Expand Down Expand Up @@ -541,39 +553,45 @@ public function testToBn()
$this->assertEquals($bn->toString(), '-1');

$bn = Utils::toBn('-0.1');
$this->assertEquals(count($bn), 3);
$this->assertEquals(count($bn), 4);
$this->assertEquals($bn[0]->toString(), '0');
$this->assertEquals($bn[1]->toString(), '1');
$this->assertEquals($bn[2]->toString(), '-1');
$this->assertEquals($bn[2], 1);
$this->assertEquals($bn[3]->toString(), '-1');

$bn = Utils::toBn(-0.1);
$this->assertEquals(count($bn), 3);
$this->assertEquals(count($bn), 4);
$this->assertEquals($bn[0]->toString(), '0');
$this->assertEquals($bn[1]->toString(), '1');
$this->assertEquals($bn[2]->toString(), '-1');
$this->assertEquals($bn[2], 1);
$this->assertEquals($bn[3]->toString(), '-1');

$bn = Utils::toBn('0.1');
$this->assertEquals(count($bn), 3);
$this->assertEquals(count($bn), 4);
$this->assertEquals($bn[0]->toString(), '0');
$this->assertEquals($bn[1]->toString(), '1');
$this->assertEquals($bn[2], false);
$this->assertEquals($bn[2], 1);
$this->assertEquals($bn[3], false);

$bn = Utils::toBn('-1.69');
$this->assertEquals(count($bn), 3);
$this->assertEquals(count($bn), 4);
$this->assertEquals($bn[0]->toString(), '1');
$this->assertEquals($bn[1]->toString(), '69');
$this->assertEquals($bn[2]->toString(), '-1');
$this->assertEquals($bn[2], 2);
$this->assertEquals($bn[3]->toString(), '-1');

$bn = Utils::toBn(-1.69);
$this->assertEquals($bn[0]->toString(), '1');
$this->assertEquals($bn[1]->toString(), '69');
$this->assertEquals($bn[2]->toString(), '-1');
$this->assertEquals($bn[2], 2);
$this->assertEquals($bn[3]->toString(), '-1');

$bn = Utils::toBn('1.69');
$this->assertEquals(count($bn), 3);
$this->assertEquals(count($bn), 4);
$this->assertEquals($bn[0]->toString(), '1');
$this->assertEquals($bn[1]->toString(), '69');
$this->assertEquals($bn[2], false);
$this->assertEquals($bn[2], 2);
$this->assertEquals($bn[3], false);

$bn = Utils::toBn(new BigNumber(1));
$this->assertEquals($bn->toString(), '1');
Expand Down

0 comments on commit 5d04cec

Please sign in to comment.