Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve custom chains provisioning and eslint conflicts #1

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"root": true,
"extends": ["react-app", "plugin:prettier/recommended", "./.eslintrc.base"]
"extends": ["react-app", "./.eslintrc.base", "plugin:prettier/recommended"]
}
2 changes: 2 additions & 0 deletions example-next/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const config = {
argsIgnorePattern: '^_',
},
],
'jsx-quotes': 'off',
'import/no-default-export': 'off',
'@typescript-eslint/require-await': 'off',
'@typescript-eslint/no-misused-promises': [
'error',
Expand Down
2 changes: 1 addition & 1 deletion example-next/src/app/wallets/mynearwallet/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function NearPage() {

return (
<div>
<h1 className='text-lg font-semibold'>Oh, I see you're trying to connect to My Near Wallet.</h1>
<h1 className='text-lg font-semibold'>Oh, I see you are trying to connect to My Near Wallet.</h1>
<h2 className='text-center text-neutral-300 mt-2'>Please wait...</h2>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion example-next/src/components/ConnectedAccounts.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
ConnectedAccount,
type ConnectedAccount,
useAccounts,
useChain,
useConnect,
Expand Down
2 changes: 1 addition & 1 deletion example-next/src/components/Example.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
ChainId,
type ChainId,
useChain,
useChains,
useCurrentAccount,
Expand Down
6 changes: 3 additions & 3 deletions example-next/src/components/Providers.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client';
import { CosmsosChainType, TangledContextProvider, solana } from '@noble-assets/tangled-react';
import { type CosmosChainType, TangledContextProvider, solana } from '@noble-assets/tangled-react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { ReactNode } from 'react';
import { type ReactNode } from 'react';

const dydx: CosmsosChainType = {
const dydx: CosmosChainType = {
id: 'dydx-mainnet-1' as `${string}-${number}`,
chainName: 'dydx',
name: 'dYdX',
Expand Down
2 changes: 1 addition & 1 deletion example-next/src/components/Tokens.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ETH_ADDRESS, SOL_ADDRESS, TokenMetadata, useChain, useToken } from '@noble-assets/tangled-react';
import { ETH_ADDRESS, SOL_ADDRESS, type TokenMetadata, useChain, useToken } from '@noble-assets/tangled-react';

export const Tokens = () => {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
"@near-wallet-selector/my-near-wallet": "^8.9.13",
"@near-wallet-selector/near-mobile-wallet": "^8.9.13",
"@near-wallet-selector/wallet-connect": "^8.9.13",
"@noble-assets/tangled-solana-react": "^1.0.0",
"@safe-global/safe-apps-sdk": "^9.0.0",
"@solana/spl-token": "^0.4.8",
"@solana/wallet-adapter-base": "^0.9.23",
"@solana/web3.js": "^1.94.0",
"@noble-assets/tangled-solana-react": "^1.0.0",
"@tanstack/react-query": "^5.59.20",
"@wagmi/core": "^2.11.0",
"detect-browser": "^5.3.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/actions/cosmos/getCosmosToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate';
import { maxInt256 } from 'viem';
import { ChainData, ConnectionOrConfig, CosmsosChainType } from '../../types/index.js';
import { ChainData, ConnectionOrConfig, CosmosChainType } from '../../types/index.js';
import { areTokensEqual, formatTokenAddress, isNativeOrFactoryToken } from '../../utils/index.js';

export const getCosmosTokenMetadata = async ({
Expand All @@ -9,7 +9,7 @@ export const getCosmosTokenMetadata = async ({
getCosmosClient,
}: {
token: string;
chain: CosmsosChainType;
chain: CosmosChainType;
code-z2 marked this conversation as resolved.
Show resolved Hide resolved
getCosmosClient: ConnectionOrConfig['getCosmosClient'];
}) => {
const registryClient = await getCosmosClient().getChainRegistry();
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/chains/injective.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CosmsosChainType } from '../types/index.js';
import { CosmosChainType } from '../types/index.js';

export const injective: CosmsosChainType = {
export const injective: CosmosChainType = {
id: 'injective-1',
name: 'Injective',
type: 'cosmos',
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/chains/noble.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CosmsosChainType } from '../types/index.js';
import { CosmosChainType } from '../types/index.js';

export const noble: CosmsosChainType = {
export const noble: CosmosChainType = {
id: 'noble-1',
chainName: 'noble',
name: 'Noble',
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/chains/osmosis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CosmsosChainType } from '../types/index.js';
import { CosmosChainType } from '../types/index.js';

export const osmosis: CosmsosChainType = {
export const osmosis: CosmosChainType = {
id: 'osmosis-1',
chainName: 'osmosis',
name: 'Osmosis',
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/providers/CosmosProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { useStore } from 'zustand';
import { useTangledConfig } from '../hooks/useTangledConfig.js';
import { CosmosStore, createCosmosStore } from '../store/Cosmos.js';
import { CosmsosChainType } from '../types/index.js';
import { CosmosChainType } from '../types/index.js';
import { RemoveReadonly } from '../types/utils.js';

export interface CosmosContextValues {
Expand All @@ -32,7 +32,7 @@
store: CosmosStore | null;
}

export const CosmosContext = createContext<CosmosContextValues>({

Check warning on line 35 in packages/react/src/providers/CosmosProvider.tsx

View workflow job for this annotation

GitHub Actions / build (20)

Fast refresh only works when a file only exports components. Move your React context(s) to a separate file
connect: async () => ({ chainWallets: [], mainWallet: {} as MainWalletBase, walletId: '', chainId: '' }),
disconnect: async () => {},
store: null,
Expand All @@ -43,7 +43,7 @@
chains,
}: {
children: React.ReactNode;
chains: CosmsosChainType[];
chains: CosmosChainType[];
code-z2 marked this conversation as resolved.
Show resolved Hide resolved
}) => {
const tangledConfig = useTangledConfig((state) => state.config);
const cosmosStore = useRef(createCosmosStore(chains)).current;
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/store/Cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ChainRegistryClient as CosmosChainRegistryClient,
getCosmosChainRegistryClient,
} from '../actions/cosmos/getCosmosChainRegistryClient.js';
import { ChainData, CosmsosChainType } from '../types/index.js';
import { ChainData, CosmosChainType } from '../types/index.js';

export type GetCosmosClient = () => {
walletManager: WalletManager | undefined;
Expand Down Expand Up @@ -55,7 +55,7 @@ export const createCosmosStore = (chains: ChainData[]) => {
if (get().chainRegistry) return get().chainRegistry!;

const chainRegistry = await getCosmosChainRegistryClient(
chains.map((chain) => (chain as CosmsosChainType).chainName.toString()),
chains.map((chain) => (chain as CosmosChainType).chainName.toString()),
code-z2 marked this conversation as resolved.
Show resolved Hide resolved
);

set(() => ({ chainRegistry: chainRegistry }));
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface SuiChainType extends ChainDataGeneric {
type: Extract<'sui', ChainType>;
suiNetwork: 'mainnet' | 'testnet' | 'devnet' | 'localnet';
}
export interface CosmsosChainType extends ChainDataGeneric {
export interface CosmosChainType extends ChainDataGeneric {
type: Extract<'cosmos', ChainType>;
chainName: string;
evmId?: string;
Expand All @@ -65,7 +65,7 @@ export type OtherChainData<T extends ChainType = OtherChainTypes> = ChainDataGen
};

// Chain data discriminated union for all supported chains
export type ChainData = EVMChain | SuiChainType | CosmsosChainType | OtherChainData;
export type ChainData = EVMChain | SuiChainType | CosmosChainType | OtherChainData;

export type SupportedChainsByType = {
[K in ChainData as K['type']]: K[];
Expand Down
48 changes: 32 additions & 16 deletions packages/react/src/utils/createChainConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { CHAIN_DATA, CHAIN_NAME } from '../constants/index.js';
import { Chain, ChainConfig, ChainData, ChainId, SupportedChainsByType } from '../types/index.js';
import type {
Chain,
ChainConfig,
CosmosChainType,
EVMChain,
OtherChainData,
OtherChainTypes,
SuiChainType,
SupportedChainsByType,
} from '../types/index.js';
import getDefaultSupportedChains from './getDefaultSupportedChains.js';

const createChainConfigs = (
Expand All @@ -11,24 +19,32 @@ const createChainConfigs = (

const overrideChainConfig = (
chainsByType: Partial<SupportedChainsByType>,
overrides: Partial<Record<Chain, ChainConfig>> | undefined,
overrides: Partial<Record<string, ChainConfig>> | undefined,
) => {
code-z2 marked this conversation as resolved.
Show resolved Hide resolved
const supportedChains = getDefaultSupportedChains();

for (const chains of Object.values(chainsByType)) {
for (const chain of chains) {
if (supportedChains[chain.type].some((c) => c.id === chain.id)) {
continue;
for (const [type, chains] of Object.entries(chainsByType)) {
if (chains?.length) {
const customChains = chains.map((chain) => {
return {
...chain,
...overrides?.[chain.name],
};
});
switch (type) {
case 'cosmos':
supportedChains.cosmos = customChains as CosmosChainType[];
break;
case 'evm':
supportedChains.evm = customChains as EVMChain[];
break;
case 'sui':
supportedChains.sui = customChains as SuiChainType[];
break;
default:
supportedChains[type as OtherChainTypes] = customChains as OtherChainData[];
break;
}
// todo: simplify... this is a bit redundant
const chainId = chain.id.toString() as ChainId;
const chainData = {
...(CHAIN_DATA[chainId] ?? chain),
...overrides?.[CHAIN_NAME[chainId] ?? chain.name],
} as ChainData;

// @ts-expect-error - resolves to never
supportedChains[chain.type].push(chainData);
}
}

Expand Down
14 changes: 7 additions & 7 deletions packages/react/src/utils/getDefaultSupportedChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as evm from '../chains/evm.js';
import { near } from '../chains/near.js';
import { solana } from '../chains/solana.js';
import { sui } from '../chains/sui.js';
import { CosmsosChainType, EVMChain, OtherChainData, SuiChainType, SupportedChainsByType } from '../types/index.js';
import { SupportedChainsByType } from '../types/index.js';

const getDefaultSupportedChains = (): SupportedChainsByType => {
const supportedChains: SupportedChainsByType = {
Expand Down Expand Up @@ -34,12 +34,12 @@ const getDefaultSupportedChains = (): SupportedChainsByType => {
evm.polygonZkEvm,
evm.scroll,
evm.zkSync,
] as EVMChain[];
supportedChains.cosmos = [cosmos.osmosis, cosmos.injective, cosmos.noble] as CosmsosChainType[];
supportedChains.solana = [solana] as OtherChainData<'solana'>[];
supportedChains.sui = [sui] as SuiChainType[];
supportedChains.near = [near] as OtherChainData<'near'>[];
supportedChains.bitcoin = [bitcoin] as OtherChainData<'bitcoin'>[];
];
supportedChains.cosmos = [cosmos.osmosis, cosmos.injective, cosmos.noble];
supportedChains.solana = [solana];
supportedChains.sui = [sui];
supportedChains.near = [near];
supportedChains.bitcoin = [bitcoin];
code-z2 marked this conversation as resolved.
Show resolved Hide resolved

return supportedChains;
};
Expand Down
Loading
Loading