Fixed loadLastBlock

This commit is contained in:
Tornado Contrib 2024-11-20 06:02:43 +00:00
parent 0b5adaa9c3
commit 1f9889c8f0
Signed by: tornadocontrib
GPG Key ID: 60B4DF1A076C64B1
2 changed files with 11 additions and 11 deletions

16
lib/services/data.js vendored

@ -87,18 +87,18 @@ async function loadLastBlock({ name, directory }) {
}
async function loadSavedEvents({ name, userDirectory, }) {
const filePath = path_1.default.join(userDirectory, `${name}.json`.toLowerCase());
if (!(await existsAsync(filePath))) {
return {
events: [],
lastBlock: 0,
};
}
try {
const events = JSON.parse(await (0, promises_1.readFile)(filePath, { encoding: 'utf8' }));
const loadedBlock = await loadLastBlock({
name,
directory: userDirectory,
});
if (!(await existsAsync(filePath))) {
return {
events: [],
lastBlock: loadedBlock || 0,
};
}
try {
const events = JSON.parse(await (0, promises_1.readFile)(filePath, { encoding: 'utf8' }));
return {
events,
lastBlock: loadedBlock || events[events.length - 1]?.blockNumber || 0,

@ -114,21 +114,21 @@ export async function loadSavedEvents<T extends MinimalEvents>({
}): Promise<BaseEvents<T>> {
const filePath = path.join(userDirectory, `${name}.json`.toLowerCase());
const loadedBlock = await loadLastBlock({
name,
directory: userDirectory,
});
if (!(await existsAsync(filePath))) {
return {
events: [] as T[],
lastBlock: 0,
lastBlock: loadedBlock || 0,
};
}
try {
const events = JSON.parse(await readFile(filePath, { encoding: 'utf8' })) as T[];
const loadedBlock = await loadLastBlock({
name,
directory: userDirectory,
});
return {
events,
lastBlock: loadedBlock || events[events.length - 1]?.blockNumber || 0,