@reduxjs/toolkit#nanoid TypeScript Examples
The following examples show how to use
@reduxjs/toolkit#nanoid.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useFetchListCallback.ts From cheeseswap-interface with GNU General Public License v3.0 | 6 votes |
export function useFetchListCallback(): (listUrl: string) => Promise<TokenList> {
const { chainId, library } = useActiveWeb3React()
const dispatch = useDispatch<AppDispatch>()
const ensResolver = useCallback(
(ensName: string) => {
if (!library || chainId !== ChainId.MAINNET) {
if (NETWORK_CHAIN_ID === ChainId.MAINNET) {
const networkLibrary = getNetworkLibrary()
if (networkLibrary) {
return resolveENSContentHash(ensName, networkLibrary)
}
}
throw new Error('Could not construct mainnet ENS resolver')
}
return resolveENSContentHash(ensName, library)
},
[chainId, library]
)
return useCallback(
async (listUrl: string) => {
const requestId = nanoid()
dispatch(fetchTokenList.pending({ requestId, url: listUrl }))
return getTokenList(listUrl, ensResolver)
.then(tokenList => {
dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }))
return tokenList
})
.catch(error => {
console.debug(`Failed to get list at url ${listUrl}`, error)
dispatch(fetchTokenList.rejected({ url: listUrl, requestId, errorMessage: error.message }))
throw error
})
},
[dispatch, ensResolver]
)
}
Example #2
Source File: useFetchListCallback.ts From dyp with Do What The F*ck You Want To Public License | 6 votes |
export function useFetchListCallback(): (listUrl: string) => Promise<TokenList> {
const { chainId, library } = useActiveWeb3React()
const dispatch = useDispatch<AppDispatch>()
const ensResolver = useCallback(
(ensName: string) => {
if (!library || chainId !== ChainId.MAINNET) {
if (NETWORK_CHAIN_ID === ChainId.MAINNET) {
const networkLibrary = getNetworkLibrary()
if (networkLibrary) {
return resolveENSContentHash(ensName, networkLibrary)
}
}
throw new Error('Could not construct mainnet ENS resolver')
}
return resolveENSContentHash(ensName, library)
},
[chainId, library]
)
return useCallback(
async (listUrl: string) => {
const requestId = nanoid()
dispatch(fetchTokenList.pending({ requestId, url: listUrl }))
return getTokenList(listUrl, ensResolver)
.then(tokenList => {
dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }))
return tokenList
})
.catch(error => {
console.debug(`Failed to get list at url ${listUrl}`, error)
dispatch(fetchTokenList.rejected({ url: listUrl, requestId, errorMessage: error.message }))
throw error
})
},
[dispatch, ensResolver]
)
}
Example #3
Source File: useFetchListCallback.ts From goose-frontend-amm with GNU General Public License v3.0 | 6 votes |
export function useFetchListCallback(): (listUrl: string) => Promise<TokenList> {
const { chainId, library } = useActiveWeb3React()
const dispatch = useDispatch<AppDispatch>()
const ensResolver = useCallback(
(ensName: string) => {
if (!library || chainId !== ChainId.MAINNET) {
if (NETWORK_CHAIN_ID === ChainId.MAINNET) {
const networkLibrary = getNetworkLibrary()
if (networkLibrary) {
return resolveENSContentHash(ensName, networkLibrary)
}
}
throw new Error('Could not construct mainnet ENS resolver')
}
return resolveENSContentHash(ensName, library)
},
[chainId, library]
)
return useCallback(
async (listUrl: string) => {
const requestId = nanoid()
dispatch(fetchTokenList.pending({ requestId, url: listUrl }))
return getTokenList(listUrl, ensResolver)
.then((tokenList) => {
dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }))
return tokenList
})
.catch((error) => {
console.error(`Failed to get list at url ${listUrl}`, error)
dispatch(fetchTokenList.rejected({ url: listUrl, requestId, errorMessage: error.message }))
throw error
})
},
[dispatch, ensResolver]
)
}
Example #4
Source File: useFetchListCallback.ts From pancake-swap-exchange-testnet with GNU General Public License v3.0 | 6 votes |
export function useFetchListCallback(): (listUrl: string) => Promise<TokenList> {
const { chainId, library } = useActiveWeb3React()
const dispatch = useDispatch<AppDispatch>()
const ensResolver = useCallback(
(ensName: string) => {
if (!library || chainId !== ChainId.MAINNET) {
if (NETWORK_CHAIN_ID === ChainId.MAINNET) {
const networkLibrary = getNetworkLibrary()
if (networkLibrary) {
return resolveENSContentHash(ensName, networkLibrary)
}
}
throw new Error('Could not construct mainnet ENS resolver')
}
return resolveENSContentHash(ensName, library)
},
[chainId, library]
)
return useCallback(
async (listUrl: string) => {
const requestId = nanoid()
dispatch(fetchTokenList.pending({ requestId, url: listUrl }))
return getTokenList(listUrl, ensResolver)
.then((tokenList) => {
dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }))
return tokenList
})
.catch((error) => {
console.error(`Failed to get list at url ${listUrl}`, error)
dispatch(fetchTokenList.rejected({ url: listUrl, requestId, errorMessage: error.message }))
throw error
})
},
[dispatch, ensResolver]
)
}
Example #5
Source File: useFetchListCallback.ts From panther-frontend-dex with GNU General Public License v3.0 | 6 votes |
export function useFetchListCallback(): (listUrl: string) => Promise<TokenList> {
const { chainId, library } = useActiveWeb3React()
const dispatch = useDispatch<AppDispatch>()
const ensResolver = useCallback(
(ensName: string) => {
if (!library || chainId !== ChainId.MAINNET) {
if (NETWORK_CHAIN_ID === ChainId.MAINNET) {
const networkLibrary = getNetworkLibrary()
if (networkLibrary) {
return resolveENSContentHash(ensName, networkLibrary)
}
}
throw new Error('Could not construct mainnet ENS resolver')
}
return resolveENSContentHash(ensName, library)
},
[chainId, library]
)
return useCallback(
async (listUrl: string) => {
const requestId = nanoid()
dispatch(fetchTokenList.pending({ requestId, url: listUrl }))
return getTokenList(listUrl, ensResolver)
.then((tokenList) => {
dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }))
return tokenList
})
.catch((error) => {
console.error(`Failed to get list at url ${listUrl}`, error)
dispatch(fetchTokenList.rejected({ url: listUrl, requestId, errorMessage: error.message }))
throw error
})
},
[dispatch, ensResolver]
)
}
Example #6
Source File: useFetchListCallback.ts From forward.swaps with GNU General Public License v3.0 | 6 votes |
export function useFetchListCallback(): (listUrl: string, sendDispatch?: boolean) => Promise<TokenList> {
const { chainId, library } = useActiveWeb3React()
const dispatch = useDispatch<AppDispatch>()
const ensResolver = useCallback(
(ensName: string) => {
if (!library || chainId !== ChainId.MAINNET) {
if (NETWORK_CHAIN_ID === ChainId.MAINNET) {
const networkLibrary = getNetworkLibrary()
if (networkLibrary) {
return resolveENSContentHash(ensName, networkLibrary)
}
}
throw new Error('Could not construct mainnet ENS resolver')
}
return resolveENSContentHash(ensName, library)
},
[chainId, library]
)
// note: prevent dispatch if using for list search or unsupported list
return useCallback(
async (listUrl: string, sendDispatch = true) => {
const requestId = nanoid()
sendDispatch && dispatch(fetchTokenList.pending({ requestId, url: listUrl }))
return getTokenList(listUrl, ensResolver)
.then(tokenList => {
sendDispatch && dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }))
return tokenList
})
.catch(error => {
console.debug(`Failed to get list at url ${listUrl}`, error)
sendDispatch && dispatch(fetchTokenList.rejected({ url: listUrl, requestId, errorMessage: error.message }))
throw error
})
},
[dispatch, ensResolver]
)
}
Example #7
Source File: useFetchListCallback.ts From luaswap-interface with GNU General Public License v3.0 | 6 votes |
export function useFetchListCallback(): (listUrl: string) => Promise<TokenList> {
const { chainId, library } = useActiveWeb3React()
const dispatch = useDispatch<AppDispatch>()
const ensResolver = useCallback(
(ensName: string) => {
if (!library || chainId !== ChainId.MAINNET) {
if (NETWORK_CHAIN_ID === ChainId.MAINNET) {
const networkLibrary = getNetworkLibrary()
if (networkLibrary) {
return resolveENSContentHash(ensName, networkLibrary)
}
}
throw new Error('Could not construct mainnet ENS resolver')
}
return resolveENSContentHash(ensName, library)
},
[chainId, library]
)
return useCallback(
async (listUrl: string) => {
const requestId = nanoid()
dispatch(fetchTokenList.pending({ requestId, url: listUrl }))
return getTokenList(listUrl, ensResolver)
.then(tokenList => {
dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }))
return tokenList
})
.catch(error => {
console.debug(`Failed to get list at url ${listUrl}`, error)
dispatch(fetchTokenList.rejected({ url: listUrl, requestId, errorMessage: error.message }))
throw error
})
},
[dispatch, ensResolver]
)
}
Example #8
Source File: useFetchListCallback.ts From pancakeswap-testnet with GNU General Public License v3.0 | 6 votes |
export function useFetchListCallback(): (listUrl: string) => Promise<TokenList> {
const { chainId, library } = useActiveWeb3React()
const dispatch = useDispatch<AppDispatch>()
const ensResolver = useCallback(
(ensName: string) => {
if (!library || chainId !== ChainId.MAINNET) {
if (NETWORK_CHAIN_ID === ChainId.MAINNET) {
const networkLibrary = getNetworkLibrary()
if (networkLibrary) {
return resolveENSContentHash(ensName, networkLibrary)
}
}
throw new Error('Could not construct mainnet ENS resolver')
}
return resolveENSContentHash(ensName, library)
},
[chainId, library]
)
return useCallback(
async (listUrl: string) => {
const requestId = nanoid()
dispatch(fetchTokenList.pending({ requestId, url: listUrl }))
return getTokenList(listUrl, ensResolver)
.then((tokenList) => {
dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }))
return tokenList
})
.catch((error) => {
console.error(`Failed to get list at url ${listUrl}`, error)
dispatch(fetchTokenList.rejected({ url: listUrl, requestId, errorMessage: error.message }))
throw error
})
},
[dispatch, ensResolver]
)
}
Example #9
Source File: useFetchListCallback.ts From interface-v2 with GNU General Public License v3.0 | 5 votes |
export function useFetchListCallback(): (
listUrl: string,
) => Promise<TokenList> {
const { chainId, library } = useActiveWeb3React();
const dispatch = useDispatch<AppDispatch>();
const ensResolver = useCallback(
(ensName: string) => {
if (!library || chainId !== ChainId.MUMBAI) {
if (NETWORK_CHAIN_ID === ChainId.MUMBAI) {
const networkLibrary = getNetworkLibrary();
if (networkLibrary) {
return resolveENSContentHash(ensName, networkLibrary);
}
}
throw new Error('Could not construct mainnet ENS resolver');
}
return resolveENSContentHash(ensName, library);
},
[chainId, library],
);
return useCallback(
async (listUrl: string) => {
const requestId = nanoid();
dispatch(fetchTokenList.pending({ requestId, url: listUrl }));
return getTokenList(listUrl, ensResolver)
.then((tokenList) => {
dispatch(
fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }),
);
return tokenList;
})
.catch((error) => {
console.debug(`Failed to get list at url ${listUrl}`, error);
dispatch(
fetchTokenList.rejected({
url: listUrl,
requestId,
errorMessage: error.message,
}),
);
throw error;
});
},
[dispatch, ensResolver],
);
}
Example #10
Source File: useFetchListCallback.ts From limit-orders-lib with GNU General Public License v3.0 | 5 votes |
export function useFetchListCallback(): (
library: Web3Provider,
listUrl: string,
sendDispatch?: boolean
) => Promise<TokenList> {
const dispatch = useDispatch<AppDispatch>();
const ensResolver = useCallback(
async (library: Web3Provider, ensName: string) => {
return resolveENSContentHash(ensName, library);
},
[]
);
// note: prevent dispatch if using for list search or unsupported list
return useCallback(
async (library: Web3Provider, listUrl: string, sendDispatch = true) => {
const requestId = nanoid();
sendDispatch &&
dispatch(fetchTokenList.pending({ requestId, url: listUrl }));
return getTokenList(library, listUrl, ensResolver)
.then((tokenList) => {
sendDispatch &&
dispatch(
fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId })
);
return tokenList;
})
.catch((error) => {
console.debug(`Failed to get list at url ${listUrl}`, error);
sendDispatch &&
dispatch(
fetchTokenList.rejected({
url: listUrl,
requestId,
errorMessage: error.message,
})
);
throw error;
});
},
[dispatch, ensResolver]
);
}
Example #11
Source File: useFetchListCallback.ts From glide-frontend with GNU General Public License v3.0 | 5 votes |
function useFetchListCallback(): (listUrl: string, sendDispatch?: boolean) => Promise<TokenList> {
const { library } = useWeb3Provider()
const { chainId } = useActiveWeb3React()
const dispatch = useDispatch<AppDispatch>()
const ensResolver = useCallback(
(ensName: string) => {
if (chainId !== ChainId.MAINNET) {
throw new Error('Could not construct mainnet ENS resolver')
}
return resolveENSContentHash(ensName, library)
},
[chainId, library],
)
// note: prevent dispatch if using for list search or unsupported list
return useCallback(
async (listUrl: string, sendDispatch = true) => {
const requestId = nanoid()
if (sendDispatch) {
dispatch(fetchTokenList.pending({ requestId, url: listUrl }))
}
return getTokenList(listUrl, ensResolver)
.then((tokenList) => {
if (sendDispatch) {
dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }))
}
return tokenList
})
.catch((error) => {
console.error(`Failed to get list at url ${listUrl}`, error)
if (sendDispatch) {
dispatch(fetchTokenList.rejected({ url: listUrl, requestId, errorMessage: error.message }))
}
throw error
})
},
[dispatch, ensResolver],
)
}
Example #12
Source File: useFetchListCallback.ts From vvs-ui with GNU General Public License v3.0 | 5 votes |
function useFetchListCallback(): (listUrl: string, sendDispatch?: boolean) => Promise<TokenList> {
const { library } = useWeb3Provider()
const { chainId } = useActiveWeb3React()
const dispatch = useDispatch<AppDispatch>()
const ensResolver = useCallback(
(ensName: string) => {
if (chainId !== ChainId.MAINNET) {
throw new Error('Could not construct mainnet ENS resolver')
}
return resolveENSContentHash(ensName, library)
},
[chainId, library],
)
// note: prevent dispatch if using for list search or unsupported list
return useCallback(
async (listUrl: string, sendDispatch = true) => {
const requestId = nanoid()
if (sendDispatch) {
dispatch(fetchTokenList.pending({ requestId, url: listUrl }))
}
return getTokenList(listUrl, ensResolver)
.then((tokenList) => {
if (sendDispatch) {
dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId }))
}
return tokenList
})
.catch((error) => {
console.error(`Failed to get list at url ${listUrl}`, error)
if (sendDispatch) {
dispatch(fetchTokenList.rejected({ url: listUrl, requestId, errorMessage: error.message }))
}
throw error
})
},
[dispatch, ensResolver],
)
}