You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.6 KiB
60 lines
1.6 KiB
9 months ago
|
# @woop-js/utils
|
||
4 years ago
|
|
||
|
This package provides a collection of utility apis for unit conversions like `fromWei`, `toWei`, `hexToNumber`, `numberToHex`, `isAddress`, etc.
|
||
|
|
||
|
## Installation
|
||
|
|
||
|
```
|
||
9 months ago
|
npm install @woop-js/utils
|
||
4 years ago
|
```
|
||
|
|
||
|
## Usage
|
||
|
|
||
|
Available units
|
||
|
```
|
||
9 months ago
|
const { Units } = require('@woop-js/utils');
|
||
4 years ago
|
|
||
|
[Units.wei, '1'], // 1 wei
|
||
|
[Units.Kwei, '1000'], // 1e3 wei
|
||
|
[Units.Mwei, '1000000'], // 1e6 wei
|
||
|
[Units.Gwei, '1000000000'], // 1e9 wei
|
||
|
[Units.szabo, '1000000000000'], // 1e12 wei
|
||
|
[Units.finney, '1000000000000000'], // 1e15 wei
|
||
|
[Units.ether, '1000000000000000000'], // 1e18 wei
|
||
|
[Units.one, '1000000000000000000'], // 1e18 wei
|
||
|
[Units.Kether, '1000000000000000000000'], // 1e21 wei
|
||
|
[Units.Mether, '1000000000000000000000000'], // 1e24 wei
|
||
|
[Units.Gether, '1000000000000000000000000000'], // 1e27 wei
|
||
|
[Units.Tether, '1000000000000000000000000000000'], // 1e30 wei
|
||
|
```
|
||
|
|
||
|
Converting between different units
|
||
|
```javascript
|
||
9 months ago
|
const { Units, Unit, numberToString, add0xToString, fromWei, toWei, numToStr} = require('@woop-js/utils');
|
||
|
const { BN } = require('@woop-js/crypto');
|
||
4 years ago
|
|
||
|
const one = new Unit('1').asOne();
|
||
|
const oneToGwei = one.toGwei();
|
||
|
console.log(oneToGwei);
|
||
|
|
||
|
// numberToString
|
||
|
const num = 123;
|
||
|
const str = numberToString(num)
|
||
|
console.log(str);
|
||
|
|
||
|
// add0xToString
|
||
|
const str = '12345';
|
||
|
const expected = add0xToString(str)
|
||
|
console.log(expected);
|
||
|
|
||
|
// fromWei
|
||
|
const Wei = new BN('1000000000000000000');
|
||
|
const expected = fromWei(Wei, Units.one);
|
||
|
console.log(expected);
|
||
|
|
||
|
// toWei
|
||
|
const one = new BN('1');
|
||
9 months ago
|
const expected = toWei(one, wiki.utils.Units.one);
|
||
4 years ago
|
const num = numToStr(expected);
|
||
|
console.log(num);
|
||
|
```
|