Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrates new impact image creation workflow #3697

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 10 additions & 93 deletions packages/grant-explorer/src/attestationStore.ts
Original file line number Diff line number Diff line change
@@ -1,117 +1,34 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { create } from "zustand";
import { devtools } from "zustand/middleware";
import { CartProject, AttestationFrameProps } from "./features/api/types";
import { persist } from "zustand/middleware";

import { Hex } from "viem";

interface AttestationState {
checkedOutProjectsByTx: Record<Hex, CartProject[]>;
setCheckedOutProjectsByTx: (tx: Hex, projects: CartProject[]) => void;
getCheckedOutProjectsByTx: (tx: Hex) => CartProject[];
cleanCheckedOutProjects: () => void;
checkedOutTransactions: Hex[];
addCheckedOutTransaction: (tx: Hex) => void;
getCheckedOutTransactions: () => Hex[];
getFrameProps: (txHashes: Hex[]) => AttestationFrameProps;
cleanCheckedOutTransactions: () => void;
}

export const useAttestationStore = create<AttestationState>()(
persist(
devtools((set, get) => ({
checkedOutProjectsByTx: {},
setCheckedOutProjectsByTx: (tx: Hex, projects: CartProject[]) => {
checkedOutTransactions: [],
addCheckedOutTransaction: (tx: Hex) => {
set((oldState) => ({
checkedOutProjectsByTx: {
...oldState.checkedOutProjectsByTx,
[tx]: projects,
},
checkedOutTransactions: [...oldState.checkedOutTransactions, tx],
}));
},
getCheckedOutProjectsByTx: (tx: Hex) => {
return get().checkedOutProjectsByTx[tx] || [];
getCheckedOutTransactions: () => {
return get().checkedOutTransactions;
},
cleanCheckedOutProjects: () => {
cleanCheckedOutTransactions: () => {
set({
checkedOutProjectsByTx: {},
checkedOutTransactions: [],
});
},
// Create a function that gets an array of transactionHashes and returns the FrameProps object where projects Array
// contains the top 3 projects based on those checked out transactions max donation amount in usd
// The top round is the round with the most funds allocated in total amount of projects allocated to all transactions in total rounds in all transaction in total chains allocated in these transactions
getCheckedOutTransactions: () => {
return Object.keys(get().checkedOutProjectsByTx) as Hex[];
},
getFrameProps: (txHashes: Hex[]) => {
const allProjects: CartProject[] = [];
const roundsSet = new Set<string>();
const chainsSet = new Set<number>();
const amountByRound: Record<
string,
{
roundId: string;
chainId: number;
totalAmount: number;
}
> = {};

if (txHashes.length === 0) {
return {
selectedBackground: "",
topRound: {
roundId: "",
chainId: 0,
},
projectsFunded: 0,
roundsSupported: 0,
checkedOutChains: 0,
projects: [],
} as AttestationFrameProps;
}

for (const txHash of txHashes) {
const projects = get().getCheckedOutProjectsByTx(txHash);
allProjects.push(...projects);
projects.forEach((project) => {
roundsSet.add(project.roundId);
chainsSet.add(project.chainId);
amountByRound[project.roundId] = amountByRound[project.roundId] || {
roundName: project.roundId,
totalAmount: 0,
};
// TODO CHANGE WITH ACTUAL ROUNDNAME
amountByRound[project.roundId].roundId = project.roundId;
amountByRound[project.roundId].chainId = project.chainId;
amountByRound[project.roundId].totalAmount += Number(
project.amount
);
});
}
const topProjects = allProjects
.sort((a, b) => Number(b.amount) - Number(a.amount))
.slice(0, 3)
.map((project, i) => ({
rank: i + 1,
name: project.projectMetadata.title,
round: project.roundId,
roundId: project.roundId,
image:
project.projectMetadata?.logoImg ??
project.projectMetadata?.bannerImg ??
"",
chainId: project.chainId,
}));
const topRound = Object.values(amountByRound).sort(
(a, b) => b.totalAmount - a.totalAmount
)[0];
return {
selectedBackground: "",
topRound: topRound,
projectsFunded: allProjects.length,
roundsSupported: roundsSet.size,
checkedOutChains: chainsSet.size,
projects: topProjects,
} as AttestationFrameProps;
},
})),
{
name: "attestation-store",
Expand Down
2 changes: 1 addition & 1 deletion packages/grant-explorer/src/checkoutStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export const useCheckoutStore = create<CheckoutState>()(
});
useAttestationStore
.getState()
.setCheckedOutProjectsByTx(receipt.transactionHash, donations);
.addCheckedOutTransaction(receipt.transactionHash);
} catch (error) {
let context: Record<string, unknown> = {
chainId,
Expand Down
24 changes: 0 additions & 24 deletions packages/grant-explorer/src/features/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,3 @@ export type ChainBalances = {
export type BalanceMap = {
[chainId: string | number]: ChainBalances;
};

export type AttestationProject = {
rank: number;
name: string;
round: string;
image: string;
amount?: number;
chainId?: number;
roundId?: string;
};

export type AttestationFrameProps = {
topRound?: {
roundId: string;
chainId: number;
};
topRoundName?: string;
projectsFunded: number;
roundsSupported: number;
checkedOutChains: number;
projects: AttestationProject[];
address?: string;
name?: string;
};
Loading
Loading