Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/endpoints/transactions/transaction.price.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Injectable } from "@nestjs/common";
import { ApiConfigService } from "src/common/api.config.service";
import { CachingService } from "src/common/caching.service";
import { DataApiService } from "src/common/data.api.service";
import { DataQuoteType } from "src/common/entities/data.quote.type";
import { Constants } from "src/utils/constants";
import { TransactionDetailed } from "./entities/transaction.detailed";

@Injectable()
export class TransactionPriceService {

constructor(
private readonly cachingService: CachingService,
private readonly apiConfigService: ApiConfigService,
private readonly dataApiService: DataApiService,
) { }

async getTransactionPrice(transaction: TransactionDetailed): Promise<number | undefined> {
let dataUrl = this.apiConfigService.getDataUrl();
if (!dataUrl) {
return undefined;
}

let transactionDate = transaction.getDate();
if (!transactionDate) {
return undefined;
}

let price = await this.getTransactionPriceForDate(transactionDate);
if (price) {
price = Number(price).toRounded(2);
}

return price;
}

private async getTransactionPriceForDate(date: Date): Promise<number | undefined> {
if (date.isToday()) {
return await this.getTransactionPriceToday();
}

return await this.getTransactionPriceHistorical(date);
}

private async getTransactionPriceToday(): Promise<number | undefined> {
return await this.cachingService.getOrSetCache(
'currentPrice',
async () => await this.dataApiService.getQuotesHistoricalLatest(DataQuoteType.price),
Constants.oneHour()
);
}

private async getTransactionPriceHistorical(date: Date): Promise<number | undefined> {
return await this.cachingService.getOrSetCache(
`price:${date.toISODateString()}`,
async () => await this.dataApiService.getQuotesHistoricalTimestamp(DataQuoteType.price, date.getTime() / 1000),
Constants.oneDay() * 7
);
}
}
53 changes: 3 additions & 50 deletions src/endpoints/transactions/transaction.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Injectable, Logger } from '@nestjs/common';
import { ApiConfigService } from 'src/common/api.config.service';
import { CachingService } from 'src/common/caching.service';
import { DataApiService } from 'src/common/data.api.service';
import { DataQuoteType } from 'src/common/entities/data.quote.type';
import { AbstractQuery } from 'src/common/entities/elastic/abstract.query';
import { ElasticPagination } from 'src/common/entities/elastic/elastic.pagination';
import { ElasticQuery } from 'src/common/entities/elastic/elastic.query';
Expand All @@ -14,7 +11,6 @@ import { GatewayService } from 'src/common/gateway.service';
import { AddressUtils } from 'src/utils/address.utils';
import { ApiUtils } from 'src/utils/api.utils';
import { BinaryUtils } from 'src/utils/binary.utils';
import { Constants } from 'src/utils/constants';
import { ElasticService } from '../../common/elastic.service';
import { SmartContractResult } from './entities/smart.contract.result';
import { Transaction } from './entities/transaction';
Expand All @@ -32,17 +28,17 @@ import { TransactionOperationAction } from './entities/transaction.operation.act
import { QueryOperator } from 'src/common/entities/elastic/query.operator';
import { TransactionScamCheckService } from './scam-check/transaction-scam-check.service';
import { TransactionScamInfo } from './entities/transaction-scam-info';
import { TransactionPriceService } from './transaction.price.service';

@Injectable()
export class TransactionService {
private readonly logger: Logger

constructor(
private readonly elasticService: ElasticService,
private readonly cachingService: CachingService,
private readonly gatewayService: GatewayService,
private readonly apiConfigService: ApiConfigService,
private readonly dataApiService: DataApiService,
private readonly transactionPriceService: TransactionPriceService,
private readonly transactionScamCheckService: TransactionScamCheckService,
) {
this.logger = new Logger(TransactionService.name);
Expand Down Expand Up @@ -192,7 +188,7 @@ export class TransactionService {
if (transaction !== null) {
try {
const [price, scamInfo] = await Promise.all([
this.getTransactionPrice(transaction),
this.transactionPriceService.getTransactionPrice(transaction),
this.getScamInfo(transaction),
]);

Expand All @@ -207,49 +203,6 @@ export class TransactionService {
return transaction;
}

private async getTransactionPrice(transaction: TransactionDetailed): Promise<number | undefined> {
let dataUrl = this.apiConfigService.getDataUrl();
if (!dataUrl) {
return undefined;
}

let transactionDate = transaction.getDate();
if (!transactionDate) {
return undefined;
}

let price = await this.getTransactionPriceForDate(transactionDate);
if (price) {
price = Number(price).toRounded(2);
}

return price;
}

private async getTransactionPriceForDate(date: Date): Promise<number | undefined> {
if (date.isToday()) {
return await this.getTransactionPriceToday();
}

return await this.getTransactionPriceHistorical(date);
}

private async getTransactionPriceToday(): Promise<number | undefined> {
return await this.cachingService.getOrSetCache(
'currentPrice',
async () => await this.dataApiService.getQuotesHistoricalLatest(DataQuoteType.price),
Constants.oneHour()
);
}

private async getTransactionPriceHistorical(date: Date): Promise<number | undefined> {
return await this.cachingService.getOrSetCache(
`price:${date.toISODateString()}`,
async () => await this.dataApiService.getQuotesHistoricalTimestamp(DataQuoteType.price, date.getTime() / 1000),
Constants.oneDay() * 7
);
}

private async tryGetTransactionFromElasticBySenderAndNonce(sender: string, nonce: number): Promise<TransactionDetailed | undefined> {
const query: ElasticQuery = new ElasticQuery();
query.pagination = { from: 0, size: 1 };
Expand Down
2 changes: 2 additions & 0 deletions src/public.app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import "./utils/extensions/date.extensions";
import "./utils/extensions/number.extensions";
import { NftThumbnailService } from './common/nft.thumbnail.service';
import { NftExtendedAttributesService } from './common/nft.extendedattributes.service';
import { TransactionPriceService } from './endpoints/transactions/transaction.price.service';

@Module({
imports: [
Expand Down Expand Up @@ -103,6 +104,7 @@ import { NftExtendedAttributesService } from './common/nft.extendedattributes.se
DelegationService, CacheConfigService, CachingInterceptor, ShardService, MetricsService, IdentitiesService,
TokenAssetService, DataApiService, KeysService, WaitingListService, BlsService, TagService, ExtrasApiService,
TransactionScamCheckService, PotentialScamTransactionChecker, NftThumbnailService, NftExtendedAttributesService,
TransactionPriceService,
],
exports: [
ApiConfigService, RoundService, CachingService, TransactionService, GatewayService, MetricsService, NodeService,
Expand Down