|
1 | | -import { base58 } from '@scure/base'; |
| 1 | +import { base58 } from "@scure/base"; |
2 | 2 | import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"; |
3 | 3 | import path from "path/posix"; |
4 | 4 | import { bigIntReplacer, bigIntReviver } from "../../../../ftso-core/src/utils/big-number-serialization"; |
5 | 5 | import { CALCULATIONS_FOLDER, STAKING_DATA_FOLDER } from "../../constants"; |
6 | | -import { MINIMAL_CONDITIONS_FILE, PASSES_FILE } from '../../utils/stat-info/constants'; |
7 | | -import { DataProviderConditions, DataProviderPasses, ListedProviderList, ValidatorInfo } from "./minimal-conditions-interfaces"; |
| 6 | +import { MINIMAL_CONDITIONS_FILE, PASSES_FILE } from "../../utils/stat-info/constants"; |
| 7 | +import { |
| 8 | + DataProviderConditions, |
| 9 | + DataProviderPasses, |
| 10 | + ListedProviderList, |
| 11 | + ValidatorInfo, |
| 12 | +} from "./minimal-conditions-interfaces"; |
8 | 13 |
|
9 | 14 | /** |
10 | | - * Reads the staking info for a given reward epoch id. |
11 | | - * The data is stored in the staking data folder. |
| 15 | + * Reads the staking info for a given reward epoch id. |
| 16 | + * The data is stored in the staking data folder. |
12 | 17 | */ |
13 | | -export function readStakingInfo( |
14 | | - rewardEpochId: number, |
15 | | - stakingDataFolder = STAKING_DATA_FOLDER() |
16 | | -): ValidatorInfo[] { |
17 | | - const fname = path.join(stakingDataFolder, `${rewardEpochId}-nodes-data.json`); |
18 | | - const data = readFileSync(fname, 'utf8'); |
19 | | - const result: ValidatorInfo[] = JSON.parse(data, bigIntReviver); |
20 | | - for(let validatorInfo of result) { |
21 | | - // "NodeID-2a7BPY7UeJv2njMuyUHfBSTeQCYZj6bwV" |
22 | | - // Checksum is not validated |
23 | | - validatorInfo.nodeId20Byte = "0x" + Buffer.from(base58.decode(validatorInfo.nodeId.slice(7)).subarray(0, -4)).toString("hex"); |
24 | | - } |
25 | | - return result; |
| 18 | +export function readStakingInfo(rewardEpochId: number, stakingDataFolder = STAKING_DATA_FOLDER()): ValidatorInfo[] { |
| 19 | + const fname = path.join(stakingDataFolder, `${rewardEpochId}-nodes-data.json`); |
| 20 | + const data = readFileSync(fname, "utf8"); |
| 21 | + const result: ValidatorInfo[] = JSON.parse(data, bigIntReviver); |
| 22 | + for (const validatorInfo of result) { |
| 23 | + // "NodeID-2a7BPY7UeJv2njMuyUHfBSTeQCYZj6bwV" |
| 24 | + // Checksum is not validated |
| 25 | + validatorInfo.nodeId20Byte = |
| 26 | + "0x" + Buffer.from(base58.decode(validatorInfo.nodeId.slice(7)).subarray(0, -4)).toString("hex"); |
| 27 | + } |
| 28 | + return result; |
26 | 29 | } |
27 | 30 |
|
28 | 31 | /** |
29 | 32 | * Reads the passes info for a given reward epoch id. |
30 | 33 | * The data is stored in the passes data folder. |
31 | 34 | */ |
32 | | -export function readPassesInfo( |
33 | | - rewardEpochId: number, |
34 | | - calculationFolder = CALCULATIONS_FOLDER() |
35 | | -): DataProviderPasses[] { |
36 | | - const fname = path.join(calculationFolder, `${rewardEpochId}`, PASSES_FILE); |
37 | | - if(!existsSync(fname)) { |
38 | | - throw new Error(`Passes file not found: ${fname}`); |
39 | | - } |
40 | | - const data = readFileSync(fname, 'utf8'); |
41 | | - return JSON.parse(data) as DataProviderPasses[]; |
| 35 | +export function readPassesInfo(rewardEpochId: number, calculationFolder = CALCULATIONS_FOLDER()): DataProviderPasses[] { |
| 36 | + const fname = path.join(calculationFolder, `${rewardEpochId}`, PASSES_FILE); |
| 37 | + if (!existsSync(fname)) { |
| 38 | + throw new Error(`Passes file not found: ${fname}`); |
| 39 | + } |
| 40 | + const data = readFileSync(fname, "utf8"); |
| 41 | + return JSON.parse(data) as DataProviderPasses[]; |
42 | 42 | } |
43 | 43 |
|
44 | 44 | /** |
45 | 45 | * Writes the staking info for a given reward epoch id. |
46 | 46 | */ |
47 | 47 | export function writePassesInfo( |
48 | | - rewardEpochId: number, |
49 | | - data: DataProviderPasses[], |
50 | | - calculationFolder = CALCULATIONS_FOLDER() |
51 | | -): void { |
52 | | - if (!existsSync(calculationFolder)) { |
53 | | - mkdirSync(calculationFolder, { recursive: true }); |
54 | | - } |
55 | | - const fname = path.join(calculationFolder, `${rewardEpochId}`, PASSES_FILE); |
56 | | - writeFileSync(fname, JSON.stringify(data, null, 2)); |
| 48 | + rewardEpochId: number, |
| 49 | + data: DataProviderPasses[], |
| 50 | + calculationFolder = CALCULATIONS_FOLDER() |
| 51 | +): void { |
| 52 | + if (!existsSync(calculationFolder)) { |
| 53 | + mkdirSync(calculationFolder, { recursive: true }); |
| 54 | + } |
| 55 | + const fname = path.join(calculationFolder, `${rewardEpochId}`, PASSES_FILE); |
| 56 | + writeFileSync(fname, JSON.stringify(data, null, 2)); |
57 | 57 | } |
58 | 58 |
|
59 | 59 | /** |
60 | 60 | * Writes the staking info for a given reward epoch id. |
61 | 61 | */ |
62 | 62 | export function writeDataProviderConditions( |
63 | | - rewardEpochId: number, |
64 | | - data: DataProviderConditions[], |
65 | | - calculationFolder = CALCULATIONS_FOLDER() |
| 63 | + rewardEpochId: number, |
| 64 | + data: DataProviderConditions[], |
| 65 | + calculationFolder = CALCULATIONS_FOLDER() |
66 | 66 | ): void { |
67 | | - if (!existsSync(calculationFolder)) { |
68 | | - mkdirSync(calculationFolder, { recursive: true }); |
69 | | - } |
70 | | - const fname = path.join(calculationFolder, `${rewardEpochId}`, MINIMAL_CONDITIONS_FILE); |
71 | | - writeFileSync(fname, JSON.stringify(data, bigIntReplacer, 2)); |
| 67 | + if (!existsSync(calculationFolder)) { |
| 68 | + mkdirSync(calculationFolder, { recursive: true }); |
| 69 | + } |
| 70 | + const fname = path.join(calculationFolder, `${rewardEpochId}`, MINIMAL_CONDITIONS_FILE); |
| 71 | + writeFileSync(fname, JSON.stringify(data, bigIntReplacer, 2)); |
72 | 72 | } |
73 | 73 |
|
74 | 74 | export function readListedDataProviders(): ListedProviderList { |
75 | | - const fname = path.join(`listed-data-providers`, `bifrost-wallet.providerlist.json`); |
76 | | - const data = readFileSync(fname, 'utf8'); |
77 | | - return JSON.parse(data); |
| 75 | + const fname = path.join(`listed-data-providers`, `bifrost-wallet.providerlist.json`); |
| 76 | + const data = readFileSync(fname, "utf8"); |
| 77 | + return JSON.parse(data); |
78 | 78 | } |
0 commit comments