Skip to content

Commit

Permalink
DEC-603: Unify backend & frontend types
Browse files Browse the repository at this point in the history
  • Loading branch information
luixo committed Nov 8, 2022
1 parent 11981dd commit bdd303c
Show file tree
Hide file tree
Showing 151 changed files with 3,058 additions and 3,373 deletions.
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"mailgun.js": "^7.0.2",
"nanoid": "^3.1.30",
"@pc/database": "*",
"@pc/common": "*",
"passport": "^0.6.0",
"passport-http-bearer": "^1.0.1",
"pg": "^8.8.0",
Expand Down
2 changes: 0 additions & 2 deletions backend/src/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { AuthModule } from './auth/auth.module';
import validate from '../config/validate';

import { ApiKeysModule } from './keys/apiKeys.module';
import { IndexerService } from './indexer.service';
import { EmailModule } from './email/email.module';

@Module({
Expand All @@ -26,7 +25,6 @@ import { EmailModule } from './email/email.module';
EmailModule,
ExplorerModule,
],
providers: [IndexerService],
exports: [EmailModule],
})
export class CoreModule {}
75 changes: 8 additions & 67 deletions backend/src/core/explorer/actions.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,5 @@
import * as RPC from '../near-rpc/types';

export type Action =
| {
kind: 'createAccount';
args: Record<string, never>;
}
| {
kind: 'deployContract';
args: {
code: string;
};
}
| {
kind: 'functionCall';
args: {
methodName: string;
args: string;
gas: number;
deposit: string;
};
}
| {
kind: 'transfer';
args: {
deposit: string;
};
}
| {
kind: 'stake';
args: {
stake: string;
publicKey: string;
};
}
| {
kind: 'addKey';
args: {
publicKey: string;
accessKey: {
nonce: number;
permission:
| {
type: 'fullAccess';
}
| {
type: 'functionCall';
contractId: string;
methodNames: string[];
};
};
};
}
| {
kind: 'deleteKey';
args: {
publicKey: string;
};
}
| {
kind: 'deleteAccount';
args: {
beneficiaryId: string;
};
};
import { Explorer } from '@pc/common/types/core';
import * as RPC from '@pc/common/types/rpc';

