Skip to content

Commit

Permalink
fetch rounds from indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
boudra committed Feb 8, 2024
1 parent 555915b commit 77c12fd
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 15 deletions.
27 changes: 23 additions & 4 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 {
SearchBasedProjectCategory,
TimestampVariables,
V2Round,
V2RoundWithRoles,
v2Project,
} from "./data.types";
import {
Expand All @@ -33,7 +34,9 @@ import {
getProjects,
getProjectsAndRolesByAddress,
getRoundByIdAndChainId,
getRoundsByProgramIdAndUserAddress,
} from "./queries";
import { Address } from "viem";

/**
* DataLayer is a class that provides a unified interface to the various data sources.
Expand Down Expand Up @@ -351,18 +354,34 @@ 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] ?? [];
}

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
13 changes: 8 additions & 5 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 @@ -229,11 +227,16 @@ export type V2Round = {
donationsStartTime: string;
donationsEndTime: string;
matchTokenAddress: string;
roundMetadata: any;
roundMetadata: unknown;
roundMetadataCid: string;
applicationMetadata: RoundApplicationMetadata;
applicationMetadataCid: string;
}
projectId: string;
};

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

export type ProjectEvents = {
createdAtBlock: number | undefined;
Expand Down
39 changes: 37 additions & 2 deletions packages/data-layer/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,37 @@ export const getBlockNumberQuery = gql`

export const getRoundByIdAndChainId = gql`
query getRoundByIdAndChainId($roundId: String!, $chainId: Int!) {
rounds(filter: { id: { equalTo: $roundId }, chainId: { equalTo: $chainId } }) {
rounds(
filter: { id: { equalTo: $roundId }, chainId: { equalTo: $chainId } }
) {
id
chainId
applicationsStartTime
applicationsEndTime
donationsStartTime
donationsEndTime
matchTokenAddress
roundMetadata
roundMetadataCid
applicationMetadata
applicationMetadataCid
}
}
`;

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
Expand All @@ -261,6 +291,11 @@ export const getRoundByIdAndChainId = gql`
roundMetadataCid
applicationMetadata
applicationMetadataCid
roles {
role
address
createdAtBlock
}
}
}
`;
`;
55 changes: 51 additions & 4 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, V2Round, V2RoundWithRoles, useDataLayer } from "data-layer";
import { maxDateForUint256 } from "../../constants";

export interface RoundState {
data: Round[];
Expand Down Expand Up @@ -34,10 +36,44 @@ export const RoundContext = createContext<
{ state: RoundState; dispatch: Dispatch } | undefined
>(undefined);

function indexerV2RoundToRound(round: V2RoundWithRoles): Round {
const operatorWallets = round.roles.map(
(account: { address: string }) => account.address
);

return {
id: round.id,
roundMetadata: round.roundMetadata as Round["roundMetadata"],
applicationMetadata:
round.applicationMetadata as unknown as Round["applicationMetadata"],
applicationsStartTime: new Date(round.applicationsStartTime),
applicationsEndTime:
round.applicationsEndTime === null
? maxDateForUint256
: new Date(round.applicationsEndTime),
roundStartTime: new Date(round.donationsStartTime),
roundEndTime:
round.donationsEndTime === null
? maxDateForUint256
: new Date(round.donationsEndTime),
token: round.matchTokenAddress,
votingStrategy: "unknown",
payoutStrategy: {
id: "0x0",
isReadyForPayout: false,
strategyName: "unknown",
},
ownedBy: round.projectId,
operatorWallets: operatorWallets,
finalized: false,
};
}

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

try {
const { rounds } = await listRounds(address, walletProvider, programId);
const rounds = await dataLayer
.getRoundsByProgramIdAndUserAddress({
chainId: chainId,
programId,
userAddress: address as `0x${string}`,
})
.then((rounds) => rounds.map(indexerV2RoundToRound));

// const { rounds } = await listRounds(address, walletProvider, programId);
dispatch({ type: ActionType.SET_ROUNDS, payload: rounds });
dispatch({
type: ActionType.SET_FETCH_ROUNDS_STATUS,
Expand Down Expand Up @@ -135,16 +179,19 @@ 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);
const chainId = provider.network.chainId;
fetchRounds(context.dispatch, dataLayer, address, chainId, programId);
}
}, [address, provider, programId, context.dispatch]);
}, [address, dataLayer, provider, programId, context.dispatch]);

return { ...context.state, dispatch: context.dispatch };
};
Expand Down

0 comments on commit 77c12fd

Please sign in to comment.