Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing referral in the URL for dApp #679

Merged
merged 27 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ebf3fbf
Create a custom impl of `useLocalStorage` hook
kkosiorowska Jul 30, 2024
1a2572e
Detect the referral through the URL parameter
kkosiorowska Jul 30, 2024
d6a9fc3
Fix failed dApp build
kkosiorowska Jul 30, 2024
4b2256f
Set the default referral to 0
kkosiorowska Jul 31, 2024
b94b0a6
Support the default value for the referral
kkosiorowska Jul 31, 2024
ea4a13b
Merge branch 'main' of github.com:thesis/acre into referral-program
kkosiorowska Jul 31, 2024
216583f
Allow setting of unregistered referral
kkosiorowska Aug 5, 2024
33e8ba4
Use search referral param for all router path
kkosiorowska Aug 5, 2024
bc3c8e7
Referral should be a number only up to max `uint16`
kkosiorowska Aug 5, 2024
b0c62ea
Check if referral is greater than or equal to zero
kkosiorowska Aug 5, 2024
ac44490
Merge branch 'main' of github.com:thesis/acre into referral-program
kkosiorowska Aug 6, 2024
e09acd9
Separate `UnexpectedErrorModal` into a dedicated component
kkosiorowska Aug 6, 2024
ebf4c19
Show unexpected error modal when referral is invalid
kkosiorowska Aug 6, 2024
57e1cb6
Navigate to subpage with the referral search param
kkosiorowska Aug 6, 2024
fbdab50
Fix an eslint issue
kkosiorowska Aug 6, 2024
6abe11a
Fix a failed build
kkosiorowska Aug 6, 2024
06f31e2
Simplifying the use of searchParams
kkosiorowska Aug 6, 2024
5a45b7f
Remove unnecessary parameter
kkosiorowska Aug 6, 2024
565beb8
Create a utils function for getting the referral form URL
kkosiorowska Aug 6, 2024
3d95a56
Merge branch 'main' of github.com:thesis/acre into referral-program
kkosiorowska Aug 7, 2024
da29068
Fix an issue after merge
kkosiorowska Aug 7, 2024
6a6c007
Use `useAppNavigate` instead of `useNavigate`
kkosiorowska Aug 7, 2024
056cd50
Log an error to the console for incorrect referral
kkosiorowska Aug 7, 2024
13de34b
Fix the referral value check condition
kkosiorowska Aug 7, 2024
e507fd9
Make sure that the error modal will always be open
kkosiorowska Aug 8, 2024
2c82d27
Merge branch 'main' of github.com:thesis/acre into referral-program
kkosiorowska Aug 12, 2024
6e52e05
Merge branch 'main' into referral-program
nkuba Aug 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions dapp/.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ VITE_SENTRY_DSN=""

# TODO: Use a more general source
VITE_ETH_HOSTNAME_HTTP="https://sepolia.infura.io/v3/c80e8ccdcc4c4a809bce4fc165310617"
# TODO: Set the correct referral
VITE_REFERRAL=123
VITE_REFERRAL=0

# TODO: Set this env variable in CI.
VITE_TBTC_API_ENDPOINT=""
Expand Down
1 change: 1 addition & 0 deletions dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@orangekit/react": "1.0.0-beta.29",
"@orangekit/sign-in-with-wallet": "1.0.0-beta.6",
"@reduxjs/toolkit": "^2.2.0",
"@rehooks/local-storage": "^2.4.5",
"@safe-global/safe-core-sdk-types": "^5.0.1",
"@sentry/react": "^7.98.0",
"@sentry/types": "^7.102.0",
Expand Down
1 change: 1 addition & 0 deletions dapp/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from "./staking"
export { default as tbtc } from "./tbtc"
export * from "./time"
export { default as wallets } from "./wallets"
export { default as referrals } from "./referrals"
6 changes: 6 additions & 0 deletions dapp/src/constants/referrals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import env from "#/constants/env"

export default {
DEFAULT: env.REFERRAL,
STAKING_REWARDS: 48450,
}
7 changes: 4 additions & 3 deletions dapp/src/contexts/StakeFlowContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
useAcreContext,
useStakeFlow,
} from "#/acre-react/hooks"
import { env } from "#/constants"
import useBitcoinRecoveryAddress from "#/hooks/useBitcoinRecoveryAddress"
import useReferral from "#/hooks/useReferral"

