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 Gitcoin backend (#55)
Co-authored-by: p-sad <[email protected]>
- Loading branch information
1 parent
7d26659
commit da17eb1
Showing
17 changed files
with
254 additions
and
60 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,68 @@ | ||
import { isApiErrorResponse } from '@/types/api/error' | ||
import { | ||
GetPassportScorerNonceResponseSchema, | ||
GetResponseSchema, | ||
SubmitAddressForScoringRequest, | ||
SubmitAddressForScoringResponseSchema, | ||
} from '@/types/api/scorer' | ||
import { useMutation } from '@tanstack/react-query' | ||
import { Hex } from 'viem' | ||
import { useAccount, useChainId, useSignMessage } from 'wagmi' | ||
|
||
export const useSendForScoring = () => { | ||
const { address } = useAccount() | ||
const chainId = useChainId() | ||
const { signMessageAsync } = useSignMessage() | ||
|
||
return useMutation({ | ||
mutationFn: async () => { | ||
if (!address) { | ||
throw new Error('No address') | ||
} | ||
|
||
try { | ||
return await getGitcoinScore(address, chainId) | ||
} catch (error) { | ||
if (!(error instanceof ErrorWithStatus) || error.status != 404) { | ||
throw error | ||
} | ||
} | ||
|
||
const nonceData = await getGitcoinNonce() | ||
const signature = await signMessageAsync({ message: nonceData.message }) | ||
await sendForScoring({ userAddress: address as Hex, signature, nonce: nonceData.nonce }) | ||
}, | ||
}) | ||
} | ||
|
||
class ErrorWithStatus extends Error { | ||
constructor(message: string, public status: number) { | ||
super(message) | ||
} | ||
} | ||
|
||
export const getGitcoinScore = async (userAddress: Hex, chainId: number) => { | ||
const result = await fetch(`/api/scorer/${userAddress}?chainId=${chainId}`) | ||
const data = GetResponseSchema.parse(await result.json()) | ||
if (isApiErrorResponse(data)) throw new ErrorWithStatus(data.error, result.status) | ||
return data | ||
} | ||
|
||
const getGitcoinNonce = async () => { | ||
const response = await fetch(`/api/scorer/nonce`) | ||
const data = GetPassportScorerNonceResponseSchema.parse(await response.json()) | ||
if (isApiErrorResponse(data)) throw new Error(data.error) | ||
return data | ||
} | ||
|
||
const sendForScoring = async (requestData: SubmitAddressForScoringRequest) => { | ||
const response = await fetch(`/api/scorer`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(requestData), | ||
}) | ||
const data = SubmitAddressForScoringResponseSchema.parse(await response.json()) | ||
if (isApiErrorResponse(data)) throw new Error(data.error) | ||
} |
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
25 changes: 25 additions & 0 deletions
25
packages/frontend/src/components/userActious/bid/InitialBidFlow.tsx
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,25 @@ | ||
import { GitcoinCredentials } from '@/types/passport/GticoinCredentials' | ||
import { useState } from 'react' | ||
import { GitcoinFlow } from '../gitcoin/GitcoinFlow' | ||
import { PlaceBidFlow } from './PlaceBid/PlaceBidFlow' | ||
|
||
interface Props { | ||
setTransactionViewLock: (value: boolean) => void | ||
} | ||
|
||
export const InitialBidFlow = ({ setTransactionViewLock }: Props) => { | ||
const [gitcoinCredentials, setGitcoinCredentials] = useState<GitcoinCredentials | undefined>() | ||
const [gitcoinSettled, setGitcoinSettled] = useState<boolean>(false) | ||
|
||
if (!gitcoinCredentials || !gitcoinSettled) { | ||
return ( | ||
<GitcoinFlow | ||
setGitcoinCredentials={setGitcoinCredentials} | ||
gitcoinCredentials={gitcoinCredentials} | ||
gitcoinSettled={() => setGitcoinSettled(true)} | ||
/> | ||
) | ||
} | ||
|
||
return <PlaceBidFlow setTransactionViewLock={setTransactionViewLock} gitcoinCredentials={gitcoinCredentials} /> | ||
} |
15 changes: 12 additions & 3 deletions
15
packages/frontend/src/components/userActious/bid/PlaceBid/PlaceBidFlow.tsx
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
76 changes: 76 additions & 0 deletions
76
packages/frontend/src/components/userActious/gitcoin/GitcoinFlow.tsx
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,76 @@ | ||
import { useState } from 'react' | ||
import { CheckGitcoinPassport } from './CheckGitcoinPassport' | ||
import { CheckGitcoinScore } from './CheckingGitcoinScore' | ||
import { UserGitcoinScore } from '@/components/userActious/gitcoin/UserGitcoinScore' | ||
import { MissingGitcoinPassport } from './MissingGitcoinPassport' | ||
import { useSendForScoring } from '@/backend/getPassportScore' | ||
import { GitcoinCredentials } from '@/types/passport/GticoinCredentials' | ||
import { GetScoreResponseSuccess } from '@/types/api/scorer' | ||
|
||
enum GitcoinState { | ||
INITIAL_PAGE, | ||
CHECKING_SCORE, | ||
MISSING_PASSPORT, | ||
YOUR_SCORE, | ||
} | ||
|
||
const ScoreDecimals = 8 | ||
const ScoreMultiplier = 10 ** ScoreDecimals | ||
|
||
interface Props { | ||
setGitcoinCredentials: (credentials: GitcoinCredentials) => void | ||
gitcoinCredentials: GitcoinCredentials | undefined | ||
gitcoinSettled: () => void | ||
} | ||
|
||
export const GitcoinFlow = ({ gitcoinCredentials, setGitcoinCredentials, gitcoinSettled }: Props) => { | ||
const [gitcoinState, setGitcoinState] = useState<GitcoinState>(GitcoinState.INITIAL_PAGE) | ||
const { mutateAsync, isSuccess, isError } = useSendForScoring() | ||
|
||
const setCredentials = (credentials: GetScoreResponseSuccess) => { | ||
setGitcoinCredentials({ | ||
score: BigInt(credentials?.score), | ||
proof: credentials.signature, | ||
}) | ||
setGitcoinState(GitcoinState.YOUR_SCORE) | ||
} | ||
|
||
const sendForScoring = async () => { | ||
const data = await mutateAsync() | ||
if (data?.status === 'done') { | ||
setCredentials(data) | ||
} | ||
} | ||
|
||
const onCheckScoreClick = () => { | ||
setGitcoinState(GitcoinState.CHECKING_SCORE) | ||
sendForScoring() | ||
} | ||
|
||
switch (gitcoinState) { | ||
case GitcoinState.INITIAL_PAGE: | ||
return <CheckGitcoinPassport onCheckScoreClick={onCheckScoreClick} /> | ||
case GitcoinState.CHECKING_SCORE: | ||
return ( | ||
<CheckGitcoinScore | ||
setGitcoinCredentials={setCredentials} | ||
gitcoinRequestSettled={isSuccess} | ||
gitcoinRequestError={isError} | ||
onSignAgainClick={sendForScoring} | ||
/> | ||
) | ||
case GitcoinState.YOUR_SCORE: | ||
return ( | ||
<UserGitcoinScore | ||
userScore={Number(gitcoinCredentials?.score) / ScoreMultiplier} | ||
gitcoinSettled={gitcoinSettled} | ||
getBackToScoring={onCheckScoreClick} | ||
/> | ||
) | ||
case GitcoinState.MISSING_PASSPORT: | ||
return <MissingGitcoinPassport afterCreateClick={() => setGitcoinState(GitcoinState.INITIAL_PAGE)} /> | ||
|
||
default: | ||
return <CheckGitcoinPassport onCheckScoreClick={onCheckScoreClick} /> | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
packages/frontend/src/components/userActious/gitcoin/GitcointFlow.tsx
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.