From 681d7cc0003b92aded76fba15f6e13b1be887486 Mon Sep 17 00:00:00 2001 From: p_sparacino <72105234+psparacino@users.noreply.github.com> Date: Mon, 19 Feb 2024 01:37:18 -0500 Subject: [PATCH] Remove ICR API from filters (and carbonmark-api) (#2250) * api: remove all ICR api from filters * api: removed all ICR filters from tests * api: run prettier and increment version * api: remove all ICR api from filters * api: removed all ICR filters from tests * api: run prettier and increment version * api: updated version in package-lock * api: increment version * Updated api version --------- Co-authored-by: Michael Collins <0xemc@protonmail.com> Co-authored-by: biwano --- carbonmark-api/package.json | 2 +- carbonmark-api/src/app.constants.ts | 29 ------------- carbonmark-api/src/routes/countries/get.ts | 2 +- carbonmark-api/src/routes/projects/get.ts | 2 +- .../src/routes/projects/get.utils.ts | 7 ++-- carbonmark-api/src/routes/vintages/get.ts | 2 +- carbonmark-api/src/utils/ICR/icr.utils.ts | 33 --------------- carbonmark-api/src/utils/helpers/utils.ts | 41 ++++--------------- .../test/plugins/rate-limit.test.ts | 6 +-- .../test/routes/countries/get.test.ts | 27 +----------- .../test/routes/projects/get.test.mocks.ts | 11 +---- .../test/routes/projects/get.test.ts | 11 +---- .../test/routes/vintages/get.test.ts | 25 +---------- package-lock.json | 2 +- 14 files changed, 22 insertions(+), 178 deletions(-) diff --git a/carbonmark-api/package.json b/carbonmark-api/package.json index 2a887fea0c..1aa1d30123 100644 --- a/carbonmark-api/package.json +++ b/carbonmark-api/package.json @@ -1,6 +1,6 @@ { "name": "@klimadao/carbonmark-api", - "version": "6.2.1", + "version": "6.2.2", "description": "An API for exploring Carbonmark project data, prices and activity.", "main": "app.ts", "scripts": { diff --git a/carbonmark-api/src/app.constants.ts b/carbonmark-api/src/app.constants.ts index 80a7841fcd..c45ed2ced1 100644 --- a/carbonmark-api/src/app.constants.ts +++ b/carbonmark-api/src/app.constants.ts @@ -1,5 +1,3 @@ -import { NetworkParam } from "./models/NetworkParam.model"; - /** Adhere to JSONSchema spec by using a URI */ export const COMMON_SCHEMA_URI = "http://api.carbonmark.com/schemas"; @@ -105,33 +103,6 @@ export const REGISTRIES = { }, }; -export const ICR_API = ( - network?: NetworkParam -): { ICR_API_URL: string; ICR_API_KEY: string } => { - const ICR_CONFIG = { - polygon: { - url: "https://api.carbonregistry.com/v0", - apiKey: process.env.ICR_MAINNET_API_KEY, - }, - mumbai: { - url: "https://gaia-api-dev.mojoflower.io/v0", - apiKey: process.env.ICR_MUMBAI_API_KEY, - }, - }; - - const VALIDATED_NETWORK: NetworkParam = - network === "polygon" || network === "mumbai" ? network : "polygon"; - - const API_CONFIG = ICR_CONFIG["polygon"]; - - if (!API_CONFIG.apiKey) { - throw new Error( - `ICR api key is undefined for network: ${VALIDATED_NETWORK}` - ); - } - - return { ICR_API_URL: API_CONFIG.url, ICR_API_KEY: API_CONFIG.apiKey }; -}; /** Message shared with frontend, to be combined with user's nonce and signed by private key. */ export const SIGN_PROFILE_MESSAGE = process.env.SIGN_PROFILE_MESSAGE || "VerifyCarbonmarkProfileEdit"; diff --git a/carbonmark-api/src/routes/countries/get.ts b/carbonmark-api/src/routes/countries/get.ts index 728db3e4a8..3f6fd31f47 100644 --- a/carbonmark-api/src/routes/countries/get.ts +++ b/carbonmark-api/src/routes/countries/get.ts @@ -12,7 +12,7 @@ const handler = ( const network = request.query.network ?? "polygon"; const sdk = gql_sdk(network); try { - response = await getAllCountries(sdk, fastify, network); + response = await getAllCountries(sdk, fastify); } catch (error) { //Return bad gateway and pass the error console.error(error); diff --git a/carbonmark-api/src/routes/projects/get.ts b/carbonmark-api/src/routes/projects/get.ts index c0efb74601..77eb7e405a 100644 --- a/carbonmark-api/src/routes/projects/get.ts +++ b/carbonmark-api/src/routes/projects/get.ts @@ -42,7 +42,7 @@ const handler = (fastify: FastifyInstance) => ); //Get the default args to return all results unless specified - const allOptions = await getDefaultQueryArgs(sdk, fastify, network); + const allOptions = await getDefaultQueryArgs(sdk, fastify); const [ marketplaceProjectsData, diff --git a/carbonmark-api/src/routes/projects/get.utils.ts b/carbonmark-api/src/routes/projects/get.utils.ts index 50654c5aa8..031aaa9cde 100644 --- a/carbonmark-api/src/routes/projects/get.utils.ts +++ b/carbonmark-api/src/routes/projects/get.utils.ts @@ -45,13 +45,12 @@ import { formatListings } from "../../utils/marketplace.utils"; export const getDefaultQueryArgs = async ( sdk: GQL_SDK, - fastify: FastifyInstance, - network: NetworkParam + fastify: FastifyInstance ) => { const [category, country, vintage] = await Promise.all([ getAllCategories(sdk, fastify).then(map(extract("id"))), - getAllCountries(sdk, fastify, network).then(map(extract("id"))), - getAllVintages(sdk, fastify, network), + getAllCountries(sdk, fastify).then(map(extract("id"))), + getAllVintages(sdk, fastify), ]); return { diff --git a/carbonmark-api/src/routes/vintages/get.ts b/carbonmark-api/src/routes/vintages/get.ts index d7363397f6..e2f6c975ba 100644 --- a/carbonmark-api/src/routes/vintages/get.ts +++ b/carbonmark-api/src/routes/vintages/get.ts @@ -13,7 +13,7 @@ const handler = (fastify: FastifyInstance) => const network = request.query.network ?? "polygon"; const sdk = gql_sdk(network); try { - response = await getAllVintages(sdk, fastify, network); + response = await getAllVintages(sdk, fastify); } catch (error) { //Return bad gateway and pass the error console.error(error); diff --git a/carbonmark-api/src/utils/ICR/icr.utils.ts b/carbonmark-api/src/utils/ICR/icr.utils.ts index 6aba1c49b6..051c676e7d 100644 --- a/carbonmark-api/src/utils/ICR/icr.utils.ts +++ b/carbonmark-api/src/utils/ICR/icr.utils.ts @@ -1,8 +1,3 @@ -import { isArray } from "lodash"; -import fetch from "node-fetch"; -import { NetworkParam } from "src/models/NetworkParam.model"; -import { ICR_API } from "../../../src/app.constants"; - export const convertIcrCountryCodeToName = (code: string) => { if (!code) return; const regionNames = new Intl.DisplayNames(["en"], { type: "region" }); @@ -16,31 +11,3 @@ export const createICRProjectID = (serialization: string) => { const num = elements[3]; return `${registry}-${num}`; }; - -export const fetchIcrFilters = async (network: NetworkParam) => { - const { ICR_API_URL, ICR_API_KEY } = ICR_API(network); - - const url = `${ICR_API_URL}/public/projects/filters`; - const IcrResponse = await fetch(url, { - method: "GET", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${ICR_API_KEY}`, - }, - }); - - const { vintages: IcrVintages, countryCodes: IcrCountryCodes } = - await IcrResponse.json(); - - if (!isArray(IcrCountryCodes) || !isArray(IcrVintages)) { - throw new Error("Response from server did not match schema definition"); - } - - const countryNames = await Promise.all( - IcrCountryCodes.map((countryCode: string) => - convertIcrCountryCodeToName(countryCode) - ) - ); - - return { IcrVintages, countryNames }; -}; diff --git a/carbonmark-api/src/utils/helpers/utils.ts b/carbonmark-api/src/utils/helpers/utils.ts index fad6750837..764a3e3cbc 100644 --- a/carbonmark-api/src/utils/helpers/utils.ts +++ b/carbonmark-api/src/utils/helpers/utils.ts @@ -9,9 +9,7 @@ import { } from "../../.generated/types/marketplace.types"; import { CarbonOffset } from "../../.generated/types/offsets.types"; -import type { NetworkParam } from "../../../src/models/NetworkParam.model"; import { TOKEN_ADDRESSES } from "../../app.constants"; -import { fetchIcrFilters } from "../ICR/icr.utils"; import { extract, notEmptyOrNil } from "../functional.utils"; import { GQL_SDK } from "../gqlSdk"; import { CarbonProject } from "./cms.utils"; @@ -25,8 +23,7 @@ const ENV = (process.env.VERCEL_ENV ?? "development") as // combines them, removes duplicates, and returns the result as a sorted array of strings. export async function getAllVintages( sdk: GQL_SDK, - fastify: FastifyInstance, - network: NetworkParam + fastify: FastifyInstance ): Promise { const uniqueValues = new Set(); const cacheKey = `vintages`; @@ -36,22 +33,14 @@ export async function getAllVintages( return cachedResult; } - const [ - { projects }, - { carbonProjects: digitalCarbonProjects }, - { IcrVintages }, - ] = await Promise.all([ - sdk.marketplace.getVintages(), - sdk.digital_carbon.getDigitalCarbonProjectsVintages(), - fetchIcrFilters(network), - ]); + const [{ projects }, { carbonProjects: digitalCarbonProjects }] = + await Promise.all([ + sdk.marketplace.getVintages(), + sdk.digital_carbon.getDigitalCarbonProjectsVintages(), + ]); /** Handle invalid responses */ - if ( - !isArray(projects) || - !isArray(digitalCarbonProjects) || - !isArray(IcrVintages) - ) { + if (!isArray(projects) || !isArray(digitalCarbonProjects)) { throw new Error("Response from server did not match schema definition"); } @@ -62,7 +51,6 @@ export async function getAllVintages( uniqueValues.add(credit.vintage.toString()); } }); - IcrVintages.forEach((item: string) => uniqueValues.add(item)); }); const result = Array.from(uniqueValues).sort().filter(notEmptyOrNil); @@ -129,11 +117,7 @@ export async function getAllCategories(sdk: GQL_SDK, fastify: FastifyInstance) { return result; } -export async function getAllCountries( - sdk: GQL_SDK, - fastify: FastifyInstance, - network: NetworkParam -) { +export async function getAllCountries(sdk: GQL_SDK, fastify: FastifyInstance) { const cacheKey = `countries`; const cachedResult = await fastify.lcache?.get(cacheKey)?.payload; @@ -145,19 +129,13 @@ export async function getAllCountries( const [ { countries: marketplaceCountries }, { carbonProjects: digitalCarbonProjects }, - { countryNames: icrCountries }, ] = await Promise.all([ sdk.marketplace.getCountries(), sdk.digital_carbon.getDigitalCarbonProjectsCountries(), - fetchIcrFilters(network), ]); /** Handle invalid responses */ - if ( - !isArray(marketplaceCountries) || - !isArray(digitalCarbonProjects) || - !isArray(icrCountries) - ) { + if (!isArray(marketplaceCountries) || !isArray(digitalCarbonProjects)) { throw new Error("Response from server did not match schema definition"); } @@ -172,7 +150,6 @@ export async function getAllCountries( const result: Country[] = fn([ marketplaceCountries?.map(extract("id")), digitalCarbonProjects.map(extract("country")), - icrCountries, ]); await fastify.lcache?.set(cacheKey, { payload: result }); diff --git a/carbonmark-api/test/plugins/rate-limit.test.ts b/carbonmark-api/test/plugins/rate-limit.test.ts index 91639ffaba..d43048cab0 100644 --- a/carbonmark-api/test/plugins/rate-limit.test.ts +++ b/carbonmark-api/test/plugins/rate-limit.test.ts @@ -3,10 +3,7 @@ import nock from "nock"; import { GRAPH_URLS } from "../../src/app.constants"; import { LIMIT } from "../../src/plugins/rate-limit"; import { build } from "../helper"; -import { - mockICRFilters, - mockMarketplaceArgs, -} from "../routes/projects/get.test.mocks"; +import { mockMarketplaceArgs } from "../routes/projects/get.test.mocks"; import { mock_fetch } from "../test.utils"; describe("Rate Limiter", () => { @@ -26,7 +23,6 @@ describe("Rate Limiter", () => { .persist(true); mockMarketplaceArgs(); - mockICRFilters(); //Because we are throwing errors in mock fetch we need to catch try { diff --git a/carbonmark-api/test/routes/countries/get.test.ts b/carbonmark-api/test/routes/countries/get.test.ts index 72eb2a3d5d..4758d6ee03 100644 --- a/carbonmark-api/test/routes/countries/get.test.ts +++ b/carbonmark-api/test/routes/countries/get.test.ts @@ -1,19 +1,14 @@ import { FastifyInstance } from "fastify"; import nock from "nock"; -import { GRAPH_URLS, ICR_API } from "../../../src/app.constants"; -import { COUNTRY_CODES, VINTAGES } from "../../../test/fixtures/icr"; +import { GRAPH_URLS } from "../../../src/app.constants"; import { build } from "../../helper"; import { COUNTRIES, DEV_URL, ERROR } from "../../test.constants"; describe("GET /countries", () => { let fastify: FastifyInstance; - let ICR_API_URL: string; // Setup the server beforeEach(async () => { - const icrApiValues = ICR_API("polygon"); - ICR_API_URL = icrApiValues.ICR_API_URL; - fastify = await build(); nock.cleanAll(); }); @@ -35,11 +30,6 @@ describe("GET /countries", () => { .post("") .reply(200, { data: { carbonProjects: COUNTRIES } }); - nock(ICR_API_URL).persist().get("/public/projects/filters").reply(200, { - vintages: VINTAGES, - countryCodes: COUNTRY_CODES, - }); - const response = await fastify.inject({ method: "GET", url: `${DEV_URL}/countries`, @@ -60,11 +50,6 @@ describe("GET /countries", () => { nock(GRAPH_URLS["polygon"].digitalCarbon).post("").reply(200, []); - nock(ICR_API_URL).persist().get("/public/projects/filters").reply(200, { - vintages: VINTAGES, - countryCodes: COUNTRY_CODES, - }); - const response = await fastify.inject({ method: "GET", url: `${DEV_URL}/countries`, @@ -83,11 +68,6 @@ describe("GET /countries", () => { .post("") .reply(200, { data: { carbonProjects: [] } }); - nock(ICR_API_URL).persist().get("/public/projects/filters").reply(200, { - vintages: [], - countryCodes: [], - }); - const response = await fastify.inject({ method: "GET", url: `${DEV_URL}/countries`, @@ -108,11 +88,6 @@ describe("GET /countries", () => { .post("") .reply(200, { data: { carbonProjects: "invalid data" } }); - nock(ICR_API_URL).persist().get("/public/projects/filters").reply(200, { - vintages: "invalid data", - countryCodes: "invalid data", - }); - const response = await fastify.inject({ method: "GET", url: `${DEV_URL}/countries`, diff --git a/carbonmark-api/test/routes/projects/get.test.mocks.ts b/carbonmark-api/test/routes/projects/get.test.mocks.ts index 77d2c56b7c..052a3fcfbc 100644 --- a/carbonmark-api/test/routes/projects/get.test.mocks.ts +++ b/carbonmark-api/test/routes/projects/get.test.mocks.ts @@ -5,8 +5,7 @@ import { } from "src/.generated/types/cms.types"; import { CarbonProject } from "src/.generated/types/digitalCarbon.types"; import { Project } from "src/.generated/types/marketplace.types"; -import { GRAPH_URLS, ICR_API, SANITY_URLS } from "../../../src/app.constants"; -import { COUNTRY_CODES, VINTAGES } from "../../../test/fixtures/icr"; +import { GRAPH_URLS, SANITY_URLS } from "../../../src/app.constants"; import { fixtures } from "../../fixtures"; /** @@ -15,8 +14,6 @@ import { fixtures } from "../../fixtures"; */ let cmsInterceptor: Interceptor; -const ICR_API_URL = ICR_API("polygon").ICR_API_URL; - export const mockCms = (overrides?: { projects?: CMSProject[]; content?: CMSProjectContent[]; @@ -41,12 +38,6 @@ export const mockCms = (overrides?: { }); }; -export const mockICRFilters = () => - nock(ICR_API_URL).persist().get("/public/projects/filters").reply(200, { - vintages: VINTAGES, - countryCodes: COUNTRY_CODES, - }); - export const mockTokens = () => nock(GRAPH_URLS["polygon"].tokens) .post("", (body) => body.query.includes("getPoolPrices")) diff --git a/carbonmark-api/test/routes/projects/get.test.ts b/carbonmark-api/test/routes/projects/get.test.ts index e44f6148a0..5e968695df 100644 --- a/carbonmark-api/test/routes/projects/get.test.ts +++ b/carbonmark-api/test/routes/projects/get.test.ts @@ -24,7 +24,6 @@ import { mockCms, mockDigitalCarbonArgs, mockDigitalCarbonProjects, - mockICRFilters, mockMarketplaceArgs, mockMarketplaceProjects, mockTokens, @@ -91,7 +90,7 @@ describe("GET /projects", () => { // Setup default mocks beforeEach(async () => { mockMarketplaceArgs(); - mockICRFilters(); + mockDigitalCarbonArgs(); mockTokens(); mockCms({ @@ -298,10 +297,6 @@ describe("GET /projects", () => { describe("Supply filtering", () => { let projects: Project[]; - beforeEach(() => { - mockICRFilters(); - }); - test("No filtering when supply greater than 0 (DigitalCarbon)", async () => { mockMarketplaceProjects([]); //Return two projects with supply @@ -375,10 +370,6 @@ describe("GET /projects", () => { const duplicateMarketplaceProject = cloneDeep(mockMarketplaceProject); const duplicateDigitalCarbonProject = cloneDeep(mockDigitalCarbonProject); - beforeEach(() => { - mockICRFilters(); - }); - test("Marketplace projects", async () => { mockMarketplaceProjects([ mockMarketplaceProject, diff --git a/carbonmark-api/test/routes/vintages/get.test.ts b/carbonmark-api/test/routes/vintages/get.test.ts index 9f51e268e0..0482ff738a 100644 --- a/carbonmark-api/test/routes/vintages/get.test.ts +++ b/carbonmark-api/test/routes/vintages/get.test.ts @@ -1,20 +1,15 @@ import { FastifyInstance } from "fastify"; import nock from "nock"; import { aProject } from "../../../src/.generated/mocks/marketplace.mocks"; -import { GRAPH_URLS, ICR_API } from "../../../src/app.constants"; -import { COUNTRY_CODES, VINTAGES } from "../../../test/fixtures/icr"; +import { GRAPH_URLS } from "../../../src/app.constants"; import { build } from "../../helper"; import { DEV_URL, ERROR } from "../../test.constants"; describe("GET /vintages", () => { let fastify: FastifyInstance; - let ICR_API_URL: string; // Setup the server beforeEach(async () => { - const icrApiValues = ICR_API("polygon"); - ICR_API_URL = icrApiValues.ICR_API_URL; - fastify = await build(); }); @@ -31,10 +26,6 @@ describe("GET /vintages", () => { nock(GRAPH_URLS["polygon"].marketplace) .post("") .reply(200, { data: { projects: [mock] } }); - nock(ICR_API_URL).get("/public/projects/filters").reply(200, { - vintages: VINTAGES, - countryCodes: COUNTRY_CODES, - }); const response = await fastify.inject({ method: "GET", @@ -55,11 +46,6 @@ describe("GET /vintages", () => { errors: [ERROR], }); - nock(ICR_API_URL).get("/public/projects/filters").reply(200, { - vintages: VINTAGES, - countryCodes: COUNTRY_CODES, - }); - const response = await fastify.inject({ method: "GET", url: `${DEV_URL}/vintages`, @@ -74,11 +60,6 @@ describe("GET /vintages", () => { .post("") .reply(200, { data: { projects: [] } }); - nock(ICR_API_URL).get("/public/projects/filters").reply(200, { - vintages: [], - countryCodes: [], - }); - const response = await fastify.inject({ method: "GET", url: `${DEV_URL}/vintages`, @@ -94,10 +75,6 @@ describe("GET /vintages", () => { .post("") .reply(200, { data: { projects: "invalid data" } }); - nock(ICR_API_URL).get("/public/projects/filters").reply(200, { - vintages: "invalid data", - countryCodes: "invalid data", - }); const response = await fastify.inject({ method: "GET", url: `${DEV_URL}/vintages`, diff --git a/package-lock.json b/package-lock.json index 5a8db3c895..fe98601120 100644 --- a/package-lock.json +++ b/package-lock.json @@ -590,7 +590,7 @@ }, "carbonmark-api": { "name": "@klimadao/carbonmark-api", - "version": "6.2.1", + "version": "6.2.2", "license": "ISC", "dependencies": { "@fastify/autoload": "^5.0.0",