forked from alexbosworth/ln-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy_fee.js
43 lines (33 loc) · 956 Bytes
/
policy_fee.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const rateDivisor = 1e6;
/** Fee for policy
{
mtokens: <Millitokens String>
policy: {
base_fee_mtokens: <Base Fee Millitokens String>
fee_rate: <Fee Rate Number>
}
}
@throws
<Error>
@returns
{
fee_mtokens: <Fee Millitokens String>
}
*/
module.exports = ({mtokens, policy}) => {
if (mtokens === undefined) {
throw new Error('ExpectedMillitokensForPolicyFeeCalculation');
}
if (!policy.base_fee_mtokens) {
throw new Error('ExpectedBaseFeeMillitokensForPolicyFeeCalculation');
}
if (policy.fee_rate === undefined) {
throw new Error('ExpectedFeeRateForPolicyFeeCalculation');
}
const baseFeeMtokens = BigInt(policy.base_fee_mtokens);
const feeRate = BigInt(policy.fee_rate);
const forwardMtokens = BigInt(mtokens);
const feeRateMtokens = forwardMtokens * feeRate / BigInt(rateDivisor);
const fee = baseFeeMtokens + feeRateMtokens;
return {fee_mtokens: fee.toString()};
};