import { CodeTile, StatTile } from './tile'; import { VegaIcon, VegaIconNames, truncateMiddle, } from '@vegaprotocol/ui-toolkit'; import { useVegaWallet } from '@vegaprotocol/wallet'; import { useReferral } from './hooks/use-referral'; import { CreateCodeContainer } from './create-code-form'; import classNames from 'classnames'; import { Table } from './table'; import { addDecimalsFormatNumber, getDateFormat, getDateTimeFormat, getNumberFormat, getUserLocale, removePaginationWrapper, } from '@vegaprotocol/utils'; import { useReferralSetStatsQuery } from './hooks/__generated__/ReferralSetStats'; import compact from 'lodash/compact'; import { useReferralProgram } from './hooks/use-referral-program'; import { useStakeAvailable } from './hooks/use-stake-available'; import sortBy from 'lodash/sortBy'; import { useLayoutEffect, useRef, useState } from 'react'; import { useCurrentEpochInfoQuery } from './hooks/__generated__/Epoch'; import BigNumber from 'bignumber.js'; export const ReferralStatistics = () => { const { pubKey } = useVegaWallet(); const { data: referee } = useReferral({ pubKey, role: 'referee', }); const { data: referrer } = useReferral({ pubKey, role: 'referrer', }); if (referee?.code) { return ; } if (referrer?.code) { return ; } return ; }; export const Statistics = ({ data, as, }: { data: NonNullable['data']>; as: 'referrer' | 'referee'; }) => { const { data: epochData } = useCurrentEpochInfoQuery(); const { stakeAvailable } = useStakeAvailable(); const { benefitTiers } = useReferralProgram(); const { data: statsData } = useReferralSetStatsQuery({ variables: { code: data.code, }, skip: !data?.code, fetchPolicy: 'cache-and-network', }); const currentEpoch = Number(epochData?.epoch.id); const stats = statsData?.referralSetStats.edges && compact(removePaginationWrapper(statsData.referralSetStats.edges)); const refereeInfo = data.referee; const refereeStats = stats?.find( (r) => r.partyId === data.referee?.refereeId ); const statsAvailable = stats && stats.length > 0 && stats[0]; const baseCommissionValue = statsAvailable ? Number(statsAvailable.rewardFactor) : 0; const runningVolumeValue = statsAvailable ? Number(statsAvailable.referralSetRunningNotionalTakerVolume) : 0; const multiplier = statsAvailable ? Number(statsAvailable.rewardsMultiplier) : 1; const finalCommissionValue = !isNaN(multiplier) ? baseCommissionValue : multiplier * baseCommissionValue; const discountFactorValue = refereeStats?.discountFactor ? Number(refereeStats.discountFactor) : 0; const currentBenefitTierValue = benefitTiers.find( (t) => !isNaN(discountFactorValue) && !isNaN(t.discountFactor) && t.discountFactor === discountFactorValue ); const nextBenefitTierValue = currentBenefitTierValue && benefitTiers.find((t) => t.tier === currentBenefitTierValue.tier - 1); const epochsValue = !isNaN(currentEpoch) && refereeInfo?.atEpoch ? currentEpoch - refereeInfo?.atEpoch : 0; const nextBenefitTierVolumeValue = nextBenefitTierValue ? nextBenefitTierValue.minimumVolume - runningVolumeValue : 0; const nextBenefitTierEpochsValue = nextBenefitTierValue ? nextBenefitTierValue.epochs - epochsValue : 0; const baseCommissionTile = ( {baseCommissionValue * 100}% ); const stakingMultiplierTile = ( {multiplier || 'None'} ); const finalCommissionTile = ( {finalCommissionValue * 100}% ); const numberOfTradersValue = data.referees.length; const numberOfTradersTile = ( {numberOfTradersValue} ); const codeTile = ; const createdAtTile = ( {getDateFormat().format(new Date(data.createdAt))} ); const totalCommissionValue = data.referees .map((r) => new BigNumber(r.totalRefereeGeneratedRewards)) .reduce((all, r) => all.plus(r), new BigNumber(0)); const totalCommissionTile = ( {getNumberFormat(0).format(Number(totalCommissionValue))} ); const referrerTiles = ( <>
{baseCommissionTile} {stakingMultiplierTile} {finalCommissionTile}
{codeTile} {createdAtTile} {numberOfTradersTile} {totalCommissionTile}
); const compactNumFormat = new Intl.NumberFormat(getUserLocale(), { minimumFractionDigits: 0, maximumFractionDigits: 2, notation: 'compact', compactDisplay: 'short', }); const currentBenefitTierTile = ( {currentBenefitTierValue?.tier || '-'} ); const discountFactorTile = ( {discountFactorValue * 100}% ); const runningVolumeTile = ( {compactNumFormat.format(runningVolumeValue)} ); const epochsTile = {epochsValue}; const nextTierVolumeTile = ( {nextBenefitTierVolumeValue <= 0 ? '-' : compactNumFormat.format(nextBenefitTierVolumeValue)} ); const nextTierEpochsTile = ( {nextBenefitTierEpochsValue <= 0 ? '-' : nextBenefitTierEpochsValue} ); const refereeTiles = ( <>
{currentBenefitTierTile} {discountFactorTile} {codeTile}
{runningVolumeTile} {nextTierVolumeTile} {epochsTile} {nextTierEpochsTile}
); const [collapsed, setCollapsed] = useState(false); const tableRef = useRef(null); useLayoutEffect(() => { if ((tableRef.current?.getBoundingClientRect().height || 0) > 384) { setCollapsed(true); } }, []); return ( <> {/* Stats tiles */}
{as === 'referrer' && referrerTiles} {as === 'referee' && refereeTiles}
{/* Referees (only for referrer view) */} {as === 'referrer' && data.referees.length > 0 && (

Referees

({ party: ( {truncateMiddle(r.refereeId)} ), joined: getDateTimeFormat().format(new Date(r.joinedAt)), volume: Number(r.totalRefereeNotionalTakerVolume), commission: Number(r.totalRefereeGeneratedRewards), })), (r) => r.volume ) .map((r) => ({ ...r, volume: getNumberFormat(0).format(r.volume), commission: getNumberFormat(0).format(r.commission), })) .reverse()} /> )} ); };