From 95b6ff44bd0dc2f2eb2abe2ce3f152f6f8a2bac3 Mon Sep 17 00:00:00 2001 From: Ben Goldberg Date: Wed, 29 Nov 2023 23:26:28 -0800 Subject: [PATCH] hook up some logic to claim signing --- src/graphql/queries/metadata.graphql | 4 + src/navigation/Routes.android.tsx | 7 + src/navigation/Routes.ios.tsx | 2 +- src/navigation/effects.tsx | 5 + src/screens/{ => points}/ConsoleSheet.tsx | 167 ++++++++++++++------ src/screens/points/PointsScreen.tsx | 4 +- src/screens/points/content/ClaimContent.tsx | 2 + 7 files changed, 143 insertions(+), 48 deletions(-) rename src/screens/{ => points}/ConsoleSheet.tsx (84%) diff --git a/src/graphql/queries/metadata.graphql b/src/graphql/queries/metadata.graphql index e37082565a9..2266f1670d3 100644 --- a/src/graphql/queries/metadata.graphql +++ b/src/graphql/queries/metadata.graphql @@ -121,6 +121,10 @@ query getdApp($shortName: String!, $url: String!, $status: Boolean!) { query getPointsDataForWallet($address: String!) { points(address: $address) { + error { + message + type + } meta { distribution { next diff --git a/src/navigation/Routes.android.tsx b/src/navigation/Routes.android.tsx index 3967add7d59..95331e62414 100644 --- a/src/navigation/Routes.android.tsx +++ b/src/navigation/Routes.android.tsx @@ -54,6 +54,7 @@ import { addCashSheet, nftSingleOfferSheetPreset, walletconnectBottomSheetPreset, + consoleSheetPreset, } from './effects'; import { InitialRouteContext } from './initialRoute'; import { onNavigationStateChange } from './onNavigationStateChange'; @@ -82,6 +83,7 @@ import { PositionSheet } from '@/screens/positions/PositionSheet'; import MintSheet from '@/screens/mints/MintSheet'; import { MintsSheet } from '@/screens/MintsSheet/MintsSheet'; import { RemotePromoSheet } from '@/components/remote-promo-sheet/RemotePromoSheet'; +import { ConsoleSheet } from '@/screens/points/ConsoleSheet'; const Stack = createStackNavigator(); const OuterStack = createStackNavigator(); @@ -386,6 +388,11 @@ function BSNavigator() { name={Routes.CONFIRM_REQUEST} options={walletconnectBottomSheetPreset} /> + ); } diff --git a/src/navigation/Routes.ios.tsx b/src/navigation/Routes.ios.tsx index e9b49a60d0d..bbce43643e5 100644 --- a/src/navigation/Routes.ios.tsx +++ b/src/navigation/Routes.ios.tsx @@ -96,7 +96,7 @@ import { NFTSingleOfferSheet } from '@/screens/NFTSingleOfferSheet'; import MintSheet from '@/screens/mints/MintSheet'; import { MintsSheet } from '@/screens/MintsSheet/MintsSheet'; import { RemotePromoSheet } from '@/components/remote-promo-sheet/RemotePromoSheet'; -import { ConsoleSheet } from '@/screens/ConsoleSheet'; +import { ConsoleSheet } from '@/screens/points/ConsoleSheet'; type StackNavigatorParams = { [Routes.SEND_SHEET]: unknown; diff --git a/src/navigation/effects.tsx b/src/navigation/effects.tsx index b85fe0cabda..bad6f9430e9 100644 --- a/src/navigation/effects.tsx +++ b/src/navigation/effects.tsx @@ -463,6 +463,11 @@ export const walletconnectBottomSheetPreset: BottomSheetNavigationOptions = { backdropOpacity: 1, }; +export const consoleSheetPreset: BottomSheetNavigationOptions = { + backdropColor: 'black', + backdropOpacity: 1, +}; + export const expandedPresetWithSmallGestureResponseDistance: StackNavigationOptions & BottomSheetNavigationOptions = { ...expandedPreset, diff --git a/src/screens/ConsoleSheet.tsx b/src/screens/points/ConsoleSheet.tsx similarity index 84% rename from src/screens/ConsoleSheet.tsx rename to src/screens/points/ConsoleSheet.tsx index b3669b9d4fe..451a1ea691b 100644 --- a/src/screens/ConsoleSheet.tsx +++ b/src/screens/points/ConsoleSheet.tsx @@ -35,18 +35,36 @@ import { } from '@/design-system'; import { alignHorizontalToFlexAlign } from '@/design-system/layout/alignment'; import { IS_DEV } from '@/env'; -import { useDimensions } from '@/hooks'; +import { useAccountProfile, useDimensions } from '@/hooks'; import { fonts } from '@/styles'; import { useTheme } from '@/theme'; import { safeAreaInsetValues } from '@/utils'; import { HapticFeedbackType } from '@/utils/haptics'; +import { metadataClient } from '@/graphql'; +import { signPersonalMessage } from '@/model/wallet'; +import { RouteProp, useRoute } from '@react-navigation/native'; +import { WrappedAlert as Alert } from '@/helpers/alert'; +import { RainbowError, logger } from '@/logger'; +import { PointsErrorType } from '@/graphql/__generated__/metadata'; +import { pointsQueryKey } from '@/resources/points'; +import { queryClient } from '@/react-query'; const SCREEN_BOTTOM_INSET = safeAreaInsetValues.bottom + 20; const CHARACTER_WIDTH = 9.2725; +type ConsoleSheetParams = { + ConsoleSheet: { + referralCode?: string; + }; +}; + export const ConsoleSheet = () => { + const { params } = useRoute>(); + const referralCode = params?.referralCode; + const [didConfirmOwnership, setDidConfirmOwnership] = useState(false); const [showSignInButton, setShowSignInButton] = useState(false); + const { accountAddress } = useAccountProfile(); useEffect(() => { if (IS_DEV) { @@ -55,6 +73,54 @@ export const ConsoleSheet = () => { } }, []); + const signIn = useCallback(async () => { + let points; + let signature; + let challenge; + const challengeResponse = await metadataClient.getPointsOnboardChallenge({ + address: accountAddress, + referral: referralCode, + }); + challenge = challengeResponse?.pointsOnboardChallenge; + if (challenge) { + const signatureResponse = await signPersonalMessage(challenge); + signature = signatureResponse?.result; + if (signature) { + points = await metadataClient.onboardPoints({ + address: accountAddress, + signature, + referral: referralCode, + }); + } + } + if (!points) { + logger.error( + new RainbowError( + `Error onboarding points user - accountAddress: ${accountAddress}, referralCode: ${referralCode}, challenge: ${challenge}, signature: ${signature}` + ) + ); + Alert.alert('Something went wrong, please try again.'); + } else { + if (points.onboardPoints?.error) { + const errorType = points.onboardPoints?.error?.type; + if (errorType === PointsErrorType.ExistingUser) { + Alert.alert('Points already claimed. Please restart the app.'); + } else if (errorType === PointsErrorType.InvalidReferralCode) { + Alert.alert( + 'Invalid referral code. Please check your code and try again.' + ); + } + } else { + setDidConfirmOwnership(true); + queryClient.setQueryData( + pointsQueryKey({ address: accountAddress }), + points + ); + console.log(points); + } + } + }, [accountAddress, referralCode]); + return ( { > setTimeout(() => setDidConfirmOwnership(true), 1000)} + // onPress={() => setTimeout(() => setDidConfirmOwnership(true), 1000)} + onPress={signIn} /> @@ -109,55 +176,65 @@ const NeonButton = ({ const green = useForegroundColor('green'); return ( - - - - - - - + + + - {label} - - - - + + {label} + + + + + ); }; @@ -773,8 +850,6 @@ const triggerHapticFeedback = (hapticType: HapticFeedbackType) => const styles = StyleSheet.create({ neonButtonWrapper: { alignSelf: 'center', - bottom: 16, - position: 'absolute', }, neonButton: { alignContent: 'center', @@ -809,7 +884,7 @@ const styles = StyleSheet.create({ paddingHorizontal: 30, paddingVertical: 45, width: '100%', - zIndex: 2, + zIndex: -1, }, text: { fontFamily: fonts.family.SFMono, diff --git a/src/screens/points/PointsScreen.tsx b/src/screens/points/PointsScreen.tsx index 4dc670e877f..c28cec993f8 100644 --- a/src/screens/points/PointsScreen.tsx +++ b/src/screens/points/PointsScreen.tsx @@ -18,6 +18,7 @@ import { createMaterialTopTabNavigator } from '@react-navigation/material-top-ta import { deviceUtils } from '@/utils'; import ClaimContent from './content/ClaimContent'; import ReferralContent from './content/ReferralContent'; +import { PointsErrorType } from '@/graphql/__generated__/metadata'; const Swipe = createMaterialTopTabNavigator(); @@ -35,7 +36,8 @@ export default function PointsScreen() { walletAddress: accountAddress, }); - const isOnboarded = !!data?.points?.user?.referralCode; + const isOnboarded = + data?.points?.error?.type !== PointsErrorType.NonExistingUser; return ( diff --git a/src/screens/points/content/ClaimContent.tsx b/src/screens/points/content/ClaimContent.tsx index 300df84a24b..841a84c504a 100644 --- a/src/screens/points/content/ClaimContent.tsx +++ b/src/screens/points/content/ClaimContent.tsx @@ -13,6 +13,7 @@ import { TAB_BAR_HEIGHT } from '@/navigation/SwipeNavigator'; import React from 'react'; import Svg, { Path } from 'react-native-svg'; import * as i18n from '@/languages'; +import Routes from '@/navigation/routesNames'; export default function ClaimContent() { const { accentColor } = useAccountAccentColor(); @@ -70,6 +71,7 @@ export default function ClaimContent() { alignItems: 'center', justifyContent: 'center', }} + onPress={() => navigate(Routes.CONSOLE_SHEET)} >