type DatabaseAddKey = {
kind: 'ADD_KEY';
Expand Down Expand Up @@ -151,7 +88,9 @@ export type DatabaseAction = {
| DatabaseTransfer
);

export const mapDatabaseActionToAction = (action: DatabaseAction): Action => {
export const mapDatabaseActionToAction = (
action: DatabaseAction,
): Explorer.Action => {
switch (action.kind) {
case 'ADD_KEY': {
if (action.args.access_key.permission.permission_kind === 'FULL_ACCESS') {
Expand Down Expand Up @@ -239,7 +178,9 @@ export const mapDatabaseActionToAction = (action: DatabaseAction): Action => {
}
};

export const mapRpcActionToAction = (rpcAction: RPC.ActionView): Action => {
export const mapRpcActionToAction = (
rpcAction: RPC.ActionView,
): Explorer.Action => {
if (rpcAction === 'CreateAccount') {
return {
kind: 'createAccount',
Expand Down
46 changes: 26 additions & 20 deletions backend/src/core/explorer/dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,40 @@
// and had many unaddressed github issues

import * as Joi from 'joi';
import { Net } from '@pc/database/clients/core';
import { Api } from '@pc/common/types/api';

const netSchema = Joi.alternatives('MAINNET', 'TESTNET');

// activity
export type ActivityInputDto = {
net: Net;
contractId: string;
};
export const ActivityInputSchemas = Joi.object({
net: Joi.string(),
export const ActivityInputSchemas = Joi.object<
Api.Query.Input<'/explorer/activity'>,
true
>({
net: netSchema,
contractId: Joi.string(),
});
// transaction
export type TransactionInputDto = {
net: Net;
hash: string;
};
export const TransactionInputSchemas = Joi.object({
net: Joi.string(),
export const TransactionInputSchemas = Joi.object<
Api.Query.Input<'/explorer/transaction'>,
true
>({
net: netSchema,
hash: Joi.string(),
});
// balance changes
export type BalanceChangesInputDto = {
net: Net;
receiptId: string;
accountIds: string[];
};
export const BalanceChangesInputSchemas = Joi.object({
net: Joi.string(),
export const BalanceChangesInputSchemas = Joi.object<
Api.Query.Input<'/explorer/balanceChanges'>,
true
>({
net: netSchema,
receiptId: Joi.string(),
accountIds: Joi.array().items(Joi.string()),
});

export const GetTransactionsSchema = Joi.object<
Api.Query.Input<'/explorer/getTransactions'>,
true
>({
contracts: Joi.array().items(Joi.string()),
net: netSchema,
});
38 changes: 23 additions & 15 deletions backend/src/core/explorer/explorer.controller.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
import { Api } from '@pc/common/types/api';
import {
Controller,
Post,
UsePipes,
Body,
BadRequestException,
} from '@nestjs/common';
import {
ExplorerService,
AccountActivity,
Transaction,
} from './explorer.service';
import { ExplorerService } from './explorer.service';
import { JoiValidationPipe } from 'src/pipes/JoiValidationPipe';
import {
ActivityInputSchemas,
TransactionInputSchemas,
BalanceChangesInputSchemas,
ActivityInputDto,
TransactionInputDto,
BalanceChangesInputDto,
GetTransactionsSchema,
} from './dto';
import { IndexerService } from './indexer.service';

@Controller('explorer')
export class ExplorerController {
constructor(private readonly explorerService: ExplorerService) {}
constructor(
private readonly explorerService: ExplorerService,
private readonly indexerService: IndexerService,
) {}

@Post('activity')
@UsePipes(new JoiValidationPipe(ActivityInputSchemas))
async activity(
@Body() { net, contractId }: ActivityInputDto,
): Promise<AccountActivity> {
@Body() { net, contractId }: Api.Query.Input<'/explorer/activity'>,
): Promise<Api.Query.Output<'/explorer/activity'>> {
return this.explorerService.fetchActivity(net, contractId, 50);
}

@Post('transaction')
@UsePipes(new JoiValidationPipe(TransactionInputSchemas))
async transaction(
@Body() { net, hash }: TransactionInputDto,
): Promise<Transaction> {
@Body() { net, hash }: Api.Query.Input<'/explorer/transaction'>,
): Promise<Api.Query.Output<'/explorer/transaction'>> {
const tx = await this.explorerService.fetchTransaction(net, hash);
if (!tx) {
throw new BadRequestException('TX_NOT_FOUND');
Expand All @@ -47,8 +46,17 @@ export class ExplorerController {
@Post('balanceChanges')
@UsePipes(new JoiValidationPipe(BalanceChangesInputSchemas))
async balanceChanges(
@Body() { net, receiptId, accountIds }: BalanceChangesInputDto,
): Promise<(string | undefined)[]> {
@Body()
{ net, receiptId, accountIds }: Api.Query.Input<'/explorer/balanceChanges'>,
): Promise<Api.Query.Output<'/explorer/balanceChanges'>> {
return this.explorerService.fetchBalanceChanges(net, receiptId, accountIds);
}

@Post('getTransactions')
@UsePipes(new JoiValidationPipe(GetTransactionsSchema))
async getTransactions(
@Body() { contracts, net }: Api.Query.Input<'/explorer/getTransactions'>,
): Promise<Api.Query.Output<'/explorer/getTransactions'>> {
return this.indexerService.fetchRecentTransactions(contracts, net);
}
}
5 changes: 3 additions & 2 deletions backend/src/core/explorer/explorer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Module } from '@nestjs/common';
import { NearRpcService } from '../near-rpc/near-rpc.service';
import { ExplorerController } from './explorer.controller';
import { ExplorerService } from './explorer.service';
import { IndexerService } from './indexer.service';

@Module({
providers: [ExplorerService, NearRpcService],
controllers: [ExplorerController],
providers: [ExplorerService, IndexerService, NearRpcService],
controllers: [ExplorerController, IndexerService],
exports: [ExplorerService],
})
export class ExplorerModule {}
Loading

0 comments on commit bdd303c

Please sign in to comment.