From d58e2e751c1c0c039dd14292fb601da5671d4d5d Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 24 Dec 2017 20:35:28 +0800 Subject: [PATCH] Add toWei and bignumber --- composer.json | 3 +- src/Utils.php | 69 ++++++++++++++++++++++++++++++++++++++++- test/unit/UtilsTest.php | 28 +++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 761c23e..ffa25e2 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "require": { "guzzlehttp/guzzle": "~6.0", "PHP": "^7.1", - "kornrunner/keccak": "dev-master" + "kornrunner/keccak": "dev-master", + "phpseclib/phpseclib": "dev-master" }, "require-dev": { "phpunit/phpunit": "~6.0" diff --git a/src/Utils.php b/src/Utils.php index ebfa200..5320fcc 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -13,6 +13,7 @@ namespace Web3; use InvalidArgumentException; use kornrunner\Keccak; +use phpseclib\Math\BigInteger as BigNumber; class Utils { @@ -23,6 +24,42 @@ class Utils */ const SHA3_NULL_HASH = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'; + /** + * UNITS + * from web3.js + * + * @const array + */ + const UNITS = [ + 'noether' => '0', + 'wei' => '1', + 'kwei' => '1000', + 'Kwei' => '1000', + 'babbage' => '1000', + 'femtoether' => '1000', + 'mwei' => '1000000', + 'Mwei' => '1000000', + 'lovelace' => '1000000', + 'picoether' => '1000000', + 'gwei' => '1000000000', + 'Gwei' => '1000000000', + 'shannon' => '1000000000', + 'nanoether' => '1000000000', + 'nano' => '1000000000', + 'szabo' => '1000000000000', + 'microether' => '1000000000000', + 'micro' => '1000000000000', + 'finney' => '1000000000000000', + 'milliether' => '1000000000000000', + 'milli' => '1000000000000000', + 'ether' => '1000000000000000000', + 'kether' => '1000000000000000000000', + 'grand' => '1000000000000000000000', + 'mether' => '1000000000000000000000000', + 'gether' => '1000000000000000000000000000', + 'tether' => '1000000000000000000000000000000' + ]; + /** * construct * @@ -119,5 +156,35 @@ class Utils return null; } return '0x' . $hash; - } + } + + /** + * toWei + * + * @param BigNumber|string|int $number + * @param string $unit + * @return string + */ + public static function toWei($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); + } elseif (!$number instanceof BigNumber){ + throw new InvalidArgumentException('toWei number must be BigNumber, string or int.'); + } + if (!is_string($unit)) { + throw new InvalidArgumentException('toWei unit must be string.'); + } + if (!isset(self::UNITS[$unit])) { + throw new InvalidArgumentException('toWei doesn\'t support ' . $unit . ' unit.'); + } + $bnt = new BigNumber(self::UNITS[$unit]); + + return $bn->multiply($bnt); + } } \ No newline at end of file diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index 96761ca..01e72ff 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -110,4 +110,32 @@ class UtilsTest extends TestCase $this->assertEquals(mb_substr($str, 0, 10), '0xcdcd77c0'); } + + /** + * testToWei + * + * @return void + */ + public function testToWei() + { + $bn = Utils::toWei('0x1', 'wei'); + + $this->assertEquals($bn->toString(), '1'); + + $bn = Utils::toWei('1', 'wei'); + + $this->assertEquals($bn->toString(), '1'); + + $bn = Utils::toWei(1, 'wei'); + + $this->assertEquals($bn->toString(), '1'); + + $bn = Utils::toWei('1', 'ether'); + + $this->assertEquals($bn->toString(), '1000000000000000000'); + + $bn = Utils::toWei('0x5218', 'wei'); + + $this->assertEquals($bn->toString(), '21016'); + } } \ No newline at end of file