-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
53 lines (43 loc) · 1.34 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
const KOMODO_ENDOFERA = 7777777;
const LOCKTIME_THRESHOLD = 500000000;
const MIN_SATOSHIS = 1000000000;
const ONE_MONTH_CAP_HARDFORK = 1000000;
const ONE_HOUR = 60;
const ONE_MONTH = 31 * 24 * 60;
const ONE_YEAR = 365 * 24 * 60;
const DEVISOR = 10512000;
const getKomodoRewards = utxo => {
// Validate types
['tiptime', 'locktime', 'height', 'satoshis'].forEach(property => {
if (typeof utxo[property] !== 'number') {
throw new TypeError(`\`${property}\` option must be a number.`);
}
});
// Destructure UTXO properties
const {tiptime, locktime, height, satoshis} = utxo;
// Calculate coinage
const coinage = Math.floor((tiptime - locktime) / ONE_HOUR);
// Return early if UTXO is not eligible for rewards
if (
(height >= KOMODO_ENDOFERA) ||
(locktime < LOCKTIME_THRESHOLD) ||
(satoshis < MIN_SATOSHIS) ||
(coinage < ONE_HOUR) ||
(!height)
) {
return 0;
}
// Cap reward periods
const limit = (height >= ONE_MONTH_CAP_HARDFORK) ? ONE_MONTH : ONE_YEAR;
let rewardPeriod = Math.min(coinage, limit);
// The first hour of coinage should not accrue rewards
rewardPeriod -= 59;
// Calculate rewards
const rewards = Math.floor(satoshis / DEVISOR) * rewardPeriod;
// Ensure reward value is never negative
if (rewards < 0) {
throw new Error('Reward should never be negative');
}
return rewards;
};
module.exports = getKomodoRewards;