vega-frontend-monorepo/apps/trading/components/fees-container/use-referral-stats.ts

53 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-10-25 21:59:30 +00:00
import type { DiscountProgramsQuery, FeesQuery } from './__generated__/Fees';
2023-10-25 21:59:30 +00:00
export const useReferralStats = (
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'],
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 || [];
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
);
const referralTierIndex = referralTiers.findIndex(
(tier) => tier.referralDiscountFactor === referralStats?.discountFactor
);
2023-10-25 21:59:30 +00:00
return {
referralDiscount,
referralVolumeInWindow,
referralTierIndex,
referralTiers,
epochsInSet: referralStats.atEpoch - setReferees.atEpoch,
code: (setIfReferrer || setIfReferee)?.id,
isReferrer: Boolean(setIfReferrer),
2023-10-25 21:59:30 +00:00
};
};