Skip to content

Commit

Permalink
fix(shell): use fallback for unsupported chains parsed on serve side
Browse files Browse the repository at this point in the history
  • Loading branch information
olegshilov committed Aug 29, 2024
1 parent 865406e commit e200c38
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 33 deletions.
7 changes: 5 additions & 2 deletions apps/shell/src/app/authz/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import {
QueryClient,
} from '@tanstack/react-query';
import { headers } from 'next/headers';
import { haqqMainnet } from 'wagmi/chains';
import { createCosmosService, getChainParams } from '@haqq/data-access-cosmos';
import { AuthzPage } from '@haqq/shell-authz';
import {
ethToHaqq,
indexerBalancesFetcher,
parseWagmiCookies,
} from '@haqq/shell-shared';
import { supportedChainsIds } from '../../config/wagmi-config';

export default async function Authz() {
const cookies = headers().get('cookie');
const { chainId, walletAddress } = parseWagmiCookies(cookies);
const chainIdToUse = chainId ?? haqqMainnet.id;
const chainIdToUse =
chainId && supportedChainsIds.includes(chainId)
? chainId
: supportedChainsIds[0];
const { cosmosRestEndpoint } = getChainParams(chainIdToUse);
const queryClient = new QueryClient();
const { getAuthzGranteeGrants, getAuthzGranterGrants } =
Expand Down
7 changes: 5 additions & 2 deletions apps/shell/src/app/dao/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import {
QueryClient,
} from '@tanstack/react-query';
import { headers } from 'next/headers';
import { haqqMainnet } from 'wagmi/chains';
import { createCosmosService, getChainParams } from '@haqq/data-access-cosmos';
import { DaoPage } from '@haqq/shell-dao';
import {
ethToHaqq,
indexerBalancesFetcher,
parseWagmiCookies,
} from '@haqq/shell-shared';
import { supportedChainsIds } from '../../config/wagmi-config';

export default async function Authz() {
const cookies = headers().get('cookie');
const { chainId, walletAddress } = parseWagmiCookies(cookies);
const chainIdToUse = chainId ?? haqqMainnet.id;
const chainIdToUse =
chainId && supportedChainsIds.includes(chainId)
? chainId
: supportedChainsIds[0];
const queryClient = new QueryClient();
const { cosmosRestEndpoint } = getChainParams(chainIdToUse);
const { getErc20TokenPairs } = createCosmosService(cosmosRestEndpoint);
Expand Down
10 changes: 7 additions & 3 deletions apps/shell/src/app/faucet/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
import dynamic from 'next/dynamic';
import { headers } from 'next/headers';
import { notFound } from 'next/navigation';
import { haqqMainnet, haqqTestedge2 } from 'wagmi/chains';
import { haqqTestedge2 } from 'wagmi/chains';
import {
ethToHaqq,
indexerBalancesFetcher,
parseWagmiCookies,
} from '@haqq/shell-shared';
import { supportedChainsIds } from '../../config/wagmi-config';
import { env } from '../../env/client';

const AuthProvider = dynamic(async () => {
Expand All @@ -26,8 +27,11 @@ const FaucetPage = dynamic(async () => {

export default async function Faucet() {
const cookies = headers().get('cookie');
const { chainId: parsedChainId, walletAddress } = parseWagmiCookies(cookies);
const chainIdToUse = parsedChainId ?? haqqMainnet.id;
const { chainId, walletAddress } = parseWagmiCookies(cookies);
const chainIdToUse =
chainId && supportedChainsIds.includes(chainId)
? chainId
: supportedChainsIds[0];

if (chainIdToUse !== haqqTestedge2.id) {
return notFound();
Expand Down
7 changes: 5 additions & 2 deletions apps/shell/src/app/governance/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import {
dehydrate,
} from '@tanstack/react-query';
import { headers } from 'next/headers';
import { haqqMainnet } from 'wagmi/chains';
import { createCosmosService, getChainParams } from '@haqq/data-access-cosmos';
import { ProposalListPage } from '@haqq/shell-governance';
import { parseWagmiCookies } from '@haqq/shell-shared';
import { supportedChainsIds } from '../../config/wagmi-config';

export const dynamic = 'force-dynamic';
export const fetchCache = 'force-no-store';

export default async function ProposalList() {
const cookies = headers().get('cookie');
const { chainId } = parseWagmiCookies(cookies);
const chainIdToUse = chainId ?? haqqMainnet.id;
const chainIdToUse =
chainId && supportedChainsIds.includes(chainId)
? chainId
: supportedChainsIds[0];
const { cosmosRestEndpoint } = getChainParams(chainIdToUse);
const { getProposals, getGovernanceParams, getProposalTally } =
createCosmosService(cosmosRestEndpoint);
Expand Down
7 changes: 5 additions & 2 deletions apps/shell/src/app/governance/proposal/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
} from '@tanstack/react-query';
import { headers } from 'next/headers';
import { notFound } from 'next/navigation';
import { haqqMainnet } from 'wagmi/chains';
import { createCosmosService, getChainParams } from '@haqq/data-access-cosmos';
import { ProposalDetailsPage } from '@haqq/shell-governance';
import { parseWagmiCookies } from '@haqq/shell-shared';
import { supportedChainsIds } from '../../../../config/wagmi-config';

export const dynamic = 'force-dynamic';
export const fetchCache = 'force-no-store';
Expand All @@ -26,7 +26,10 @@ export default async function ProposalDetails({

const cookies = headers().get('cookie');
const { chainId } = parseWagmiCookies(cookies);
const chainIdToUse = chainId ?? haqqMainnet.id;
const chainIdToUse =
chainId && supportedChainsIds.includes(chainId)
? chainId
: supportedChainsIds[0];
const { cosmosRestEndpoint } = getChainParams(chainIdToUse);
const {
getProposalDetails,
Expand Down
8 changes: 6 additions & 2 deletions apps/shell/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
QueryClient,
} from '@tanstack/react-query';
import { headers } from 'next/headers';
import { haqqMainnet } from 'wagmi/chains';
import { createCosmosService, getChainParams } from '@haqq/data-access-cosmos';
import { MainPage } from '@haqq/shell-main';
import {
Expand All @@ -13,12 +12,17 @@ import {
indexerBalancesFetcher,
parseWagmiCookies,
} from '@haqq/shell-shared';
import { supportedChainsIds } from '../config/wagmi-config';

export default async function IndexPage() {
const headersList = headers();
const cookies = headersList.get('cookie');
const { chainId, walletAddress } = parseWagmiCookies(cookies);
const chainIdToUse = chainId ?? haqqMainnet.id;
const chainIdToUse =
chainId && supportedChainsIds.includes(chainId)
? chainId
: supportedChainsIds[0];

const { cosmosRestEndpoint } = getChainParams(chainIdToUse);
const {
getProposals,
Expand Down
7 changes: 5 additions & 2 deletions apps/shell/src/app/staking/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {
dehydrate,
} from '@tanstack/react-query';
import { headers } from 'next/headers';
import { haqqMainnet } from 'wagmi/chains';
import { createCosmosService, getChainParams } from '@haqq/data-access-cosmos';
import {
ethToHaqq,
indexerBalancesFetcher,
parseWagmiCookies,
} from '@haqq/shell-shared';
import { ValidatorListPage } from '@haqq/shell-staking';
import { supportedChainsIds } from '../../config/wagmi-config';

export const dynamic = 'force-dynamic';
export const fetchCache = 'force-no-store';
Expand All @@ -20,7 +20,10 @@ export default async function ValidatorList() {
const headersList = headers();
const cookies = headersList.get('cookie');
const { chainId, walletAddress } = parseWagmiCookies(cookies);
const chainIdToUse = chainId ?? haqqMainnet.id;
const chainIdToUse =
chainId && supportedChainsIds.includes(chainId)
? chainId
: supportedChainsIds[0];
const { cosmosRestEndpoint } = getChainParams(chainIdToUse);
const {
getStakingParams,
Expand Down
8 changes: 6 additions & 2 deletions apps/shell/src/app/staking/validator/[address]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
} from '@tanstack/react-query';
import { headers } from 'next/headers';
import { notFound } from 'next/navigation';
import { haqqMainnet } from 'wagmi/chains';
import { createCosmosService, getChainParams } from '@haqq/data-access-cosmos';
import {
ethToHaqq,
indexerBalancesFetcher,
parseWagmiCookies,
} from '@haqq/shell-shared';
import { ValidatorDetailsPage } from '@haqq/shell-staking';
import { supportedChainsIds } from '../../../../config/wagmi-config';

export default async function ValidatorDetails({
params: { address },
Expand All @@ -25,7 +25,11 @@ export default async function ValidatorDetails({

const cookies = headers().get('cookie');
const { chainId, walletAddress } = parseWagmiCookies(cookies);
const chainIdToUse = chainId ?? haqqMainnet.id;
const chainIdToUse =
chainId && supportedChainsIds.includes(chainId)
? chainId
: supportedChainsIds[0];

const { cosmosRestEndpoint } = getChainParams(chainIdToUse);
const {
getValidators,
Expand Down
20 changes: 15 additions & 5 deletions apps/shell/src/config/wagmi-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Transport } from 'viem';
import {
createConfig,
http,
Expand All @@ -8,6 +9,18 @@ import {
import { haqqMainnet, haqqTestedge2 } from 'wagmi/chains';
import { walletConnect } from 'wagmi/connectors';

export const supportedChains = [haqqMainnet, haqqTestedge2] as const;
export const supportedChainsIds = supportedChains.map((chain): number => {
return chain.id;
});
const supportedChainsTransports = supportedChains.reduce(
(acc, chain) => {
acc[chain.id] = http();
return acc;
},
{} as Record<number, Transport>,
);

export function createWagmiConfig(walletConnectProjectId?: string) {
const connectors: CreateConnectorFn[] = [];

Expand All @@ -24,11 +37,8 @@ export function createWagmiConfig(walletConnectProjectId?: string) {
}

return createConfig({
chains: [haqqMainnet, haqqTestedge2],
transports: {
[haqqMainnet.id]: http(),
[haqqTestedge2.id]: http(),
},
chains: supportedChains,
transports: supportedChainsTransports,
connectors,
ssr: true,
multiInjectedProviderDiscovery: true,
Expand Down
2 changes: 0 additions & 2 deletions libs/data-access/cosmos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"version": "0.0.1",
"dependencies": {
"@swc/helpers": "~0.5.11",
"@evmos/proto": "0.1.27",
"@evmos/provider": "0.2.8",
"@evmos/transactions": "0.2.13",
"@haqq/data-access-falconer": "0.0.1"
},
Expand Down
13 changes: 6 additions & 7 deletions libs/data-access/cosmos/src/lib/get-chain-params.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { Fee } from '@evmos/transactions';
import { chains } from './chains';

export function getChainParams(chainId: number) {
const currentChain = chains[chainId];

if (!currentChain) {
throw new Error(`No configuration for chain with id ${chainId}`);
}
// Add a default chain ID (HAQQ mainnet)
const DEFAULT_CHAIN_ID = 11235;

return currentChain;
export function getChainParams(chainId: number) {
// Check if the chainId is supported, otherwise use the default
const supportedChainId = chainId in chains ? chainId : DEFAULT_CHAIN_ID;
return chains[supportedChainId];
}

export const DEFAULT_FEE: Fee = {
Expand Down
2 changes: 1 addition & 1 deletion libs/ui-kit/src/lib/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client';
import { Fragment, PropsWithChildren, SyntheticEvent } from 'react';
import { PropsWithChildren, SyntheticEvent } from 'react';
import { Dialog } from '@headlessui/react';
import clsx from 'clsx';

Expand Down
2 changes: 1 addition & 1 deletion libs/ui-kit/src/lib/sort-select.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client';
import { Fragment, useMemo } from 'react';
import { useMemo } from 'react';
import { Menu, MenuButton, MenuItem, MenuItems } from '@headlessui/react';
import clsx from 'clsx';

Expand Down

0 comments on commit e200c38

Please sign in to comment.