Skip to content

Commit

Permalink
refactor: remove accounts, use account & rename chains and wallets (#137
Browse files Browse the repository at this point in the history
)

* refactor: rename accounts and chains

* chore: update docs

* fix: doc build failed

---------

Co-authored-by: yutingzhao1991 <[email protected]>
  • Loading branch information
yutingzhao1991 and yutingzhao1991 authored Nov 29, 2023
1 parent 2b6936d commit a81b4c9
Show file tree
Hide file tree
Showing 43 changed files with 612 additions and 279 deletions.
2 changes: 1 addition & 1 deletion docs/guide/demos/guide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const config = createConfig({

const App: React.FC = () => {
return (
<WagmiWeb3ConfigProvider chains={chains} config={config}>
<WagmiWeb3ConfigProvider availableChains={chains} config={config}>
<Connector>
<ConnectButton />
</Connector>
Expand Down
22 changes: 11 additions & 11 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Account[]>;
availableWallets?: Wallet[];
availableChains?: Chain[];

connect?: (wallet?: Wallet) => Promise<void>;
disconnect?: () => Promise<void>;
switchChain?: (chain: Chain) => Promise<void>;

Expand Down Expand Up @@ -141,14 +143,12 @@ export type Banlance = {
};

export interface ConnectorTriggerProps {
address?: string;
account?: Account;
loading?: boolean;
onConnectClick?: () => void;
onDisconnectClick?: () => Promise<void>;
onSwitchChain?: (chain: Chain) => Promise<void>;
name?: string;
connected?: boolean;
chains?: Chain[];
currentChain?: Chain;
availableChains?: Chain[];
chain?: Chain;
banlance?: Banlance[] | Banlance;
}
2 changes: 1 addition & 1 deletion packages/ethereum/src/ethereum-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const EthereumProvider: React.FC<Web3ProviderProps> = (props) => {
}, [provider]);

return (
<Web3ConfigProvider accounts={accounts} wallets={wallets} {...provider}>
<Web3ConfigProvider account={accounts[0]} availableWallets={wallets} {...provider}>
{children}
</Web3ConfigProvider>
);
Expand Down
48 changes: 20 additions & 28 deletions packages/wagmi/src/wagmi-provider/config-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<AntDesignWeb3ConfigProviderProps> = (props) => {
const { children, assets, chains, ens } = props;
const { children, assets, availableChains, ens } = props;
const { address, isDisconnected } = useAccount();
const [accounts, setAccounts] = React.useState<Account[]>([]);
const [account, setAccount] = React.useState<Account | undefined>();
const { connectors, connectAsync } = useConnect();
const { switchNetwork } = useSwitchNetwork();
const { chain } = useNetwork();
Expand All @@ -30,16 +30,14 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr

React.useEffect(() => {
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]);
Expand All @@ -57,7 +55,7 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr
}, [connectors, assets]);

const chainList: Chain[] = React.useMemo(() => {
return chains.map((item) => {
return availableChains.map((item) => {
const c = assets?.find((asset) => {
return (asset as Chain).id === item.id;
}) as Chain;
Expand All @@ -69,14 +67,14 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr
}
return c;
});
}, [chains, assets]);
}, [availableChains, assets]);

React.useEffect(() => {
if (!chain && currentChain) {
// not connected any chain, keep current chain
return;
}
const currentWagmiChain = chain ?? chains[0];
const currentWagmiChain = chain ?? availableChains[0];
if (!currentWagmiChain) {
return;
}
Expand All @@ -89,26 +87,20 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr
}
setCurrentChain(c);
return;
}, [chain, assets, chains, currentChain]);
}, [chain, assets, availableChains, currentChain]);

return (
<Web3ConfigProvider
chains={chainList}
currentChain={currentChain}
accounts={accounts}
wallets={wallets}
requestAccounts={async (wallet) => {
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();
Expand Down
12 changes: 6 additions & 6 deletions packages/wagmi/src/wagmi-provider/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ describe('WagmiWeb3ConfigProvider', () => {
});

const CustomButton: React.FC<React.PropsWithChildren<ConnectorTriggerProps>> = (props) => {
const { currentChain, onSwitchChain } = props;
const { chain, onSwitchChain } = props;
return (
<div
onClick={() => {
onSwitchChain?.(Mainnet);
}}
className="content"
>
{currentChain?.name}
{chain?.name}
</div>
);
};

const switchChain = vi.fn();

const App = () => (
<WagmiWeb3ConfigProvider chains={chains} config={config}>
<WagmiWeb3ConfigProvider availableChains={chains} config={config}>
<Connector switchChain={switchChain}>
<CustomButton />
</Connector>
Expand Down Expand Up @@ -81,15 +81,15 @@ describe('WagmiWeb3ConfigProvider', () => {
});

const CustomButton: React.FC<React.PropsWithChildren<ConnectorTriggerProps>> = (props) => {
const { currentChain, onSwitchChain } = props;
const { chain, onSwitchChain } = props;
return (
<div
onClick={() => {
onSwitchChain?.(chains[0]);
}}
className="content"
>
{currentChain?.name}
{chain?.name}
</div>
);
};
Expand All @@ -109,7 +109,7 @@ describe('WagmiWeb3ConfigProvider', () => {
];

const App = () => (
<WagmiWeb3ConfigProvider assets={assets} chains={chains} config={config}>
<WagmiWeb3ConfigProvider assets={assets} availableChains={chains} config={config}>
<Connector switchChain={switchChain}>
<CustomButton />
</Connector>
Expand Down
6 changes: 3 additions & 3 deletions packages/wagmi/src/wagmi-provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type WagmiWeb3ConfigProviderProps<
TWebSocketPublicClient extends WebSocketPublicClient = WebSocketPublicClient,
> = {
config: Config<TPublicClient, TWebSocketPublicClient>;
chains?: WagmiChain[];
availableChains?: WagmiChain[];
assets?: (Chain | WalletFactory)[];
ens?: boolean;
};
Expand All @@ -25,7 +25,7 @@ export function WagmiWeb3ConfigProvider<
>({
children,
assets = [],
chains = [mainnet],
availableChains = [mainnet],
ens,
...restProps
}: React.PropsWithChildren<
Expand All @@ -35,7 +35,7 @@ export function WagmiWeb3ConfigProvider<
<WagmiConfig {...restProps}>
<AntDesignWeb3ConfigProvider
assets={[...assets, MetaMask, WallectConnect, Mainnet, Polygon, BSC, Goerli]}
chains={chains}
availableChains={availableChains}
ens={ens}
>
{children}
Expand Down
13 changes: 13 additions & 0 deletions packages/wagmi/src/wagmi-provider/methods/addNameToAccount.ts
Original file line number Diff line number Diff line change
@@ -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<Account> {
const name = await fetchEnsName({
address: account.address as `0x${string}`,
chainId,
});
return {
...account,
name: name ?? undefined,
};
}
17 changes: 0 additions & 17 deletions packages/wagmi/src/wagmi-provider/methods/addNameToAccounts.ts

This file was deleted.

35 changes: 16 additions & 19 deletions packages/wagmi/src/wagmi-provider/methods/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addNameToAccounts } from './index';
import { addNameToAccount } from './index';
import { vi, describe, it, expect } from 'vitest';

vi.mock('@wagmi/core', () => {
Expand All @@ -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',
});
});
});
2 changes: 1 addition & 1 deletion packages/wagmi/src/wagmi-provider/methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './addNameToAccounts';
export * from './addNameToAccount';
export * from './getNFTMetadata';
Loading

0 comments on commit a81b4c9

Please sign in to comment.