Skip to content

Commit

Permalink
refactor: migrate rounds and passport query to data layer (#2731)
Browse files Browse the repository at this point in the history
* chore: migrate `get round by id` query to data layer

* chore: eagerly validate chains against chain ids

* chore: make config item optional

* chore: move get rounds query to data layer

* chore: move passport verification to the data layer

* chore: tidy up test

* chore: remove positional arguments

* lint

* fix: handle empty string for round filter
  • Loading branch information
bard authored Dec 19, 2023
1 parent 362d944 commit 6516537
Show file tree
Hide file tree
Showing 35 changed files with 1,004 additions and 546 deletions.
24 changes: 12 additions & 12 deletions packages/data-layer/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"prettier",
"@typescript-eslint",
"import"
],
"plugins": ["prettier", "@typescript-eslint", "import"],
"extends": [
"eslint:recommended",
"prettier",
Expand All @@ -14,13 +10,17 @@
"env": {
"node": true
},
"parserOptions": {
"project": ["./tsconfig.json"]
},
"rules": {
"@typescript-eslint/no-unused-vars": [
1,
{
"argsIgnorePattern": "^_"
}
],
"import/no-default-export": "error"
"import/no-default-export": "error",
"no-console": "error",
"no-process-env": "error",
"@typescript-eslint/no-unused-vars": [1, { "argsIgnorePattern": "^_" }],
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/ban-ts-comment": "error",
"@typescript-eslint/strict-boolean-expressions": "error"
}
}
3 changes: 3 additions & 0 deletions packages/data-layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"start": "node -r dotenv-flow/config dist/index.js",
"lint": "eslint --cache --max-warnings=0",
"lint:fix": "eslint --cache --max-warnings=0 --fix",
"lint:all:fix": "eslint --fix --cache --max-warnings=0 src",
"lint:all": "eslint --cache --max-warnings=0 src",
"generate:openapi": "openapi-generator-cli generate -i https://gitcoin-search-dev.fly.dev/openapi.json -o src/openapi-search-client -g typescript-fetch --additional-properties=importFileExtension=.js",
"generate:openapi:local": "openapi-generator-cli generate -i http://localhost:8000/openapi.json -o src/openapi-search-client -g typescript-fetch --additional-properties=importFileExtension=.js",
Expand All @@ -22,6 +23,7 @@
"prepare": "tsc"
},
"devDependencies": {
"@faker-js/faker": "^8.3.1",
"@openapitools/openapi-generator-cli": "^2.7.0",
"@tsconfig/node18": "^18.2.2",
"@types/debug": "^4.1.8",
Expand Down Expand Up @@ -49,6 +51,7 @@
},
"dependencies": {
"@gitcoinco/passport-sdk-types": "^0.2.0",
"@gitcoinco/passport-sdk-verifier": "^0.2.2",
"cross-fetch": "^4.0.0",
"debug": "^4.3.4",
"dotenv-flow": "^3.3.0",
Expand Down
127 changes: 127 additions & 0 deletions packages/data-layer/src/backends/legacy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { faker } from "@faker-js/faker";
import { describe, test, expect } from "vitest";
import { cleanRoundData, sortRounds } from "./legacy";
import { RoundMetadata, RoundOverview } from "..";

// TODO switch to ESM and move these tests into `legacy.ts` using in-source test
// definition with `import.meta.vitest`

describe("legacy", () => {
const MOCKED_ROUNDS_COUNT = 5;

const MOCKED_ROUNDS = Array.from({ length: MOCKED_ROUNDS_COUNT }).map(() =>
generateRoundOverviewData(),
);

const DEFAULT_FILTER = {
orderBy: "",
orderDirection: "",
status: "",
network: "",
type: "",
} as const;

test("cleanRoundData", async () => {
expect(
// Fix milliseconds to seconds
cleanRoundData([
{
...MOCKED_ROUNDS[0],
// Multiply by 1000 and check that it's still the value in seconds
roundStartTime: String(+MOCKED_ROUNDS[0].roundStartTime * 1000),
},
])[0].roundStartTime,
).toBe(MOCKED_ROUNDS[0].roundStartTime);

expect(
// Fix overflowed timestamp
cleanRoundData([
{
...MOCKED_ROUNDS[0],
// Multiply by 1000 and check that it's still the value in seconds
roundStartTime:
"115792089237316195423570985008687907853269984665640564039457584007913129639935",
},
])[0].roundStartTime,
).toBe(undefined);
});

test("sortRounds", async () => {
const sortedAsc = sortRounds(
[{ matchAmount: "10" }, { matchAmount: "1" }] as RoundOverview[],
{
...DEFAULT_FILTER,
orderBy: "matchAmount",
orderDirection: "asc",
},
);
expect(sortedAsc[0].matchAmount).toEqual("1");
expect(sortedAsc[1].matchAmount).toEqual("10");

const sortedDesc = sortRounds(
[{ matchAmount: "10" }, { matchAmount: "1" }] as RoundOverview[],
{
...DEFAULT_FILTER,
orderBy: "matchAmount",
orderDirection: "desc",
},
);
expect(sortedDesc[0].matchAmount).toEqual("10");
expect(sortedDesc[1].matchAmount).toEqual("1");
});
});

const generateRoundOverviewData = (
overrides?: Partial<RoundOverview>,
roundMetadataOverrides?: Partial<RoundMetadata>,
): RoundOverview => {
return {
id: faker.finance.ethereumAddress(),
chainId: 1,
createdAt: generateTimestamp(),
roundMetaPtr: {
protocol: 1,
pointer: generateIpfsCid(),
},
applicationMetaPtr: {
protocol: 1,
pointer: generateIpfsCid(),
},
applicationsStartTime: generateTimestamp(),
applicationsEndTime: generateTimestamp(10),
roundStartTime: generateTimestamp(20),
roundEndTime: generateTimestamp(30),
matchAmount: "1000000000000000000000000",
token: faker.finance.ethereumAddress(),
roundMetadata: generateRoundMetadata(roundMetadataOverrides),
projects: Array.from({ length: 2 }).map((_, i) => ({ id: String(i) })),
payoutStrategy: {
id: "someid",
strategyName: "MERKLE",
},
...overrides,
};
};

const generateIpfsCid = (): string => {
return faker.string.alpha({ length: { min: 59, max: 59 }, casing: "lower" });
};

const generateTimestamp = (days?: number): string =>
Math.floor(Number(faker.date.soon({ days })) / 1000).toString();

const generateRoundMetadata = (
overrides?: Partial<RoundMetadata>,
): RoundMetadata => ({
name: faker.company.name(),
roundType: "public",
eligibility: {
description: faker.lorem.sentence(),
requirements: [
{ requirement: faker.lorem.sentence() },
{ requirement: faker.lorem.sentence() },
],
},
programContractAddress: faker.finance.ethereumAddress(),
...overrides,
});
Loading

5 comments on commit 6516537

@vercel
Copy link

@vercel vercel bot commented on 6516537 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

manager-staging – ./packages/round-manager

manager-staging-grants-stack.vercel.app
manager-staging-git-main-grants-stack.vercel.app
grants-stack-manager-staging.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 6516537 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

manager – ./packages/round-manager

manager.gitcoin.co
manager-git-main-grants-stack.vercel.app
grants-stack-round-manager.vercel.app
manager-grants-stack.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 6516537 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder – ./packages/builder

builder-grants-stack.vercel.app
builder-git-main-grants-stack.vercel.app
builder.gitcoin.co
builder-kappa-one.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 6516537 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

explorer-staging – ./packages/grant-explorer

explorer-staging-grants-stack.vercel.app
grants-stack-explorer-staging.vercel.app
explorer-staging-git-main-grants-stack.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 6516537 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-staging – ./packages/builder

grants-stack-builder-staging.vercel.app
builder-staging-git-main-grants-stack.vercel.app
builder-staging-grants-stack.vercel.app

Please sign in to comment.