Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
2120/refactor time hooks (#2179)
Browse files Browse the repository at this point in the history
* Refactored all time related hooks into a single one

* Updated time hooks usage with the new one

Co-authored-by: Leandro <[email protected]>
  • Loading branch information
alfetopito and Leandro authored Jan 18, 2022
1 parent b7f6eee commit 1599ccf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 65 deletions.
14 changes: 3 additions & 11 deletions src/custom/pages/Claim/ClaimsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ClaimType,
useAirdropDeadline,
useClaimState,
useDeploymentTimestamp,
useInvestmentDeadline,
} from 'state/claim/hooks'
import { ClaimType, useClaimState, useClaimTimeInfo } from 'state/claim/hooks'
import styled from 'styled-components/macro'
import { ClaimTable, ClaimBreakdown, TokenLogo } from 'pages/Claim/styled'
import CowProtocolLogo from 'components/CowProtocolLogo'
Expand Down Expand Up @@ -130,9 +124,7 @@ export default function ClaimsTable({
const hideTable =
isAirdropOnly || !hasClaims || !activeClaimAccount || claimStatus !== ClaimStatus.DEFAULT || isInvestFlowActive

const start = useDeploymentTimestamp()
const investmentEnd = useInvestmentDeadline()
const airdropEnd = useAirdropDeadline()
const { deployment: start, investmentDeadline, airdropDeadline } = useClaimTimeInfo()

if (hideTable) return null

Expand Down Expand Up @@ -162,7 +154,7 @@ export default function ClaimsTable({
selected={selected}
handleSelect={handleSelect}
start={start}
end={claim.isFree ? airdropEnd : investmentEnd}
end={claim.isFree ? airdropDeadline : investmentDeadline}
/>
))}
</tbody>
Expand Down
16 changes: 2 additions & 14 deletions src/custom/pages/Claim/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { useEffect, useMemo } from 'react'
import { Trans } from '@lingui/macro'
import { CurrencyAmount, MaxUint256 } from '@uniswap/sdk-core'
import { useActiveWeb3React } from 'hooks/web3'
import {
useUserEnhancedClaimData,
useUserUnclaimedAmount,
useClaimCallback,
useInvestmentStillAvailable,
useAirdropStillAvailable,
} from 'state/claim/hooks'
import { useUserEnhancedClaimData, useUserUnclaimedAmount, useClaimCallback } from 'state/claim/hooks'
import { ButtonPrimary, ButtonSecondary } from 'components/Button'
import { PageWrapper, FooterNavButtons } from 'pages/Claim/styled'
import EligibleBanner from './EligibleBanner'
Expand Down Expand Up @@ -91,10 +85,6 @@ export default function Claim() {
const hasClaims = useMemo(() => userClaimData.length > 0, [userClaimData])
const isAirdropOnly = useMemo(() => !hasPaidClaim(userClaimData), [userClaimData])

// checks regarding investment time window
const isInvestmentStillAvailable = useInvestmentStillAvailable()
const isAirdropStillAvailable = useAirdropStillAvailable()

// claim callback
const { claimCallback } = useClaimCallback(activeClaimAccount)

Expand Down Expand Up @@ -169,9 +159,7 @@ export default function Claim() {
`[unclaimedAmount ${unclaimedAmount?.toFixed(2)}]`,
`[hasClaims ${hasClaims}]`,
`[activeClaimAccount ${activeClaimAccount}]`,
`[isAirdropOnly ${isAirdropOnly}]`,
`[isInvestmentStillAvailable ${isInvestmentStillAvailable}]`,
`[isAirdropStillAvailable ${isAirdropStillAvailable}]`
`[isAirdropOnly ${isAirdropOnly}]`
)

// on account change
Expand Down
77 changes: 37 additions & 40 deletions src/custom/state/claim/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ export function useClassifiedUserClaims(account: Account): ClassifiedUserClaims
const userClaims = useUserClaims(account)
const contract = useVCowContract()

const isInvestmentStillAvailable = useInvestmentStillAvailable()
const isAirdropStillAvailable = useAirdropStillAvailable()
const { isInvestmentWindowOpen, isAirdropWindowOpen } = useClaimTimeInfo()

// build list of parameters, with the claim index
// we check for all claims because expired now might have been claimed before
Expand All @@ -173,15 +172,15 @@ export function useClassifiedUserClaims(account: Account): ClassifiedUserClaims
result.result?.[0] === true // result true means claimed
) {
claimed.push(claim)
} else if (!isAirdropStillAvailable || (!isInvestmentStillAvailable && PAID_CLAIM_TYPES.includes(claim.type))) {
} else if (!isAirdropWindowOpen || (!isInvestmentWindowOpen && PAID_CLAIM_TYPES.includes(claim.type))) {
expired.push(claim)
} else {
available.push(claim)
}
})

