Skip to content

Commit

Permalink
feat(ethers): init sub package for ethers
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonstudio committed Mar 22, 2024
1 parent 6f4c55f commit 0a1d90f
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@ant-design/web3-icons": "workspace:*",
"@ant-design/web3-solana": "workspace:*",
"@ant-design/web3-wagmi": "workspace:*",
"@ant-design/web3-ethers": "workspace:*",
"@biomejs/biome": "^1.6.1",
"@changesets/changelog-git": "^0.2.0",
"@changesets/cli": "^2.27.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,7 @@ export interface Locale {
NFTCard?: Partial<RequiredLocale['NFTCard']>;
Address?: Partial<RequiredLocale['Address']>;
}

export interface UniversalEIP6963Config {
autoAddInjectedWallets?: boolean;
}
5 changes: 5 additions & 0 deletions packages/ethers/.fatherrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'father';

export default defineConfig({
extends: '../../.fatherrc.base.ts',
});
3 changes: 3 additions & 0 deletions packages/ethers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @ant-design/web3-ethers

[https://web3.ant.design](https://web3.ant.design)
69 changes: 69 additions & 0 deletions packages/ethers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@ant-design/web3-ethers",
"version": "0.0.1",
"type": "module",
"main": "dist/esm/index.js",
"module": "dist/esm/index.js",
"typings": "dist/esm/index.d.ts",
"exports": {
"import": "./dist/esm/index.js",
"require": "./dist/lib/index.js",
"types": "./dist/esm/index.d.ts"
},
"files": [
"dist",
"CHANGELOG.md",
"README.md"
],
"keywords": [
"ant",
"component",
"components",
"design",
"framework",
"frontend",
"react",
"react-component",
"ui",
"web3",
"ethers",
"ethersjs"
],
"homepage": "https://we3.ant.design",
"bugs": {
"url": "https://github.com/ant-design/ant-design-web3/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/ant-design/ant-design-web3"
},
"scripts": {
"dev": "father dev",
"build": "father build"
},
"dependencies": {
"@ant-design/web3-assets": "workspace:*",
"@ant-design/web3-common": "workspace:*",
"debug": "^4.3.4",
"@tanstack/react-query": "^5.28.4"
},
"devDependencies": {
"@types/debug": "^4.1.12",
"ethers": "^6.11.1",
"father": "^4.3.8",
"typescript": "^5.4.2"
},
"peerDependencies": {
"ethers": "^6.0.0"
},
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
},
"browserslist": [
"last 2 versions",
"Firefox ESR",
"> 1%",
"ie >= 11"
]
}
43 changes: 43 additions & 0 deletions packages/ethers/src/ethers-provider/config-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import { Web3ConfigProvider, type Chain, type Locale } from '@ant-design/web3-common';

import type { EthersEIP6963Config } from '../interface';

export interface AntDesignWeb3ConfigProviderProps {
chainAssets: Chain[];
// walletFactorys: WalletFactory[];
locale?: Locale;
ens?: boolean;
balance?: boolean;
eip6963?: EthersEIP6963Config;
}

export const AntDesignWeb3ConfigProvider: React.FC<
React.PropsWithChildren<AntDesignWeb3ConfigProviderProps>
> = ({ locale, children }) => {
return (
<Web3ConfigProvider
locale={locale}
// TODO
account={undefined}
chain={undefined}
balance={undefined}
availableChains={[]}
availableWallets={[]}
connect={async () => {
throw new Error('Method not implemented');
}}
disconnect={async () => {
throw new Error('Method not implemented');
}}
switchChain={async () => {
throw new Error('Method not implemented');
}}
getNFTMetadata={async () => {
throw new Error('Method not implemented');
}}
>
{children}
</Web3ConfigProvider>
);
};

Check warning on line 43 in packages/ethers/src/ethers-provider/config-provider.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ethers/src/ethers-provider/config-provider.tsx#L2-L43

Added lines #L2 - L43 were not covered by tests
45 changes: 45 additions & 0 deletions packages/ethers/src/ethers-provider/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';
import { Mainnet } from '@ant-design/web3-assets';
import type { Chain, Locale } from '@ant-design/web3-common';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

import type { EthersEIP6963Config } from '../interface';
import { AntDesignWeb3ConfigProvider } from './config-provider';

export interface EthersWeb3ConfigProviderProps {
// config: Config;
// wallets?: WalletFactory[];
locale?: Locale;
chains?: Chain[];
ens?: boolean;
balance?: boolean;
queryClient?: QueryClient;
eip6963?: EthersEIP6963Config;
}

export const EthersWeb3ConfigProvider: React.FC<
React.PropsWithChildren<EthersWeb3ConfigProviderProps>
> = ({ children, chains = [], ens, balance, locale, eip6963, queryClient }) => {
const chainAssets = [...chains, Mainnet];

const mergedQueryClient = React.useMemo(() => {
return queryClient ?? new QueryClient();
}, [queryClient]);

return (
<QueryClientProvider client={mergedQueryClient}>
<AntDesignWeb3ConfigProvider
locale={locale}
chainAssets={chainAssets}
// walletFactorys={wallets}
// availableChains={config.chains}
// availableConnectors={config.connectors}
ens={ens}
balance={balance}
eip6963={eip6963}
>
{children}
</AntDesignWeb3ConfigProvider>
</QueryClientProvider>
);
};

