Skip to content

Commit

Permalink
Merge pull request #119 from subquery/fix/current-era-rank
Browse files Browse the repository at this point in the history
fix: type
  • Loading branch information
HuberTRoy authored Jul 8, 2024
2 parents 523d7df + 38988be commit 0809e19
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
41 changes: 26 additions & 15 deletions src/components/DelegationCampaign/DelegationCampaign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const useAccount = () => {
};

const minDelegationAmount = 3000;
const rankPointRule = (rank: number, delegation: string) => {
const rankPointRule = (rank: number, delegation: string, isCurEra: boolean) => {
if (isCurEra) return 0;
if (BigNumber(delegation).lt(minDelegationAmount)) return 0;
if (rank === 1) return 15_000;
if (rank === 2) return 10_000;
Expand All @@ -53,11 +54,11 @@ const rankPointRule = (rank: number, delegation: string) => {

const delegationAmountPoint = (amount: string) => {
if (BigNumber(amount).lt(minDelegationAmount)) return '0';
return BigNumber(amount).decimalPlaces(0).div(10).toFixed(0);
return Math.ceil(Math.floor(+amount / 10)).toFixed(0);
};
const rewardsAmountPoint = (amount: string, delegation: string) => {
if (BigNumber(delegation).lt(minDelegationAmount)) return '0';
return BigNumber(amount).decimalPlaces(0).multipliedBy(70).toFixed(0);
return Math.ceil(70 * Math.floor(+amount)).toFixed(0);
};

const LootboxItem = (props: { item: IMyLootboxItem; refresh: () => void }) => {
Expand Down Expand Up @@ -176,6 +177,8 @@ const SecondStep = (props: { userInfo?: IDelegationUserInfo['data'] }) => {
// one scroll chunk is 337px 321 width + 16 gap
const [scrollChunk] = useState(337 * 2);

const curEra = useMemo(() => userInfo?.era_config.find((i) => i.key === 'current_era'), [userInfo]);

const fetchLootboxes = async (era: number | string, shouldLoading = true) => {
try {
if (shouldLoading) {
Expand All @@ -201,26 +204,26 @@ const SecondStep = (props: { userInfo?: IDelegationUserInfo['data'] }) => {
wallet: account || ''
});

const startEra = userInfo?.era_config.find((i) => i.key === 'start_era');
const curEra = userInfo?.era_config.find((i) => i.key === 'current_era');

const eraInfos = new Array(+(curEra?.value || 0) - +(startEra?.value || 0) + 1)
const startEraTemp = userInfo?.era_config.find((i) => i.key === 'start_era');
const curEraTemp = userInfo?.era_config.find((i) => i.key === 'current_era');
const counts = +(curEraTemp?.value || 0) - +(startEraTemp?.value || 0) + 1;
const eraInfos = new Array(counts < 0 ? 0 : counts)
.fill(0)
.map((_, index) => {
const era = index + +(startEra?.value || 0);
const era = index + +(startEraTemp?.value || 0);
const eraInfo = res.data.data?.find((i) => +i.era === era);

return (
eraInfo || {
id: +new Date() + Math.random() + index,
user_id: account || '',
era: era.toString(),
point: '0',
user_id: account || 0,
era,
point: 0,
reward: '0',
delegation: '0',
apy: '0',
rank: '0',
lootbox: '0'
rank: 0,
lootbox: 0
}
);
})
Expand Down Expand Up @@ -631,15 +634,23 @@ const SecondStep = (props: { userInfo?: IDelegationUserInfo['data'] }) => {
<div
className={clsx(
styles.colorfulButtonBorder,
rankPointRule(+(currentSelectEra?.rank || 99999999), currentSelectEra?.delegation || '0') === 0
rankPointRule(
+(currentSelectEra?.rank || 99999999),
currentSelectEra?.delegation || '0',
currentSelectEra?.era === +(curEra?.value || currentSelectEra?.era || 0)
) === 0
? styles.disableColorful
: ''
)}
>
<Button type="primary" shape="round" size="small">
+
{formatNumberWithLocale(
rankPointRule(+(currentSelectEra?.rank || 99999999), currentSelectEra?.delegation || '0'),
rankPointRule(
+(currentSelectEra?.rank || 99999999),
currentSelectEra?.delegation || '0',
currentSelectEra?.era === +(curEra?.value || currentSelectEra?.era || 0)
),
0
)}{' '}
points
Expand Down
28 changes: 14 additions & 14 deletions src/hooks/useDelegationCampaignApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface IOneoffChallenge {
description: string;
id: number;
title: string;
point: string;
point: number;
};
userChallenge?: {
completed: boolean;
Expand All @@ -61,7 +61,7 @@ interface IRankInfo {
id: number;
wallet: string;
apy: string;
total_score: string;
total_score: number;
total_reward: string;
rank: string;
}
Expand All @@ -77,14 +77,14 @@ export interface ILeaderboardRecord {

export interface IMyEraInfoItem {
id: number;
user_id: string;
user_id: number | string;
delegation: string;
reward: string;
era: string;
point: string;
era: number;
point: number;
apy: string;
rank: string;
lootbox: string;
rank: number;
lootbox: number;
}

export interface IMyEraInfo {
Expand All @@ -95,12 +95,12 @@ export interface IMyLootboxItem {
created_at: string;
updated_at: string;
id: number;
user_id: string;
challenge_id: string;
type: string;
era: string;
num: string;
point: string;
user_id: number;
challenge_id: number;
type: number;
era: number;
num: number;
point: number;
completed: boolean;
completed_at: string;
}
Expand Down Expand Up @@ -155,7 +155,7 @@ export const useDelegationCampaignApi = (props: { alert?: boolean }) => {
return res;
}, []);

const openLootbox = useCallback(async (params: { wallet: string; era: number | string; num: string }) => {
const openLootbox = useCallback(async (params: { wallet: string; era: number | string; num: number }) => {
const res = await instance.post<IBaseResponse>(`/challenge/mark-lootbox`, params);

return res;
Expand Down

0 comments on commit 0809e19

Please sign in to comment.