This commit is contained in:
sc0Vu 2017-12-24 23:42:46 +08:00
parent d58e2e751c
commit 46c252a44e
2 changed files with 52 additions and 1 deletions

View File

@ -163,7 +163,7 @@ class Utils
*
* @param BigNumber|string|int $number
* @param string $unit
* @return string
* @return \phpseclib\Math\BigInteger
*/
public static function toWei($number, $unit)
{
@ -187,4 +187,22 @@ class Utils
return $bn->multiply($bnt);
}
/**
* toEther
*
* @param BigNumber|string|int $number
* @param string $unit
* @return array
*/
public static function toEther($number, $unit)
{
if ($unit === 'ether') {
throw new InvalidArgumentException('Please use another unit.');
}
$wei = self::toWei($number, $unit);
$bnt = new BigNumber(self::UNITS['ether']);
return $wei->divide($bnt);
}
}

View File

@ -138,4 +138,37 @@ class UtilsTest extends TestCase
$this->assertEquals($bn->toString(), '21016');
}
/**
* testToEther
*
* @return void
*/
public function testToEther()
{
list($bnq, $bnr) = Utils::toEther('0x1', 'wei');
$this->assertEquals($bnq->toString(), '0');
$this->assertEquals($bnr->toString(), '1');
list($bnq, $bnr) = Utils::toEther('1', 'wei');
$this->assertEquals($bnq->toString(), '0');
$this->assertEquals($bnr->toString(), '1');
list($bnq, $bnr) = Utils::toEther(1, 'wei');
$this->assertEquals($bnq->toString(), '0');
$this->assertEquals($bnr->toString(), '1');
list($bnq, $bnr) = Utils::toEther('1', 'kether');
$this->assertEquals($bnq->toString(), '1000');
$this->assertEquals($bnr->toString(), '0');
list($bnq, $bnr) = Utils::toEther('0x5218', 'wei');
$this->assertEquals($bnq->toString(), '0');
$this->assertEquals($bnr->toString(), '21016');
}
}