Skip to content

Commit

Permalink
resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
thelostone-mc committed Feb 14, 2024
2 parents 8fee3ba + 0bd1dea commit 5064f18
Show file tree
Hide file tree
Showing 10 changed files with 790 additions and 646 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ node_modules
dist/
coverage/
.eslintcache

bun.lockb
tmp
47 changes: 40 additions & 7 deletions packages/data-layer/src/data-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as collections from "./backends/collections";
import * as legacy from "./backends/legacy";
import { AlloVersion, PaginationInfo } from "./data-layer.types";
import {
Application,
Collection,
Program,
ProjectApplication,
Expand All @@ -16,16 +17,17 @@ import {
RoundOverview,
SearchBasedProjectCategory,
TimestampVariables,
V2Round,
v2Project,
V2Round,
} from "./data.types";
import {
ApplicationSummary,
DefaultApi as SearchApi,
Configuration as SearchApiConfiguration,
DefaultApi as SearchApi,
SearchResult,
} from "./openapi-search-client/index";
import {
getApplication,
getApplicationsByProjectId,
getProgramName,
getProgramsByUser,
Expand All @@ -34,6 +36,7 @@ import {
getProjectsAndRolesByAddress,
getRoundByIdAndChainId,
} from "./queries";
import { Address } from "viem";

/**
* DataLayer is a class that provides a unified interface to the various data sources.
Expand Down Expand Up @@ -299,10 +302,10 @@ export class DataLayer {
return projectEventsMap;
}

// getApplicationsByProjectId
/**
* getApplicationsByProjectId() returns a list of projects by address.
* @param projectId
* @param chainIds
*/
async getApplicationsByProjectId({
projectId,
Expand All @@ -325,6 +328,34 @@ export class DataLayer {
return response.applications ?? [];
}

/**
* Returns a single application as identified by its id, round name and chain name
* @param projectId
*/
async getApplication({
roundId,
chainId,
applicationId,
}: {
roundId: Lowercase<Address>;
chainId: number;
applicationId: string;
}): Promise<Application | undefined> {
const requestVariables = {
roundId,
chainId,
applicationId,
};

const response: { application: Application } = await request(
this.gsIndexerEndpoint,
getApplication,
requestVariables,
);

return response.application ?? [];
}

async getProgramName({
projectId,
}: {
Expand All @@ -351,14 +382,16 @@ export class DataLayer {
roundId: string;
chainId: number;
}): Promise<V2Round> {

const requestVariables = {
roundId,
chainId
chainId,
};

const response : {rounds: V2Round[]} =
await request(this.gsIndexerEndpoint, getRoundByIdAndChainId, requestVariables);
const response: { rounds: V2Round[] } = await request(
this.gsIndexerEndpoint,
getRoundByIdAndChainId,
requestVariables,
);

return response.rounds[0] ?? [];
}
Expand Down
34 changes: 31 additions & 3 deletions packages/data-layer/src/data.types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { VerifiableCredential } from "@gitcoinco/passport-sdk-types";
import {
RoundApplicationMetadata,
} from './roundApplication.types';
import { RoundApplicationMetadata } from "./roundApplication.types";
// TODO `RoundPayoutType` and `RoundVisibilityType` are duplicated from `common` to
// avoid further spaghetti dependencies. They should probably be relocated here.
export type RoundPayoutType = "MERKLE" | "DIRECT";
Expand Down Expand Up @@ -420,3 +418,33 @@ export type Collection = {
description: string;
applicationRefs: string[];
};

export type Application = {
id: string;
chainId: string;
roundId: string;
projectId: string;
status: ApplicationStatus;
totalAmountDonatedInUsd: number;
totalDonationsCount: string;
uniqueDonorsCount: number;
round: {
donationsStartTime: string;
donationsEndTime: string;
applicationsStartTime: string;
applicationsEndTime: string;
roundMetadata: RoundMetadata;
matchTokenAddress: string;
tags: string[];
};
project: {
id: string;
metadata: ProjectMetadata;
};
metadata: {
application: {
recipient: string;
answers: GrantApplicationFormAnswer[];
};
};
};
34 changes: 33 additions & 1 deletion packages/data-layer/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,38 @@ export const getApplicationsByProjectId = gql`
}
`;

export const getApplication = gql`
query Application(
$chainId: Int!
$applicationId: String!
$roundId: String!
) {
application(chainId: $chainId, id: $applicationId, roundId: $roundId) {
id
chainId
roundId
projectId
status
totalAmountDonatedInUsd
uniqueDonorsCount
round {
donationsStartTime
donationsEndTime
applicationsStartTime
applicationsEndTime
matchTokenAddress
roundMetadata
}
metadata
project {
tags
id
metadata
}
}
}
`;

export const getProgramName = gql`
query getProgramNameQuery($projectId: String!) {
projects(filter: { id: { equalTo: $projectId } }) {
Expand Down Expand Up @@ -275,4 +307,4 @@ export const getRoundByIdAndChainId = gql`
strategyAddress
}
}
`;
`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, Mock } from "vitest";
import { DataLayer } from "data-layer";
import { useApplication } from "../useApplication";
import { renderWithContext } from "../../../../test-utils";
import { createElement } from "react";
Expand All @@ -20,7 +21,13 @@ describe("useApplication", () => {
const variables = { chainId: 1, roundId: "1", id: "1" };
renderWithContext(
createElement(() => {
const { data } = useApplication(variables);
const dataLayer = new DataLayer({
fetch: vi.fn().mockResolvedValue({}),
search: { baseUrl: "https://example.com" },
indexer: { baseUrl: "https://example.com" },
});

const { data } = useApplication(variables, dataLayer);
if (data) {
// Expect the hook to return the data
expect(data).toEqual(applicationData.application);
Expand Down
118 changes: 20 additions & 98 deletions packages/grant-explorer/src/features/projects/hooks/useApplication.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import useSWR from "swr";
import {
ApplicationStatus,
GrantApplicationFormAnswer,
Project,
ProjectMetadata,
Round,
RoundMetadata,
} from "data-layer";
import { Application, DataLayer, Project, Round } from "data-layer";
import { getConfig } from "common/src/config";
import { Address, getAddress, zeroAddress } from "viem";

type Params = {
chainId?: number;
Expand All @@ -16,86 +10,29 @@ type Params = {
};

const {
dataLayer: { gsIndexerEndpoint },
allo: { version },
} = getConfig();

const allo_v2_url = gsIndexerEndpoint + "/graphql";

const APPLICATION_QUERY = `
query Application($chainId: Int!, $applicationId: String!, $roundId: String!) {
application(chainId: $chainId, id: $applicationId, roundId: $roundId) {
id
chainId
roundId
projectId
status
totalAmountDonatedInUsd
uniqueDonorsCount
round {
donationsStartTime
donationsEndTime
applicationsStartTime
applicationsEndTime
matchTokenAddress
roundMetadata
}
metadata
project {
tags
id
metadata
}
}
}
`;

export type Application = {
id: string;
chainId: string;
roundId: string;
projectId: string;
status: ApplicationStatus;
totalAmountDonatedInUsd: string;
totalDonationsCount: string;
round: {
donationsStartTime: string;
donationsEndTime: string;
applicationsStartTime: string;
applicationsEndTime: string;
roundMetadata: RoundMetadata;
matchTokenAddress: string;
};
project: {
id: string;
metadata: ProjectMetadata;
};
metadata: {
application: {
recipient: string;
answers: GrantApplicationFormAnswer[];
};
};
};

export function useApplication(params: Params) {
export function useApplication(params: Params, dataLayer: DataLayer) {
const shouldFetch = Object.values(params).every(Boolean);
return useSWR(shouldFetch ? ["applications", params] : null, async () => {
return request(allo_v2_url, {
query: APPLICATION_QUERY,
variables: params,
})
.then((r) => r.data?.application)
.then((application) => {
/* Don't fetch v2 rounds when allo version is set to v1 */
if (
version === "allo-v1" &&
application.project.tags.includes("allo-v2")
) {
return;
}
return application;
});
const validatedParams = {
chainId: params.chainId as number,
applicationId: params.applicationId as string,
roundId: getAddress(
params.roundId ?? zeroAddress
).toLowerCase() as Lowercase<Address>,
};
return dataLayer.getApplication(validatedParams).then((application) => {
/* Don't fetch v2 rounds when allo version is set to v1 */
if (
version === "allo-v1" &&
application?.round?.tags?.includes("allo-v2")
) {
return;
}
return application;
});
});
}

Expand Down Expand Up @@ -137,18 +74,3 @@ export function mapApplicationToRound(
ownedBy: "",
};
}

async function request(url: string, body: unknown) {
return fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
}).then(async (res) => {
if (!res.ok) {
throw new Error(`${res.status} ${res.statusText}`);
}
return await res.json();
});
}
Loading

0 comments on commit 5064f18

Please sign in to comment.