|
| 1 | +import { createReadStream, existsSync } from "fs"; |
| 2 | +import { join } from "path"; |
| 3 | + |
| 4 | +export type InitialBalanceRecord = { |
| 5 | + account: string; |
| 6 | + token: string; |
| 7 | + free: string; |
| 8 | + reserved: string; |
| 9 | + frozen: string; |
| 10 | +}; |
| 11 | + |
| 12 | +type Network = "acala" | "karura"; |
| 13 | + |
| 14 | +const DATA_FILES: Record<Network, string[]> = { |
| 15 | + acala: ["acala-1870000-native.json", "acala-1870000-non-native.json"], |
| 16 | + karura: ["karura-2650000-native.json", "karura-2650000-non-native.json"], |
| 17 | +}; |
| 18 | + |
| 19 | +const getWorkingDirectory = () => { |
| 20 | + const cwd = (globalThis as { process?: { cwd?: unknown } }).process?.cwd; |
| 21 | + |
| 22 | + return typeof cwd === "function" ? cwd() : "/app"; |
| 23 | +}; |
| 24 | + |
| 25 | +const getDataDirCandidates = () => { |
| 26 | + const cwd = getWorkingDirectory(); |
| 27 | + |
| 28 | + return [ |
| 29 | + join(cwd, "dist", "data"), |
| 30 | + join(cwd, "src", "data"), |
| 31 | + join(cwd, "data"), |
| 32 | + join(cwd, "packages", "chain-stats-subql", "dist", "data"), |
| 33 | + join(cwd, "packages", "chain-stats-subql", "src", "data"), |
| 34 | + "/app/dist/data", |
| 35 | + "/app/src/data", |
| 36 | + ]; |
| 37 | +}; |
| 38 | + |
| 39 | +type ReaderState = { |
| 40 | + nextIndex: number; |
| 41 | + iterator: AsyncIterator<InitialBalanceRecord>; |
| 42 | +}; |
| 43 | + |
| 44 | +const readers = new Map<Network, ReaderState>(); |
| 45 | + |
| 46 | +const getDataFilePath = (fileName: string) => { |
| 47 | + for (const directory of getDataDirCandidates()) { |
| 48 | + const filePath = join(directory, fileName); |
| 49 | + |
| 50 | + if (existsSync(filePath)) { |
| 51 | + return filePath; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + throw new Error(`Unable to locate chain-stats balance data file ${fileName}`); |
| 56 | +}; |
| 57 | + |
| 58 | +async function* readJsonArrayObjects(filePath: string): AsyncGenerator<InitialBalanceRecord> { |
| 59 | + let buffer = ""; |
| 60 | + let depth = 0; |
| 61 | + let inString = false; |
| 62 | + let escaped = false; |
| 63 | + let capturing = false; |
| 64 | + |
| 65 | + for await (const chunk of createReadStream(filePath, { encoding: "utf8" })) { |
| 66 | + for (const char of chunk) { |
| 67 | + if (!capturing) { |
| 68 | + if (char === "{") { |
| 69 | + capturing = true; |
| 70 | + depth = 1; |
| 71 | + buffer = char; |
| 72 | + } |
| 73 | + |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + buffer += char; |
| 78 | + |
| 79 | + if (escaped) { |
| 80 | + escaped = false; |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + if (char === "\\") { |
| 85 | + escaped = true; |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + if (char === "\"") { |
| 90 | + inString = !inString; |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + if (inString) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + if (char === "{") { |
| 99 | + depth += 1; |
| 100 | + } else if (char === "}") { |
| 101 | + depth -= 1; |
| 102 | + |
| 103 | + if (depth === 0) { |
| 104 | + yield JSON.parse(buffer) as InitialBalanceRecord; |
| 105 | + buffer = ""; |
| 106 | + capturing = false; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +async function* readNetworkRecords(network: Network): AsyncGenerator<InitialBalanceRecord> { |
| 114 | + for (const fileName of DATA_FILES[network]) { |
| 115 | + yield* readJsonArrayObjects(getDataFilePath(fileName)); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +const getReader = (network: Network, startIndex: number) => { |
| 120 | + const current = readers.get(network); |
| 121 | + |
| 122 | + if (current && startIndex >= current.nextIndex) { |
| 123 | + return current; |
| 124 | + } |
| 125 | + |
| 126 | + const next = { |
| 127 | + nextIndex: 0, |
| 128 | + iterator: readNetworkRecords(network)[Symbol.asyncIterator](), |
| 129 | + }; |
| 130 | + |
| 131 | + readers.set(network, next); |
| 132 | + return next; |
| 133 | +}; |
| 134 | + |
| 135 | +export const getInitialBalanceRecords = async ( |
| 136 | + network: Network, |
| 137 | + startIndex: number, |
| 138 | + limit: number, |
| 139 | +) => { |
| 140 | + const reader = getReader(network, startIndex); |
| 141 | + |
| 142 | + while (reader.nextIndex < startIndex) { |
| 143 | + const skipped = await reader.iterator.next(); |
| 144 | + |
| 145 | + if (skipped.done) { |
| 146 | + return []; |
| 147 | + } |
| 148 | + |
| 149 | + reader.nextIndex += 1; |
| 150 | + } |
| 151 | + |
| 152 | + const records: InitialBalanceRecord[] = []; |
| 153 | + |
| 154 | + while (records.length < limit) { |
| 155 | + const next = await reader.iterator.next(); |
| 156 | + |
| 157 | + if (next.done) { |
| 158 | + break; |
| 159 | + } |
| 160 | + |
| 161 | + records.push(next.value); |
| 162 | + reader.nextIndex += 1; |
| 163 | + } |
| 164 | + |
| 165 | + return records; |
| 166 | +}; |
| 167 | + |
| 168 | +export const resetInitialBalanceReadersForTests = () => { |
| 169 | + readers.clear(); |
| 170 | +}; |
0 commit comments