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

Commit

Permalink
use actual request
Browse files Browse the repository at this point in the history
  • Loading branch information
W3stside committed Dec 20, 2021
1 parent 8e2035e commit 56cfc95
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/custom/hooks/useGetGpPriceStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import ms from 'ms.macro'
import { useState, useEffect } from 'react'
import { DEFAULT_GP_PRICE_STRATEGY } from 'constants/index'
import { registerOnWindow } from '../utils/misc'
import { registerOnWindow } from 'utils/misc'
import { getPriceStrategy } from 'api/gnosisProtocol/api'
import { useActiveWeb3React } from 'hooks'
import { supportedChainId } from 'utils/supportedChainId'
import { SupportedChainId } from 'constants/chains'

export type GpPriceStrategy = 'COWSWAP' | 'LEGACY'
// TODO: use actual API call
// https://github.com/gnosis/gp-v2-contracts/issues/904
export async function checkGpPriceStrategy(): Promise<GpPriceStrategy> {
return new Promise((accept) => setTimeout(() => accept(DEFAULT_GP_PRICE_STRATEGY), 500))
}

// arbitrary, could be more/less
const GP_PRICE_STRATEGY_INTERVAL_TIME = ms`30 minutes`

export default function useGetGpPriceStrategy(): GpPriceStrategy {
const [gpPriceStrategy, setGpPriceStrategy] = useState<GpPriceStrategy>(DEFAULT_GP_PRICE_STRATEGY)
const { chainId: preChainId } = useActiveWeb3React()

useEffect(() => {
const chainId = supportedChainId(preChainId)
console.debug('[useGetGpPriceStrategy::GP Price Strategy]::', gpPriceStrategy)

const getStrategy = () => {
checkGpPriceStrategy()
// default to MAINNET if not connected, or incorrect network
getPriceStrategy(chainId || SupportedChainId.MAINNET)
.then(setGpPriceStrategy)
.catch((err: Error) => {
console.error('[useGetGpPriceStrategy::useEffect] Error getting GP price strategy::', err)
Expand All @@ -36,7 +39,7 @@ export default function useGetGpPriceStrategy(): GpPriceStrategy {
}, GP_PRICE_STRATEGY_INTERVAL_TIME)

return () => clearInterval(intervalId)
}, [gpPriceStrategy])
}, [gpPriceStrategy, preChainId])

// TODO: REMOVE
return process.env.NODE_ENV !== 'production' ? (window as any).GP_STRATEGY : gpPriceStrategy
Expand Down

0 comments on commit 56cfc95

Please sign in to comment.