Skip to content

Commit

Permalink
Remove ICR API from filters (and carbonmark-api) (#2250)
Browse files Browse the repository at this point in the history
* 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 <biwano@gmail.com>
  • Loading branch information
3 people authored Feb 19, 2024
1 parent 60a1cb6 commit 681d7cc
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 178 deletions.
2 changes: 1 addition & 1 deletion carbonmark-api/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
29 changes: 0 additions & 29 deletions carbonmark-api/src/app.constants.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion carbonmark-api/src/routes/countries/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion carbonmark-api/src/routes/projects/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions carbonmark-api/src/routes/projects/get.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion carbonmark-api/src/routes/vintages/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 0 additions & 33 deletions carbonmark-api/src/utils/ICR/icr.utils.ts
Original file line number Diff line number Diff line change
@@ -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" });
Expand All @@ -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 };
};
41 changes: 9 additions & 32 deletions carbonmark-api/src/utils/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<string[]> {
const uniqueValues = new Set<string>();
const cacheKey = `vintages`;
Expand All @@ -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");
}

Expand All @@ -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);
Expand Down Expand Up @@ -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<Country[]>(cacheKey)?.payload;
Expand All @@ -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");
}

Expand All @@ -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 });
Expand Down
6 changes: 1 addition & 5 deletions carbonmark-api/test/plugins/rate-limit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -26,7 +23,6 @@ describe("Rate Limiter", () => {
.persist(true);

mockMarketplaceArgs();
mockICRFilters();

//Because we are throwing errors in mock fetch we need to catch
try {
Expand Down
27 changes: 1 addition & 26 deletions carbonmark-api/test/routes/countries/get.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand Down
11 changes: 1 addition & 10 deletions carbonmark-api/test/routes/projects/get.test.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand All @@ -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[];
Expand All @@ -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"))
Expand Down
11 changes: 1 addition & 10 deletions carbonmark-api/test/routes/projects/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
mockCms,
mockDigitalCarbonArgs,
mockDigitalCarbonProjects,
mockICRFilters,
mockMarketplaceArgs,
mockMarketplaceProjects,
mockTokens,
Expand Down Expand Up @@ -91,7 +90,7 @@ describe("GET /projects", () => {
// Setup default mocks
beforeEach(async () => {
mockMarketplaceArgs();
mockICRFilters();

mockDigitalCarbonArgs();
mockTokens();
mockCms({
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -375,10 +370,6 @@ describe("GET /projects", () => {
const duplicateMarketplaceProject = cloneDeep(mockMarketplaceProject);
const duplicateDigitalCarbonProject = cloneDeep(mockDigitalCarbonProject);

beforeEach(() => {
mockICRFilters();
});

test("Marketplace projects", async () => {
mockMarketplaceProjects([
mockMarketplaceProject,
Expand Down
Loading

0 comments on commit 681d7cc

Please sign in to comment.