Skip to content

Commit

Permalink
feat: add transactions module
Browse files Browse the repository at this point in the history
Signed-off-by: Anmol Sharma <anmolsharma0234@gmail.com>
  • Loading branch information
theanmolsharma committed Apr 14, 2024
1 parent 10dfaec commit 8011753
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AppService } from '@/app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import configuration from '@/configuration';
import { TransactionsModule } from '@/transactions/transactions.module';

@Module({
imports: [
Expand All @@ -26,6 +27,7 @@ import configuration from '@/configuration';
autoLoadEntities: true,
}),
}),
TransactionsModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
28 changes: 28 additions & 0 deletions src/transactions/transaction.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Column, Entity, PrimaryColumn } from 'typeorm';

export type TransactionOutput = {
pubKey: string;
vout: number;
value: number;
};

@Entity()
export class Transaction {
@PrimaryColumn('text')
id: string; // txid

@Column({ type: 'integer', nullable: false })
blockHeight: number;

@Column({ type: 'text', nullable: false })
blockHash: string;

@Column({ type: 'text', nullable: false })
scanTweak: string;

@Column({ type: 'jsonb', nullable: false })
outputs: TransactionOutput[];

@Column({ type: 'boolean', nullable: false })
isSpent: boolean;
}
12 changes: 12 additions & 0 deletions src/transactions/transactions.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TransactionsService } from '@/transactions/transactions.service';
import { Transaction } from '@/transactions/transaction.entity';

@Module({
imports: [TypeOrmModule.forFeature([Transaction])],
controllers: [],
providers: [TransactionsService],
exports: [TransactionsService],
})
export class TransactionsModule {}
22 changes: 22 additions & 0 deletions src/transactions/transactions.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Transaction } from '@/transactions/transaction.entity';
import { Repository } from 'typeorm';

@Injectable()
export class TransactionsService {
constructor(
@InjectRepository(Transaction)
private readonly transactionRepository: Repository<Transaction>,
) {}

async getTransactionByBlockHeight(
blockHeight: number,
): Promise<Transaction[]> {
return this.transactionRepository.find({ where: { blockHeight } });
}

async getTransactionByBlockHash(blockHash: string): Promise<Transaction[]> {
return this.transactionRepository.find({ where: { blockHash } });
}
}

0 comments on commit 8011753

Please sign in to comment.