diff --git a/lib/dirExists.mjs b/lib/dirExists.mjs new file mode 100644 index 0000000..ded8dca --- /dev/null +++ b/lib/dirExists.mjs @@ -0,0 +1,7 @@ +import { stat } from 'fs' + +const dirExists = async storeDir => await new Promise(resolve => stat(storeDir, staterr => resolve(!staterr))) + +export { + dirExists, +} diff --git a/lib/onLedger.mjs b/lib/onLedger.mjs index 507fb76..7381133 100644 --- a/lib/onLedger.mjs +++ b/lib/onLedger.mjs @@ -1,7 +1,7 @@ import { writeFile } from 'fs' -import { stat } from 'fs' import { ledgerIndexToFolders } from './ledgerIndexToFolders.mjs' import { computeBinaryTransactionHash } from './computeBinaryTransactionHash.mjs' +import { dirExists } from './dirExists.mjs' import 'dotenv/config' const obtainedHumanReadableLedgers = [] @@ -17,9 +17,7 @@ const onLedger = async ({ const relativeStoreDir = 'store/' + networkId + '/' + ledgerIndexToFolders(ledger.ledger_index) const storeDir = new URL('../' + relativeStoreDir, import.meta.url).pathname - const dirExists = await new Promise(resolve => stat(storeDir, staterr => resolve(!staterr))) - - if (dirExists) { + if (await dirExists(storeDir)) { ;[ ...( obtainedBinaryTxLedgers.indexOf(ledger.ledger_index) < 0 @@ -66,7 +64,7 @@ const onLedger = async ({ /** * Merge transaction hashes with the raw tx data */ - results.ledger.transactions.map(t => { + ;(results?.ledger?.transactions || []).map(t => { return Object.assign(t, { tx_id: computeBinaryTransactionHash(t.tx_blob), }) }) @@ -79,6 +77,7 @@ const onLedger = async ({ if ( results?.ledger?.parent_hash && obtainedHumanReadableLedgers.indexOf(ledger.ledger_index) < 0 + && results?.ledger?.ledger_hash === ledger?.ledger_hash ) { /** * See comment above "Store in array" ... - first one will be stored