Skip to content

Commit

Permalink
feat: fetch only for snax chain (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrX-SNX authored Aug 31, 2024
1 parent c737b3a commit fc599f2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
7 changes: 4 additions & 3 deletions governance/ui/src/mutations/useUpdateUserDetailsMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { SiweMessage } from 'siwe';
import { useGetIsUUIDValidQuery } from '../queries/';
import { utils } from 'ethers';
import { GetUserDetails } from '../queries/useGetUserDetailsQuery';
import { useWallet, useSigner } from '../queries/useWallet';
import { profileContract } from '../utils/contracts';
import { useWallet, useSigner, useNetwork } from '../queries/useWallet';
import { isMotherchain, profileContract } from '../utils/contracts';

type UpdateUserDetailsResponse = {
data: GetUserDetails & {
Expand Down Expand Up @@ -52,6 +52,7 @@ function useUpdateUserDetailsMutation() {
const { activeWallet } = useWallet();
const signer = useSigner();
const toast = useToast();
const { network } = useNetwork();

const [uuid, setUuid] = useState<null | string>(null);

Expand Down Expand Up @@ -110,7 +111,7 @@ function useUpdateUserDetailsMutation() {
mutationFn: async (userProfile: GetUserDetails) => {
const address = await signer?.getAddress();
const isContract = await signer?.provider.getCode(address || '');
if (isContract !== '0x' && signer) {
if (isContract !== '0x' && signer && isMotherchain(network?.id)) {
await profileContract.connect(signer).updateProfile(
{
username: userProfile.username,
Expand Down
31 changes: 21 additions & 10 deletions governance/ui/src/queries/useGetUserDetailsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GET_PITCHES_FOR_USER_API_URL, GET_USER_DETAILS_API_URL } from '../utils
import { useQuery } from '@tanstack/react-query';
import { motherShipProvider } from '../utils/providers';
import { profileContract } from '../utils/contracts';
import { utils } from 'ethers';

export type GetUserDetails = {
address: string;
Expand Down Expand Up @@ -48,16 +49,7 @@ export async function getUserDetails<T extends string | string[]>(
): Promise<(T extends string ? GetUserDetails : GetUserDetails[]) | undefined> {
if (typeof walletAddress === 'string') {
const randomNumber = Math.random();
const isMultiSig = await motherShipProvider(2192).getCode(walletAddress);
if (isMultiSig !== '0x') {
const profile = await profileContract
.connect(motherShipProvider(2192))
.getProfile(walletAddress);

return { ...profile, address: walletAddress } as T extends string
? GetUserDetails
: GetUserDetails[];
}
const userDetailsResponse = await fetch(GET_USER_DETAILS_API_URL(walletAddress), {
method: 'POST',
});
Expand All @@ -82,6 +74,18 @@ export async function getUserDetails<T extends string | string[]>(
}
delete userProfile.data.delegationPitches;

const isMultiSig = await motherShipProvider(2192).getCode(walletAddress);
if (isMultiSig !== '0x' && profileIsEmpty(userProfile.data)) {
const profile = await profileContract
.connect(motherShipProvider(2192))
.getProfile(walletAddress);

//check if on other network it exists
return { ...profile, address: walletAddress } as T extends string
? GetUserDetails
: GetUserDetails[];
}

return { ...userProfile.data, delegationPitch: synthetixPitch } as T extends string
? GetUserDetails
: GetUserDetails[];
Expand Down Expand Up @@ -141,6 +145,13 @@ export async function getUserDetails<T extends string | string[]>(
return userProfile;
}
})
.concat(multiSigsProfiles) as T extends string ? GetUserDetails : GetUserDetails[];
.concat(multiSigsProfiles.filter((profile) => !profileIsEmpty(profile))) as T extends string
? GetUserDetails
: GetUserDetails[];
}
}

const profileIsEmpty = (profile: GetUserDetails) => {
const values = Object.values(profile).filter((val) => !utils.isAddress(val) && !!val.trim());
return values.every((val) => !!val);
};

0 comments on commit fc599f2

Please sign in to comment.