Skip to content

Commit

Permalink
tracking payments to db for frontend logging
Browse files Browse the repository at this point in the history
  • Loading branch information
argonmining committed Aug 11, 2024
1 parent 58b8b0b commit b09acd5
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/trxs/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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) {
Expand All @@ -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}`);
}
Expand All @@ -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()
// }


// }
}

0 comments on commit b09acd5

Please sign in to comment.