-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #999 from airswap/fix/metadata-cache
Removed metadata all tokens from cache
- Loading branch information
Showing
24 changed files
with
400 additions
and
422 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
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
83 changes: 26 additions & 57 deletions
83
src/components/@widgets/OrderDetailWidget/hooks/useTakerTokenInfo.tsx
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 |
---|---|---|
@@ -1,97 +1,66 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
import { TokenInfo } from "@airswap/utils"; | ||
import { Web3Provider } from "@ethersproject/providers"; | ||
import { useWeb3React } from "@web3-react/core"; | ||
|
||
import { useAppDispatch, useAppSelector } from "../../../../app/hooks"; | ||
import { getAllTokensFromLocalStorage } from "../../../../features/metadata/metadataApi"; | ||
import { | ||
addActiveToken, | ||
addTokenInfo, | ||
fetchUnkownTokens, | ||
} from "../../../../features/metadata/metadataActions"; | ||
import { | ||
selectActiveTokenAddresses, | ||
selectAllTokens, | ||
} from "../../../../features/metadata/metadataSlice"; | ||
import { selectTakeOtcReducer } from "../../../../features/takeOtc/takeOtcSlice"; | ||
import findEthOrTokenByAddress from "../../../../helpers/findEthOrTokenByAddress"; | ||
import scrapeToken from "../../../../helpers/scrapeToken"; | ||
import useDefaultLibrary from "../../../../hooks/useDefaultLibrary"; | ||
import useJsonRpcProvider from "../../../../hooks/useJsonRpcProvider"; | ||
|
||
// OTC Taker version of useTokenInfo. Look at chainId of the active FullOrderERC20 instead | ||
// of active wallet chainId. This way we don't need to connect a wallet to show order tokens. | ||
|
||
const useTakerTokenInfo = ( | ||
address: string | null | ||
address: string | null, | ||
chainId: number | ||
): [TokenInfo | null, boolean] => { | ||
const dispatch = useAppDispatch(); | ||
const { provider: library } = useWeb3React<Web3Provider>(); | ||
const { isActive } = useAppSelector((state) => state.web3); | ||
// Using JsonRpcProvider for unconnected wallets or for wallets connected to a different chain | ||
const library = useJsonRpcProvider(chainId); | ||
|
||
const allTokens = useAppSelector(selectAllTokens); | ||
const activeTokenAddresses = useAppSelector(selectActiveTokenAddresses); | ||
const { activeOrder } = useAppSelector(selectTakeOtcReducer); | ||
|
||
const [token, setToken] = useState<TokenInfo>(); | ||
const [scrapedToken, setScrapedToken] = useState<TokenInfo>(); | ||
const [isCallScrapeTokenLoading, setIsCallScrapeTokenLoading] = | ||
useState(false); | ||
const [isCallScrapeTokenSuccess, setIsCallScrapeTokenSuccess] = | ||
useState(false); | ||
|
||
useEffect(() => { | ||
if (scrapedToken) { | ||
dispatch(addTokenInfo(scrapedToken)); | ||
// Add active token so balance will be fetched | ||
dispatch(addActiveToken(scrapedToken.address)); | ||
if ( | ||
address && | ||
allTokens.find((token) => token.address === address) && | ||
!activeTokenAddresses.includes(address) | ||
) { | ||
// Add as active token so balance and token info will be fetched | ||
dispatch(addActiveToken(address)); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [scrapedToken]); | ||
}, [address, allTokens]); | ||
|
||
useEffect(() => { | ||
if (!activeOrder || !address || !allTokens.length) { | ||
if (!address || !allTokens.length || token || !library) { | ||
return; | ||
} | ||
|
||
const chainId = activeOrder.chainId; | ||
|
||
// If wallet is not connected the metadata tokens can't be filled yet because it gets chainId from | ||
// the wallet. But in this case we have the chainId from the order already. So we can get tokens from | ||
// localStorage directly so we don't have to wait for the wallet getting connected. | ||
|
||
const tokensObject = getAllTokensFromLocalStorage(chainId); | ||
|
||
const tokens = [ | ||
...allTokens, | ||
...(!isActive ? Object.values(tokensObject) : []), | ||
]; | ||
|
||
const callScrapeToken = async () => { | ||
setIsCallScrapeTokenLoading(true); | ||
|
||
if (library) { | ||
const result = await scrapeToken(address, library); | ||
if (result) { | ||
setScrapedToken(result); | ||
} | ||
setIsCallScrapeTokenSuccess(true); | ||
} else { | ||
setIsCallScrapeTokenSuccess(false); | ||
} | ||
setIsCallScrapeTokenLoading(false); | ||
}; | ||
|
||
const tokenFromStore = findEthOrTokenByAddress(address, tokens, chainId); | ||
const tokenFromStore = findEthOrTokenByAddress(address, allTokens, chainId); | ||
|
||
if (tokenFromStore) { | ||
setToken(tokenFromStore); | ||
} else if ( | ||
!tokenFromStore && | ||
!isCallScrapeTokenLoading && | ||
!isCallScrapeTokenSuccess | ||
) { | ||
callScrapeToken(); | ||
} else { | ||
dispatch(addActiveToken(address)); | ||
dispatch(fetchUnkownTokens({ provider: library, tokens: [address] })); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [allTokens, address, activeOrder, isActive]); | ||
}, [address, activeOrder, allTokens.length]); | ||
|
||
return [token || scrapedToken || null, isCallScrapeTokenLoading]; | ||
return [token || null, false]; | ||
}; | ||
|
||
export default useTakerTokenInfo; |
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
Oops, something went wrong.