Skip to content

Commit

Permalink
[TRYC-16] Fix TransactionID (#5)
Browse files Browse the repository at this point in the history
* Change transactionId to string

* Fix blockChecker module to check via transaction hash
  • Loading branch information
piotrmoszkowicz authored Sep 19, 2019
1 parent 96ebf8e commit f4baed5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
20 changes: 8 additions & 12 deletions src/blockChecker/blockchecker.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Injectable, Logger, OnModuleInit } from "@nestjs/common";
import { InjectEventEmitter } from "nest-emitter";

import * as fs from "fs";
import { Socket } from "blockchain.info";
import { blockexplorer, Socket } from "blockchain.info";

import axiosWrapper from "../axiosWrapper";

Expand Down Expand Up @@ -60,14 +59,15 @@ export class BlockCheckerService implements OnModuleInit {

async onBlockHandler(data: IBlockResponse) {
this.logger.debug("OnBlock called");
this.logger.debug(`Hash: ${data.hash}`);
const transactionsToLookFor = await this.transactionService.findAll();
const blockData = await blockexplorer.getBlock(data.hash);
const arrayOfTransactionToLookFrom = transactionsToLookFor.map(
transaction => transaction.transactionId,
);
const transactionsToUpdate = data.txIndexes.filter(
index => -1 !== arrayOfTransactionToLookFrom.indexOf(index),
);
this.logger.debug(transactionsToUpdate);
const transactionsToUpdate = blockData.tx.filter(
transaction => -1 !== arrayOfTransactionToLookFrom.indexOf(transaction.hash),
).map(transaction => transaction.hash);

for (const transactionId of transactionsToUpdate) {
const transaction = transactionsToLookFor.filter(
Expand All @@ -79,8 +79,6 @@ export class BlockCheckerService implements OnModuleInit {
};
await this.callWebook(transaction.wallet.webhookUrl, response);
}

fs.writeFileSync("./blockHandler.txt", JSON.stringify(data));
}

async onTransactionHandler(data: any) {
Expand Down Expand Up @@ -111,7 +109,7 @@ export class BlockCheckerService implements OnModuleInit {
(acc, input) => acc + input.prev_out.value,
0,
),
transactionId: data.tx_index,
transactionId: data.hash,
numberOfConfirmations: 0,
transactionType,
trackedWalletId: lookedWalletsInThisTransaction[0],
Expand All @@ -121,11 +119,9 @@ export class BlockCheckerService implements OnModuleInit {
lookedWalletsInThisTransaction[0],
);

await this.transactionService.create(data.tx_index, wallet.id);
await this.transactionService.create(data.hash, wallet.id);

await this.callWebook(wallet.webhookUrl, response);

fs.writeFileSync("./transactionHandler.txt", JSON.stringify(data));
} catch (err) {
this.logger.error(err.message, err.trace);
}
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/transaction.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Transaction extends Model<Transaction> {

@Unique
@Column
public transactionId: number;
public transactionId: string;

@ForeignKey(() => Wallet)
@Column
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class TransactionService {
private readonly transactionService: typeof Transaction,
) {}

async create(transactionId: number, walletId: number): Promise<Transaction> {
async create(transactionId: string, walletId: number): Promise<Transaction> {
const transaction = new Transaction();
transaction.transactionId = transactionId;
transaction.walletId = walletId;
Expand Down

0 comments on commit f4baed5

Please sign in to comment.