Skip to content

Commit

Permalink
TW-1614: Temple Tap Airdrop confirmation. + Checking confirm status i…
Browse files Browse the repository at this point in the history
…n interval
  • Loading branch information
alex-tsx committed Jan 6, 2025
1 parent 07633ce commit 0e9b0f9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
59 changes: 43 additions & 16 deletions src/app/pages/TempleTapAirdrop/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { Alert, Anchor, FormField, FormSubmitButton } from 'app/atoms';
import { ReactComponent as TelegramSvg } from 'app/icons/social-tg.svg';
import { ReactComponent as XSocialSvg } from 'app/icons/social-x.svg';
import PageLayout from 'app/layouts/PageLayout';
import { makeSigAuthMessageBytes } from 'lib/apis/temple/sig-auth';
import { sendTempleTapAirdropUsernameConfirmation } from 'lib/apis/temple-tap';
import { makeSigAuthMessageBytes, SigAuthValues } from 'lib/apis/temple/sig-auth';
import { checkTempleTapAirdropConfirmation, sendTempleTapAirdropUsernameConfirmation } from 'lib/apis/temple-tap';
import { t } from 'lib/i18n';
import { useTypedSWR } from 'lib/swr';
import { useAccount, useTempleClient, useTezos } from 'lib/temple/front';
import { TempleAccountType } from 'lib/temple/types';
import { useLocalStorage } from 'lib/ui/local-storage';
Expand Down Expand Up @@ -37,30 +38,56 @@ export const TempleTapAirdropPage = memo(() => {
null
);

const { register, handleSubmit, errors, setError, clearError, formState, reset } = useForm<FormData>();

const [confirmSent, setConfirmSent] = useState(false);
const [confirmed, setConfirmed] = useState(storedRecord?.[accountPkh] ?? false);

const prepSigAuthValues = useCallback(async () => {
const [publicKey, messageBytes] = await Promise.all([
tezos.signer.publicKey(),
makeSigAuthMessageBytes(accountPkh)
]);

const { prefixSig: signature } = await silentSign(accountPkh, messageBytes);

const values: SigAuthValues = { publicKey, messageBytes, signature };

return values;
}, [silentSign, tezos.signer, accountPkh]);

useTypedSWR(
[accountPkh],
async () => {
if (confirmed || !canSign) return;

const sigAuthValues = await prepSigAuthValues();

const confirmedRes = await checkTempleTapAirdropConfirmation(accountPkh, sigAuthValues);

if (!confirmedRes) return false;

setConfirmed(true);

return true;
},
{
suspense: true,
revalidateOnFocus: false,
refreshInterval: 60_000
}
);

const { register, handleSubmit, errors, setError, clearError, formState, reset } = useForm<FormData>();

const submitting = formState.isSubmitting;

const onSubmit = useCallback<OnSubmit<FormData>>(
async ({ username }) => {
clearError();

try {
const [publicKey, messageBytes] = await Promise.all([
tezos.signer.publicKey(),
makeSigAuthMessageBytes(accountPkh)
]);

const { prefixSig: signature } = await silentSign(accountPkh, messageBytes);
const sigAuthValues = await prepSigAuthValues();

const res = await sendTempleTapAirdropUsernameConfirmation(accountPkh, username, {
publicKey,
messageBytes,
signature
});
const res = await sendTempleTapAirdropUsernameConfirmation(accountPkh, username, sigAuthValues);

switch (res.data.status) {
case 'ACCEPTED':
Expand All @@ -79,7 +106,7 @@ export const TempleTapAirdropPage = memo(() => {
setError('username', 'submit-error', error?.response?.data?.message || 'Something went wrong...');
}
},
[reset, clearError, setError, setStoredRecord, silentSign, tezos.signer, accountPkh]
[reset, clearError, setError, setStoredRecord, prepSigAuthValues, accountPkh]
);

return (
Expand Down
14 changes: 14 additions & 0 deletions src/lib/apis/temple-tap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ export function sendTempleTapAirdropUsernameConfirmation(
}
);
}

export function checkTempleTapAirdropConfirmation(accountPkh: string, sigAuthValues: SigAuthValues) {
return templeWalletApi
.post<boolean>(
'/temple-tap/check-airdrop-confirmation',
{
accountPkh
},
{
headers: buildSigAuthHeaders(sigAuthValues)
}
)
.then(({ data }) => data);
}

0 comments on commit 0e9b0f9

Please sign in to comment.