Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@impact-market/utils",
"version": "3.4.0-alpha.6",
"version": "3.4.0",
"description": "impactMarket utils",
"repository": {
"type": "git",
Expand Down
34 changes: 33 additions & 1 deletion src/useAirdropRecurring.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { internalUseTransaction } from './internalUseTransaction';
import { toNumber } from './toNumber';
import { updatePACTBalance } from './usePACTBalance';
import AirdropRecurringABI from './abi/AirdropRecurringABI.json';
import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import type { BigNumber } from 'bignumber.js';

/**
Expand All @@ -31,6 +31,35 @@ export const useAirdropRecurring = (airdropSmartContractAddress: string) => {
const [isReady, setIsReady] = useState(false);
const airdropper = new Contract(airdropSmartContractAddress, AirdropRecurringABI, provider) as AirdropRecurring;
const executeTransaction = internalUseTransaction();
const updateIntervalRef = useRef<NodeJS.Timer>();
const clockInterval = 120000;

// update claim data at the end of the cooldown
const _startUpdateInterval = (userAddress: string, lastClaimTime: number, cooldown: number) => {
const now = new Date().getTime() / 1000;
const end = lastClaimTime + cooldown;

// Cancel any existing interval
if (updateIntervalRef.current) {
clearInterval(updateIntervalRef.current);
}

// Start a new interval
if (now + clockInterval < end) {
updateIntervalRef.current = setInterval(() => {
if (now + clockInterval >= end) {
clearInterval(updateIntervalRef.current);
updateIntervalRef.current = setTimeout(() => {
_reloadingClaimStatus(userAddress);
}, end - now + 1000);
}
}, clockInterval);
} else {
updateIntervalRef.current = setTimeout(() => {
_reloadingClaimStatus(userAddress);
}, end - now + 1000);
}
};

/**
* Claims airgrab rewards.
Expand Down Expand Up @@ -68,6 +97,9 @@ export const useAirdropRecurring = (airdropSmartContractAddress: string) => {
setAmountClaimed(toNumber(claimedAmount));
if (lastClaimTime.toNumber() !== 0) {
setNextClaim(new Date((lastClaimTime.toNumber() + cooldown.toNumber()) * 1000));
_startUpdateInterval(address, lastClaimTime.toNumber(), cooldown.toNumber());
} else {
setNextClaim(new Date(0));
}
};

Expand Down