Check warning on line 45 in packages/ethers/src/ethers-provider/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ethers/src/ethers-provider/index.tsx#L2-L45

Added lines #L2 - L45 were not covered by tests
3 changes: 3 additions & 0 deletions packages/ethers/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './ethers-provider';
export * from './wallets';
export * from './interface';
19 changes: 19 additions & 0 deletions packages/ethers/src/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
ConnectOptions,
UniversalEIP6963Config,
Wallet,
WalletMetadata,
} from '@ant-design/web3-common';
import type { Provider } from 'ethers';

export type EthersEIP6963Config = boolean | UniversalEIP6963Config;

export interface EthersWallet extends Wallet {
getEthersProvider?: (connectOptions?: ConnectOptions) => Promise<Provider | null>;
}

export interface EthersWalletFactory {
create: () => EthersWallet;
}

export type EthereumWallet = (metadata?: Partial<WalletMetadata>) => EthersWalletFactory;
2 changes: 2 additions & 0 deletions packages/ethers/src/wallets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './metamask';
export * from './universal-wallet';
10 changes: 10 additions & 0 deletions packages/ethers/src/wallets/metamask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { metadata_MetaMask } from '@ant-design/web3-assets';

import type { EthereumWallet } from '../interface';
import { UniversalWallet } from './universal-wallet';

export const MetaMask: EthereumWallet = (metadata) =>
new UniversalWallet({
...metadata_MetaMask,
...metadata,
});
10 changes: 10 additions & 0 deletions packages/ethers/src/wallets/universal-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { WalletMetadata } from '@ant-design/web3-common';

import type { EthersWallet, EthersWalletFactory } from '../interface';

export class UniversalWallet implements EthersWalletFactory {
constructor(private readonly wallet: WalletMetadata) {}
create = (): EthersWallet => {
throw new Error('Method not implemented.');
};
}
4 changes: 4 additions & 0 deletions packages/ethers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src", "global.d.ts"]
}
13 changes: 7 additions & 6 deletions packages/wagmi/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { ConnectOptions, Wallet, WalletMetadata } from '@ant-design/web3-common';
import type {
ConnectOptions,
UniversalEIP6963Config,
Wallet,
WalletMetadata,
} from '@ant-design/web3-common';
import type { Connector } from 'wagmi';

export interface WalletUseInWagmiAdapter extends Wallet {
Expand All @@ -12,8 +17,4 @@ export interface WalletFactory {
create: (connector?: readonly Connector[]) => WalletUseInWagmiAdapter;
}

export type EIP6963Config =
| boolean
| {
autoAddInjectedWallets?: boolean;
};
export type WagmiEIP6963Config = boolean | UniversalEIP6963Config;
4 changes: 2 additions & 2 deletions packages/wagmi/src/wagmi-provider/config-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type Connector as WagmiConnector,
} from 'wagmi';

import type { EIP6963Config, WalletFactory, WalletUseInWagmiAdapter } from '../interface';
import type { WagmiEIP6963Config, WalletFactory, WalletUseInWagmiAdapter } from '../interface';
import { isEIP6963Connector } from '../utils';
import { EIP6963Wallet } from '../wallets/eip6963';
import { addNameToAccount, getNFTMetadata } from './methods';
Expand All @@ -30,7 +30,7 @@ export interface AntDesignWeb3ConfigProviderProps {
children?: React.ReactNode;
ens?: boolean;
balance?: boolean;
eip6963?: EIP6963Config;
eip6963?: WagmiEIP6963Config;
readonly availableChains: readonly WagmiChain[];
readonly availableConnectors: readonly WagmiConnector[];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/wagmi/src/wagmi-provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { WagmiProvider } from 'wagmi';
import type { Config } from 'wagmi';

import type { EIP6963Config, WalletFactory } from '../interface';
import type { WagmiEIP6963Config, WalletFactory } from '../interface';
import { AntDesignWeb3ConfigProvider } from './config-provider';

export type WagmiWeb3ConfigProviderProps = {
Expand All @@ -17,7 +17,7 @@ export type WagmiWeb3ConfigProviderProps = {
ens?: boolean;
queryClient?: QueryClient;
balance?: boolean;
eip6963?: EIP6963Config;
eip6963?: WagmiEIP6963Config;
};

export function WagmiWeb3ConfigProvider({
Expand Down
1 change: 1 addition & 0 deletions packages/web3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"devDependencies": {
"@ant-design/web3-solana": "workspace:*",
"@ant-design/web3-wagmi": "workspace:*",
"@ant-design/web3-ethers": "workspace:*",
"@types/react": "^18.2.65",
"@types/react-dom": "^18.2.19",
"father": "^4.3.8",
Expand Down
Loading

0 comments on commit 0a1d90f

Please sign in to comment.