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

[Issue #2479] Tie streams to hat #2562

Merged
merged 16 commits into from
Nov 28, 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
41 changes: 8 additions & 33 deletions src/hooks/DAO/loaders/useHatsTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HatsSubgraphClient, Tree } from '@hatsprotocol/sdk-v1-subgraph';
import { useCallback, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { toast } from 'sonner';
import { Address, formatUnits, getAddress, getContract, Hex } from 'viem';
import { Address, formatUnits, getAddress, getContract } from 'viem';
import { usePublicClient } from 'wagmi';
import { StreamsQueryDocument } from '../../../../.graphclient';
import { SablierV2LockupLinearAbi } from '../../../assets/abi/SablierV2LockupLinear';
Expand Down Expand Up @@ -39,7 +39,6 @@ const useHatsTree = () => {
setHatsTree,
updateRolesWithStreams,
resetHatsStore,
hatIdsToStreamIds,
} = useRolesStore();

const ipfsClient = useIPFSClient();
Expand Down Expand Up @@ -263,23 +262,6 @@ const useHatsTree = () => {
[apolloClient, publicClient, sablierSubgraph],
);

const getTermedPaymentStreams = useCallback(
async (allTermRecipients: Address[], hatId: Hex): Promise<SablierPayment[]> => {
const assignedStreamIds = hatIdsToStreamIds
.filter(item => item.hatId === BigInt(hatId))
.map(({ streamId }) => {
return streamId;
});
const uniqueRecipients = [...new Set(allTermRecipients)];
const payments: SablierPayment[] = [];
for (const recipient of uniqueRecipients) {
payments.push(...(await getPaymentStreams(recipient)));
}
return payments.filter(stream => assignedStreamIds.includes(stream.streamId));
},
[getPaymentStreams, hatIdsToStreamIds],
);

useEffect(() => {
async function getHatsStreams() {
if (hatsTree && hatsTree.roleHats.length > 0 && !streamsFetched) {
Expand All @@ -290,12 +272,12 @@ const useHatsTree = () => {
}
const payments: SablierPayment[] = [];
if (hat.isTermed) {
payments.push(
...(await getTermedPaymentStreams(
hat.roleTerms.allTerms.map(term => term.nominee),
hat.id,
)),
);
const uniqueRecipients = [
...new Set(hat.roleTerms.allTerms.map(term => term.nominee)),
];
for (const recipient of uniqueRecipients) {
payments.push(...(await getPaymentStreams(recipient)));
}
} else {
if (!hat.smartAddress) {
throw new Error('Smart account address not found');
Expand All @@ -312,14 +294,7 @@ const useHatsTree = () => {
}

getHatsStreams();
}, [
hatsTree,
updateRolesWithStreams,
getPaymentStreams,
streamsFetched,
hatIdsToStreamIds,
getTermedPaymentStreams,
]);
}, [hatsTree, updateRolesWithStreams, getPaymentStreams, streamsFetched]);

useEffect(() => {
if (safeAddress && safe?.address && safeAddress !== safe.address && hatsTree) {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/DAO/useKeyValuePairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ const useKeyValuePairs = () => {
hatsTreeId: getHatsTreeId(logs, chain.id),
contextChainId: chain.id,
});
setHatIdsToStreamIds(getHatIdsToStreamIds(logs, sablierV2LockupLinear, chain.id));
}, 20_000);
},
},
Expand Down
1 change: 0 additions & 1 deletion src/store/roles/rolesStoreUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export const initialHatsStore: RolesStoreData = {
decentHatsAddress: undefined,
streamsFetched: false,
contextChainId: null,
hatIdsToStreamIds: [],
};

export const predictHatId = ({ adminHatId, hatsCount }: { adminHatId: Hex; hatsCount: number }) => {
Expand Down
26 changes: 24 additions & 2 deletions src/store/roles/useRolesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { convertStreamIdToBigInt } from '../../hooks/streams/useCreateSablierStr
import { DecentRoleHat, RolesStore } from '../../types/roles';
import { initialHatsStore, sanitize } from './rolesStoreUtils';

const hatToStreamIdMap = new Map<BigInt, string>();
const getHatToStreamIdMap = () => {
return Array.from(hatToStreamIdMap.entries()).map(([hatId, streamId]) => ({
hatId,
streamId,
}));
};
const setHatToStreamId = (key: BigInt, value: string) => hatToStreamIdMap.set(key, value);
DarksightKellar marked this conversation as resolved.
Show resolved Hide resolved

const useRolesStore = create<RolesStore>()((set, get) => ({
...initialHatsStore,
getHat: hatId => {
Expand Down Expand Up @@ -96,10 +105,21 @@ const useRolesStore = create<RolesStore>()((set, get) => ({
updateRolesWithStreams: (updatedRoles: DecentRoleHat[]) => {
const existingHatsTree = get().hatsTree;
if (!existingHatsTree) return;
const hatIdToStreamIdMap = getHatToStreamIdMap();

const updatedDecentTree = {
...existingHatsTree,
roleHats: updatedRoles,
roleHats: updatedRoles.map(roleHat => {
const filteredStreamIds = hatIdToStreamIdMap
.filter(hatToStreamId => hatToStreamId.hatId === BigInt(roleHat.id))
.map(hatToStreamId => hatToStreamId.streamId);
return {
...roleHat,
payments: roleHat.payments?.filter(payment => {
return filteredStreamIds.includes(payment.streamId);
}),
};
}),
};

set(() => ({ hatsTree: updatedDecentTree, streamsFetched: true }));
Expand Down Expand Up @@ -130,7 +150,9 @@ const useRolesStore = create<RolesStore>()((set, get) => ({
}));
},
setHatIdsToStreamIds: hatIdsToStreamIds => {
set(() => ({ hatIdsToStreamIds }));
for (const { hatId, streamId } of hatIdsToStreamIds) {
setHatToStreamId(hatId, streamId);
}
},
resetHatsStore: () => set(() => initialHatsStore),
}));
Expand Down
1 change: 0 additions & 1 deletion src/types/roles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ export interface RolesStoreData {
hatsTree: undefined | null | DecentTree;
streamsFetched: boolean;
contextChainId: number | null;
hatIdsToStreamIds: { hatId: BigInt; streamId: string }[];
}

export interface RolesStore extends RolesStoreData {
Expand Down