return { available, expired, claimed }
}, [isAirdropStillAvailable, isInvestmentStillAvailable, results, userClaims])
}, [isAirdropWindowOpen, isInvestmentWindowOpen, results, userClaims])
}

/**
Expand Down Expand Up @@ -282,7 +281,7 @@ const createMockTx = (data: number[]) => ({
*
* Returns null if in there's no network or vCowContract doesn't exist
*/
export function useDeploymentTimestamp(): number | null {
function useDeploymentTimestamp(): number | null {
const { chainId } = useActiveWeb3React()
const vCowContract = useVCowContract()
const [timestamp, setTimestamp] = useState<number | null>(null)
Expand All @@ -301,42 +300,41 @@ export function useDeploymentTimestamp(): number | null {
return timestamp
}

/**
* Returns the timestamp of when the investment window closes
*/
export function useInvestmentDeadline(): number | null {
const deploymentTimestamp = useDeploymentTimestamp()

return deploymentTimestamp && deploymentTimestamp + TWO_WEEKS
}

/**
* Returns whether vCOW contract is still open for investments
* That is, there has been less than 2 weeks since it was deployed
*/
export function useInvestmentStillAvailable(): boolean {
const investmentDeadline = useInvestmentDeadline()

return Boolean(investmentDeadline && investmentDeadline > Date.now())
type ClaimTimeInfo = {
/**
* Time when contract was deployed, fetched from chain
*/
deployment: number | null
/**
* Time when investment window will close (2 weeks after contract deployment)
*/
investmentDeadline: number | null
/**
* Time when airdrop window will close (6 weeks after contract deployment)
*/
airdropDeadline: number | null
/**
* Whether investment window is still open, based on local time
*/
isInvestmentWindowOpen: boolean
/**
* Whether airdrop window is still open, based on local time
*/
isAirdropWindowOpen: boolean
}

/**
* Returns the timestamp of when the airdrop window closes
* Overall Claim time related properties
*/
export function useAirdropDeadline(): number | null {
const deploymentTimestamp = useDeploymentTimestamp()

return deploymentTimestamp && deploymentTimestamp + SIX_WEEKS
}
export function useClaimTimeInfo(): ClaimTimeInfo {
const deployment = useDeploymentTimestamp()
const investmentDeadline = deployment && deployment + TWO_WEEKS
const airdropDeadline = deployment && deployment + SIX_WEEKS

/**
* Returns whether vCOW contract is still open for airdrops
* That is, there has been less than 6 weeks since it was deployed
*/
export function useAirdropStillAvailable(): boolean {
const airdropDeadline = useAirdropDeadline()
const isInvestmentWindowOpen = Boolean(investmentDeadline && investmentDeadline > Date.now())
const isAirdropWindowOpen = Boolean(airdropDeadline && airdropDeadline > Date.now())

return Boolean(airdropDeadline && airdropDeadline > Date.now())
return { deployment, investmentDeadline, airdropDeadline, isInvestmentWindowOpen, isAirdropWindowOpen }
}

/**
Expand Down Expand Up @@ -379,8 +377,7 @@ export function useClaimCallback(account: string | null | undefined): {
const claims = useUserAvailableClaims(account)
const vCowContract = useVCowContract()

const isInvestmentStillAvailable = useInvestmentStillAvailable()
const isAirdropStillAvailable = useAirdropStillAvailable()
const { isInvestmentWindowOpen, isAirdropWindowOpen } = useClaimTimeInfo()

// used for popup summary
const addTransaction = useTransactionAdder()
Expand Down Expand Up @@ -427,7 +424,7 @@ export function useClaimCallback(account: string | null | undefined): {
throw new Error("Not initialized, can't claim")
}

_validateClaimable(claims, claimInput, isInvestmentStillAvailable, isAirdropStillAvailable)
_validateClaimable(claims, claimInput, isInvestmentWindowOpen, isAirdropWindowOpen)

const { args, totalClaimedAmount } = _getClaimManyArgs({ claimInput, claims, account, connectedAccount, chainId })

Expand Down Expand Up @@ -460,8 +457,8 @@ export function useClaimCallback(account: string | null | undefined): {
chainId,
claims,
connectedAccount,
isAirdropStillAvailable,
isInvestmentStillAvailable,
isAirdropWindowOpen,
isInvestmentWindowOpen,
vCowContract,
vCowToken,
]
Expand Down

0 comments on commit 1599ccf

Please sign in to comment.