From b09acd501af706423fef5916c560bd2915905d8b Mon Sep 17 00:00:00 2001 From: argonmining Date: Sun, 11 Aug 2024 10:52:38 -0500 Subject: [PATCH] tracking payments to db for frontend logging --- src/trxs/index.ts | 50 +++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/trxs/index.ts b/src/trxs/index.ts index 950bb48..b2b2e82 100644 --- a/src/trxs/index.ts +++ b/src/trxs/index.ts @@ -1,4 +1,4 @@ -import Database from '../database'; +import Database from '../database'; // Use the existing Database class import { sompiToKaspaStringWithSuffix, type IPaymentOutput, createTransactions, PrivateKey, UtxoProcessor, UtxoContext, type RpcClient } from "../../wasm/kaspa"; import Monitoring from '../monitoring'; import { DEBUG } from "../index"; @@ -9,27 +9,34 @@ export default class trxManager { private address: string; private processor: UtxoProcessor; private context: UtxoContext; - private db: Database; + private db: Database; // Existing property for katpool-app database connection private monitoring: Monitoring; - constructor(networkId: string, privKey: string, databaseUrl: string, rpc: RpcClient) { this.monitoring = new Monitoring(); this.networkId = networkId; if (DEBUG) this.monitoring.debug(`TrxManager: Network ID is: ${this.networkId}`); - this.db = new Database(databaseUrl); + this.db = new Database(databaseUrl); // Initialize with the existing DATABASE_URL for katpool-app database this.privateKey = new PrivateKey(privKey); this.address = this.privateKey.toAddress(networkId).toString(); if (DEBUG) this.monitoring.debug(`TrxManager: Pool Treasury Address: ${this.address}`); this.processor = new UtxoProcessor({ rpc, networkId }); this.context = new UtxoContext({ processor: this.processor }); - this.registerProcessor() + this.registerProcessor(); + } + + private async recordPayment(walletAddress: string, amount: bigint, transactionHash: string) { + // Log payment into the katpool-app's payments table using the existing db connection + await this.db.client.query(` + INSERT INTO payments (wallet_address, amount, timestamp, transaction_hash) + VALUES ($1, $2, NOW(), $3) + `, [walletAddress, amount.toString(), transactionHash]); } async transferBalances() { const balances = await this.db.getAllBalancesExcludingPool(); let payments: { [address: string]: bigint } = {}; - + // Aggregate balances by wallet address for (const { address, balance } of balances) { if (balance > 0) { @@ -40,23 +47,24 @@ export default class trxManager { } } } - + // Convert the payments object into an array of IPaymentOutput const paymentOutputs: IPaymentOutput[] = Object.entries(payments).map(([address, amount]) => ({ address, amount, })); - + if (paymentOutputs.length === 0) { return this.monitoring.log('TrxManager: No payments found for current transfer cycle.'); } - + const transactionId = await this.send(paymentOutputs); this.monitoring.log(`TrxManager: Sent payments. Transaction ID: ${transactionId}`); - + if (transactionId) { - // Reset balances for all affected addresses - for (const address of Object.keys(payments)) { + // Log each payment and reset balances for all affected addresses + for (const [address, amount] of Object.entries(payments)) { + await this.recordPayment(address, amount, transactionId); // Log payment to the database await this.db.resetBalancesByWallet(address); this.monitoring.log(`TrxManager: Reset balances for wallet ${address}`); } @@ -83,23 +91,19 @@ export default class trxManager { if (DEBUG) this.monitoring.debug(`TrxManager: Summary Final Transaction ID: ${summary.finalTransactionId}`); return summary.finalTransactionId; - } - - private registerProcessor () { + private registerProcessor() { this.processor.addEventListener("utxo-proc-start", async () => { if (DEBUG) this.monitoring.debug(`TrxManager: registerProcessor - this.context.clear()`); - await this.context.clear() + await this.context.clear(); if (DEBUG) this.monitoring.debug(`TrxManager: registerProcessor - tracking pool address`); - await this.context.trackAddresses([ this.address ]) - }) - this.processor.start() - } + await this.context.trackAddresses([this.address]); + }); + this.processor.start(); + } // stopProcessor () { // this.processor.stop() - // } - - + // } }