From fbc85e74d06e534f7859103ab68e57d2b0a2bdd4 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Mon, 25 Dec 2017 10:10:17 +0800 Subject: [PATCH] Add fromWei in Utils. --- src/Utils.php | 44 +++++++++++++++++++++++++++++++++++++++++ test/unit/UtilsTest.php | 33 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/src/Utils.php b/src/Utils.php index 2627fcc..c039096 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -160,6 +160,10 @@ class Utils /** * toWei + * Change number from unit to wei. + * For example: + * $wei = Utils::toWei('1', 'kwei'); + * $wei->toString(); // 1000 * * @param BigNumber|string|int $number * @param string $unit @@ -192,6 +196,10 @@ class Utils /** * toEther + * Change number from unit to ether. + * For example: + * list($bnq, $bnr) = Utils::toEther('1', 'kether'); + * $bnq->toString(); // 1000 * * @param BigNumber|string|int $number * @param string $unit @@ -207,4 +215,40 @@ class Utils return $wei->divide($bnt); } + + /** + * fromWei + * Change number from wei to unit. + * For example: + * list($bnq, $bnr) = Utils::fromWei('1000', 'kwei'); + * $bnq->toString(); // 1 + * + * @param BigNumber|string|int $number + * @param string $unit + * @return \phpseclib\Math\BigInteger + */ + public static function fromWei($number, $unit) + { + if (is_int($number)) { + $bn = new BigNumber($number); + } elseif (is_string($number)) { + if (self::isZeroPrefixed($number)) { + $number = self::stripZero($number); + $bn = new BigNumber($number, 16); + } else { + $bn = new BigNumber($number); + } + } elseif (!$number instanceof BigNumber){ + throw new InvalidArgumentException('fromWei number must be BigNumber, string or int.'); + } + if (!is_string($unit)) { + throw new InvalidArgumentException('fromWei unit must be string.'); + } + if (!isset(self::UNITS[$unit])) { + throw new InvalidArgumentException('fromWei doesn\'t support ' . $unit . ' unit.'); + } + $bnt = new BigNumber(self::UNITS[$unit]); + + return $bn->divide($bnt); + } } \ No newline at end of file diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index 7e1ea6c..c79307b 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -180,4 +180,37 @@ class UtilsTest extends TestCase $this->assertEquals($bnq->toString(), '0'); $this->assertEquals($bnr->toString(), '21016'); } + + /** + * testFromWei + * + * @return void + */ + public function testFromWei() + { + list($bnq, $bnr) = Utils::fromWei('1000000000000000000', 'ether'); + + $this->assertEquals($bnq->toString(), '1'); + $this->assertEquals($bnr->toString(), '0'); + + list($bnq, $bnr) = Utils::fromWei('18', 'wei'); + + $this->assertEquals($bnq->toString(), '18'); + $this->assertEquals($bnr->toString(), '0'); + + list($bnq, $bnr) = Utils::fromWei(1, 'femtoether'); + + $this->assertEquals($bnq->toString(), '0'); + $this->assertEquals($bnr->toString(), '1'); + + list($bnq, $bnr) = Utils::fromWei(0x11, 'nano'); + + $this->assertEquals($bnq->toString(), '0'); + $this->assertEquals($bnr->toString(), '17'); + + list($bnq, $bnr) = Utils::fromWei('0x5218', 'kwei'); + + $this->assertEquals($bnq->toString(), '21'); + $this->assertEquals($bnr->toString(), '16'); + } } \ No newline at end of file