UNPKG

1.67 kBJavaScriptView Raw
1const format = require('./util/format');
2
3/**
4 * Positive decimal integer string in `Drip`
5 */
6class Drip extends String {
7 /**
8 * Get `Drip` string from `CFX`
9 *
10 * @param value {string|number}
11 * @return {Drip}
12 *
13 * @example
14 * > Drip.fromCFX(3.14)
15 [String (Drip): '3140000000000000000']
16 * > Drip.fromCFX('0xab')
17 [String (Drip): '171000000000000000000']
18 */
19 static fromCFX(value) {
20 return new this(format.big(value).times(1e18).toFixed());
21 }
22
23 /**
24 * Get `Drip` string from `GDrip`
25 *
26 * @param value {string|number}
27 * @return {Drip}
28 *
29 * @example
30 * > Drip.fromGDrip(3.14)
31 [String (Drip): '3140000000']
32 * > Drip.fromGDrip('0xab')
33 [String (Drip): '171000000000']
34 */
35 static fromGDrip(value) {
36 return new this(format.big(value).times(1e9).toFixed());
37 }
38
39 /**
40 * @param value {number|string}
41 * @return {Drip}
42 *
43 * @example
44 * > Drip(1.00)
45 [String (Drip): '1']
46 * > Drip('0xab')
47 [String (Drip): '171']
48 */
49 constructor(value) {
50 super(format.bigUInt(value).toString(10));
51 }
52
53 /**
54 * Get `CFX` number string
55 * @return {string}
56 *
57 * @example
58 * > Drip.fromDrip(1e9).toCFX()
59 "0.000000001"
60 */
61 toCFX() {
62 return format.big(this).div(1e18).toFixed();
63 }
64
65 /**
66 * Get `GDrip` number string
67 * @return {string}
68 *
69 * @example
70 * > Drip.fromDrip(1e9).toGDrip()
71 "1"
72 */
73 toGDrip() {
74 return format.big(this).div(1e9).toFixed();
75 }
76}
77
78module.exports = new Proxy(Drip, {
79 apply(target, thisArg, argArray) {
80 return new Drip(...argArray);
81 },
82});