forked from TrueFiEng/devcon-raffle
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🍦 Integrate with voucher claiming api (#54)
- Loading branch information
1 parent
0aefe81
commit 7d26659
Showing
9 changed files
with
219 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useMutation } from '@tanstack/react-query' | ||
import { GetVoucherNonceResponseSchema, GetVoucherResponseSchema, GetVoucherWithSigRequest } from '@/types/api/voucher' | ||
import { isApiErrorResponse } from '@/types/api/error' | ||
import { buildVoucherClaimMessage } from '@/utils/buildVoucherClaimMessage' | ||
import { useAccount, useChainId, useSignMessage } from 'wagmi' | ||
import { voucherCodeJwt } from '@/constants/jwt' | ||
|
||
export const useClaimVoucher = (setVoucher: (voucher: string) => void) => { | ||
const { address } = useAccount() | ||
const chainId = useChainId() | ||
const { signMessageAsync } = useSignMessage() | ||
|
||
return useMutation({ | ||
mutationFn: async () => { | ||
if (!address) throw new Error('Wallet not connected') | ||
if (getVoucherCodeJwt()) { | ||
return await getVoucherCodeUsingJwt() | ||
} | ||
|
||
const nonce = await getVoucherNonce() | ||
const signature = await signMessageAsync({ message: buildVoucherClaimMessage(chainId, address, nonce) }) | ||
return await getVoucherCode({ | ||
nonce, | ||
chainId, | ||
signature, | ||
userAddress: address, | ||
}) | ||
}, | ||
onSuccess: setVoucher, | ||
}) | ||
} | ||
|
||
const getVoucherNonce = async (): Promise<string> => { | ||
const response = await fetch('/api/voucher/nonce') | ||
const data = GetVoucherNonceResponseSchema.parse(await response.json()) | ||
if (isApiErrorResponse(data)) throw new Error(data.error) | ||
|
||
return data.nonce | ||
} | ||
|
||
const getVoucherCode = async (requestData: GetVoucherWithSigRequest): Promise<string> => { | ||
const voucherFetchResponse = await fetch('/api/voucher', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(requestData), | ||
}) | ||
return parseVoucherCodeResponse(await voucherFetchResponse.json()) | ||
} | ||
|
||
const getVoucherCodeUsingJwt = async () => { | ||
const voucherFetchResponse = await fetch('/api/voucher') | ||
return parseVoucherCodeResponse(await voucherFetchResponse.json()) | ||
} | ||
|
||
const parseVoucherCodeResponse = (response: unknown) => { | ||
const voucherResponse = GetVoucherResponseSchema.parse(response) | ||
if (isApiErrorResponse(voucherResponse)) throw new Error(voucherResponse.error) | ||
|
||
return voucherResponse.voucherCode | ||
} | ||
|
||
const getVoucherCodeJwt = () => { | ||
const cookies = document.cookie.split(';') | ||
for (const cookie of cookies) { | ||
const [key, value] = cookie.split('=') | ||
if (key === voucherCodeJwt) { | ||
return value | ||
} | ||
} | ||
return undefined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const voucherCodeJwt = 'voucherCodeJwt' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..1H7GnaOBGvdnpqQ4.WPJ_oQGQuE5umc4C4QXyah_9ITH90jeDR_1ZT38oG_KdvqDzJNQuI7s2v4-JWPrswu_xgID2KUqshABk2cRGubXj5eHE3gPnluMe8LUeOlImNH-LDfl43CeI5rMbi5ZjKUS-E_v-KxPfhm8G3jJLyMYjtyY4s0nUbmWfNp-QDy8ENhU5UPtUd2iA4d7aCDi9dkDICDm_6IMGHFEne-Xn-eHg0qW9QxuFDhaSYt036Dp52royS0bnIKq11RmxwSjfS8cpurS9x9egGO4ZabSF74d4zKgMfq1vzRolrhVd05VdDbX7EdiQVyF98naKg4L_LsC4124XJmp_CyUbpN6LbCJnONZ4H3G5bwVX1stH6AD-2IQXb8mGq_zNRuR1sizSYpPWVNrt6x21a16Fe7tzMyotkv8WzXDrHUolmBNP6JOjyLTyeF5uvFIxfkU1Zcl4dA3mlkrw38mfumh_bbSAlGJRFgbZY5-RQXbg14PB3ax6Uz34QBk_hBduebUAZ25E7LVRtkqyiwPRWrbm017tG0PJrY_-efmkhs7VByxtlwo1_fMiK-qulaeRfjksN2CCkV1M_K_DcRpjnQ6OckFGtQGJkR0cxRvn3zE9sLvrnht7qzDZCAOkigKdUDCKwa87LN5nmWAdO3dk33XpliZ26w1OGXWZwfo-9QO6X2rovey-YYy91JoZ6Y3Ewsg5avaEzpNu_exfVnz4lQHf_8ZgvGSVgYlDfBfNNnl5G7Z9attCM7YM4oPBvqEUtMvQ3-DXT1qjA7Zz_fFLvfW53u3hm0j5x4XUVsbgOhwpVfSwiPkvf10xqX-Y9AkzsyC4gafsMZQ0nN43YJ0dOy5rcDlFVeLWZ4dbf_47LtB3iziE2TtawLrFL9mNIzyqVjKMceGybm028L2lo0ZW30uP66t5s-VxuweAe1io3E0ZhB8gFAUX6Onm9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