toEther
This commit is contained in:
parent
d58e2e751c
commit
46c252a44e
@ -163,7 +163,7 @@ class Utils
|
|||||||
*
|
*
|
||||||
* @param BigNumber|string|int $number
|
* @param BigNumber|string|int $number
|
||||||
* @param string $unit
|
* @param string $unit
|
||||||
* @return string
|
* @return \phpseclib\Math\BigInteger
|
||||||
*/
|
*/
|
||||||
public static function toWei($number, $unit)
|
public static function toWei($number, $unit)
|
||||||
{
|
{
|
||||||
@ -187,4 +187,22 @@ class Utils
|
|||||||
|
|
||||||
return $bn->multiply($bnt);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -138,4 +138,37 @@ class UtilsTest extends TestCase
|
|||||||
|
|
||||||
$this->assertEquals($bn->toString(), '21016');
|
$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');
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user