Skip to content

Commit

Permalink
feat: API V2 EVM contract data
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Feb 6, 2024
1 parent 7620de2 commit d2e3489
Show file tree
Hide file tree
Showing 13 changed files with 919 additions and 81 deletions.
149 changes: 147 additions & 2 deletions lib/api/v2/routers/ChainRouter.ts
@@ -1,8 +1,13 @@
import { Request, Response, Router } from 'express';
import Logger from '../../../Logger';
import { mapToObject } from '../../../Utils';
import Service from '../../../service/Service';
import { createdResponse, successResponse, validateRequest } from '../../Utils';
import Service, { NetworkContracts } from '../../../service/Service';
import {
createdResponse,
errorResponse,
successResponse,
validateRequest,
} from '../../Utils';
import RouterBase from './RouterBase';

class ChainRouter extends RouterBase {
Expand Down Expand Up @@ -61,6 +66,68 @@ class ChainRouter extends RouterBase {
*/
router.get('/heights', this.handleError(this.getHeights));

/**
* @openapi
* components:
* schemas:
* Contracts:
* type: object
* properties:
* network:
* type: object
* description: Information about the network
* properties:
* chainId:
* type: number
* description: ID of the chain
* name:
* type: string
* description: Name of the chain if applicable
* swapContracts:
* type: object
* description: Mapping of the names of swap contracts to their address
* properties:
* EtherSwap:
* type: string
* description: Address of the EtherSwap contract
* ERC20Swap:
* type: string
* description: Address of the ERC20 contract
* tokens:
* type: object
* description: Mapping of the symbol of tokens to their address
* additionalProperties:
* type: string
* description: Address of the token
*/

/**
* @openapi
* /chain/contracts:
* get:
* tags: [Chain]
* description: Get the network information and contract addresses for all supported EVM chains
* responses:
* '200':
* description: Network details and contract addresses on the chain
* content:
* application/json:
* schema:
* type: object
* additionalProperties:
* $ref: '#/components/schemas/Contracts'
* examples:
* json:
* value: '{"rsk":{"network":{"chainId":31337},"tokens":{"USDT":"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"},"swapContracts":{"EtherSwap":"0x5FbDB2315678afecb367f032d93F642f64180aa3","ERC20Swap":"0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512"}}}'
* '400':
* description: Error that caused the request to fail
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ErrorResponse'
*/
router.get('/contracts', this.handleError(this.getContracts));

/**
* @openapi
* /chain/{currency}/fee:
Expand Down Expand Up @@ -215,6 +282,44 @@ class ChainRouter extends RouterBase {
this.handleError(this.postTransaction),
);

/**
* @openapi
* /chain/{currency}/contracts:
* get:
* tags: [Chain]
* description: Get the network information and contract addresses for a supported EVM chains
* parameters:
* - in: path
* name: currency
* required: true
* schema:
* type: string
* description: Currency of the chain to query for
* responses:
* '200':
* description: Raw transaction
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Contracts'
* '400':
* description: Error that caused the query for the transaction to fail
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ErrorResponse'
* '404':
* description: Error when the currency is not on an EVM chain
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ErrorResponse'
*/
router.get(
'/:currency/contracts',
this.handleError(this.getContractsForCurrency),
);

return router;
};

Expand All @@ -224,6 +329,17 @@ class ChainRouter extends RouterBase {
private getHeights = async (_: Request, res: Response) =>
successResponse(res, mapToObject(await this.service.getBlockHeights()));

private getContracts = async (_: Request, res: Response) => {
const contracts: Record<string, any> = {};
Object.entries(await this.service.getContracts()).forEach(
([key, value]) => {
contracts[key] = this.formatContracts(value);
},
);

successResponse(res, contracts);
};

private getFeeForChain = async (req: Request, res: Response) => {
const currency = this.getCurrencyFromPath(req);
successResponse(res, {
Expand Down Expand Up @@ -258,9 +374,38 @@ class ChainRouter extends RouterBase {
createdResponse(res, { id });
};

private getContractsForCurrency = async (req: Request, res: Response) => {
const currency = this.getCurrencyFromPath(req);
const manager = this.service.walletManager.ethereumManagers.find(
(manager) => manager.hasSymbol(currency),
);

if (manager === undefined) {
errorResponse(
this.logger,
req,
res,
'chain does not have contracts',
404,
);
return;
}

successResponse(
res,
this.formatContracts(await manager.getContractDetails()),
);
};

private getCurrencyFromPath = (req: Request): string =>
validateRequest(req.params, [{ name: 'currency', type: 'string' }])
.currency;

private formatContracts = (contracts: NetworkContracts) => ({
network: contracts.network,
tokens: mapToObject(contracts.tokens),
swapContracts: mapToObject(contracts.swapContracts),
});
}

export default ChainRouter;
22 changes: 21 additions & 1 deletion lib/api/v2/routers/NodesRouter.ts
@@ -1,7 +1,7 @@
import { Request, Response, Router } from 'express';
import Logger from '../../../Logger';
import Service from '../../../service/Service';
import { successResponse } from '../../Utils';
import { successResponse, validateRequest } from '../../Utils';
import RouterBase from './RouterBase';

class NodesRouter extends RouterBase {
Expand Down Expand Up @@ -98,6 +98,11 @@ class NodesRouter extends RouterBase {
*/
router.get('/stats', this.handleError(this.getNodeStats));

router.get(
'/:currency/:node/hints',
this.handleError(this.getRoutingHints),
);

return router;
};

Expand Down Expand Up @@ -135,6 +140,21 @@ class NodesRouter extends RouterBase {

successResponse(res, stats);
};

private getRoutingHints = async (req: Request, res: Response) => {
const { currency, node } = validateRequest(req.params, [
{ name: 'currency', type: 'string' },
{ name: 'node', type: 'string' },
]);

successResponse(
res,
await this.service.swapManager.routingHints.getRoutingHints(
currency,
node,
),
);
};
}

export default NodesRouter;

0 comments on commit d2e3489

Please sign in to comment.