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

Safe subgraph queries #2746

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 29 additions & 0 deletions src/graphql/SafeQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Address, Hex } from 'viem';

// Define the base DAO structure that can be nested
interface Safe {
id: Address;
created: string;
creator: Address;
transactionHash: Hex;
factoryAddress: Address;
singleton: Address;
setupData: Hex;
}

// Define the top-level DAO response structure
export interface SafeQueryResponse {
safes: Safe[];
}

export const SafeQuery = `query SafeQuery($safeAddress: Bytes) {
safes(where: { id: $safeAddress }) {
id
created
creator
transactionHash
factoryAddress
singleton
setupData
}
}`;
40 changes: 24 additions & 16 deletions src/hooks/DAO/useSearchDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,31 @@ export const useSearchDao = () => {

const findSafes = useCallback(
async (resolvedAddressesWithChainId: { address: Address; chainId: number }[]) => {
/*
This function only checks if the address is on any of the EVM networks.
The same safe could of on multiple networks

Changes requested inside getSafeCreationInfo
*/
for await (const resolved of resolvedAddressesWithChainId) {
try {
const safeAPI = getSafeAPI(getConfigByChainId(resolved.chainId));
await safeAPI.getSafeCreationInfo(resolved.address);
// We're going to call getSafeCreationInfo for each resolved address
// If it throws, we don't have a Safe at that address
// If it doesn't throw, we add the resolved address and chainId to our list
const creationInfos = (
await Promise.all(
resolvedAddressesWithChainId.map(async resolved => {
try {
const safeAPI = getSafeAPI(getConfigByChainId(resolved.chainId));

setSafeResolvedAddressesWithPrefix(prevState => [...prevState, resolved]);
} catch (e) {
// Safe not found
continue;
}
}
// if this throws, we don't have a Safe
await safeAPI.getSafeCreationInfo(resolved.address);

// We actually only care about returning the resolved address and chainId
return resolved;
} catch (e) {
// Safe not found
return null;
}
}),
)
).filter(creationInfo => creationInfo !== null);

// We're left with a list of chains and addresses
// (all the same address) that have a Safe at that address.
setSafeResolvedAddressesWithPrefix(creationInfos);
Comment on lines +31 to +55
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is here to make the DAO Searching do all network requests in parallel, not in sequence. Faster searching.

},
[getConfigByChainId],
);
Expand Down
15 changes: 12 additions & 3 deletions src/providers/App/hooks/useSafeAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
} from 'viem';
import GnosisSafeL2Abi from '../../../assets/abi/GnosisSafeL2';
import { SENTINEL_ADDRESS } from '../../../constants/common';
import { createDecentSubgraphClient } from '../../../graphql';
import { SafeQuery } from '../../../graphql/SafeQueries';
import { SafeWithNextNonce } from '../../../types';
import { NetworkConfig } from '../../../types/network';
import { useNetworkConfigStore } from '../../NetworkConfig/useNetworkConfigStore';
Expand Down Expand Up @@ -163,9 +165,16 @@ class EnhancedSafeApiKit extends SafeApiKit {
}

try {
// TODO ENG-291
// add another layer of onchain fallback here
// use subgraph to get this data
const client = createDecentSubgraphClient(this.networkConfig);
const queryResult = await client.query<any>(SafeQuery, { safeAddress });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the actual type from SafeQueries, not "any"


const safeCreationInfo = queryResult.data?.safes[0];
if (safeCreationInfo) {
// setupData is 0x from the subgraph, so it's good that it's the final fallback.
// Not that it's ever used anywhere currently anyway.
return safeCreationInfo;
}
console.log('Safe creation info not found');
} catch (error) {
console.error('Error fetching getSafeCreationInfo from subgraph:', error);
}
Expand Down