Remove support for int and float in toWei

This commit is contained in:
sc0Vu 2019-01-06 23:26:21 +08:00
parent 9dd5056790
commit 734dbebb76
No known key found for this signature in database
GPG Key ID: A53323E7065B1FA7
2 changed files with 21 additions and 52 deletions

View File

@ -266,12 +266,15 @@ class Utils
* $wei = Utils::toWei('1', 'kwei'); * $wei = Utils::toWei('1', 'kwei');
* $wei->toString(); // 1000 * $wei->toString(); // 1000
* *
* @param BigNumber|string|int $number * @param BigNumber|string $number
* @param string $unit * @param string $unit
* @return \phpseclib\Math\BigInteger * @return \phpseclib\Math\BigInteger
*/ */
public static function toWei($number, $unit) public static function toWei($number, $unit)
{ {
if (!is_string($number) && !($number instanceof BigNumber)) {
throw new InvalidArgumentException('toWei number must be string or bignumber.');
}
$bn = self::toBn($number); $bn = self::toBn($number);
if (!is_string($unit)) { if (!is_string($unit)) {

View File

@ -252,82 +252,58 @@ class UtilsTest extends TestCase
public function testToWei() public function testToWei()
{ {
$bn = Utils::toWei('0x1', 'wei'); $bn = Utils::toWei('0x1', 'wei');
$this->assertEquals($bn->toString(), '1'); $this->assertEquals('1', $bn->toString());
$bn = Utils::toWei('18', 'wei'); $bn = Utils::toWei('18', 'wei');
$this->assertEquals($bn->toString(), '18'); $this->assertEquals('18', $bn->toString());
$bn = Utils::toWei(1, 'wei');
$this->assertEquals($bn->toString(), '1');
$bn = Utils::toWei(0x11, 'wei');
$this->assertEquals($bn->toString(), '17');
$bn = Utils::toWei('1', 'ether'); $bn = Utils::toWei('1', 'ether');
$this->assertEquals($bn->toString(), '1000000000000000000'); $this->assertEquals('1000000000000000000', $bn->toString());
$bn = Utils::toWei('0x5218', 'wei'); $bn = Utils::toWei('0x5218', 'wei');
$this->assertEquals($bn->toString(), '21016'); $this->assertEquals('21016', $bn->toString());
$bn = Utils::toWei('0.000012', 'ether');
$this->assertEquals('12000000000000', $bn->toString());
$bn = Utils::toWei('0.1', 'ether'); $bn = Utils::toWei('0.1', 'ether');
$this->assertEquals($bn->toString(), '100000000000000000'); $this->assertEquals('100000000000000000', $bn->toString());
$bn = Utils::toWei('1.69', 'ether'); $bn = Utils::toWei('1.69', 'ether');
$this->assertEquals($bn->toString(), '1690000000000000000'); $this->assertEquals('1690000000000000000', $bn->toString());
$bn = Utils::toWei('0.01', 'ether'); $bn = Utils::toWei('0.01', 'ether');
$this->assertEquals($bn->toString(), '10000000000000000'); $this->assertEquals('10000000000000000', $bn->toString());
$bn = Utils::toWei('0.002', 'ether'); $bn = Utils::toWei('0.002', 'ether');
$this->assertEquals($bn->toString(), '2000000000000000'); $this->assertEquals('2000000000000000', $bn->toString());
$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'); $bn = Utils::toWei('-0.1', 'ether');
$this->assertEquals($bn->toString(), '-100000000000000000'); $this->assertEquals('-100000000000000000', $bn->toString());
$bn = Utils::toWei('-1.69', 'ether'); $bn = Utils::toWei('-1.69', 'ether');
$this->assertEquals($bn->toString(), '-1690000000000000000'); $this->assertEquals('-1690000000000000000', $bn->toString());
$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('', 'ether'); $bn = Utils::toWei('', 'ether');
$this->assertEquals($bn->toString(), '0'); $this->assertEquals('0', $bn->toString());
$bn = Utils::toWei(-1.697, 'kwei');
$this->assertEquals($bn->toString(), '-1697');
try { try {
$bn = Utils::toWei('0x5218', new stdClass); $bn = Utils::toWei('0x5218', new stdClass);
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null); $this->assertEquals('toWei unit must be string.', $e->getMessage());
} }
try { try {
$bn = Utils::toWei('0x5218', 'test'); $bn = Utils::toWei('0x5218', 'test');
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null); $this->assertEquals('toWei doesn\'t support test unit.', $e->getMessage());
} }
try { try {
// out of limit // out of limit
$bn = Utils::toWei(-1.6977, 'kwei'); $bn = Utils::toWei(-1.6977, 'kwei');
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null); $this->assertEquals('toWei number must be string or bignumber.', $e->getMessage());
} }
} }
@ -348,16 +324,6 @@ class UtilsTest extends TestCase
$this->assertEquals($bnq->toString(), '0'); $this->assertEquals($bnq->toString(), '0');
$this->assertEquals($bnr->toString(), '18'); $this->assertEquals($bnr->toString(), '18');
list($bnq, $bnr) = Utils::toEther(1, 'wei');
$this->assertEquals($bnq->toString(), '0');
$this->assertEquals($bnr->toString(), '1');
list($bnq, $bnr) = Utils::toEther(0x11, 'wei');
$this->assertEquals($bnq->toString(), '0');
$this->assertEquals($bnr->toString(), '17');
list($bnq, $bnr) = Utils::toEther('1', 'kether'); list($bnq, $bnr) = Utils::toEther('1', 'kether');
$this->assertEquals($bnq->toString(), '1000'); $this->assertEquals($bnq->toString(), '1000');