Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add domains endpoint #40

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 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
8 changes: 1 addition & 7 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@ DB_NAME=postgres
DB_PASS=postgres
DB_PORT=5432
DB_USERNAME=postgres
DB_HOST=db
SHARED_CONFIG_URL="https://ipfs.io/ipfs/bafkreiasnla2ya55of6nwm3swjstip4q2ixfa3t6tvixyibclfovxnerte"
1_METADATA='{"id": 1, "rpcUrl": "http://evm1:8545"}'
2_METADATA='{"id": 2, "rpcUrl": "http://evm2:8545"}'
3_METADATA='{"id": 3, "rpcUrl": "ws://substrate-pallet:9944"}'

ENV_DOMAINS='[1,2,3]'
DB_HOST=db

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions envs/.env.api

This file was deleted.

13 changes: 0 additions & 13 deletions envs/.env.evm-1

This file was deleted.

13 changes: 0 additions & 13 deletions envs/.env.evm-2

This file was deleted.

13 changes: 0 additions & 13 deletions envs/.env.substrate

This file was deleted.

2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type Domain @entity {
id: ID!
type: String!
name: String!
iconURL: String
explorerURL: String
routesFrom: [Route!] @derivedFrom(field: "fromDomain")
routesTo: [Route!] @derivedFrom(field: "toDomain")
token: [Token!] @derivedFrom(field: "domain")
Expand Down
30 changes: 30 additions & 0 deletions src/api/controllers/DomainsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
The Licensed Work is (c) 2024 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import type { FastifyReply, FastifyRequest } from "fastify";
import type { DataSource } from "typeorm";

import { logger } from "../../utils/logger";
import { DomainsService } from "../services/dataAccess/domains.service";

export class DomainsController {
private domainsService: DomainsService;

constructor(dataSource: DataSource) {
this.domainsService = new DomainsService(dataSource);
}

public async getDomains(
request: FastifyRequest,
reply: FastifyReply,
): Promise<void> {
try {
const domainsResult = await this.domainsService.findDomains({});
await reply.status(200).send(domainsResult);
} catch (error) {
logger.error("Error occurred when fetching domains", error);
await reply.status(500).send({ error: "Internal server error" });
}
}
}
18 changes: 18 additions & 0 deletions src/api/routes/domains.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
The Licensed Work is (c) 2024 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import type { FastifyInstance } from "fastify";

import { DomainsController } from "../controllers/DomainsController";
import { domainsSchema } from "../schemas/domains.schema";

export async function domainRoutes(server: FastifyInstance): Promise<void> {
const domainsController = new DomainsController(server.db);
server.get(
"/domains",
{ schema: domainsSchema },
domainsController.getDomains.bind(domainsController),
);
return Promise.resolve();
}
2 changes: 2 additions & 0 deletions src/api/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ SPDX-License-Identifier: LGPL-3.0-only
*/
import type { FastifyInstance } from "fastify";

import { domainRoutes } from "./domains.routes";
import { transferRoutes } from "./transfers.routes";

export async function registerRoutes(server: FastifyInstance): Promise<void> {
await server.register(transferRoutes, { prefix: "/api" });
await server.register(domainRoutes, { prefix: "/api" });
mj52951 marked this conversation as resolved.
Show resolved Hide resolved
}
25 changes: 25 additions & 0 deletions src/api/schemas/domains.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
The Licensed Work is (c) 2024 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { domainMetadataSchema, domainSchema } from ".";

export const domainsSchema = {
summary: "Get domains",
response: {
200: {
description: "List of domains",
content: {
"application/json": {
schema: {
type: "array",
items: {
...domainSchema.properties,
...domainMetadataSchema.properties,
},
},
},
},
},
},
};
8 changes: 8 additions & 0 deletions src/api/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export const domainSchema = {
},
};

export const domainMetadataSchema = {
type: "object",
properties: {
iconURL: { type: "string", example: "https://example.com/icon1.png" },
explorerURL: { type: "string", example: "https://explorer.com/1" },
},
};

