import { useEffect, useState, useCallback } from 'react' import PageBodyContainer from '../components/PageBodyContainer' import TopBar from '../components/TopBar' import { serverSideTranslations } from 'next-i18next/serverSideTranslations' import useMangoStore from '../stores/useMangoStore' import { mangoAccountSelector, mangoClientSelector, mangoGroupConfigSelector, mangoGroupSelector, walletSelector, } from '../stores/selectors' import { IconButton, LinkButton } from '../components/Button' import { abbreviateAddress, copyToClipboard } from '../utils' import { notify } from '../utils/notifications' import { getMarketIndexBySymbol, ReferrerIdRecord, } from '@blockworks-foundation/mango-client' import { useTranslation } from 'next-i18next' import EmptyState from '../components/EmptyState' import { CheckIcon, CurrencyDollarIcon, DuplicateIcon, LinkIcon, } from '@heroicons/react/outline' import { MngoMonoIcon } from '../components/icons' import Link from 'next/link' import Modal from '../components/Modal' import { Table, Td, Th, TrBody, TrHead } from '../components/TableElements' import dayjs from 'dayjs' import AccountsModal from '../components/AccountsModal' import { useViewport } from '../hooks/useViewport' import { breakpoints } from '../components/TradePageGrid' import { ExpandableRow } from '../components/TableElements' import MobileTableHeader from '../components/mobile/MobileTableHeader' import Input from '../components/Input' import InlineNotification from '../components/InlineNotification' export async function getStaticProps({ locale }) { return { props: { ...(await serverSideTranslations(locale, ['common'])), // Will be passed to the page component as props }, } } const referralHistory = [ { time: '2022-02-09T19:28:59Z', referralLink: 'test2', referee: '22JS1jkvkLcdxhHo1LpWXUh6sTErkt54j1YaszYWZoCi', fee: 0.22, }, { time: '2022-02-08T19:28:59Z', referralLink: 'test2', referee: '22JS1jkvkLcdxhHo1LpWXUh6sTErkt54j1YaszYWZoCi', fee: 0.21, }, { time: '2022-02-07T19:28:59Z', referralLink: 'test2', referee: '22JS1jkvkLcdxhHo1LpWXUh6sTErkt54j1YaszYWZoCi', fee: 0.15, }, ] export default function Referral() { const { t } = useTranslation('common') const mangoGroup = useMangoStore(mangoGroupSelector) const mangoAccount = useMangoStore(mangoAccountSelector) const groupConfig = useMangoStore(mangoGroupConfigSelector) const client = useMangoStore(mangoClientSelector) const wallet = useMangoStore(walletSelector) const connected = useMangoStore((s) => s.wallet.connected) const [customRefLinkInput, setCustomRefLinkInput] = useState('') const [existingCustomRefLinks, setexistingCustomRefLinks] = useState() const [hasCopied, setHasCopied] = useState(null) const [showMoreInfoModal, setShowMoreInfoModal] = useState(false) const [showAccountsModal, setShowAccountsModal] = useState(false) const [hasReferrals] = useState(true) // Placeholder to show/hide users referral stats const [loading, setLoading] = useState(false) const [inputError, setInputError] = useState('') const { width } = useViewport() const isMobile = width ? width < breakpoints.sm : false const fetchCustomReferralLinks = useCallback(async () => { setLoading(true) const referrerIds = await client.getReferrerIdsForMangoAccount(mangoAccount) setexistingCustomRefLinks(referrerIds) setLoading(false) }, [mangoAccount]) useEffect(() => { if (mangoAccount) { fetchCustomReferralLinks() } }, [mangoAccount]) useEffect(() => { let timer if (hasCopied) { timer = setTimeout(() => setHasCopied(null), 1000) } return () => { clearTimeout(timer) } }, [hasCopied]) const onChangeRefIdInput = (value) => { const id = value.replace(/ /g, '') setCustomRefLinkInput(id) if (id.length > 32) { setInputError('Referral IDs must be less then 33 characters') } else { setInputError('') } } const validateRefIdInput = () => { if (customRefLinkInput.length >= 33) { setInputError('Referral IDs must be less then 33 characters') } if (customRefLinkInput.length === 0) { setInputError('Enter a referral ID') } } const submitRefLink = async () => { if (!inputError) { try { const txid = await client.registerReferrerId( mangoGroup, mangoAccount, wallet, customRefLinkInput ) notify({ txid, title: 'Custom referal link created', }) } catch (e) { notify({ type: 'error', title: 'Unable to create referral link', description: e.message, txid: e.txid, }) } } else return } const handleCopyLink = (link, index) => { copyToClipboard(link) setHasCopied(index) } const mngoIndex = getMarketIndexBySymbol(groupConfig, 'MNGO') const hasRequiredMngo = mangoGroup && mangoAccount ? mangoAccount.deposits[mngoIndex].toNumber() > 10000 : false const hasCustomRefLinks = existingCustomRefLinks && existingCustomRefLinks.length > 0 return (

Sow the Mango Seed

Earn 20% of the perp fees paid by anyone you refer. Plus, they get a 5% perp fee discount.

setShowMoreInfoModal(true)} > More Info
{connected ? ( mangoAccount ? ( <> {hasReferrals ? (

Your Referrals

Total Earnings
$150.50
Total referrals
15
) : null}
{hasRequiredMngo ? (

Your Links

{!loading ? ( !hasCustomRefLinks ? ( {/* */}
Link Copy Link
{!isMobile ? ( ) : null}

{isMobile ? abbreviateAddress( mangoAccount.publicKey ) : `https://trade.mango.markets?ref= ${mangoAccount.publicKey.toString()}`}

5 handleCopyLink( `https://trade.mango.markets?ref=${mangoAccount.publicKey.toString()}`, 1 ) } > {hasCopied === 1 ? ( ) : ( )}
) : ( {existingCustomRefLinks.map( (customRefs, index) => ( {/* */} ) )}
Link
Copy Link
{!isMobile ? ( ) : null}

{isMobile ? customRefs.referrerId : `https://trade.mango.markets?ref=${customRefs.referrerId}`}

5 handleCopyLink( `https://trade.mango.markets?ref=${customRefs.referrerId}`, index + 1 ) } > {hasCopied === index + 1 ? ( ) : ( )}
) ) : (
)}
) : (

You need 10,000 MNGO in your Mango Account

Buy MNGO
)}
{hasCustomRefLinks && existingCustomRefLinks.length < 5 ? (

Custom Referral ID

You can generate up to 5 custom IDs

onChangeRefIdInput(e.target.value)} value={customRefLinkInput} /> {inputError ? (
) : null}
) : null}
{referralHistory.length > 0 ? (

Earnings History

{!isMobile ? ( {referralHistory.map((ref, index) => ( ))}
{t('date')} Referral ID Referee
Fee Earned
{dayjs(ref.time).format('DD MMM YYYY h:mma')} {ref.referralLink} {abbreviateAddress(mangoAccount.publicKey)} ${ref.fee}
) : ( <> {referralHistory.map((ref, index) => (
{dayjs(ref.time).format('DD MMM YYYY h:mma')}
${ref.fee}
} key={`${ref.fee + index}`} index={index} panelTemplate={ <>
Referral ID
{ref.referralLink}
} /> ))} )}
) : null} ) : (
} onClickButton={() => setShowAccountsModal(true)} title={t('no-account-found')} />
) ) : (
} onClickButton={() => wallet.connect()} title={t('connect-wallet')} />
)}
{showMoreInfoModal ? ( setShowMoreInfoModal(false)} >

Referral Program Details

  • Your referral code is automatically applied whenever your link is used to create an account.
  • When any of your referrals trade Mango Perps, you earn 20% of their trade fees.
  • Plus, for using your link they get a 5% discount off their Mango Perp fees.
  • You must have at least 10,000 MNGO in your Mango Account to qualify for generating referrals and earning referral rewards.
) : null} {showAccountsModal ? ( setShowAccountsModal(false)} isOpen={showAccountsModal} /> ) : null}
) }