import { useVegaWallet, useVegaWalletDialogStore, determineId, } from '@vegaprotocol/wallet'; import { RainbowButton } from './buttons'; import { useState } from 'react'; import { CopyWithTooltip, Dialog, ExternalLink, InputError, Intent, TradingAnchorButton, TradingButton, VegaIcon, VegaIconNames, } from '@vegaprotocol/ui-toolkit'; import { addDecimalsFormatNumber } from '@vegaprotocol/utils'; import { DApp, TokenStaticLinks, useLinks } from '@vegaprotocol/environment'; import { useStakeAvailable } from './hooks/use-stake-available'; import { ABOUT_REFERRAL_DOCS_LINK } from './constants'; import { useIsInReferralSet, useReferral } from './hooks/use-referral'; import { useT } from '../../lib/use-t'; import { Navigate } from 'react-router-dom'; import { Routes } from '../../lib/links'; import { useReferralProgram } from './hooks/use-referral-program'; export const CreateCodeContainer = () => { const { pubKey } = useVegaWallet(); const isInReferralSet = useIsInReferralSet(pubKey); // Navigate to the index page when already in the referral set. if (isInReferralSet) { return ; } return ; }; export const CreateCodeForm = () => { const t = useT(); const [dialogOpen, setDialogOpen] = useState(false); const openWalletDialog = useVegaWalletDialogStore( (store) => store.openVegaWalletDialog ); const { pubKey, isReadOnly } = useVegaWallet(); return (

{t('Create a referral code')}

{t( 'Generate a referral code to share with your friends and access the commission benefits of the current program.' )}

{ if (pubKey) { setDialogOpen(true); } else { openWalletDialog(); } }} > {pubKey ? t('Create a referral code') : t('Connect wallet')}
setDialogOpen(false)} size="small" >
); }; const CreateCodeDialog = ({ setDialogOpen, }: { setDialogOpen: (open: boolean) => void; }) => { const t = useT(); const createLink = useLinks(DApp.Governance); const { isReadOnly, pubKey, sendTx } = useVegaWallet(); const { refetch } = useReferral({ pubKey, role: 'referrer' }); const [err, setErr] = useState(null); const [code, setCode] = useState(null); const [status, setStatus] = useState< 'idle' | 'loading' | 'success' | 'error' >('idle'); const { stakeAvailable: currentStakeAvailable, requiredStake } = useStakeAvailable(); const { details: programDetails } = useReferralProgram(); const onSubmit = () => { if (isReadOnly || !pubKey) { setErr('Not connected'); } else { setErr(null); setStatus('loading'); setCode(null); sendTx(pubKey, { createReferralSet: { isTeam: false, }, }) .then((res) => { if (!res) { setErr(`Invalid response: ${JSON.stringify(res)}`); return; } const code = determineId(res.signature); setCode(code); setStatus('success'); }) .catch((err) => { if (err.message.includes('user rejected')) { setStatus('idle'); return; } setStatus('error'); setErr(err.message); }); } }; const getButtonProps = () => { if (status === 'idle' || status === 'error') { return { children: t('Generate code'), onClick: () => onSubmit(), }; } if (status === 'loading') { return { children: t('Confirm in wallet...'), disabled: true, }; } if (status === 'success') { return { children: t('Close'), intent: Intent.Success, onClick: () => { refetch(); setDialogOpen(false); }, }; } }; if (!pubKey || currentStakeAvailable == null || requiredStake == null) { return (

{t('You must be connected to the Vega wallet.')}

setDialogOpen(false)} > {t('Close')}
); } if (currentStakeAvailable < requiredStake) { return (

{t( 'You need at least {{requiredStake}} VEGA staked to generate a referral code and participate in the referral program.', { requiredStake: addDecimalsFormatNumber( requiredStake.toString(), 18 ), } )}

{t('Stake some $VEGA now')}
); } if (!programDetails) { return (
{(status === 'idle' || status === 'loading' || status === 'error') && ( <> {

{t( 'There is currently no referral program active, are you sure you want to create a code?' )}

} )} {status === 'success' && code && (

{code}

} > {t('Copy')}
)} onSubmit()} {...getButtonProps()} > {t('Yes')} {status === 'idle' && ( { refetch(); setDialogOpen(false); }} > {t('No')} )} {err && {err}}
{t('About the referral program')}
); } return (
{(status === 'idle' || status === 'loading' || status === 'error') && (

{t( 'Generate a referral code to share with your friends and access the commission benefits of the current program.' )}

)} {status === 'success' && code && (

{code}

} > {t('Copy')}
)} {err && {err}}
{t('About the referral program')}
); };