From 9b542962be0e7bd88585502230653fa0fc8470a7 Mon Sep 17 00:00:00 2001 From: coinchimp Date: Sun, 11 Aug 2024 20:14:28 +0000 Subject: [PATCH] adding processor for maturity event between transactions --- package.json | 1 + src/trxs/index.ts | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 0ff5b01..8b0eb49 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "dotenv": "^16.4.5", + "events": "^3.3.0", "node-cron": "^3.0.3", "p-queue": "^8.0.1", "pg": "^8.12.0" diff --git a/src/trxs/index.ts b/src/trxs/index.ts index 223ebd7..00c7548 100644 --- a/src/trxs/index.ts +++ b/src/trxs/index.ts @@ -1,9 +1,10 @@ +import { EventEmitter } from 'events' import Database from '../database'; import { sompiToKaspaStringWithSuffix, type IPaymentOutput, createTransactions, PrivateKey, UtxoProcessor, UtxoContext, type RpcClient } from "../../wasm/kaspa"; import Monitoring from '../monitoring'; import { DEBUG } from "../index"; -export default class trxManager { +export default class trxManager extends EventEmitter { private networkId: string; private privateKey: PrivateKey; private address: string; @@ -14,6 +15,7 @@ export default class trxManager { constructor(networkId: string, privKey: string, databaseUrl: string, rpc: RpcClient) { + super() this.monitoring = new Monitoring(); this.networkId = networkId; if (DEBUG) this.monitoring.debug(`TrxManager: Network ID is: ${this.networkId}`); @@ -71,12 +73,14 @@ export default class trxManager { }); for (const transaction of transactions) { - if (DEBUG) this.monitoring.debug(`TrxManager: Payment with ransaction ID: ${transaction.id} to be signed`); - await transaction.sign([this.privateKey]); - if (DEBUG) this.monitoring.debug(`TrxManager: Payment with ransaction ID: ${transaction.id} to be submitted`); - await transaction.submit(this.processor.rpc); - if (DEBUG) this.monitoring.debug(`TrxManager: Payment with ransaction ID: ${transaction.id} submitted`); - await new Promise(resolve => setTimeout(resolve, 5000)); // 5-second delay + if (DEBUG) this.monitoring.debug(`TrxManager: Payment with transaction ID: ${transaction.id} to be signed`); + transaction.sign([this.privateKey]); + this.on('time-to-submit', () => { + if (DEBUG) this.monitoring.debug(`TrxManager: Payment with transaction ID: ${transaction.id} to be submitted`); + transaction.submit(this.processor.rpc); + if (DEBUG) this.monitoring.debug(`TrxManager: Payment with transaction ID: ${transaction.id} submitted`); + //await new Promise(resolve => setTimeout(resolve, 5000)); // 5-second delay + }) } if (DEBUG) this.monitoring.debug(`TrxManager: summary.finalTransactionId: ${summary.finalTransactionId}`); @@ -92,12 +96,13 @@ export default class trxManager { if (DEBUG) this.monitoring.debug(`TrxManager: registerProcessor - tracking pool address`); await this.context.trackAddresses([ this.address ]) }) - this.processor.start() - } - // stopProcessor () { - // this.processor.stop() - // } + this.processor.addEventListener('maturity', () => { + if (DEBUG) this.monitoring.debug(`TrxManager: maturity event`) + this.emit('time-to-submit') + }) + this.processor.start() + } }