Skip to content

Commit

Permalink
feat(#236): check nimi-ipns
Browse files Browse the repository at this point in the history
  • Loading branch information
adamazad committed Jan 17, 2023
1 parent ca625a5 commit cbf2bcf
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 66 deletions.
120 changes: 120 additions & 0 deletions src/api/GraphQl/schemas/generated/ens.ts

Large diffs are not rendered by default.

48 changes: 0 additions & 48 deletions src/api/GraphQl/schemas/generated/lens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,6 @@ export type AccessConditionOutput = {
token?: Maybe<Erc20OwnershipOutput>;
};

export type AchRequest = {
ethereumAddress: Scalars['EthereumAddress'];
freeTextHandle?: InputMaybe<Scalars['Boolean']>;
handle?: InputMaybe<Scalars['CreateHandle']>;
overrideAlreadyClaimed: Scalars['Boolean'];
overrideTradeMark: Scalars['Boolean'];
secret: Scalars['String'];
};

/** The request object to add interests to a profile */
export type AddProfileInterestsRequest = {
/** The profile interest to add */
Expand Down Expand Up @@ -934,10 +925,6 @@ export type CreateUnfollowBroadcastItemResult = {
typedData: CreateBurnEip712TypedData;
};

export type CurRequest = {
secret: Scalars['String'];
};

/** The custom filters types */
export enum CustomFiltersTypes {
Gardeners = 'GARDENERS'
Expand Down Expand Up @@ -1541,12 +1528,6 @@ export type HasTxHashBeenIndexedRequest = {
txId?: InputMaybe<Scalars['TxId']>;
};

export type HelRequest = {
handle: Scalars['Handle'];
remove: Scalars['Boolean'];
secret: Scalars['String'];
};

export type HidePublicationRequest = {
/** Publication id */
publicationId: Scalars['InternalPublicationId'];
Expand Down Expand Up @@ -1863,7 +1844,6 @@ export type ModuleInfo = {

export type Mutation = {
__typename?: 'Mutation';
ach?: Maybe<Scalars['Void']>;
/** Adds profile interests to the given profile */
addProfileInterests?: Maybe<Scalars['Void']>;
addReaction?: Maybe<Scalars['Void']>;
Expand All @@ -1890,7 +1870,6 @@ export type Mutation = {
createSetProfileMetadataViaDispatcher: RelayResult;
createToggleFollowTypedData: CreateToggleFollowBroadcastItemResult;
createUnfollowTypedData: CreateUnfollowBroadcastItemResult;
hel?: Maybe<Scalars['Void']>;
hidePublication?: Maybe<Scalars['Void']>;
idKitPhoneVerifyWebhook: IdKitPhoneVerifyWebhookResultStatusType;
proxyAction: Scalars['ProxyActionId'];
Expand All @@ -1902,11 +1881,6 @@ export type Mutation = {
};


export type MutationAchArgs = {
request: AchRequest;
};


export type MutationAddProfileInterestsArgs = {
request: AddProfileInterestsRequest;
};
Expand Down Expand Up @@ -2046,11 +2020,6 @@ export type MutationCreateUnfollowTypedDataArgs = {
};


export type MutationHelArgs = {
request: HelRequest;
};


export type MutationHidePublicationArgs = {
request: HidePublicationRequest;
};
Expand Down Expand Up @@ -3089,7 +3058,6 @@ export type Query = {
challenge: AuthChallengeResult;
claimableHandles: ClaimableHandles;
claimableStatus: ClaimStatus;
cur: Array<Scalars['String']>;
defaultProfile?: Maybe<Profile>;
doesFollow: Array<DoesFollowResponse>;
enabledModuleCurrencies: Array<Erc20>;
Expand Down Expand Up @@ -3127,7 +3095,6 @@ export type Query = {
publicationRevenue?: Maybe<PublicationRevenue>;
publications: PaginatedPublicationResult;
recommendedProfiles: Array<Profile>;
rel?: Maybe<Scalars['Void']>;
search: SearchResult;
/** @deprecated You should be using feed, this will not be supported after 15th November 2021, please migrate. */
timeline: PaginatedTimelineResult;
Expand Down Expand Up @@ -3156,11 +3123,6 @@ export type QueryChallengeArgs = {
};


export type QueryCurArgs = {
request: CurRequest;
};


export type QueryDefaultProfileArgs = {
request: DefaultProfileRequest;
};
Expand Down Expand Up @@ -3316,11 +3278,6 @@ export type QueryRecommendedProfilesArgs = {
};


export type QueryRelArgs = {
request: RelRequest;
};


export type QuerySearchArgs = {
request: SearchQueryRequest;
};
Expand Down Expand Up @@ -3413,11 +3370,6 @@ export type RefreshRequest = {
refreshToken: Scalars['Jwt'];
};

export type RelRequest = {
ethereumAddress: Scalars['EthereumAddress'];
secret: Scalars['String'];
};

export type RelayError = {
__typename?: 'RelayError';
reason: RelayErrorReasons;
Expand Down
20 changes: 12 additions & 8 deletions src/api/RestAPI/hooks/useDeployedPageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useQuery } from '@tanstack/react-query';
import { useRainbow } from '../../../hooks/useRainbow';
import { nimiClient } from '../utils';

interface DeployedNimiPageType {
interface NimiSnapshot {
publisher: string;
cid: string | null;
cidV1: string | null;
Expand All @@ -22,18 +22,22 @@ export function useDeployedPageData(ensName: string) {
const { chainId } = useRainbow();

const getDeployedPageData = async () => {
const params = {
ens: ensName,
};

const { data } = await nimiClient.get<{ data: DeployedNimiPageType[] }>(`/nimi/by`, { params });
const { data } = await nimiClient.get<{
data: {
ipns?: string;
nimi?: NimiSnapshot;
};
}>(`/ens/has-nimi-ipns`, {
params: {
domain: ensName,
},
});
return data;
};

return useQuery(['fetchDeployedNimiData', ensName, chainId], getDeployedPageData, {
select: ({ data }) => {
if (data.length) return data[0];
else return undefined;
return data;
},
});
}
21 changes: 19 additions & 2 deletions src/api/RestAPI/hooks/usePublishNimiIPNS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { useMutation } from '@tanstack/react-query';

import { nimiClient } from '../utils';

interface PublishNimiViaIPNSParams {
export interface PublishNimiViaIPNSParams {
chainId: number;
nimi: Nimi;
signature: string;
}

type UpdateNimiViaIPNSParams = PublishNimiViaIPNSParams & {
signature: string;
};

interface PublishNimiIPNSResponse {
cidV1: string;
ipns: string;
Expand All @@ -21,9 +24,23 @@ const postUserData = async (params: PublishNimiViaIPNSParams) => {
return data.data;
};

const updateNimiIPNS = async (params: UpdateNimiViaIPNSParams) => {
const { data } = await nimiClient.put<{
data: PublishNimiIPNSResponse;
}>('/nimi/publish/ipns', params);
return data.data;
};

/**
* Returns mutation for getting IPNS hash
*/
export function usePublishNimiIPNS() {
return useMutation(['publishNimiIPNS'], postUserData);
}

/**
* Returns mutation for getting IPNS hash
*/
export function useUpdateNimiIPNS() {
return useMutation(['updateNimiIPNS'], updateNimiIPNS);
}
26 changes: 21 additions & 5 deletions src/components/CreateNimi/CreateNimi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { FormProvider, useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useSignMessage } from 'wagmi';

import { usePublishNimiIPNS } from '../../api/RestAPI/hooks/usePublishNimiIPNS';
import { usePublishNimiIPNS, useUpdateNimiIPNS } from '../../api/RestAPI/hooks/usePublishNimiIPNS';
import { useUploadImageToIPFS } from '../../api/RestAPI/hooks/useUploadImageToIPFS';
import PlaceholderMini from '../../assets/images/nimi-placeholder.png';
// Partials
Expand Down Expand Up @@ -86,13 +86,15 @@ export interface CreateNimiProps {
* The initial Nimi to edit
*/
initialNimi: Nimi;
nimiIPNSKey?: string;
}

const debug = createDebugger('Nimi:CreateNimi');

export function CreateNimi({ ensAddress, ensName, availableThemes, initialNimi }: CreateNimiProps) {
export function CreateNimi({ ensAddress, ensName, availableThemes, initialNimi, nimiIPNSKey }: CreateNimiProps) {
const { loading: loadingLensProfile, defaultProfileData: lensProfile } = useLensDefaultProfileData();
const { mutateAsync: publishNimiAsync } = usePublishNimiIPNS();
const { mutateAsync: updateNimiAsync } = useUpdateNimiIPNS();
const { mutateAsync: uploadImageAsync } = useUploadImageToIPFS();

const { t } = useTranslation('nimi');
Expand Down Expand Up @@ -178,11 +180,26 @@ export function CreateNimi({ ensAddress, ensName, availableThemes, initialNimi }
throw new Error('ENS Public Resolver contract is not available.');
}

const signature = await signMessageAsync({ message: JSON.stringify(nimi) });
// Updating a current Nimi IPNS record
if (nimiIPNSKey) {
const signature = await signMessageAsync({ message: JSON.stringify(nimi) });
const updateNimiResponse = await updateNimiAsync({
nimi,
chainId: 1, // always mainnet
signature,
});
if (!updateNimiResponse || !updateNimiResponse.cidV1) {
throw new Error('No response from updateNimiAsync');
}

setIsNimiPublished(true);
setIsPublishingNimi(false);
return;
}

// Publishing a new Nimi IPNS record
const { cidV1, ipns } = await publishNimiAsync({
nimi,
signature,
chainId: 1, // always mainnet
});

Expand All @@ -191,7 +208,6 @@ export function CreateNimi({ ensAddress, ensName, availableThemes, initialNimi }
}

const contentHash = `ipns://${ipns}`;

// Get current content hash from ENS contract
const currentContentHashEncoded = await publicResolverContract.contenthash(ensNameHash(ensName));
const newContentHashEncoded = encodeContenthash(contentHash).encoded as unknown as string;
Expand Down
7 changes: 6 additions & 1 deletion src/components/CreateNimi/CreateNimiContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export function CreateNimiContainer({ ensName }: CreateNimiContainerProps) {
const { avaliableThemes, isLoading: isThemeLoading, hasPoaps } = useAvaliableThemesFromPoaps(account);

//check for users current Nimi profile data or else adds data generated from ens
const { data: initialNimi, loading: initialNimiLoading } = useInitialtNimiData({
const {
data: initialNimi,
loading: initialNimiLoading,
ipns,
} = useInitialtNimiData({
ensName,
account,
});
Expand All @@ -33,6 +37,7 @@ export function CreateNimiContainer({ ensName }: CreateNimiContainerProps) {
ensName={ensName as string}
availableThemes={avaliableThemes}
initialNimi={insertPoapWidgetIntoNimi(initialNimi, hasPoaps, account)}
nimiIPNSKey={ipns}
/>
</Container>
);
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/useDefaultNimiData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const debug = createDebugger('hooks:useDefaultNimiData');

interface UseInitialNimiData {
data?: Nimi;
ipns?: string;
loading: boolean;
}
interface InitialNimiDataProps {
Expand Down Expand Up @@ -45,8 +46,11 @@ export function useInitialtNimiData({ ensName, account }: InitialNimiDataProps):
widgets: [],
};

if (isDeployedSuccess && deployedNimi && !isDepoyedLoading) nimi = deployedNimi.nimi;
else if (isGeneratedSuccess && generatedNimi && !isGeneratedFetching) nimi = generatedNimi;
if (isDeployedSuccess && deployedNimi.nimi && !isDepoyedLoading) {
nimi = deployedNimi.nimi.nimi;
} else if (isGeneratedSuccess && generatedNimi && !isGeneratedFetching) {
nimi = generatedNimi;
}

if (!(isDepoyedLoading || isGeneratedFetching)) {
if (!nimi.theme) nimi.theme = defaultTheme;
Expand All @@ -69,5 +73,6 @@ export function useInitialtNimiData({ ensName, account }: InitialNimiDataProps):
return {
loading: isDepoyedLoading || isGeneratedFetching,
data: nimiObject,
ipns: deployedNimi?.ipns,
};
}

0 comments on commit cbf2bcf

Please sign in to comment.