Skip to content

Commit

Permalink
Merge branch 'main' into 2788-builder-applyToRound
Browse files Browse the repository at this point in the history
  • Loading branch information
codenamejason authored Feb 14, 2024
2 parents c149198 + e5a9c9a commit d7217ca
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 130 deletions.
16 changes: 16 additions & 0 deletions packages/data-layer/src/data-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
RoundOverview,
SearchBasedProjectCategory,
TimestampVariables,
V2RoundWithRoles,
v2Project,
V2Round,
} from "./data.types";
Expand All @@ -35,6 +36,7 @@ import {
getProjects,
getProjectsAndRolesByAddress,
getRoundByIdAndChainId,
getRoundsByProgramIdAndUserAddress,
} from "./queries";
import { Address } from "viem";

Expand Down Expand Up @@ -396,6 +398,20 @@ export class DataLayer {
return response.rounds[0] ?? [];
}

async getRoundsByProgramIdAndUserAddress(args: {
chainId: number;
programId: string;
userAddress: Address;
}): Promise<V2RoundWithRoles[]> {
const response: { rounds: V2RoundWithRoles[] } = await request(
this.gsIndexerEndpoint,
getRoundsByProgramIdAndUserAddress,
{ ...args, userAddress: args.userAddress.toLowerCase() },
);

return response.rounds;
}

/**
* Legacy - Allo v1 queries
*/
Expand Down
10 changes: 8 additions & 2 deletions packages/data-layer/src/data.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,18 @@ export type V2Round = {
donationsStartTime: string;
donationsEndTime: string;
matchTokenAddress: string;
roundMetadata: any;
roundMetadata: RoundMetadata | null;
roundMetadataCid: string;
applicationMetadata: RoundApplicationMetadata;
applicationMetadata: RoundApplicationMetadata | null;
applicationMetadataCid: string;
strategyId: string;
projectId: string;
strategyAddress: string;
strategyName: string;
};

export type V2RoundWithRoles = V2Round & {
roles: AddressAndRole[];
};

export type ProjectEvents = {
Expand Down
35 changes: 35 additions & 0 deletions packages/data-layer/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,38 @@ export const getRoundByIdAndChainId = gql`
}
}
`;

export const getRoundsByProgramIdAndUserAddress = gql`
query getRoundsByProgramIdAndMemberAddress(
$chainId: Int!
$programId: String!
$userAddress: String!
) {
rounds(
filter: {
chainId: { equalTo: $chainId }
projectId: { equalTo: $programId }
roles: { some: { address: { equalTo: $userAddress } } }
}
) {
id
chainId
applicationsStartTime
applicationsEndTime
donationsStartTime
donationsEndTime
matchTokenAddress
roundMetadata
roundMetadataCid
applicationMetadata
applicationMetadataCid
strategyAddress
strategyName
roles {
role
address
createdAtBlock
}
}
}
`;
29 changes: 24 additions & 5 deletions packages/round-manager/src/context/round/RoundContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useWallet } from "../../features/common/Auth";
import { getRoundById, listRounds } from "../../features/api/round";
import { Web3Provider } from "@ethersproject/providers";
import { datadogLogs } from "@datadog/browser-logs";
import { DataLayer, useDataLayer } from "data-layer";
import { Address } from "viem";

export interface RoundState {
data: Round[];
Expand Down Expand Up @@ -36,8 +38,9 @@ export const RoundContext = createContext<

const fetchRounds = async (
dispatch: Dispatch,
address: string,
walletProvider: Web3Provider,
dataLayer: DataLayer,
address: Address,
chainId: number,
programId: string
) => {
datadogLogs.logger.info(`fetchRounds: program - ${programId}`);
Expand All @@ -48,7 +51,13 @@ const fetchRounds = async (
});

try {
const { rounds } = await listRounds(address, walletProvider, programId);
const { rounds } = await listRounds({
chainId: chainId,
dataLayer,
programId,
userAddress: address,
});

dispatch({ type: ActionType.SET_ROUNDS, payload: rounds });
dispatch({
type: ActionType.SET_FETCH_ROUNDS_STATUS,
Expand Down Expand Up @@ -135,16 +144,26 @@ export const RoundProvider = ({ children }: { children: React.ReactNode }) => {

export const useRounds = (programId?: string) => {
const context = useContext(RoundContext);
const dataLayer = useDataLayer();

if (context === undefined) {
throw new Error("useRounds must be used within a RoundProvider");
}
const { address, provider } = useWallet();

useEffect(() => {
if (programId) {
fetchRounds(context.dispatch, address, provider, programId);
provider.getNetwork().then((network) => {
fetchRounds(
context.dispatch,
dataLayer,
address,
network.chainId,
programId
);
});
}
}, [address, provider, programId, context.dispatch]);
}, [address, dataLayer, provider, programId, context.dispatch]);

return { ...context.state, dispatch: context.dispatch };
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { render, screen } from "@testing-library/react";
import { makeRoundData } from "../../../test-utils";
import { getRoundById, listRounds } from "../../../features/api/round";
import { ProgressStatus, Round } from "../../../features/api/types";
import { DataLayer, DataLayerProvider } from "data-layer";

jest.mock("../../../features/api/round");
jest.mock("wagmi");
Expand All @@ -15,6 +16,7 @@ jest.mock("../../../features/common/Auth", () => ({
}));
const mockWallet = {
address: "0x0",
provider: { getNetwork: () => Promise.resolve({ chainId: "0" }) },
signer: {
getChainId: () => {
/* do nothing.*/
Expand Down Expand Up @@ -61,10 +63,12 @@ describe("<RoundProvider />", () => {
);

render(
<RoundProvider>
{/*// @ts-expect-error test file*/}
<TestingUseRoundsComponent expectedProgramId={expectedProgramId} />
</RoundProvider>
<DataLayerProvider client={{} as DataLayer}>
<RoundProvider>
{/*// @ts-expect-error test file*/}
<TestingUseRoundsComponent expectedProgramId={expectedProgramId} />
</RoundProvider>
</DataLayerProvider>
);

expect(
Expand All @@ -86,9 +90,11 @@ describe("<RoundProvider />", () => {
});

render(
<RoundProvider>
<TestingUseRoundsComponent expectedProgramId={expectedProgramId} />
</RoundProvider>
<DataLayerProvider client={{} as DataLayer}>
<RoundProvider>
<TestingUseRoundsComponent expectedProgramId={expectedProgramId} />
</RoundProvider>
</DataLayerProvider>
);

expect(
Expand All @@ -108,12 +114,19 @@ describe("<RoundProvider />", () => {
it("sets fetch round status to error when fetch fails", async () => {
const expectedRound = makeRoundData();
const expectedProgramId = expectedRound.ownedBy;
(listRounds as jest.Mock).mockRejectedValue(new Error(":("));

const dataLayerMock = {
getRoundsByProgramIdAndUserAddress: jest
.fn()
.mockRejectedValue(new Error(":(")),
} as unknown as DataLayer;

render(
<RoundProvider>
<TestingUseRoundsComponent expectedProgramId={expectedProgramId} />
</RoundProvider>
<DataLayerProvider client={dataLayerMock}>
<RoundProvider>
<TestingUseRoundsComponent expectedProgramId={expectedProgramId} />
</RoundProvider>
</DataLayerProvider>
);

expect(
Expand Down
Loading

0 comments on commit d7217ca

Please sign in to comment.