This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathendpoint.spec.ts
128 lines (117 loc) · 3.82 KB
/
endpoint.spec.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright © 2021 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import { RewardModule } from '../../../../src/modules/reward';
import { createTransientModuleEndpointContext } from '../../../../src/testing';
describe('RewardModuleEndpoint', () => {
const genesisConfig: any = { blockTime: 7 };
const moduleConfig: any = {
distance: 3000000,
offset: 2160,
brackets: [
'500000000', // Initial Reward
'400000000', // Milestone 1
'300000000', // Milestone 2
'200000000', // Milestone 3
'100000000', // Milestone 4
],
tokenID: '0000000000000000',
};
let rewardModule: RewardModule;
beforeAll(async () => {
rewardModule = new RewardModule();
await rewardModule.init({ genesisConfig, moduleConfig });
rewardModule.addDependencies(
{ mint: jest.fn() } as any,
{ isSeedRevealValid: jest.fn() } as any,
);
});
const { brackets, offset, distance } = moduleConfig as {
brackets: ReadonlyArray<string>;
offset: number;
distance: number;
};
for (const [index, rewardFromConfigString] of Object.entries(brackets)) {
const nthBracket = +index;
const currentHeight = offset + nthBracket * distance;
// eslint-disable-next-line no-loop-func
it(`should getDefaultRewardAtHeight work for the ${nthBracket}th bracket`, () => {
const rewardFromEndpoint = rewardModule.endpoint.getDefaultRewardAtHeight(
createTransientModuleEndpointContext({
params: {
height: currentHeight,
},
}),
);
expect(rewardFromEndpoint).toEqual({ reward: rewardFromConfigString });
});
// eslint-disable-next-line no-loop-func
it(`should getAnnualInflation for the ${nthBracket}th bracket`, () => {
const blocksPerYear = BigInt(Math.floor((365 * 24 * 60 * 60) / genesisConfig.blockTime));
const rate = blocksPerYear * BigInt(rewardFromConfigString);
const inflationRate = rewardModule.endpoint.getAnnualInflation(
createTransientModuleEndpointContext({
params: {
height: currentHeight,
},
}),
);
expect(inflationRate).toEqual({ tokenID: moduleConfig.tokenID, rate: rate.toString() });
});
}
it('should getDefaultRewardAtHeight work for the height below offset', () => {
const rewardFromEndpoint = rewardModule.endpoint.getDefaultRewardAtHeight(
createTransientModuleEndpointContext({
params: {
height: offset - 1,
},
}),
);
expect(rewardFromEndpoint).toEqual({ reward: '0' });
});
it('should getAnnualInflation for the for the height below offset', () => {
const inflationRate = rewardModule.endpoint.getAnnualInflation(
createTransientModuleEndpointContext({
params: {
height: offset - 1,
},
}),
);
expect(inflationRate).toEqual({ tokenID: moduleConfig.tokenID, rate: '0' });
});
it('should throw an error when parameter height is not a number', () => {
expect(() =>
rewardModule.endpoint.getDefaultRewardAtHeight(
createTransientModuleEndpointContext({
params: {
height: 'Not a number',
},
}),
),
).toThrow('Parameter height must be a number.');
});
it('should throw an error when parameter height is below 0', () => {
expect(() =>
rewardModule.endpoint.getDefaultRewardAtHeight(
createTransientModuleEndpointContext({
params: {
height: -1,
},
}),
),
).toThrow('Parameter height cannot be smaller than 0.');
});
it('should return reward token ID', () => {
expect(rewardModule.endpoint.getRewardTokenID()).toEqual({ tokenID: moduleConfig.tokenID });
});
});