2023-10-25 21:59:30 +00:00
|
|
|
import type { DiscountProgramsQuery, FeesQuery } from './__generated__/Fees';
|
2024-01-05 12:43:30 +00:00
|
|
|
|
2023-10-25 21:59:30 +00:00
|
|
|
export const useReferralStats = (
|
2023-12-01 17:03:41 +00:00
|
|
|
previousEpoch?: number,
|
|
|
|
referralStats?: NonNullable<
|
|
|
|
FeesQuery['referralSetStats']['edges']['0']
|
|
|
|
>['node'],
|
|
|
|
setReferees?: NonNullable<
|
|
|
|
FeesQuery['referralSetReferees']['edges']['0']
|
|
|
|
>['node'],
|
2023-10-25 21:59:30 +00:00
|
|
|
program?: DiscountProgramsQuery['currentReferralProgram'],
|
2023-12-01 17:03:41 +00:00
|
|
|
setIfReferrer?: NonNullable<FeesQuery['referrer']['edges']['0']>['node'],
|
|
|
|
setIfReferee?: NonNullable<FeesQuery['referee']['edges']['0']>['node']
|
2023-10-25 21:59:30 +00:00
|
|
|
) => {
|
|
|
|
const referralTiers = program?.benefitTiers || [];
|
|
|
|
|
2023-12-01 17:03:41 +00:00
|
|
|
if (
|
|
|
|
!previousEpoch ||
|
|
|
|
referralStats?.atEpoch !== previousEpoch ||
|
|
|
|
!program ||
|
|
|
|
!setReferees
|
|
|
|
) {
|
2023-10-25 21:59:30 +00:00
|
|
|
return {
|
|
|
|
referralDiscount: 0,
|
|
|
|
referralVolumeInWindow: 0,
|
|
|
|
referralTierIndex: -1,
|
|
|
|
referralTiers,
|
|
|
|
epochsInSet: 0,
|
2023-11-09 15:50:50 +00:00
|
|
|
code: undefined,
|
|
|
|
isReferrer: false,
|
2023-10-25 21:59:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const referralDiscount = Number(referralStats?.discountFactor || 0);
|
|
|
|
const referralVolumeInWindow = Number(
|
|
|
|
referralStats?.referralSetRunningNotionalTakerVolume || 0
|
|
|
|
);
|
|
|
|
|
2023-12-01 17:03:41 +00:00
|
|
|
const referralTierIndex = referralTiers.findIndex(
|
|
|
|
(tier) => tier.referralDiscountFactor === referralStats?.discountFactor
|
|
|
|
);
|
2023-10-25 21:59:30 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
referralDiscount,
|
|
|
|
referralVolumeInWindow,
|
|
|
|
referralTierIndex,
|
|
|
|
referralTiers,
|
2023-12-01 17:03:41 +00:00
|
|
|
epochsInSet: referralStats.atEpoch - setReferees.atEpoch,
|
|
|
|
code: (setIfReferrer || setIfReferee)?.id,
|
|
|
|
isReferrer: Boolean(setIfReferrer),
|
2023-10-25 21:59:30 +00:00
|
|
|
};
|
|
|
|
};
|