Skip to content

Commit c32f8e2

Browse files
yutingzhao1991tingzhao.ytz
andauthored
feat: support useWalletConnectOfficialModal for wagmi provider and add customQrCodePanel for wallet (#1151)
* fix: support useWalletConnectOfficialModal * test: add useWalletConnectOfficialModal test case * chore: add changelog * fix: tslint error * docs: update en doc * fix: lint issue * fix: for test bad case * chore: fix test cov --------- Co-authored-by: tingzhao.ytz <[email protected]>
1 parent 613b265 commit c32f8e2

File tree

17 files changed

+214
-40
lines changed

17 files changed

+214
-40
lines changed

.changeset/gorgeous-shoes-wash.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@ant-design/web3-common': minor
3+
'@ant-design/web3-wagmi': minor
4+
'@ant-design/web3': minor
5+
---
6+
7+
feat: support useWalletConnectOfficialModal for wagmi provider and add customQrCodePanel for wallet

packages/common/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export interface Wallet extends WalletMetadata {
126126
hasWalletReady?: () => Promise<boolean>;
127127
hasExtensionInstalled?: () => Promise<boolean>;
128128
getQrCode?: () => Promise<{ uri: string }>;
129+
customQrCodePanel?: boolean;
129130
}
130131

131132
/**

packages/wagmi/src/interface.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ export interface WalletUseInWagmiAdapter extends Wallet {
1414

1515
export type EthereumWallet = (metadata?: Partial<WalletMetadata>) => WalletFactory;
1616

17+
export interface CreateWalletOptions {
18+
useWalletConnectOfficialModal?: boolean;
19+
}
20+
1721
export interface WalletFactory {
1822
name?: string;
1923
connectors: Connector['name'][];
20-
create: (connector?: readonly Connector[]) => WalletUseInWagmiAdapter;
24+
create: (
25+
connector?: readonly Connector[],
26+
options?: CreateWalletOptions,
27+
) => WalletUseInWagmiAdapter;
2128
createWagmiConnector?: () => CreateConnectorFn;
2229
}
2330

packages/wagmi/src/wagmi-provider/__tests__/basic.test.tsx

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import {
88
Polygon,
99
TokenPocket,
1010
WagmiWeb3ConfigProvider,
11+
WalletConnect,
1112
WalletFactory,
1213
type WalletConnectOptions,
1314
} from '@ant-design/web3-wagmi';
1415
import { fireEvent, render } from '@testing-library/react';
1516
import { describe, expect, it, vi } from 'vitest';
1617
import { http } from 'wagmi';
1718
import { base } from 'wagmi/chains';
19+
import { walletConnect } from 'wagmi/connectors';
1820

1921
describe('WagmiWeb3ConfigProvider', () => {
2022
it('mount correctly', () => {
@@ -213,14 +215,23 @@ describe('WagmiWeb3ConfigProvider', () => {
213215
const { availableWallets } = useProvider();
214216
return (
215217
<div className="wallets-qrcode">
216-
{availableWallets?.map((item) => (item.getQrCode ? 'qrcode' : 'noqrcode')).join(',')}
218+
{availableWallets
219+
?.map((item) => `${typeof item.getQrCode}-${item.customQrCodePanel}`)
220+
.join(',')}
217221
</div>
218222
);
219223
};
220224

221225
const App = () => (
222226
<WagmiWeb3ConfigProvider
223-
wallets={[MetaMask(), TokenPocket(), OkxWallet()]}
227+
wallets={[
228+
MetaMask(),
229+
TokenPocket(),
230+
OkxWallet(),
231+
WalletConnect({
232+
useWalletConnectOfficialModal: true,
233+
}),
234+
]}
224235
chains={[Polygon, Goerli, Mainnet]}
225236
walletConnect={{
226237
projectId: 'test',
@@ -235,7 +246,44 @@ describe('WagmiWeb3ConfigProvider', () => {
235246
</WagmiWeb3ConfigProvider>
236247
);
237248
const { baseElement } = render(<App />);
238-
expect(baseElement.querySelector('.wallets-qrcode')?.textContent).toBe('qrcode,qrcode,qrcode');
249+
expect(baseElement.querySelector('.wallets-qrcode')?.textContent).toBe(
250+
'function-undefined,function-undefined,function-undefined,function-true',
251+
);
252+
});
253+
254+
it('wallet connect with customQrCodePanel', () => {
255+
const CustomConnector: React.FC = () => {
256+
const { availableWallets } = useProvider();
257+
return (
258+
<div className="wallets-qrcode">
259+
{availableWallets
260+
?.map((item) => `${typeof item.getQrCode}-${item.customQrCodePanel}`)
261+
.join(',')}
262+
</div>
263+
);
264+
};
265+
266+
const App = () => (
267+
<WagmiWeb3ConfigProvider
268+
wallets={[MetaMask(), TokenPocket(), OkxWallet(), WalletConnect()]}
269+
chains={[Polygon, Goerli, Mainnet]}
270+
walletConnect={{
271+
projectId: 'test',
272+
useWalletConnectOfficialModal: true,
273+
}}
274+
transports={{
275+
[Goerli.id]: http(),
276+
[Mainnet.id]: http(),
277+
[Polygon.id]: http(),
278+
}}
279+
>
280+
<CustomConnector />
281+
</WagmiWeb3ConfigProvider>
282+
);
283+
const { baseElement } = render(<App />);
284+
expect(baseElement.querySelector('.wallets-qrcode')?.textContent).toBe(
285+
'function-true,function-true,function-true,function-true',
286+
);
239287
});
240288

241289
it('refresh walletConnect', () => {

packages/wagmi/src/wagmi-provider/config-provider.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface AntDesignWeb3ConfigProviderProps {
3333
balance?: boolean;
3434
eip6963?: EIP6963Config;
3535
wagimConfig: WagmiConfig;
36+
useWalletConnectOfficialModal?: boolean;
3637
}
3738

3839
export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderProps> = (props) => {
@@ -45,6 +46,7 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr
4546
locale,
4647
eip6963,
4748
wagimConfig,
49+
useWalletConnectOfficialModal,
4850
} = props;
4951
const { address, isDisconnected, chain } = useAccount();
5052
const config = useConfig();
@@ -93,7 +95,11 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr
9395
)
9496
) {
9597
// not config wallet and find the wallet in connectors, auto add it
96-
autoAddEIP6963Wallets.push(EIP6963Wallet().create([connector]));
98+
autoAddEIP6963Wallets.push(
99+
EIP6963Wallet().create([connector], {
100+
useWalletConnectOfficialModal,
101+
}),
102+
);
97103
}
98104
// Do not need check eip6963 wallet
99105
return;
@@ -127,7 +133,9 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr
127133
);
128134
return null;
129135
}
130-
return factory.create(connectors);
136+
return factory.create(connectors, {
137+
useWalletConnectOfficialModal,
138+
});
131139
})
132140
.filter((item) => item !== null) as Wallet[];
133141

packages/wagmi/src/wagmi-provider/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ export function WagmiWeb3ConfigProvider({
138138
balance={balance}
139139
eip6963={eip6963}
140140
wagimConfig={wagmiConfig}
141+
useWalletConnectOfficialModal={
142+
typeof walletConnect === 'object' && walletConnect?.useWalletConnectOfficialModal
143+
}
141144
>
142145
{children}
143146
</AntDesignWeb3ConfigProvider>

packages/wagmi/src/wallets/__tests__/mobile-wallet.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ describe('MobileWallet', () => {
2626
const wallet = MobileWallet({
2727
useWalletConnectOfficialModal: true,
2828
}).create();
29-
expect(wallet.getQrCode).toBe(undefined);
29+
expect(wallet.getQrCode).toBeTruthy();
30+
expect(wallet.customQrCodePanel).toBeTruthy();
3031
});
3132

3233
it('get qr code', async () => {

packages/wagmi/src/wallets/__tests__/wallet-connect.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ describe('WalletConnect', async () => {
5656
const wallet = WalletConnect({
5757
useWalletConnectOfficialModal: true,
5858
}).create();
59-
expect(wallet.getQrCode).toBe(undefined);
59+
expect(wallet.getQrCode).toBeTruthy();
60+
expect(wallet.customQrCodePanel).toBeTruthy();
6061
});
6162

6263
it('get qr code', async () => {

packages/wagmi/src/wallets/universal-wallet.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { WalletMetadata } from '@ant-design/web3-common';
22
import type { Connector, CreateConnectorFn } from 'wagmi';
33

4-
import type { WalletFactory, WalletUseInWagmiAdapter } from '../interface';
4+
import type { CreateWalletOptions, WalletFactory, WalletUseInWagmiAdapter } from '../interface';
55

66
export class UniversalWallet implements WalletFactory {
77
public name?: string;
@@ -23,7 +23,10 @@ export class UniversalWallet implements WalletFactory {
2323
this.connectors.push('WalletConnect');
2424
}
2525
}
26-
create = (connectors?: readonly Connector[]): WalletUseInWagmiAdapter => {
26+
create = (
27+
connectors?: readonly Connector[],
28+
options?: CreateWalletOptions,
29+
): WalletUseInWagmiAdapter => {
2730
const walletConnector = connectors?.find((item) => item.name === 'WalletConnect');
2831
const injectedConnector = connectors?.find((item) => item.name === this.wallet.name);
2932

@@ -53,8 +56,8 @@ export class UniversalWallet implements WalletFactory {
5356

5457
return {
5558
...this.wallet,
56-
getWagmiConnector: async (options) => {
57-
if (options?.connectType === 'qrCode') {
59+
getWagmiConnector: async (connectOptions) => {
60+
if (connectOptions?.connectType === 'qrCode') {
5861
return walletConnector;
5962
}
6063
if (await hasExtensionInstalled()) {
@@ -69,6 +72,7 @@ export class UniversalWallet implements WalletFactory {
6972
const installed = await hasExtensionInstalled();
7073
return !!(installed || walletConnector);
7174
},
75+
customQrCodePanel: options?.useWalletConnectOfficialModal,
7276
getQrCode: walletConnector ? getQrCode : undefined,
7377
};
7478
};

packages/wagmi/src/wallets/wallet-connect.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const WalletConnect: EthereumWalletConnect = (metadata) => {
1414
const { useWalletConnectOfficialModal = false, ...rest } = metadata || {};
1515
return {
1616
connectors: ['WalletConnect'],
17-
create: (connectors?: readonly Connector[]): WalletUseInWagmiAdapter => {
17+
create: (connectors?: readonly Connector[], options = {}): WalletUseInWagmiAdapter => {
1818
let qrCodeUri: string | undefined = undefined;
1919
const getQrCode = async () => {
2020
const provider = await connectors?.[0]?.getProvider();
@@ -41,7 +41,8 @@ export const WalletConnect: EthereumWalletConnect = (metadata) => {
4141
hasWalletReady: async () => {
4242
return true;
4343
},
44-
getQrCode: useWalletConnectOfficialModal ? undefined : getQrCode,
44+
getQrCode: getQrCode,
45+
customQrCodePanel: useWalletConnectOfficialModal || options.useWalletConnectOfficialModal,
4546
...rest,
4647
};
4748
},

0 commit comments

Comments
 (0)