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 4 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
55 changes: 33 additions & 22 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 @@ -141,31 +143,40 @@ class EnhancedSafeApiKit extends SafeApiKit {
console.error('Error fetching getSafeCreationInfo from safeAPI:', error);
}

// TODO: uncomment this out once subgraph testing is completed and working
// try {
// type SafeClientCreationInfoResponse = {
// readonly created: string;
// readonly creator: string;
// readonly transactionHash: string;
// readonly factoryAddress: string;

// readonly masterCopy: string;
// readonly setupData: string;
// };

// const response: SafeClientCreationInfoResponse = await this._safeClientGet(
// safeAddress,
// '/transactions/creation',
// );

// return { ...response, singleton: response.masterCopy };
// } catch (error) {
// console.error('Error fetching getSafeCreationInfo from safe-client:', error);
// }

// TODO: this is what needs to be tested
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 a good place to start.

Find out where in the codebase is calling this getSafeCreationInfo function, to trigger it to run, and test that we can get the correct data from these subgraph graphql calls.

You can use the Playground to verify that data is actually there and available:

Check out the query in SafeQueries.ts to see the one we're using.

When I left off, the subgraph calls were returning strange "Message: Not Found" errors.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not getting the error now. Seems to work for me, so I can search on mainnet on sepolia.

This DAO: 0xF222823699a84A6Dcd47A1A860554A5B9DAaaf47 should exist on Base though, but the query returns nothing

Copy link
Member Author

Choose a reason for hiding this comment

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

Were you able to figure out why, @DarksightKellar ?

try {
type SafeClientCreationInfoResponse = {
readonly created: string;
readonly creator: string;
readonly transactionHash: string;
readonly factoryAddress: string;

readonly masterCopy: string;
readonly setupData: string;
};

const response: SafeClientCreationInfoResponse = await this._safeClientGet(
safeAddress,
'/transactions/creation',
);
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"


return { ...response, singleton: response.masterCopy };
} catch (error) {
console.error('Error fetching getSafeCreationInfo from safe-client:', error);
}
console.log('queryResult', queryResult);

try {
// TODO ENG-291
// add another layer of onchain fallback here
// use subgraph to get this data
const safeCreationInfo = queryResult.data?.safes[0];
if (safeCreationInfo) {
return safeCreationInfo;
}
console.log('Safe creation info not found');
} catch (error) {
console.error('Error fetching getSafeCreationInfo from subgraph:', error);
}
Expand Down