forked from CirclesUBI/circles-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.js
45 lines (39 loc) · 1.18 KB
/
math.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
const { BigNumber, decimals } = require('./constants');
const decimalsMultiplier = (new BigNumber(10)).pow(decimals);
const convertToBaseUnit = number => (new BigNumber(number)).mul(decimalsMultiplier);
const bn = number => new BigNumber(number);
const inflate = (init, inf, div, periods) => {
const q = inf.pow(bn(periods));
const d = div.pow(bn(periods));
return (init.mul(q)).div(d);
};
const near = (num, goal, onePayout) => {
return num.eq(goal) || num.eq(goal.sub(onePayout)) || num.eq(goal.add(onePayout));
};
const periodsWhenLastTouched = (clock, hubDeployedAt, period) => {
return clock.sub(hubDeployedAt).div(period);
};
const ubiPayout = (rate, clock, time, offset, inf, div, period, hubDeployedAt) => {
let payout = bn(0);
let c = clock;
let o = offset;
let r = rate;
let p = periodsWhenLastTouched(c, hubDeployedAt, period);
while (c.add(o).lte(bn(time))) {
payout = payout.add(o.mul(r));
c = c.add(o);
o = period;
p = p.add(bn(1));
r = inflate(rate, inf, div, p);
}
const timePassed = bn(time).sub(c);
payout = payout.add(timePassed.mul(r));
return payout;
};
module.exports = {
convertToBaseUnit,
inflate,
bn,
ubiPayout,
near,
};