-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(permit): load pre generated permit info (#3316)
* chore: ignore nx cache folder * feat: add usePreGeneratedPermitInfo * feat: add usePreGeneratedPermitInfoForToken * feat: add usePermitCompatibleTokens * feat: check first whether token is permittable before checking locally * feat: wire token compatibility onto token widget * refactor: create a type for PermitCompatibleTokens * chore: fix cosmos * refactor: move SWR common settings into a const * chore: remove stale TODO * feat: use final url for PermitInfo on token-lists repo * refactor: simplify conditional
- Loading branch information
1 parent
e8c9ba9
commit 46943ad
Showing
13 changed files
with
153 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,7 @@ yalc.lock | |
# vercel | ||
.vercel | ||
|
||
analyse.html | ||
analyse.html | ||
|
||
# NX | ||
.nx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
apps/cowswap-frontend/src/modules/permit/hooks/usePermitCompatibleTokens.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useAtomValue } from 'jotai' | ||
import { useMemo, useRef } from 'react' | ||
|
||
import { useWalletInfo } from '@cowprotocol/wallet' | ||
|
||
import { usePreGeneratedPermitInfo } from './usePreGeneratedPermitInfo' | ||
|
||
import { permittableTokensAtom } from '../state/permittableTokensAtom' | ||
import { PermitCompatibleTokens } from '../types' | ||
|
||
export function usePermitCompatibleTokens(): PermitCompatibleTokens { | ||
const { chainId } = useWalletInfo() | ||
const localPermitInfo = useAtomValue(permittableTokensAtom)[chainId] | ||
const { allPermitInfo } = usePreGeneratedPermitInfo() | ||
|
||
const stableRef = JSON.stringify(Object.keys(localPermitInfo).concat(Object.keys(allPermitInfo))) | ||
|
||
const localPermitInfoRef = useRef(localPermitInfo) | ||
localPermitInfoRef.current = localPermitInfo | ||
const preGeneratedPermitInfoRef = useRef(allPermitInfo) | ||
preGeneratedPermitInfoRef.current = allPermitInfo | ||
|
||
return useMemo(() => { | ||
const permitCompatibleTokens: PermitCompatibleTokens = {} | ||
|
||
for (const address of Object.keys(preGeneratedPermitInfoRef.current)) { | ||
permitCompatibleTokens[address.toLowerCase()] = !!preGeneratedPermitInfoRef.current[address] | ||
} | ||
|
||
for (const address of Object.keys(localPermitInfoRef.current)) { | ||
permitCompatibleTokens[address.toLowerCase()] = !!localPermitInfoRef.current[address] | ||
} | ||
|
||
return permitCompatibleTokens | ||
// Reducing unnecessary re-renders | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [stableRef]) | ||
} |
29 changes: 29 additions & 0 deletions
29
apps/cowswap-frontend/src/modules/permit/hooks/usePreGeneratedPermitInfo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { SWR_NO_REFRESH_OPTIONS } from '@cowprotocol/common-const' | ||
import { PermitInfo } from '@cowprotocol/permit-utils' | ||
import { useWalletInfo } from '@cowprotocol/wallet' | ||
|
||
import useSWR from 'swr' | ||
|
||
import { PRE_GENERATED_PERMIT_URL } from '../const' | ||
|
||
/** | ||
* Fetch pre-generated permit info (stored in token-lists repo) for all tokens | ||
* | ||
* Caches result with SWR until a page refresh | ||
*/ | ||
export function usePreGeneratedPermitInfo(): { | ||
allPermitInfo: Record<string, PermitInfo> | ||
isLoading: boolean | ||
} { | ||
const { chainId } = useWalletInfo() | ||
|
||
const url = `${PRE_GENERATED_PERMIT_URL}.${chainId}.json` | ||
|
||
const { data, isLoading } = useSWR( | ||
url, | ||
(url: string): Promise<Record<string, PermitInfo>> => fetch(url).then((r) => r.json()), | ||
{ ...SWR_NO_REFRESH_OPTIONS, fallbackData: {} } | ||
) | ||
|
||
return { allPermitInfo: data, isLoading } | ||
} |
26 changes: 26 additions & 0 deletions
26
apps/cowswap-frontend/src/modules/permit/hooks/usePreGeneratedPermitInfoForToken.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Currency } from '@uniswap/sdk-core' | ||
|
||
import { Nullish } from 'types' | ||
|
||
import { usePreGeneratedPermitInfo } from './usePreGeneratedPermitInfo' | ||
|
||
import { IsTokenPermittableResult } from '../types' | ||
|
||
/** | ||
* Fetch pre-generated permit info for a single token | ||
*/ | ||
export function usePreGeneratedPermitInfoForToken(token: Nullish<Currency>): { | ||
permitInfo: IsTokenPermittableResult | ||
isLoading: boolean | ||
} { | ||
const { allPermitInfo, isLoading } = usePreGeneratedPermitInfo() | ||
|
||
const address = token && 'address' in token && token.address.toLowerCase() | ||
|
||
const permitInfo = address ? allPermitInfo[address] : undefined | ||
|
||
return { | ||
permitInfo, | ||
isLoading, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters