Skip to content

Commit

Permalink
feat(api): implement blockchain controller (#263)
Browse files Browse the repository at this point in the history
* implement votes controller

* style: resolve style guide violations

* implement blockchain controller

* style: resolve style guide violations

---------

Co-authored-by: oXtxNt9U <[email protected]>
  • Loading branch information
oXtxNt9U and oXtxNt9U authored Oct 4, 2023
1 parent 2aa67ba commit d18d3d8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
42 changes: 42 additions & 0 deletions packages/api-http/integration/routes/blockchain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, Sandbox } from "../../../test-framework";
import { prepareSandbox, ApiContext } from "../../test/helpers/prepare-sandbox";
import { request } from "../../test/helpers/request";

import blocks from "../../test/fixtures/blocks.json";

describe<{
sandbox: Sandbox;
}>("Blockchain", ({ it, afterAll, assert, afterEach, beforeAll, beforeEach, nock }) => {
let apiContext: ApiContext;

// TODO:
let options = { transform: false };

beforeAll(async (context) => {
nock.enableNetConnect();
apiContext = await prepareSandbox(context);
});

afterAll((context) => {
nock.disableNetConnect();
apiContext.dispose();
});

beforeEach(async (context) => {
await apiContext.reset();
});

afterEach(async (context) => {
await apiContext.reset();
});

it("/blockchain", async () => {
await apiContext.blockRepository.save(blocks);

const block = blocks[0];

const { statusCode, data } = await request("/blockchain", options);
assert.equal(statusCode, 200);
assert.equal(data.data, { block: { id: block.id, height: block.height }, supply: "0" });
});
});
28 changes: 28 additions & 0 deletions packages/api-http/source/controllers/blockchain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Contracts as ApiDatabaseContracts, Identifiers as ApiDatabaseIdentifiers } from "@mainsail/api-database";
import { inject, injectable } from "@mainsail/container";

import { Controller } from "./controller";

@injectable()
export class BlockchainController extends Controller {
@inject(ApiDatabaseIdentifiers.BlockRepositoryFactory)
private readonly blockRepositoryFactory!: ApiDatabaseContracts.IBlockRepositoryFactory;

public async index() {
const block = await this.blockRepositoryFactory().getLatest();

return {
data: {
block: block
? {
height: block.height,
id: block.id,
}
: null,

// TODO: calculate supply
supply: "0",
},
};
}
}
3 changes: 2 additions & 1 deletion packages/api-http/source/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Hapi from "@hapi/hapi";

import * as Blockchain from "./routes/blockchain";
import * as Blocks from "./routes/blocks";
import * as Node from "./routes/node";
import * as Transactions from "./routes/transactions";
Expand All @@ -10,7 +11,7 @@ import * as Wallets from "./routes/wallets";
export = {
name: "Public API",
async register(server: Hapi.Server): Promise<void> {
const handlers = [Blocks, Transactions, Node, ValidatorRounds, Votes, Wallets];
const handlers = [Blocks, Blockchain, Transactions, Node, ValidatorRounds, Votes, Wallets];

for (const handler of handlers) {
handler.register(server);
Expand Down
14 changes: 14 additions & 0 deletions packages/api-http/source/routes/blockchain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Hapi from "@hapi/hapi";

import { BlockchainController } from "../controllers/blockchain";

export const register = (server: Hapi.Server): void => {
const controller = server.app.app.resolve(BlockchainController);
server.bind(controller);

server.route({
handler: (request: Hapi.Request) => controller.index(request),
method: "GET",
path: "/blockchain",
});
};

0 comments on commit d18d3d8

Please sign in to comment.