export const tokenSchema = {
tpye: "object",
properties: {
Expand Down
24 changes: 24 additions & 0 deletions src/api/services/dataAccess/domains.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
The Licensed Work is (c) 2024 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import type { DataSource, FindOptionsWhere, Repository } from "typeorm";

import { Domain } from "../../../model";

export class DomainsService {
private domainRepository: Repository<Domain>;
constructor(dataSource: DataSource) {
this.domainRepository = dataSource.getRepository(Domain);
}

public async findDomains(where: FindOptionsWhere<Domain>): Promise<Domain[]> {
const domains = await this.domainRepository.find({
where,
order: {
id: "ASC",
},
});
return domains;
}
}
2 changes: 2 additions & 0 deletions src/indexer/config/envLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type DomainMetadata = {
domainId: number;
rpcUrl: string;
domainGateway?: string;
iconUrl?: string;
explorerUrl?: string;
};

export type EnvVariables = {
Expand Down
2 changes: 1 addition & 1 deletion src/indexer/evmIndexer/evmProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class EVMProcessor implements IProcessor {

for (const block of ctx.blocks) {
this.logger.info(
`Processing block ${block.header.height} on networ ${domain.name}(${domain.id})`,
`Processing block ${block.header.height} on network ${domain.name}(${domain.id})`,
);
for (const log of block.logs) {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/indexer/substrateIndexer/substrateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class SubstrateParser implements ISubstrateParser {
});
if (!resource) {
throw new NotFoundError(
`Unssupported resource with ID ${event.resourceId}`,
`Unsupported resource with ID ${event.resourceId}`,
);
}

Expand Down Expand Up @@ -218,7 +218,7 @@ export class SubstrateParser implements ISubstrateParser {
});
if (!resource) {
throw new NotFoundError(
`Unssupported resource with ID ${event.resourceId}`,
`Unsupported resource with ID ${event.resourceId}`,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/indexer/substrateIndexer/substrateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class SubstrateProcessor implements IProcessor {
const fees: FeeCollectedData[] = [];
for (const block of ctx.blocks) {
this.logger.info(
`processing bloc ${block.header.height} on networ ${domain.name}(${domain.id})`,
`Processing block ${block.header.height} on network ${domain.name}(${domain.id})`,
);
for (const event of block.events) {
try {
Expand Down
18 changes: 15 additions & 3 deletions src/main_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { EntityManager } from "typeorm";

import type { Domain as DomainConfig } from "./indexer/config";
import { fetchSharedConfig } from "./indexer/config";
import { getEnv } from "./indexer/config/envLoader";
import { getDomainMetadata, getEnv } from "./indexer/config/envLoader";
import { Domain, Resource, Token } from "./model";
import { initDatabase } from "./utils";

Expand All @@ -18,21 +18,33 @@ export async function init(): Promise<void> {
const dataSource = await initDatabase(envVars.dbConfig);
const sharedConfig = await fetchSharedConfig(envVars.sharedConfigURL);

await insertDomains(sharedConfig.domains, dataSource.manager);
await insertDomains(
sharedConfig.domains,
dataSource.manager,
envVars.envDomains,
);
await dataSource.destroy();
}

async function insertDomains(
domains: Array<DomainConfig>,
manager: EntityManager,
supportedDomainsIDs: number[],
): Promise<void> {
for (const domain of domains) {
for (const domainID of supportedDomainsIDs) {
const domain = domains.find((domain) => domain.id == domainID);
if (!domain) {
throw new Error(`domain with id ${domainID} not found in shared-config`);
}
const domainMetadata = getDomainMetadata(domain.id.toString());
await manager.upsert(
Domain,
{
id: domain.id.toString(),
type: domain.type,
name: domain.name,
iconURL: domainMetadata.iconUrl ?? "",
explorerURL: domainMetadata.explorerUrl ?? "",
},
["id"],
);
Expand Down
6 changes: 6 additions & 0 deletions src/model/generated/domain.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export class Domain {
@StringColumn_({nullable: false})
name!: string

@StringColumn_({nullable: true})
iconURL!: string | undefined | null

@StringColumn_({nullable: true})
explorerURL!: string | undefined | null

@OneToMany_(() => Route, e => e.fromDomain)
routesFrom!: Route[]

Expand Down
Loading