From d5d61bcdf5aa027103857adb47e0af9bcc477d2e Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Tue, 23 Jul 2024 15:28:09 -0300 Subject: [PATCH 1/4] feat: scaffold pricing module lib --- libs/pricing/src/index.ts | 2 + libs/pricing/src/pricing.module.ts | 9 + .../src/services/coingecko.service.spec.ts | 27 +++ .../pricing/src/services/coingecko.service.ts | 7 + libs/pricing/src/services/index.ts | 1 + libs/pricing/tsconfig.lib.json | 9 + nest-cli.json | 9 + package.json | 199 +++++++++--------- tsconfig.json | 66 +++--- 9 files changed, 194 insertions(+), 135 deletions(-) create mode 100644 libs/pricing/src/index.ts create mode 100644 libs/pricing/src/pricing.module.ts create mode 100644 libs/pricing/src/services/coingecko.service.spec.ts create mode 100644 libs/pricing/src/services/coingecko.service.ts create mode 100644 libs/pricing/src/services/index.ts create mode 100644 libs/pricing/tsconfig.lib.json diff --git a/libs/pricing/src/index.ts b/libs/pricing/src/index.ts new file mode 100644 index 0000000..6034301 --- /dev/null +++ b/libs/pricing/src/index.ts @@ -0,0 +1,2 @@ +export * from "./pricing.module"; +export * from "./services/coingecko.service"; diff --git a/libs/pricing/src/pricing.module.ts b/libs/pricing/src/pricing.module.ts new file mode 100644 index 0000000..5fc6280 --- /dev/null +++ b/libs/pricing/src/pricing.module.ts @@ -0,0 +1,9 @@ +import { Module } from "@nestjs/common"; + +import { CoingeckoService } from "./services"; + +@Module({ + providers: [CoingeckoService], + exports: [CoingeckoService], +}) +export class PricingModule {} diff --git a/libs/pricing/src/services/coingecko.service.spec.ts b/libs/pricing/src/services/coingecko.service.spec.ts new file mode 100644 index 0000000..6d330b5 --- /dev/null +++ b/libs/pricing/src/services/coingecko.service.spec.ts @@ -0,0 +1,27 @@ +import { Test, TestingModule } from "@nestjs/testing"; + +import { CoingeckoService } from "./coingecko.service"; + +describe("CoingeckoService", () => { + let service: CoingeckoService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: CoingeckoService, + useFactory: () => { + const apiKey = "COINGECKO_API_KEY"; + return new CoingeckoService(apiKey); + }, + }, + ], + }).compile(); + + service = module.get(CoingeckoService); + }); + + it("should be defined", () => { + expect(service).toBeDefined(); + }); +}); diff --git a/libs/pricing/src/services/coingecko.service.ts b/libs/pricing/src/services/coingecko.service.ts new file mode 100644 index 0000000..e93a9f9 --- /dev/null +++ b/libs/pricing/src/services/coingecko.service.ts @@ -0,0 +1,7 @@ +import { Injectable } from "@nestjs/common"; + +@Injectable() +export class CoingeckoService { + private readonly API_BASE_URL = "https://api.coingecko.com/api/v3/"; + constructor(private readonly apiKey: string) {} +} diff --git a/libs/pricing/src/services/index.ts b/libs/pricing/src/services/index.ts new file mode 100644 index 0000000..2dde225 --- /dev/null +++ b/libs/pricing/src/services/index.ts @@ -0,0 +1 @@ +export * from "./coingecko.service"; diff --git a/libs/pricing/tsconfig.lib.json b/libs/pricing/tsconfig.lib.json new file mode 100644 index 0000000..c92c214 --- /dev/null +++ b/libs/pricing/tsconfig.lib.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "declaration": true, + "outDir": "../../dist/libs/pricing" + }, + "include": ["src/**/*"], + "exclude": ["node_modules", "dist", "test", "**/*spec.ts"] +} diff --git a/nest-cli.json b/nest-cli.json index db2e7fb..7c2a3b7 100644 --- a/nest-cli.json +++ b/nest-cli.json @@ -45,6 +45,15 @@ "compilerOptions": { "tsConfigPath": "libs/dtos/tsconfig.lib.json" } + }, + "pricing": { + "type": "library", + "root": "libs/pricing", + "entryFile": "index", + "sourceRoot": "libs/pricing/src", + "compilerOptions": { + "tsConfigPath": "libs/pricing/tsconfig.lib.json" + } } } } diff --git a/package.json b/package.json index 9933d47..ae765c1 100644 --- a/package.json +++ b/package.json @@ -1,102 +1,103 @@ { - "name": "zkchainHub", - "version": "0.0.1", - "description": "", - "author": "", - "private": true, - "license": "UNLICENSED", - "scripts": { - "build": "nest build", - "format": "prettier --write \"{apps,libs}/**/*.ts\"", - "start": "nest start", - "start:dev": "nest start --watch", - "start:debug": "nest start --debug --watch", - "start:prod": "node dist/apps/api/main", - "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", - "test": "jest", - "test:watch": "jest --watch", - "test:cov": "jest --coverage", - "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./apps/api/test/jest-e2e.json", - "prepare": "husky", - "preinstall": "npx only-allow pnpm" - }, - "dependencies": { - "@nestjs/common": "10.0.0", - "@nestjs/core": "10.0.0", - "@nestjs/platform-express": "10.0.0", - "@nestjs/swagger": "7.4.0", - "abitype": "1.0.5", - "reflect-metadata": "0.1.13", - "rxjs": "7.8.1", - "viem": "2.17.5" - }, - "devDependencies": { - "@commitlint/config-conventional": "19.2.2", - "@golevelup/ts-jest": "0.5.0", - "@ianvs/prettier-plugin-sort-imports": "4.3.0", - "@nestjs/cli": "10.0.0", - "@nestjs/schematics": "10.0.0", - "@nestjs/testing": "10.0.0", - "@total-typescript/tsconfig": "1.0.4", - "@types/express": "4.17.17", - "@types/jest": "29.5.2", - "@types/node": "20.3.1", - "@types/supertest": "6.0.0", - "@typescript-eslint/eslint-plugin": "7.0.0", - "@typescript-eslint/parser": "7.0.0", - "commitlint": "19.3.0", - "eslint": "8.42.0", - "eslint-config-prettier": "9.0.0", - "eslint-plugin-prettier": "5.0.0", - "husky": "9.0.11", - "jest": "29.5.0", - "lint-staged": "15.2.7", - "prettier": "3.0.0", - "source-map-support": "0.5.21", - "supertest": "7.0.0", - "ts-jest": "29.1.0", - "ts-loader": "9.4.3", - "ts-node": "10.9.1", - "tsconfig-paths": "4.2.0", - "typescript": "5.1.3" - }, - "jest": { - "moduleFileExtensions": [ - "js", - "json", - "ts" - ], - "rootDir": ".", - "testRegex": ".*\\.spec\\.ts$", - "transform": { - ".+\\.(t|j)s$": "ts-jest" + "name": "zkchainHub", + "version": "0.0.1", + "description": "", + "author": "", + "private": true, + "license": "UNLICENSED", + "scripts": { + "build": "nest build", + "format": "prettier --write \"{apps,libs}/**/*.ts\"", + "start": "nest start", + "start:dev": "nest start --watch", + "start:debug": "nest start --debug --watch", + "start:prod": "node dist/apps/api/main", + "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", + "test": "jest", + "test:watch": "jest --watch", + "test:cov": "jest --coverage", + "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", + "test:e2e": "jest --config ./apps/api/test/jest-e2e.json", + "prepare": "husky", + "preinstall": "npx only-allow pnpm" }, - "collectCoverageFrom": [ - "**/*.(t|j)s" - ], - "coverageDirectory": "./coverage", - "coveragePathIgnorePatterns": [ - "/node_modules/", - ".e2e-spec.ts", - ".module.ts", - "main.ts" - ], - "testEnvironment": "node", - "roots": [ - "/apps/", - "/libs/" - ], - "moduleNameMapper": { - "^@packages/providers(|/.*)$": "/libs/providers/src/$1", - "^@shared/dtos(|/.*)$": "/libs/dtos/src/$1" + "dependencies": { + "@nestjs/common": "10.0.0", + "@nestjs/core": "10.0.0", + "@nestjs/platform-express": "10.0.0", + "@nestjs/swagger": "7.4.0", + "abitype": "1.0.5", + "reflect-metadata": "0.1.13", + "rxjs": "7.8.1", + "viem": "2.17.5" + }, + "devDependencies": { + "@commitlint/config-conventional": "19.2.2", + "@golevelup/ts-jest": "0.5.0", + "@ianvs/prettier-plugin-sort-imports": "4.3.0", + "@nestjs/cli": "10.0.0", + "@nestjs/schematics": "10.0.0", + "@nestjs/testing": "10.0.0", + "@total-typescript/tsconfig": "1.0.4", + "@types/express": "4.17.17", + "@types/jest": "29.5.2", + "@types/node": "20.3.1", + "@types/supertest": "6.0.0", + "@typescript-eslint/eslint-plugin": "7.0.0", + "@typescript-eslint/parser": "7.0.0", + "commitlint": "19.3.0", + "eslint": "8.42.0", + "eslint-config-prettier": "9.0.0", + "eslint-plugin-prettier": "5.0.0", + "husky": "9.0.11", + "jest": "29.5.0", + "lint-staged": "15.2.7", + "prettier": "3.0.0", + "source-map-support": "0.5.21", + "supertest": "7.0.0", + "ts-jest": "29.1.0", + "ts-loader": "9.4.3", + "ts-node": "10.9.1", + "tsconfig-paths": "4.2.0", + "typescript": "5.1.3" + }, + "jest": { + "moduleFileExtensions": [ + "js", + "json", + "ts" + ], + "rootDir": ".", + "testRegex": ".*\\.spec\\.ts$", + "transform": { + ".+\\.(t|j)s$": "ts-jest" + }, + "collectCoverageFrom": [ + "**/*.(t|j)s" + ], + "coverageDirectory": "./coverage", + "coveragePathIgnorePatterns": [ + "/node_modules/", + ".e2e-spec.ts", + ".module.ts", + "main.ts" + ], + "testEnvironment": "node", + "roots": [ + "/apps/", + "/libs/" + ], + "moduleNameMapper": { + "^@packages/providers(|/.*)$": "/libs/providers/src/$1", + "^@shared/dtos(|/.*)$": "/libs/dtos/src/$1", + "^@packages/pricing(|/.*)$": "/libs/pricing/src/$1" + } + }, + "packageManager": "pnpm@9.5.0+sha1.8c155dc114e1689d18937974f6571e0ceee66f1d", + "lint-staged": { + "(apps|libs)/**/*.(ts|js)": [ + "pnpm lint", + "pnpm format" + ] } - }, - "packageManager": "pnpm@9.5.0+sha1.8c155dc114e1689d18937974f6571e0ceee66f1d", - "lint-staged": { - "(apps|libs)/**/*.(ts|js)": [ - "pnpm lint", - "pnpm format" - ] - } -} \ No newline at end of file +} diff --git a/tsconfig.json b/tsconfig.json index 3fa7db0..aff3c40 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,37 +1,31 @@ { - "extends": "@total-typescript/tsconfig/tsc/no-dom/app", - "compilerOptions": { - "module": "commonjs", - "declaration": true, - "removeComments": true, - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "allowSyntheticDefaultImports": true, - "target": "ES2021", - "sourceMap": true, - "outDir": "./dist", - "baseUrl": "./", - "incremental": true, - "skipLibCheck": true, - "strictNullChecks": true, - "noImplicitAny": true, - "strictBindCallApply": true, - "forceConsistentCasingInFileNames": true, - "noFallthroughCasesInSwitch": true, - "paths": { - "@packages/providers": [ - "libs/providers/src" - ], - "@packages/providers/*": [ - "libs/providers/src/*" - ], - "@shared/dtos": [ - "libs/dtos/src" - ], - "@shared/dtos/*": [ - "libs/dtos/src/*" - ] - }, - "verbatimModuleSyntax": false - } -} \ No newline at end of file + "extends": "@total-typescript/tsconfig/tsc/no-dom/app", + "compilerOptions": { + "module": "commonjs", + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "target": "ES2021", + "sourceMap": true, + "outDir": "./dist", + "baseUrl": "./", + "incremental": true, + "skipLibCheck": true, + "strictNullChecks": true, + "noImplicitAny": true, + "strictBindCallApply": true, + "forceConsistentCasingInFileNames": true, + "noFallthroughCasesInSwitch": true, + "paths": { + "@packages/providers": ["libs/providers/src"], + "@packages/providers/*": ["libs/providers/src/*"], + "@shared/dtos": ["libs/dtos/src"], + "@shared/dtos/*": ["libs/dtos/src/*"], + "@packages/pricing": ["libs/pricing/src"], + "@packages/pricing/*": ["libs/pricing/src/*"] + }, + "verbatimModuleSyntax": false + } +} From e503a936ac7b1a697a462e196984f04de363ecf8 Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:29:38 -0300 Subject: [PATCH 2/4] feat: add pricing service interface --- libs/pricing/src/index.ts | 1 + libs/pricing/src/interfaces/index.ts | 1 + .../src/interfaces/pricing.interface.ts | 19 +++++++++++++++++++ .../pricing/src/services/coingecko.service.ts | 10 +++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 libs/pricing/src/interfaces/index.ts create mode 100644 libs/pricing/src/interfaces/pricing.interface.ts diff --git a/libs/pricing/src/index.ts b/libs/pricing/src/index.ts index 6034301..f7850bb 100644 --- a/libs/pricing/src/index.ts +++ b/libs/pricing/src/index.ts @@ -1,2 +1,3 @@ export * from "./pricing.module"; export * from "./services/coingecko.service"; +export * from "./interfaces"; diff --git a/libs/pricing/src/interfaces/index.ts b/libs/pricing/src/interfaces/index.ts new file mode 100644 index 0000000..2a231ae --- /dev/null +++ b/libs/pricing/src/interfaces/index.ts @@ -0,0 +1 @@ +export * from "./pricing.interface"; diff --git a/libs/pricing/src/interfaces/pricing.interface.ts b/libs/pricing/src/interfaces/pricing.interface.ts new file mode 100644 index 0000000..34055d2 --- /dev/null +++ b/libs/pricing/src/interfaces/pricing.interface.ts @@ -0,0 +1,19 @@ +/** + * Represents a pricing service that provides token prices. + */ +/** + * Represents a pricing service that retrieves token prices. + */ +export interface IPricingService { + /** + * Retrieves the prices of the specified tokens. + * @param tokenIds - An array of token IDs. + * @param [config] - Optional configuration object. + * @param config.currency - The currency in which the prices should be returned. + * @returns A promise that resolves to a record containing the token IDs as keys and their corresponding prices as values. + */ + getTokenPrices( + tokenIds: TokenId[], + config?: { currency: string }, + ): Promise>; +} diff --git a/libs/pricing/src/services/coingecko.service.ts b/libs/pricing/src/services/coingecko.service.ts index e93a9f9..ee9838d 100644 --- a/libs/pricing/src/services/coingecko.service.ts +++ b/libs/pricing/src/services/coingecko.service.ts @@ -1,7 +1,15 @@ import { Injectable } from "@nestjs/common"; +import { IPricingService } from "@packages/pricing/interfaces"; @Injectable() -export class CoingeckoService { +export class CoingeckoService implements IPricingService { private readonly API_BASE_URL = "https://api.coingecko.com/api/v3/"; constructor(private readonly apiKey: string) {} + + async getTokenPrices( + _tokenIds: string[], + _config: { currency: string } = { currency: "usd" }, + ): Promise> { + throw new Error("Method not implemented."); + } } From c75ad345139dc2e7a617784ee54848c73e3a7af7 Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Mon, 29 Jul 2024 11:06:23 -0300 Subject: [PATCH 3/4] refactor: change pricing package alias to @zkchainhub --- libs/pricing/src/services/coingecko.service.ts | 3 ++- tsconfig.json | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/libs/pricing/src/services/coingecko.service.ts b/libs/pricing/src/services/coingecko.service.ts index ee9838d..6da4328 100644 --- a/libs/pricing/src/services/coingecko.service.ts +++ b/libs/pricing/src/services/coingecko.service.ts @@ -1,5 +1,6 @@ import { Injectable } from "@nestjs/common"; -import { IPricingService } from "@packages/pricing/interfaces"; + +import { IPricingService } from "@zkchainhub/pricing/interfaces"; @Injectable() export class CoingeckoService implements IPricingService { diff --git a/tsconfig.json b/tsconfig.json index aff3c40..d9a9280 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -23,8 +23,8 @@ "@packages/providers/*": ["libs/providers/src/*"], "@shared/dtos": ["libs/dtos/src"], "@shared/dtos/*": ["libs/dtos/src/*"], - "@packages/pricing": ["libs/pricing/src"], - "@packages/pricing/*": ["libs/pricing/src/*"] + "@zkchainhub/pricing": ["libs/pricing/src"], + "@zkchainhub/pricing/*": ["libs/pricing/src/*"] }, "verbatimModuleSyntax": false } From 25da8d2fbbc98d4e89d28b720b373aaf003ab772 Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Mon, 29 Jul 2024 13:42:02 -0300 Subject: [PATCH 4/4] feat: set base url as constructor param --- libs/pricing/src/services/coingecko.service.spec.ts | 3 ++- libs/pricing/src/services/coingecko.service.ts | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/libs/pricing/src/services/coingecko.service.spec.ts b/libs/pricing/src/services/coingecko.service.spec.ts index 6d330b5..2c58646 100644 --- a/libs/pricing/src/services/coingecko.service.spec.ts +++ b/libs/pricing/src/services/coingecko.service.spec.ts @@ -12,7 +12,8 @@ describe("CoingeckoService", () => { provide: CoingeckoService, useFactory: () => { const apiKey = "COINGECKO_API_KEY"; - return new CoingeckoService(apiKey); + const apiBaseUrl = "https://api.coingecko.com/api/v3/"; + return new CoingeckoService(apiKey, apiBaseUrl); }, }, ], diff --git a/libs/pricing/src/services/coingecko.service.ts b/libs/pricing/src/services/coingecko.service.ts index 6da4328..1d853eb 100644 --- a/libs/pricing/src/services/coingecko.service.ts +++ b/libs/pricing/src/services/coingecko.service.ts @@ -4,8 +4,10 @@ import { IPricingService } from "@zkchainhub/pricing/interfaces"; @Injectable() export class CoingeckoService implements IPricingService { - private readonly API_BASE_URL = "https://api.coingecko.com/api/v3/"; - constructor(private readonly apiKey: string) {} + constructor( + private readonly apiKey: string, + private readonly apiBaseUrl: string = "https://api.coingecko.com/api/v3/", + ) {} async getTokenPrices( _tokenIds: string[],