From a81b4c971d6add170a57a6e30f9c0836a2705ea9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=84=9A=E6=8C=87=E5=AF=BC?=
Date: Wed, 29 Nov 2023 21:19:18 +0800
Subject: [PATCH] refactor: remove accounts, use account & rename chains and
wallets (#137)
* refactor: rename accounts and chains
* chore: update docs
* fix: doc build failed
---------
Co-authored-by: yutingzhao1991
---
docs/guide/demos/guide.tsx | 2 +-
packages/common/src/types.ts | 22 +-
packages/ethereum/src/ethereum-provider.tsx | 2 +-
.../src/wagmi-provider/config-provider.tsx | 48 ++-
.../wagmi/src/wagmi-provider/index.test.tsx | 12 +-
packages/wagmi/src/wagmi-provider/index.tsx | 6 +-
.../methods/addNameToAccount.ts | 13 +
.../methods/addNameToAccounts.ts | 17 -
.../src/wagmi-provider/methods/index.test.ts | 35 +-
.../wagmi/src/wagmi-provider/methods/index.ts | 2 +-
.../__snapshots__/menu.test.tsx.snap | 313 ++++++++++++++++++
.../connect-button/__tests__/index.test.tsx | 22 +-
.../connect-button/__tests__/menu.test.tsx | 24 +-
.../__tests__/profile-modal.test.tsx | 16 +-
.../connect-button/__tests__/tooltip.test.tsx | 16 +-
.../src/connect-button/connect-button.tsx | 34 +-
.../web3/src/connect-button/demos/menu.tsx | 28 +-
.../web3/src/connect-button/demos/name.tsx | 7 +-
.../src/connect-button/demos/profileModal.tsx | 9 +-
.../web3/src/connect-button/demos/simple.tsx | 2 +-
.../web3/src/connect-button/demos/tooltip.tsx | 8 +-
.../web3/src/connect-button/demos/type.tsx | 15 +-
packages/web3/src/connect-button/index.md | 4 +-
.../web3/src/connect-button/index.zh-CN.md | 4 +-
.../src/connector/__tests__/basic.test.tsx | 27 +-
.../src/connector/__tests__/chains.test.tsx | 20 +-
.../src/connector/__tests__/name.test.tsx | 18 +-
packages/web3/src/connector/conector.tsx | 27 +-
packages/web3/src/connector/demos/chains.tsx | 2 +-
packages/web3/src/connector/index.md | 13 +-
packages/web3/src/connector/index.zh-CN.md | 27 +-
packages/web3/src/connector/interface.ts | 14 +-
packages/web3/src/hooks/demos/useAccount.tsx | 13 +
packages/web3/src/hooks/demos/useAccounts.tsx | 10 -
packages/web3/src/hooks/demos/useNFT.tsx | 5 +-
packages/web3/src/hooks/index.md | 4 +-
packages/web3/src/hooks/index.tsx | 2 +-
packages/web3/src/hooks/index.zh-CN.md | 4 +-
packages/web3/src/hooks/useAccount.ts | 9 +
packages/web3/src/hooks/useAccounts.ts | 10 -
packages/web3/src/hooks/useNFT.ts | 2 +-
packages/web3/src/hooks/useWallets.ts | 4 +-
.../src/web3-config-provider/demos/simple.tsx | 19 +-
43 files changed, 612 insertions(+), 279 deletions(-)
create mode 100644 packages/wagmi/src/wagmi-provider/methods/addNameToAccount.ts
delete mode 100644 packages/wagmi/src/wagmi-provider/methods/addNameToAccounts.ts
create mode 100644 packages/web3/src/hooks/demos/useAccount.tsx
delete mode 100644 packages/web3/src/hooks/demos/useAccounts.tsx
create mode 100644 packages/web3/src/hooks/useAccount.ts
delete mode 100644 packages/web3/src/hooks/useAccounts.ts
diff --git a/docs/guide/demos/guide.tsx b/docs/guide/demos/guide.tsx
index fd93e9f2a..330603392 100644
--- a/docs/guide/demos/guide.tsx
+++ b/docs/guide/demos/guide.tsx
@@ -27,7 +27,7 @@ const config = createConfig({
const App: React.FC = () => {
return (
-
+
diff --git a/packages/common/src/types.ts b/packages/common/src/types.ts
index a2d3d5a72..049b91667 100644
--- a/packages/common/src/types.ts
+++ b/packages/common/src/types.ts
@@ -38,13 +38,15 @@ export interface NFTMetadata {
}
export interface UniversalWeb3ProviderInterface {
- accounts?: Account[];
- wallets?: Wallet[];
- chains?: Chain[];
- currentChain?: Chain;
+ // current connected account
+ account?: Account;
+ // current connected chain
+ chain?: Chain;
- // connect and return conneted accounts
- requestAccounts?: (wallet?: string) => Promise;
+ availableWallets?: Wallet[];
+ availableChains?: Chain[];
+
+ connect?: (wallet?: Wallet) => Promise;
disconnect?: () => Promise;
switchChain?: (chain: Chain) => Promise;
@@ -141,14 +143,12 @@ export type Banlance = {
};
export interface ConnectorTriggerProps {
- address?: string;
+ account?: Account;
loading?: boolean;
onConnectClick?: () => void;
onDisconnectClick?: () => Promise;
onSwitchChain?: (chain: Chain) => Promise;
- name?: string;
- connected?: boolean;
- chains?: Chain[];
- currentChain?: Chain;
+ availableChains?: Chain[];
+ chain?: Chain;
banlance?: Banlance[] | Banlance;
}
diff --git a/packages/ethereum/src/ethereum-provider.tsx b/packages/ethereum/src/ethereum-provider.tsx
index f0bb17827..252fb31cd 100644
--- a/packages/ethereum/src/ethereum-provider.tsx
+++ b/packages/ethereum/src/ethereum-provider.tsx
@@ -37,7 +37,7 @@ export const EthereumProvider: React.FC = (props) => {
}, [provider]);
return (
-
+
{children}
);
diff --git a/packages/wagmi/src/wagmi-provider/config-provider.tsx b/packages/wagmi/src/wagmi-provider/config-provider.tsx
index ed150ccc9..a1b652144 100644
--- a/packages/wagmi/src/wagmi-provider/config-provider.tsx
+++ b/packages/wagmi/src/wagmi-provider/config-provider.tsx
@@ -8,20 +8,20 @@ import {
useSwitchNetwork,
type Chain as WagmiChain,
} from 'wagmi';
-import { addNameToAccounts, getNFTMetadata } from './methods';
+import { addNameToAccount, getNFTMetadata } from './methods';
import type { WalletFactory } from '../interface';
export interface AntDesignWeb3ConfigProviderProps {
assets?: (WalletFactory | Chain)[];
children?: React.ReactNode;
ens?: boolean;
- chains: WagmiChain[];
+ availableChains: WagmiChain[];
}
export const AntDesignWeb3ConfigProvider: React.FC = (props) => {
- const { children, assets, chains, ens } = props;
+ const { children, assets, availableChains, ens } = props;
const { address, isDisconnected } = useAccount();
- const [accounts, setAccounts] = React.useState([]);
+ const [account, setAccount] = React.useState();
const { connectors, connectAsync } = useConnect();
const { switchNetwork } = useSwitchNetwork();
const { chain } = useNetwork();
@@ -30,16 +30,14 @@ export const AntDesignWeb3ConfigProvider: React.FC {
if (!address || isDisconnected) {
- setAccounts([]);
+ setAccount(undefined);
return;
}
const updateAccounts = async () => {
- const as: Account[] = [
- {
- address,
- },
- ];
- setAccounts(ens ? await addNameToAccounts(as) : as);
+ const a = {
+ address,
+ };
+ setAccount(ens ? await addNameToAccount(a) : a);
};
updateAccounts();
}, [address, isDisconnected, chain, ens]);
@@ -57,7 +55,7 @@ export const AntDesignWeb3ConfigProvider: React.FC {
- return chains.map((item) => {
+ return availableChains.map((item) => {
const c = assets?.find((asset) => {
return (asset as Chain).id === item.id;
}) as Chain;
@@ -69,14 +67,14 @@ export const AntDesignWeb3ConfigProvider: React.FC {
if (!chain && currentChain) {
// not connected any chain, keep current chain
return;
}
- const currentWagmiChain = chain ?? chains[0];
+ const currentWagmiChain = chain ?? availableChains[0];
if (!currentWagmiChain) {
return;
}
@@ -89,26 +87,20 @@ export const AntDesignWeb3ConfigProvider: React.FC {
- const connector = connectors.find((item) => item.name === wallet);
- const { account } = await connectAsync({
+ availableChains={chainList}
+ chain={currentChain}
+ account={account}
+ availableWallets={wallets}
+ connect={async (wallet) => {
+ const connector = connectors.find((item) => item.name === wallet?.name);
+ await connectAsync({
connector,
chainId: currentChain?.id,
});
- const as = [
- {
- address: account,
- },
- ];
- return ens ? addNameToAccounts(as, chain?.id) : as;
}}
disconnect={async () => {
await disconnectAsync();
diff --git a/packages/wagmi/src/wagmi-provider/index.test.tsx b/packages/wagmi/src/wagmi-provider/index.test.tsx
index 5b3105fdf..3b8c42378 100644
--- a/packages/wagmi/src/wagmi-provider/index.test.tsx
+++ b/packages/wagmi/src/wagmi-provider/index.test.tsx
@@ -35,7 +35,7 @@ describe('WagmiWeb3ConfigProvider', () => {
});
const CustomButton: React.FC> = (props) => {
- const { currentChain, onSwitchChain } = props;
+ const { chain, onSwitchChain } = props;
return (
{
@@ -43,7 +43,7 @@ describe('WagmiWeb3ConfigProvider', () => {
}}
className="content"
>
- {currentChain?.name}
+ {chain?.name}
);
};
@@ -51,7 +51,7 @@ describe('WagmiWeb3ConfigProvider', () => {
const switchChain = vi.fn();
const App = () => (
-
+
@@ -81,7 +81,7 @@ describe('WagmiWeb3ConfigProvider', () => {
});
const CustomButton: React.FC> = (props) => {
- const { currentChain, onSwitchChain } = props;
+ const { chain, onSwitchChain } = props;
return (
{
@@ -89,7 +89,7 @@ describe('WagmiWeb3ConfigProvider', () => {
}}
className="content"
>
- {currentChain?.name}
+ {chain?.name}
);
};
@@ -109,7 +109,7 @@ describe('WagmiWeb3ConfigProvider', () => {
];
const App = () => (
-
+
diff --git a/packages/wagmi/src/wagmi-provider/index.tsx b/packages/wagmi/src/wagmi-provider/index.tsx
index 3e5e68468..f2ec8408b 100644
--- a/packages/wagmi/src/wagmi-provider/index.tsx
+++ b/packages/wagmi/src/wagmi-provider/index.tsx
@@ -14,7 +14,7 @@ export type WagmiWeb3ConfigProviderProps<
TWebSocketPublicClient extends WebSocketPublicClient = WebSocketPublicClient,
> = {
config: Config;
- chains?: WagmiChain[];
+ availableChains?: WagmiChain[];
assets?: (Chain | WalletFactory)[];
ens?: boolean;
};
@@ -25,7 +25,7 @@ export function WagmiWeb3ConfigProvider<
>({
children,
assets = [],
- chains = [mainnet],
+ availableChains = [mainnet],
ens,
...restProps
}: React.PropsWithChildren<
@@ -35,7 +35,7 @@ export function WagmiWeb3ConfigProvider<
{children}
diff --git a/packages/wagmi/src/wagmi-provider/methods/addNameToAccount.ts b/packages/wagmi/src/wagmi-provider/methods/addNameToAccount.ts
new file mode 100644
index 000000000..28a1ed040
--- /dev/null
+++ b/packages/wagmi/src/wagmi-provider/methods/addNameToAccount.ts
@@ -0,0 +1,13 @@
+import type { Account } from '@ant-design/web3-common';
+import { fetchEnsName } from '@wagmi/core';
+
+export async function addNameToAccount(account: Account, chainId?: number): Promise {
+ const name = await fetchEnsName({
+ address: account.address as `0x${string}`,
+ chainId,
+ });
+ return {
+ ...account,
+ name: name ?? undefined,
+ };
+}
diff --git a/packages/wagmi/src/wagmi-provider/methods/addNameToAccounts.ts b/packages/wagmi/src/wagmi-provider/methods/addNameToAccounts.ts
deleted file mode 100644
index e21a92570..000000000
--- a/packages/wagmi/src/wagmi-provider/methods/addNameToAccounts.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import type { Account } from '@ant-design/web3-common';
-import { fetchEnsName } from '@wagmi/core';
-
-export async function addNameToAccounts(accounts: Account[], chainId?: number): Promise {
- return Promise.all(
- accounts.map(async (account) => {
- const name = await fetchEnsName({
- address: account.address as `0x${string}`,
- chainId,
- });
- return {
- ...account,
- name: name ?? undefined,
- };
- }),
- );
-}
diff --git a/packages/wagmi/src/wagmi-provider/methods/index.test.ts b/packages/wagmi/src/wagmi-provider/methods/index.test.ts
index 62108cd15..ed9602907 100644
--- a/packages/wagmi/src/wagmi-provider/methods/index.test.ts
+++ b/packages/wagmi/src/wagmi-provider/methods/index.test.ts
@@ -1,4 +1,4 @@
-import { addNameToAccounts } from './index';
+import { addNameToAccount } from './index';
import { vi, describe, it, expect } from 'vitest';
vi.mock('@wagmi/core', () => {
@@ -13,23 +13,20 @@ vi.mock('@wagmi/core', () => {
});
describe('wagmi-provider/methods/index.ts', () => {
- it('addNameToAccounts', async () => {
- const accounts = await addNameToAccounts([
- {
- address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
- },
- {
- address: '0x21CDf0974d53a6e96eF05d7B324a9803735f0000',
- },
- ]);
- expect(accounts).toEqual([
- {
- address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
- name: 'wanderingearth,eth',
- },
- {
- address: '0x21CDf0974d53a6e96eF05d7B324a9803735f0000',
- },
- ]);
+ it('addNameToAccount', async () => {
+ const accountA = await addNameToAccount({
+ address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
+ });
+ expect(accountA).toEqual({
+ address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
+ name: 'wanderingearth,eth',
+ });
+
+ const accountB = await addNameToAccount({
+ address: '0x21CDf0974d53a6e96eF05d7B324a9803735f0000',
+ });
+ expect(accountB).toEqual({
+ address: '0x21CDf0974d53a6e96eF05d7B324a9803735f0000',
+ });
});
});
diff --git a/packages/wagmi/src/wagmi-provider/methods/index.ts b/packages/wagmi/src/wagmi-provider/methods/index.ts
index 40c936c51..d8badb356 100644
--- a/packages/wagmi/src/wagmi-provider/methods/index.ts
+++ b/packages/wagmi/src/wagmi-provider/methods/index.ts
@@ -1,2 +1,2 @@
-export * from './addNameToAccounts';
+export * from './addNameToAccount';
export * from './getNFTMetadata';
diff --git a/packages/web3/src/connect-button/__tests__/__snapshots__/menu.test.tsx.snap b/packages/web3/src/connect-button/__tests__/__snapshots__/menu.test.tsx.snap
index 7cae3e8e8..27e151c5a 100644
--- a/packages/web3/src/connect-button/__tests__/__snapshots__/menu.test.tsx.snap
+++ b/packages/web3/src/connect-button/__tests__/__snapshots__/menu.test.tsx.snap
@@ -124,6 +124,140 @@ exports[`ConnectButton > Should insert menu items before default menu items when
+
+
+
+
+
@@ -183,6 +317,75 @@ exports[`ConnectButton > Should override menu items when pass items into actions
+
+
+
+
+
@@ -282,3 +485,113 @@ exports[`ConnectButton > Should show menu when hover button 1`] = `
+
+
+
+
+