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

Get deployment time per network #2407

Merged
merged 2 commits into from
Feb 8, 2022
Merged
Changes from 1 commit
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
25 changes: 18 additions & 7 deletions src/custom/state/claim/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import JSBI from 'jsbi'
import ms from 'ms.macro'
import { CurrencyAmount, Price, Token } from '@uniswap/sdk-core'
Expand Down Expand Up @@ -59,6 +59,7 @@ import { EnhancedUserClaimData } from 'pages/Claim/types'
import { supportedChainId } from 'utils/supportedChainId'
import { AMOUNT_PRECISION } from 'constants/index'
import useIsMounted from 'hooks/useIsMounted'
import { ChainId } from '@uniswap/sdk'

const CLAIMS_REPO_BRANCH = '2022-01-22-test-deployment-all-networks'
export const CLAIMS_REPO = `https://github.com/gnosis/cow-merkle-drop/${CLAIMS_REPO_BRANCH}/`
Expand Down Expand Up @@ -275,16 +276,18 @@ export function useUserClaims(account: Account, optionalChainId?: SupportedChain
return { claims: claimKey ? claimInfo[claimKey] : null, isLoading }
}

let fetch_deployment_timestamp_promise: Promise<number> | null = null
function fetchDeploymentTimestamp(vCowContract: VCowType) {
if (!fetch_deployment_timestamp_promise) {
fetch_deployment_timestamp_promise = vCowContract.deploymentTimestamp().then((ts) => {
const FETCH_DEPLOYMENT_TIME_PROMISES: Map<ChainId, Promise<number>> = new Map()
function fetchDeploymentTimestamp(vCowContract: VCowType, chainId: ChainId): Promise<number> {
let deploymentTimePromise = FETCH_DEPLOYMENT_TIME_PROMISES.get(chainId)

if (!deploymentTimePromise) {
deploymentTimePromise = vCowContract.deploymentTimestamp().then((ts) => {
console.log(`Deployment timestamp in seconds: ${ts.toString()}`)
anxolin marked this conversation as resolved.
Show resolved Hide resolved
return ts.mul('1000').toNumber()
})
anxolin marked this conversation as resolved.
Show resolved Hide resolved
}

return fetch_deployment_timestamp_promise
return deploymentTimePromise
}

/**
Expand All @@ -298,13 +301,21 @@ function useDeploymentTimestamp(): number | null {
const isMounted = useIsMounted()

const [timestamp, setTimestamp] = useState<number | null>(null)
const oldChainId = useRef(chainId)

useEffect(() => {
if (!chainId || !vCowContract) {
return
}

fetchDeploymentTimestamp(vCowContract)
// Invalidate timestamp
if (chainId != oldChainId.current) {
setTimestamp(null)
oldChainId.current = chainId
}

// Fetch timestamp
fetchDeploymentTimestamp(vCowContract, chainId)
.then((timestamp) => {
if (isMounted.current) {
setTimestamp(timestamp)
Expand Down