mirror of
https://github.com/Xahau/Validation-Ledger-Tx-Store-to-xPOP.git
synced 2025-11-18 10:45:50 +00:00
Store eligible transactions
This commit is contained in:
18
index.mjs
18
index.mjs
@@ -2,7 +2,7 @@ import { XrplClient } from 'xrpl-client'
|
||||
import { createDirectory } from './lib/createDirectory.mjs'
|
||||
import { onValidation } from './lib/onValidation.mjs'
|
||||
import { onLedger } from './lib/onLedger.mjs'
|
||||
// import { onTransaction } from './lib/onTransaction.mjs'
|
||||
import { onTransaction } from './lib/onTransaction.mjs'
|
||||
import 'dotenv/config'
|
||||
import assert from 'assert'
|
||||
|
||||
@@ -23,8 +23,8 @@ process.env.NODES.split(',').map(h => h.trim())
|
||||
c.send({ command: "subscribe", streams: [
|
||||
"validations",
|
||||
"ledger",
|
||||
// "transactions",
|
||||
// "transactions_proposed"
|
||||
"transactions",
|
||||
"transactions_proposed"
|
||||
] })
|
||||
|
||||
c.on("validation", validation => onValidation({
|
||||
@@ -40,10 +40,10 @@ process.env.NODES.split(',').map(h => h.trim())
|
||||
connection: c,
|
||||
}))
|
||||
|
||||
// c.on("transaction", transaction => onTransaction({
|
||||
// connectionUrl: c.getState()?.server?.uri,
|
||||
// networkId: c.getState()?.server?.networkId,
|
||||
// transaction,
|
||||
// connection: c,
|
||||
// }))
|
||||
c.on("transaction", transaction => onTransaction({
|
||||
connectionUrl: c.getState()?.server?.uri,
|
||||
networkId: c.getState()?.server?.networkId,
|
||||
transaction,
|
||||
connection: c,
|
||||
}))
|
||||
})
|
||||
|
||||
59
lib/onTransaction.mjs
Normal file
59
lib/onTransaction.mjs
Normal file
@@ -0,0 +1,59 @@
|
||||
import { writeFile } from 'fs'
|
||||
import { dirExists } from './dirExists.mjs'
|
||||
import { ledgerIndexToFolders } from './ledgerIndexToFolders.mjs'
|
||||
import 'dotenv/config'
|
||||
|
||||
const lastSeenTransactions = []
|
||||
|
||||
const fields = (process.env?.FIELDSREQUIRED || '')
|
||||
.split(',')
|
||||
.map(f => f.trim())
|
||||
.filter(f => f.match(/^[a-zA-Z0-9]+$/))
|
||||
|
||||
const fieldsRequired = fields.length === 1 && fields[0] === ''
|
||||
? [ 'Fee' ]
|
||||
: fields
|
||||
|
||||
const hasRequiredFields = tx => fieldsRequired.map(f => Object.keys(tx).includes(f)).every(f => !!f)
|
||||
|
||||
/**
|
||||
* TODO: FIELDSREQUIRED ENV VAR (so: decode tx) - if empty store all
|
||||
* » Store transactions
|
||||
* » Generate xPOP
|
||||
*/
|
||||
|
||||
const onTransaction = async ({
|
||||
networkId,
|
||||
transaction,
|
||||
}) => {
|
||||
if (transaction?.validated) {
|
||||
const { transaction: tx } = transaction
|
||||
|
||||
if (tx.hash && lastSeenTransactions.indexOf(tx.hash) < 0) {
|
||||
lastSeenTransactions.unshift(tx.hash)
|
||||
lastSeenTransactions.length = 3000
|
||||
|
||||
const validTx = hasRequiredFields(tx)
|
||||
console.log('TX', tx.hash, validTx)
|
||||
|
||||
if (validTx && transaction?.ledger_index) {
|
||||
const relativeStorDir = 'store/' + networkId + '/' + ledgerIndexToFolders(transaction.ledger_index)
|
||||
const storeDir = new URL('../' + relativeStorDir, import.meta.url).pathname
|
||||
|
||||
console.log('xPOP eligible', relativeStorDir, transaction)
|
||||
|
||||
if (await dirExists(storeDir)) {
|
||||
writeFile(storeDir + '/tx_' + tx.hash + '.json', Buffer.from(JSON.stringify(transaction), 'utf8'), err => {
|
||||
if (err) {
|
||||
console.log('Error writing file @ ' + storeDir)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
onTransaction,
|
||||
}
|
||||
Reference in New Issue
Block a user