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

Commit

Permalink
update API and hook
Browse files Browse the repository at this point in the history
  • Loading branch information
W3stside committed Dec 20, 2021
1 parent 56cfc95 commit 3605617
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/custom/api/gnosisProtocol/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,12 @@ export async function getProfileData(chainId: ChainId, address: string): Promise
}
}

export async function getPriceStrategy(chainId: ChainId): Promise<GpPriceStrategy> {
export type PriceStrategy = {
primary: GpPriceStrategy
secondary: GpPriceStrategy
}

export async function getPriceStrategy(chainId: ChainId): Promise<PriceStrategy> {
console.log(`[api:${API_NAME}] Get GP price strategy for`, chainId)

const response = await _fetchPriceStrategy(chainId)
Expand Down
2 changes: 1 addition & 1 deletion src/custom/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ export const STORAGE_KEY_LAST_PROVIDER = 'lastProvider'
// Default price strategy to use for getting app prices
// COWSWAP = new quote endpoint
// LEGACY = price racing logic (checking 0x, gp, paraswap, etc)
export const DEFAULT_GP_PRICE_STRATEGY = 'LEGACY'
export const DEFAULT_GP_PRICE_STRATEGY = 'COWSWAP'
10 changes: 6 additions & 4 deletions src/custom/hooks/useGetGpPriceStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ms from 'ms.macro'
import { useState, useEffect } from 'react'
import { useState, useEffect, useCallback } from 'react'
import { DEFAULT_GP_PRICE_STRATEGY } from 'constants/index'
import { registerOnWindow } from 'utils/misc'
import { getPriceStrategy } from 'api/gnosisProtocol/api'
import { getPriceStrategy, PriceStrategy } from 'api/gnosisProtocol/api'
import { useActiveWeb3React } from 'hooks'
import { supportedChainId } from 'utils/supportedChainId'
import { SupportedChainId } from 'constants/chains'
Expand All @@ -16,14 +16,16 @@ export default function useGetGpPriceStrategy(): GpPriceStrategy {
const [gpPriceStrategy, setGpPriceStrategy] = useState<GpPriceStrategy>(DEFAULT_GP_PRICE_STRATEGY)
const { chainId: preChainId } = useActiveWeb3React()

const _handleSetStrategy = useCallback((response: PriceStrategy) => setGpPriceStrategy(response.primary), [])

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

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

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

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

0 comments on commit 3605617

Please sign in to comment.