type StakeFlowContextValue = Omit<UseStakeFlowReturn, "initStake"> & {
initStake: () => Promise<void>
Expand All @@ -25,12 +25,13 @@ export function StakeFlowProvider({ children }: { children: React.ReactNode }) {
stake,
} = useStakeFlow()
const bitcoinRecoveryAddress = useBitcoinRecoveryAddress()
const { referral } = useReferral()

const initStake = useCallback(async () => {
if (!acre) throw new Error("Acre SDK not defined")

await acreInitStake(env.REFERRAL, bitcoinRecoveryAddress)
}, [acre, acreInitStake, bitcoinRecoveryAddress])
await acreInitStake(referral, bitcoinRecoveryAddress)
}, [acre, acreInitStake, bitcoinRecoveryAddress, referral])

const context = useMemo(
() => ({
Expand Down
3 changes: 3 additions & 0 deletions dapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ export { default as useTriggerConnectWalletModal } from "./useTriggerConnectWall
export { default as useMobileMode } from "./useMobileMode"
export { default as useBitcoinRecoveryAddress } from "./useBitcoinRecoveryAddress"
export { default as useIsFetchedWalletData } from "./useIsFetchedWalletData"
export { default as useLocalStorage } from "./useLocalStorage"
export { default as useDetectReferral } from "./useDetectReferral"
export { default as useReferral } from "./useReferral"
10 changes: 10 additions & 0 deletions dapp/src/hooks/useDetectReferral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useEffect } from "react"
import useReferral from "./useReferral"

export default function useDetectReferral() {
const { detectReferral } = useReferral()

useEffect(() => {
detectReferral()
}, [detectReferral])
}
2 changes: 2 additions & 0 deletions dapp/src/hooks/useInitApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { useAccountsChangedUnisat } from "./orangeKit/useAccountsChangedUnisat"
import { useDisconnectOKX } from "./orangeKit/useDisconnectOKX"
import { useInitDataFromSdk, useInitializeAcreSdk } from "./sdk"
import { useSentry } from "./sentry"
import useDetectReferral from "./useDetectReferral"
import { useDisconnectWallet } from "./useDisconnectWallet"
import { useFetchBTCPriceUSD } from "./useFetchBTCPriceUSD"

export function useInitApp() {
// TODO: Let's uncomment when dark mode is ready
// useDetectThemeMode()
useSentry()
useDetectReferral()
useInitializeAcreSdk()
useInitDataFromSdk()
useFetchBTCPriceUSD()
Expand Down
5 changes: 5 additions & 0 deletions dapp/src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { useLocalStorage as useRehooksLocalStorage } from "@rehooks/local-storage"

export default function useLocalStorage<T>(key: string, defaultValue: T) {
return useRehooksLocalStorage(key, defaultValue)
}
48 changes: 48 additions & 0 deletions dapp/src/hooks/useReferral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { referrals } from "#/constants"
import { useCallback, useMemo } from "react"
import useLocalStorage from "./useLocalStorage"

const PARAM_NAME = "ref"

function isExistingReferral(detectedReferral: number) {
const referral = Object.values(referrals)
return referral.includes(detectedReferral)
}

type UseReferralReturn = {
referral: number
detectReferral: () => void
resetReferral: () => void
}

export default function useReferral(): UseReferralReturn {
const [referral, setReferral] = useLocalStorage<number>(
"referral",
referrals.DEFAULT,
)

const detectReferral = useCallback(() => {
const params = new URLSearchParams(window.location.search)
const param = params.get(PARAM_NAME)
const detectedReferral = param ? parseInt(param, 10) : null

if (detectedReferral && isExistingReferral(detectedReferral)) {
setReferral(detectedReferral)
} else {
setReferral(referrals.DEFAULT)
nkuba marked this conversation as resolved.
Show resolved Hide resolved
}
}, [setReferral])

const resetReferral = useCallback(() => {
setReferral(referrals.DEFAULT)
}, [setReferral])

return useMemo(
() => ({
detectReferral,
resetReferral,
referral,
}),
[detectReferral, resetReferral, referral],
)
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading