@did-pay/util 1.12.6 → 1.12.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/constant.js +7 -1
- package/lib/price.js +35 -0
- package/package.json +3 -2
package/lib/constant.js
CHANGED
package/lib/price.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const { BN, fromUnitToToken, fromTokenToUnit } = require('@ocap/util');
|
|
2
|
+
const { DURATION_UNIT_SYMBOL } = require('./constant');
|
|
3
|
+
|
|
4
|
+
const DAYS_OF_MONTH = 30; // 30 days per month
|
|
5
|
+
const DAYS_OF_YEAR = 365; // 12 month per year
|
|
6
|
+
|
|
7
|
+
const UNIT_MAP = {
|
|
8
|
+
[DURATION_UNIT_SYMBOL.d]: 1,
|
|
9
|
+
[DURATION_UNIT_SYMBOL.M]: DAYS_OF_MONTH,
|
|
10
|
+
[DURATION_UNIT_SYMBOL.Y]: DAYS_OF_YEAR,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
*
|
|
15
|
+
* @param {Number} unitPrice
|
|
16
|
+
* @param {(d|M|y)} unit
|
|
17
|
+
*/
|
|
18
|
+
const getPrice = (unitPrice, unit) => {
|
|
19
|
+
const days = UNIT_MAP[unit];
|
|
20
|
+
|
|
21
|
+
const unitPriceBN = new BN(fromTokenToUnit(unitPrice));
|
|
22
|
+
const unitBN = new BN(days);
|
|
23
|
+
|
|
24
|
+
return unitPriceBN.mul(unitBN);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const calculatePrice = (duration, unitPrice) => {
|
|
28
|
+
const priceBN = getPrice(unitPrice, duration.unit);
|
|
29
|
+
const durationBN = new BN(duration.value);
|
|
30
|
+
const total = priceBN.mul(durationBN).toString();
|
|
31
|
+
|
|
32
|
+
return Number(fromUnitToToken(total).toString());
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
module.exports = { calculatePrice };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@did-pay/util",
|
|
3
|
-
"version": "1.12.
|
|
3
|
+
"version": "1.12.8",
|
|
4
4
|
"description": "Common library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"util"
|
|
@@ -32,8 +32,9 @@
|
|
|
32
32
|
"url": "https://github.com/blocklet/payment-kit/issues"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
+
"@ocap/util": "^1.18.84",
|
|
35
36
|
"lodash": "^4.17.21",
|
|
36
37
|
"moment": "^2.29.4"
|
|
37
38
|
},
|
|
38
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "928e7611e12144b29a4ae0b1cba6e51c23b4996e"
|
|
39
40
|
}
|