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 Oct 24, 2022
1 parent f2388b6 commit a084599
Show file tree
Hide file tree
Showing 138 changed files with 2,598 additions and 3,288 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
74 changes: 7 additions & 67 deletions backend/src/core/explorer/actions.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,4 @@
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 { RPC, Explorer } from '@pc/common/types';

type DatabaseAddKey = {
kind: 'ADD_KEY';
Expand Down Expand Up @@ -151,7 +87,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 +177,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
38 changes: 18 additions & 20 deletions backend/src/core/explorer/dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,32 @@
// and had many unaddressed github issues

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

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.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.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.Input<'/explorer/balanceChanges'>,
true
>({
net: netSchema,
receiptId: Joi.string(),
accountIds: Joi.array().items(Joi.string()),
});
23 changes: 9 additions & 14 deletions backend/src/core/explorer/explorer.controller.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { Api } from '@pc/common/types';
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,
} from './dto';

@Controller('explorer')
Expand All @@ -27,16 +21,16 @@ export class ExplorerController {
@Post('activity')
@UsePipes(new JoiValidationPipe(ActivityInputSchemas))
async activity(
@Body() { net, contractId }: ActivityInputDto,
): Promise<AccountActivity> {
@Body() { net, contractId }: Api.Input<'/explorer/activity'>,
): Promise<Api.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.Input<'/explorer/transaction'>,
): Promise<Api.Output<'/explorer/transaction'>> {
const tx = await this.explorerService.fetchTransaction(net, hash);
if (!tx) {
throw new BadRequestException('TX_NOT_FOUND');
Expand All @@ -47,8 +41,9 @@ export class ExplorerController {
@Post('balanceChanges')
@UsePipes(new JoiValidationPipe(BalanceChangesInputSchemas))
async balanceChanges(
@Body() { net, receiptId, accountIds }: BalanceChangesInputDto,
): Promise<(string | undefined)[]> {
@Body()
{ net, receiptId, accountIds }: Api.Input<'/explorer/balanceChanges'>,
): Promise<Api.Output<'/explorer/balanceChanges'>> {
return this.explorerService.fetchBalanceChanges(net, receiptId, accountIds);
}
}
Loading

0 comments on commit a084599

Please sign in to comment.