Skip to content

Commit

Permalink
Merge pull request #2 from near-daos/feature/contract_context_pipe
Browse files Browse the repository at this point in the history
Introduced contract context pipe
  • Loading branch information
okalenyk authored Jan 14, 2022
2 parents 78ef1b9 + bcdea7e commit 690c049
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 119 deletions.
11 changes: 8 additions & 3 deletions apps/api/src/dao/dao.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DaoService,
} from '@dao-stats/common';
import { HasDaoContractContext } from '../decorators';
import { ContractContextPipe } from '../pipes';

@ApiTags('DAOs')
@Controller('daos')
Expand All @@ -21,7 +22,9 @@ export class DaoController {
description: 'Bad Request Response based on the query params set',
})
@Get()
async daos(@Param() context: ContractContext): Promise<DaoDto[]> {
async daos(
@Param(ContractContextPipe) context: ContractContext,
): Promise<DaoDto[]> {
const { contractId } = context;

return this.daoService.find(contractId);
Expand All @@ -36,7 +39,9 @@ export class DaoController {
})
@HasDaoContractContext()
@Get('/:dao')
async dao(@Param() context: DaoContractContext): Promise<DaoDto> {
async dao(
@Param(ContractContextPipe) context: DaoContractContext,
): Promise<DaoDto> {
const { contractId, dao } = context;

const daoEntity = await this.daoService.findById(contractId, dao);
Expand All @@ -57,7 +62,7 @@ export class DaoController {
})
@Get('/autocomplete/:input')
async autocomplete(
@Param() context: ContractContext,
@Param(ContractContextPipe) context: ContractContext,
@Param('input') input: string,
): Promise<DaoDto[]> {
const { contractId } = context;
Expand Down
18 changes: 9 additions & 9 deletions apps/api/src/flow/flow.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from './dto';
import { FlowService } from './flow.service';
import { HasDaoContractContext } from '../decorators';
import { MetricQueryPipe } from '../pipes';
import { ContractContextPipe, MetricQueryPipe } from '../pipes';

@ApiTags('Flow')
@Controller('flow')
Expand All @@ -29,7 +29,7 @@ export class FlowController {
description: 'Bad Request Response based on the query params set',
})
@Get('/')
async totals(@Param() context: ContractContext): Promise<FlowTotalResponse> {
async totals(@Param(ContractContextPipe) context: ContractContext): Promise<FlowTotalResponse> {
return this.flowService.totals(context);
}

Expand All @@ -42,7 +42,7 @@ export class FlowController {
})
@Get('/funds')
async funds(
@Param() context: ContractContext,
@Param(ContractContextPipe) context: ContractContext,
@Query(MetricQueryPipe) metricQuery: MetricQuery,
): Promise<FlowMetricResponse> {
return this.flowService.history(context, FlowMetricType.Fund, metricQuery);
Expand All @@ -57,7 +57,7 @@ export class FlowController {
})
@Get('/funds/leaderboard')
async fundsLeaderboard(
@Param() context: ContractContext,
@Param(ContractContextPipe) context: ContractContext,
): Promise<FlowLeaderboardMetricResponse> {
return this.flowService.leaderboard(context, FlowMetricType.Fund);
}
Expand All @@ -71,7 +71,7 @@ export class FlowController {
})
@Get('/transactions')
async transactions(
@Param() context: ContractContext,
@Param(ContractContextPipe) context: ContractContext,
@Query(MetricQueryPipe) metricQuery: MetricQuery,
): Promise<FlowMetricResponse> {
return this.flowService.history(
Expand All @@ -90,7 +90,7 @@ export class FlowController {
})
@Get('/transactions/leaderboard')
async transactionsLeaderboard(
@Param() context: ContractContext,
@Param(ContractContextPipe) context: ContractContext,
): Promise<FlowLeaderboardMetricResponse> {
return this.flowService.leaderboard(context, FlowMetricType.Transaction);
}
Expand All @@ -105,7 +105,7 @@ export class FlowController {
@HasDaoContractContext()
@Get('/:dao')
async daoTotals(
@Param() context: DaoContractContext,
@Param(ContractContextPipe) context: DaoContractContext,
): Promise<FlowTotalResponse> {
return this.flowService.totals(context);
}
Expand All @@ -120,7 +120,7 @@ export class FlowController {
@HasDaoContractContext()
@Get('/:dao/funds')
async daoFunds(
@Param() context: DaoContractContext,
@Param(ContractContextPipe) context: DaoContractContext,
@Query(MetricQueryPipe) metricQuery: MetricQuery,
): Promise<FlowMetricResponse> {
return this.flowService.history(context, FlowMetricType.Fund, metricQuery);
Expand All @@ -136,7 +136,7 @@ export class FlowController {
@HasDaoContractContext()
@Get('/:dao/transactions')
async daoTransactions(
@Param() context: DaoContractContext,
@Param(ContractContextPipe) context: DaoContractContext,
@Query(MetricQueryPipe) metricQuery: MetricQuery,
): Promise<FlowMetricResponse> {
return this.flowService.history(
Expand Down
2 changes: 0 additions & 2 deletions apps/api/src/flow/flow.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { TransactionModule } from '@dao-stats/transaction';

import { FlowController } from './flow.controller';
import { FlowService } from './flow.service';
import { ContractModule } from '../contract/contract.module';

@Module({
imports: [
Expand All @@ -18,7 +17,6 @@ import { ContractModule } from '../contract/contract.module';
TypeOrmModule.forFeature([Contract]),
TransactionModule,
ReceiptModule,
ContractModule,
],
providers: [FlowService],
controllers: [FlowController],
Expand Down
65 changes: 22 additions & 43 deletions apps/api/src/flow/flow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,16 @@ import {
FlowLeaderboardMetricResponse,
} from './dto';
import { convertFunds, getDailyIntervals, getGrowth } from '../utils';
import { ContractService } from '../contract/contract.service';

@Injectable()
export class FlowService {
constructor(
private readonly receiptActionService: ReceiptActionService,
private readonly contractService: ContractService,
) {}
constructor(private readonly receiptActionService: ReceiptActionService) {}

async totals(context: ContractContext): Promise<FlowTotalResponse> {
const dayAgo = moment().subtract(1, 'days');

const contract = await this.contractService.findById(context.contractId);

const { contract } = context;
const { conversionFactor } = contract;

const contractContext = {
...context,
contract,
};
const dayAgo = moment().subtract(1, 'days');

const [
txInCount,
Expand All @@ -50,51 +40,51 @@ export class FlowService {
dayAgoTotalOut,
] = await Promise.all([
this.receiptActionService.getTotals(
contractContext,
context,
FlowMetricType.Transaction,
TransferType.Incoming,
),
this.receiptActionService.getTotals(
contractContext,
context,
FlowMetricType.Transaction,
TransferType.Incoming,
{
to: dayAgo.valueOf(),
},
),
this.receiptActionService.getTotals(
contractContext,
context,
FlowMetricType.Transaction,
TransferType.Outgoing,
),
this.receiptActionService.getTotals(
contractContext,
context,
FlowMetricType.Transaction,
TransferType.Outgoing,
{
to: dayAgo.valueOf(),
},
),
this.receiptActionService.getTotals(
contractContext,
context,
FlowMetricType.Fund,
TransferType.Incoming,
),
this.receiptActionService.getTotals(
contractContext,
context,
FlowMetricType.Fund,
TransferType.Incoming,
{
to: dayAgo.valueOf(),
},
),
this.receiptActionService.getTotals(
contractContext,
context,
FlowMetricType.Fund,
TransferType.Outgoing,
),
this.receiptActionService.getTotals(
contractContext,
context,
FlowMetricType.Fund,
TransferType.Outgoing,
{
Expand Down Expand Up @@ -128,24 +118,18 @@ export class FlowService {
metricType: FlowMetricType,
metricQuery?: MetricQuery,
): Promise<FlowMetricResponse> {
const contract = await this.contractService.findById(context.contractId);

const { contract } = context;
const { conversionFactor } = contract;

const contractContext = {
...context,
contract,
};

const [incoming, outgoing] = await Promise.all([
this.receiptActionService.getHistory(
contractContext,
context,
metricType,
TransferType.Incoming,
metricQuery,
),
this.receiptActionService.getHistory(
contractContext,
context,
metricType,
TransferType.Outgoing,
metricQuery,
Expand Down Expand Up @@ -187,17 +171,12 @@ export class FlowService {
context: DaoContractContext | ContractContext,
metricType: FlowMetricType,
): Promise<FlowLeaderboardMetricResponse> {
const { contract } = context;

const weekAgo = moment().subtract(7, 'days');
const days = getDailyIntervals(weekAgo.valueOf(), moment().valueOf());
const dayAgo = moment().subtract(1, 'days');

const contract = await this.contractService.findById(context.contractId);

const contractContext = {
...context,
contract,
};

const [
byDaysIncoming,
byDaysOutgoing,
Expand All @@ -207,7 +186,7 @@ export class FlowService {
outgoing,
] = await Promise.all([
this.receiptActionService.getLeaderboard(
contractContext,
context,
metricType,
TransferType.Incoming,
{
Expand All @@ -217,7 +196,7 @@ export class FlowService {
true,
),
this.receiptActionService.getLeaderboard(
contractContext,
context,
metricType,
TransferType.Outgoing,
{
Expand All @@ -227,31 +206,31 @@ export class FlowService {
true,
),
this.receiptActionService.getLeaderboard(
contractContext,
context,
metricType,
TransferType.Incoming,
{
to: dayAgo.valueOf(),
},
),
this.receiptActionService.getLeaderboard(
contractContext,
context,
metricType,
TransferType.Incoming,
{
to: moment().valueOf(),
},
),
this.receiptActionService.getLeaderboard(
contractContext,
context,
metricType,
TransferType.Outgoing,
{
to: dayAgo.valueOf(),
},
),
this.receiptActionService.getLeaderboard(
contractContext,
context,
metricType,
TransferType.Outgoing,
{
Expand Down
Loading

0 comments on commit 690c049

Please sign in to comment.