Skip to content

Commit

Permalink
Revert "Load Programs from Indexer (#2946)"
Browse files Browse the repository at this point in the history
This reverts commit 7f2292b.
  • Loading branch information
boudra authored Feb 16, 2024
1 parent 58caabd commit f82b501
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 170 deletions.
66 changes: 22 additions & 44 deletions packages/data-layer/src/data-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Round,
RoundOverview,
SearchBasedProjectCategory,
Tags,
TimestampVariables,
V2RoundWithRoles,
v2Project,
Expand All @@ -37,7 +38,6 @@ import {
getProgramByUserAndTag,
getRoundByIdAndChainId,
getRoundsByProgramIdAndUserAddress,
getProgramByIdAndUser,
} from "./queries";
import { Address } from "viem";

Expand Down Expand Up @@ -125,7 +125,7 @@ export class DataLayer {
* @example
* Here is an example:
* ```
* const program = await dataLayer.getProgramsByUser({
* const program = await dataLayer.getProgramByUser({
* address: "0x1234",
* chainId: 1,
* alloVersion: "allo-v1",
Expand All @@ -145,76 +145,54 @@ export class DataLayer {
address: string;
chainId: number;
alloVersion: AlloVersion;
}): Promise<{ programs: Program[] }> {
}): Promise<{ programs: Program[] } | null> {
const filterByTag = alloVersion === "allo-v1" ? "program" : "allo-v2";
const requestVariables = {
alloVersion,
address,
chainId,
filterByTag,
};

let programs: Program[] = [];

if (alloVersion === "allo-v1") {
const response: { projects: Program[] } = await request(
let response: { projects: Program[] } = { projects: [] };

response = await request(
this.gsIndexerEndpoint,
getProgramByUserAndTag,
{ ...requestVariables, filterByTag: "program" },
requestVariables,
);

programs = response.projects;
} else if (alloVersion === "allo-v2") {
const response: { projects: v2Project[] } = await request(
let response: { projects: v2Project[] } = { projects: [] };

response = await request(
this.gsIndexerEndpoint,
getProgramByUserAndTag,
{ ...requestVariables, filterByTag: "allo-v2" },
requestVariables,
);

programs = response.projects.map((project) => {
const projects = response.projects;

programs = projects.map((project) => {
return {
...project,
id: project.id,
chainId: project.chainId,
metadata: {
name: project.metadata?.title,
},
tags: project.tags as Tags[],
roles: project.roles,
};
});
}

return { programs };
}

async getProgramByIdAndUser({
userAddress,
programId,
chainId,
}: {
userAddress: string;
programId: string;
chainId: number;
}): Promise<{ program: Program | null }> {
const response: { projects: (Program | v2Project)[] } = await request(
this.gsIndexerEndpoint,
getProgramByIdAndUser,
{ programId, chainId, userAddress },
);

if (response.projects.length === 0) {
return { program: null };
}

const projectOrProgram = response.projects[0];

if ("name" in projectOrProgram.metadata) {
return { program: projectOrProgram as Program };
}
if (!programs) return null;

return {
program: {
...projectOrProgram,
metadata: {
name: projectOrProgram.metadata?.title,
},
},
};
return { programs };
}

/**
Expand Down
29 changes: 18 additions & 11 deletions packages/data-layer/src/data.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ export type ProjectRole = {
projectId: string;
};

export type Tags = "allo-v1" | "program update";

/**
* The program type for v1
**/

export type Program = {
id: string;
chainId: number;
metadata: {
name: string;
};
metadataCid?: MetadataPointer;
tags: Tags[];
roles: AddressAndRole[];
};

/**
* The project type for v2
*
Expand Down Expand Up @@ -168,7 +185,7 @@ export type v2Project = {
*
* The tags are used to filter the projects based on the version of Allo.
*/
tags: ("allo-v1" | "allo-v2" | "program")[];
tags: [string];
/**
* The block the project was created at
*/
Expand All @@ -182,16 +199,6 @@ export type v2Project = {
anchorAddress?: string;
};

/**
* The program type for v1
**/

export type Program = Omit<v2Project, "metadata"> & {
metadata: {
name: string;
};
};

/**
* The project application type for v2
*
Expand Down
11 changes: 6 additions & 5 deletions packages/data-layer/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const getProgramByUserAndTag = gql`
filter: {
tags: { contains: [$filterByTag] }
roles: { some: { address: { equalTo: $address } } }
chainId: { equalTo: $chainId }
and: { chainId: { equalTo: $chainId } }
}
) {
id
Expand All @@ -39,13 +39,14 @@ export const getProgramByUserAndTag = gql`
*
* @returns The programs
*/
export const getProgramByIdAndUser = gql`
query ($userAddress: String!, $programId: String!, $chainId: Int!) {
export const getProgramById = gql`
query ($alloVersion: [String!]!, $programId: String!, $chainId: Int!) {
projects(
filter: {
tags: { equalTo: $alloVersion }
tags: { contains: "program" }
id: { equalTo: $programId }
roles: { some: { address: { equalTo: $userAddress } } }
chainId: { equalTo: $chainId }
and: { chainId: { equalTo: $chainId } }
}
) {
id
Expand Down
21 changes: 3 additions & 18 deletions packages/round-manager/src/context/program/ReadProgramContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ const fetchProgramsByAddress = async (

const fetchProgramsById = async (
dispatch: Dispatch,
address: string,
programId: string,
dataLayer: DataLayer,
walletProvider: Web3Provider
) => {
datadogLogs.logger.info(`fetchProgramsById: programId - ${programId}`);
Expand All @@ -88,12 +86,7 @@ const fetchProgramsById = async (
payload: ProgressStatus.IN_PROGRESS,
});
try {
const program = await getProgramById(
address,
programId,
walletProvider,
dataLayer
);
const program = await getProgramById(programId, walletProvider);
dispatch({ type: ActionType.SET_PROGRAMS, payload: [program] });
dispatch({
type: ActionType.SET_FETCH_PROGRAM_STATUS,
Expand Down Expand Up @@ -180,27 +173,19 @@ export const useProgramById = (
getProgramByIdError?: Error;
} => {
const context = useContext(ReadProgramContext);
const dataLayer = useDataLayer();
if (context === undefined) {
throw new Error("useProgramById must be used within a ProgramProvider");
}

const { address, provider: walletProvider } = useWallet();

const { provider: walletProvider } = useWallet();
useEffect(() => {
if (id) {
const existingProgram = context.state.programs.find(
(program) => program.id === id
);

if (!existingProgram) {
fetchProgramsById(
context.dispatch,
address.toLowerCase(),
id,
dataLayer,
walletProvider
);
fetchProgramsById(context.dispatch, id, walletProvider);
}
}
}, [id, walletProvider]); // eslint-disable-line react-hooks/exhaustive-deps
Expand Down
64 changes: 44 additions & 20 deletions packages/round-manager/src/features/api/__tests__/program.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { getProgramById, listPrograms } from "../program";
import { Program } from "../types";
import { makeProgramData } from "../../../test-utils";
import { CHAINS } from "../utils";
import { fetchFromIPFS, CHAINS } from "../utils";
import { graphql_fetch } from "common";
import { ChainId } from "common";
import { zeroAddress } from "viem";

jest.mock("../utils", () => ({
...jest.requireActual("../utils"),
fetchFromIPFS: jest.fn(),
}));

jest.mock("common", () => ({
...jest.requireActual("common"),
graphql_fetch: jest.fn(),
}));

jest.mock("data-layer", () => ({
DataLayer: jest.fn().mockImplementation(() => ({
Expand Down Expand Up @@ -53,28 +63,42 @@ describe("getProgramById", () => {
chain: CHAINS[ChainId.MAINNET],
});
const programId = expectedProgram.id;

const actualProgram = await getProgramById(
zeroAddress,
programId as string,
{
getNetwork: async () =>
// @ts-expect-error Test file
Promise.resolve({ chainId: ChainId.MAINNET }),
},
{
getProgramByIdAndUser: jest.fn().mockResolvedValue({
program: {
(graphql_fetch as jest.Mock).mockResolvedValue({
data: {
programs: [
{
id: expectedProgram.id,
roles: [{ address: expectedProgram.operatorWallets[0] }],
metadata: {
name: expectedProgram.metadata?.name,
roles: [
{
accounts: [
{
address: expectedProgram.operatorWallets[0],
},
],
},
],
metaPtr: {
protocol: 1,
pointer:
"uwijkhxkpkdgkszraqzqvhssqulctxzvntxwconznfkelzbtgtqysrzkehl",
},
},
}),
}
);
],
},
});
(fetchFromIPFS as jest.Mock).mockResolvedValue({
name: expectedProgram.metadata?.name,
});

const actualProgram = await getProgramById(programId as string, {
getNetwork: async () =>
// @ts-expect-error Test file
Promise.resolve({ chainId: ChainId.MAINNET }),
});

expect(actualProgram).toEqual(expectedProgram);
const graphqlFetchCall = (graphql_fetch as jest.Mock).mock.calls[0];
const actualQuery = graphqlFetchCall[0];
expect(actualQuery).toContain("id: $programId");
});
});
Loading

0 comments on commit f82b501

Please sign in to comment.