utils#getNetworkById TypeScript Examples

The following examples show how to use utils#getNetworkById. 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: guildAvailability.tsx    From dxvote with GNU Affero General Public License v3.0 4 votes vote down vote up
GuildAvailabilityProvider = ({ children }) => {
  const routeMatch = useRouteMatch<{ guild_id?: string }>(
    '/:chain_name/:guild_id'
  );
  const guildId = routeMatch?.params?.guild_id;
  const { providers: multichainProviders } = useContext(MultichainContext);
  const [availability, setAvailability] = useState<ContractAvailability>({});
  const { chainId: currentChainId } = useWeb3React();
  const { trySwitching } = useNetworkSwitching();

  useEffect(() => {
    if (!guildId || !multichainProviders) {
      setAvailability({});
      return;
    }

    async function getAvailability() {
      let providers = Object.entries(multichainProviders);

      providers.forEach(async ([chainId, provider]) => {
        provider
          .getCode(guildId)
          .then(code => code !== '0x')
          .then(result => {
            setAvailability(prev => ({
              ...prev,
              [chainId]: result,
            }));
          })
          .catch(() => {
            setAvailability(prev => ({
              ...prev,
              [chainId]: false,
            }));
          });
      });
    }

    getAvailability();
  }, [guildId, multichainProviders]);

  const isLoading = useMemo(
    () => !Object.keys(availability).includes(String(currentChainId)),
    [availability, currentChainId]
  );

  if (!isLoading && !availability?.[currentChainId]) {
    return (
      <Result
        state={ResultState.ERROR}
        title="Guild not available."
        subtitle={
          Object.values(availability).includes(true)
            ? 'This guild is not available on this network.'
            : 'No guild exists on this address.'
        }
        extra={
          Object.values(availability).includes(true) ? (
            <>
              <GreyText>Access it on</GreyText>
              <div>
                {Object.keys(availability).map(chainId => {
                  if (!availability[chainId]) return null;
                  const chainConfig = getNetworkById(Number(chainId));
                  return (
                    <div key={chainConfig?.id}>
                      <NetworkIconButton
                        iconLeft
                        onClick={() => trySwitching(chainConfig)}
                      >
                        <ButtonIcon
                          src={iconsByChain[chainConfig?.id]}
                          alt={chainConfig?.name}
                        />{' '}
                        {chainConfig?.displayName}
                      </NetworkIconButton>
                    </div>
                  );
                })}
              </div>
            </>
          ) : (
            <UnstyledLink to={`/`}>
              <IconButton iconLeft>
                <FiArrowLeft /> Take me home
              </IconButton>
            </UnstyledLink>
          )
        }
      />
    );
  }

  return (
    <GuildAvailabilityContext.Provider
      value={{
        availability,
        isLoading,
      }}
    >
      {children}
    </GuildAvailabilityContext.Provider>
  );
}