Skip to content

Commit b5db8f6

Browse files
authored
feat: Remove metaMask support on tg (#269)
* feat: Remove debug code * fix: Fix type error * feat: Remove unused packages * feat: Update demo * feat: Export a method to check whether a provider exists * refactor: Use uniform method to get wallet behavior on evm & non-evm wallets * feat: Remove metaMask support on tg
1 parent 382e89e commit b5db8f6

22 files changed

Lines changed: 97 additions & 80 deletions

File tree

.changeset/khaki-swans-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@node-real/walletkit': patch
3+
---
4+
5+
Remove metaMask support on tg

packages/walletkit/__dev__/App.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ import {
2121
} from '@/solana/index';
2222
import { bsc, mainnet, dfk } from 'viem/chains';
2323
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
24-
import { useAccount, useDisconnect } from 'wagmi';
24+
import { useAccount, useConnect, useConnectors, useDisconnect } from 'wagmi';
2525
import { defaultTronConfig, tronLink, useTronWallet } from '@/tron/index';
2626
import { uxuyWallet } from '@/evm/wallets/uxuyWallet';
2727
import { useEvmSwitchChain } from '@/evm/hooks/useEvmSwitchChain';
2828
import { codexFieldWallet } from '@/evm/wallets/codexFieldWallet';
2929
import { SwitchNetworkModal } from '@/core/modals/SwitchNetworkModal';
30+
import { useCallback } from 'react';
3031

3132
new VConsole();
3233

@@ -96,9 +97,12 @@ function ConnectButton() {
9697
const { address: tronAddress, disconnect: tronDisconnect } = useTronWallet();
9798
const { switchChain } = useEvmSwitchChain();
9899

100+
const connectBinanceWallet = useBinanceWallet();
101+
99102
return (
100103
<>
101104
<div>
105+
<button onClick={() => connectBinanceWallet()}>connect binance wallet</button>
102106
<button
103107
onClick={() =>
104108
onOpen({
@@ -148,3 +152,28 @@ function ConnectButton() {
148152
</>
149153
);
150154
}
155+
156+
export function useBinanceWallet() {
157+
const connectors = useConnectors();
158+
const { connect } = useConnect();
159+
const { disconnectAsync, reset } = useDisconnect();
160+
161+
return useCallback(async () => {
162+
const connector = connectors?.find((connector) => connector.id === binanceWallet().id);
163+
if (connector) {
164+
reset();
165+
await disconnectAsync();
166+
167+
setTimeout(() => {
168+
try {
169+
connect({
170+
connector,
171+
chainId: 56,
172+
});
173+
} catch (err) {
174+
console.error(err);
175+
}
176+
}, 1000);
177+
}
178+
}, [connect, connectors, disconnectAsync, reset]);
179+
}

packages/walletkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"wagmi": "^2"
5555
},
5656
"dependencies": {
57-
"@binance/w3w-wagmi-connector-v2": "^1.2.3",
57+
"@binance/w3w-wagmi-connector-v2": "^1.2.5",
5858
"@metamask/jazzicon": "^2",
5959
"@solana/wallet-adapter-react": "^0",
6060
"@solana/wallet-adapter-wallets": "^0",

packages/walletkit/src/core/utils/common.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isAndroid, isIOS, isPC, isTMA } from '../base/utils/mobile';
2-
import { PlatformType } from '../configs/types';
2+
import { BaseBehavior, BaseWallet, PlatformType } from '../configs/types';
33

44
export function mergeList(list1: any[] = [], list2: any[] = []) {
55
const result: any[] = [...list1];
@@ -60,3 +60,11 @@ export function getPlatform(): PlatformType {
6060
}
6161
return 'browser-pc';
6262
}
63+
64+
export function getWalletBehaviorOnPlatform<T extends BaseBehavior = BaseBehavior>(
65+
wallet: BaseWallet,
66+
) {
67+
const platform = getPlatform();
68+
const behavior = wallet.behaviors.find((e) => e.platforms.includes(platform));
69+
return behavior as T | undefined;
70+
}

packages/walletkit/src/evm/components/EvmConnectingView/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { useWalletConnector } from '@/evm/hooks/useWalletConnector';
66
import { useCallback } from 'react';
77
import { useConnectingStatus } from '@/evm/hooks/useConnectingStatus';
88
import { useAccount } from 'wagmi';
9-
import { getEvmWalletPlatformBehavior } from '@/evm/utils/getEvmWalletPlatformBehavior';
9+
import { getWalletBehaviorOnPlatform } from '@/core/utils/common';
10+
import { EvmWalletBehavior } from '@/evm/wallets';
1011

1112
export function EvmConnectingView() {
1213
const { selectedWallet } = useWalletKit();
1314
const isConnected = useIsConnected();
1415
const selectedConnector = useWalletConnector(selectedWallet.id);
1516

16-
const behavior = getEvmWalletPlatformBehavior(selectedWallet);
17+
const behavior = getWalletBehaviorOnPlatform<EvmWalletBehavior>(selectedWallet);
1718
const { connect, status, setStatus } = useConnectingStatus();
1819
const { address } = useAccount();
1920

packages/walletkit/src/evm/components/EvmQRCodeView/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { useWalletKit } from '@/core/providers/WalletKitProvider/context';
33
import { useIsConnected } from '@/evm/hooks/useIsConnected';
44
import { useWalletConnectUri } from '@/evm/hooks/useWalletConnectUri';
55
import { useWalletConnectModal } from '@/evm/hooks/useWalletConnectModal';
6-
import { isWalletConnect, metaMask } from '@/evm/wallets';
6+
import { EvmWalletBehavior, isWalletConnect, metaMask } from '@/evm/wallets';
77
import { useMetaMaskUri } from '@/evm/hooks/userMetaMaskUri';
88
import { useMemo } from 'react';
99
import { useAccount } from 'wagmi';
10-
import { getEvmWalletPlatformBehavior } from '@/evm/utils/getEvmWalletPlatformBehavior';
10+
import { getWalletBehaviorOnPlatform } from '@/core/utils/common';
1111

1212
export function EvmQRCodeView() {
1313
const { selectedWallet } = useWalletKit();
@@ -24,7 +24,7 @@ export function EvmQRCodeView() {
2424
return metaMaskUri;
2525
}
2626

27-
const behavior = getEvmWalletPlatformBehavior(selectedWallet);
27+
const behavior = getWalletBehaviorOnPlatform<EvmWalletBehavior>(selectedWallet);
2828
return wcUri ? behavior?.getUri?.(wcUri) : wcUri;
2929
}, [metaMaskUri, selectedWallet, wcUri]);
3030

packages/walletkit/src/evm/components/EvmUriConnectingView/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ import { CONNECT_STATUS } from '@/core/constants';
22
import { TemplateConnectingView } from '@/core/modals/ConnectModal/TemplateConnectingView';
33
import { useWalletKit } from '@/core/providers/WalletKitProvider/context';
44
import { useIsConnected } from '@/evm/hooks/useIsConnected';
5-
import { metaMask } from '@/evm/wallets';
6-
import { openLink } from '@/core/utils/common';
5+
import { EvmWalletBehavior, metaMask } from '@/evm/wallets';
6+
import { getWalletBehaviorOnPlatform, openLink } from '@/core/utils/common';
77
import { useWalletConnectUri } from '@/evm/hooks/useWalletConnectUri';
88
import { useConnectingStatus } from '@/evm/hooks/useConnectingStatus';
99
import { useMetaMaskUri } from '@/evm/hooks/userMetaMaskUri';
1010
import { useEffect, useMemo, useRef } from 'react';
1111
import { useAccount } from 'wagmi';
12-
import { getEvmWalletPlatformBehavior } from '@/evm/utils/getEvmWalletPlatformBehavior';
1312

1413
export function EvmUriConnectingView() {
1514
const { selectedWallet } = useWalletKit();
@@ -30,7 +29,7 @@ export function EvmUriConnectingView() {
3029
});
3130

3231
const qrCodeUri = useMemo(() => {
33-
const behavior = getEvmWalletPlatformBehavior(selectedWallet);
32+
const behavior = getWalletBehaviorOnPlatform<EvmWalletBehavior>(selectedWallet);
3433

3534
return selectedWallet.id === metaMask().id
3635
? metaMaskUri

packages/walletkit/src/evm/components/SetEvmWalletClickRef/index.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import { useConnectModal } from '@/core/modals/ConnectModal/context';
44
import { ViewRoutes } from '@/core/providers/RouteProvider';
55
import { useRouter } from '@/core/providers/RouteProvider/context';
66
import { useWalletKit } from '@/core/providers/WalletKitProvider/context';
7-
import { openLink } from '@/core/utils/common';
7+
import { getWalletBehaviorOnPlatform, openLink } from '@/core/utils/common';
88
import { useEvmConnect } from '@/evm/hooks/useEvmConnect';
99
import { useWalletConnectModal } from '@/evm/hooks/useWalletConnectModal';
10-
import { getEvmWalletPlatformBehavior } from '@/evm/utils/getEvmWalletPlatformBehavior';
11-
import { EvmWallet } from '@/evm/wallets';
10+
import { EvmWallet, EvmWalletBehavior } from '@/evm/wallets';
1211
import { useRef } from 'react';
1312
import { useConnectors, useDisconnect } from 'wagmi';
1413

@@ -33,7 +32,7 @@ export function SetEvmWalletClickRef(props: SetEvmWalletClickRefProps) {
3332
clickRef.current = (walletId: string, e: React.MouseEvent<Element, MouseEvent>) => {
3433
const wallet = evmConfig!.wallets.find((item) => item.id === walletId)! as EvmWallet;
3534
const connector = connectors.find((item) => item.id === walletId)!;
36-
const behavior = getEvmWalletPlatformBehavior(wallet);
35+
const behavior = getWalletBehaviorOnPlatform<EvmWalletBehavior>(wallet);
3736

3837
const pass = options.onClickWallet?.(wallet, e);
3938
if (pass === false) return;
@@ -103,8 +102,7 @@ export function SetEvmWalletClickRef(props: SetEvmWalletClickRefProps) {
103102
if (isTMA() && isMobile()) {
104103
handleJumping();
105104
} else {
106-
const delay = behavior?.connectType === 'sdk' ? 0 : 300;
107-
timerRef.current = setTimeout(handleJumping, delay);
105+
timerRef.current = setTimeout(handleJumping, 600);
108106
}
109107
};
110108

packages/walletkit/src/evm/hooks/useConnectingStatus.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { useEvmConnect } from './useEvmConnect';
55
import { EventEmitter } from '@/core/utils/eventEmitter';
66
import { Config } from 'wagmi';
77
import { ConnectData } from 'wagmi/query';
8-
import { getEvmWalletPlatformBehavior } from '../utils/getEvmWalletPlatformBehavior';
8+
import { getWalletBehaviorOnPlatform } from '@/core/utils/common';
9+
import { EvmWalletBehavior } from '../wallets';
910

1011
interface UseConnectingStatusProps {
1112
initialStatus?: CONNECT_STATUS;
@@ -16,7 +17,7 @@ export function useConnectingStatus(props: UseConnectingStatusProps = {}) {
1617

1718
const { selectedWallet, evmConfig, options, action } = useWalletKit();
1819

19-
const behavior = getEvmWalletPlatformBehavior(selectedWallet);
20+
const behavior = getWalletBehaviorOnPlatform<EvmWalletBehavior>(selectedWallet);
2021

2122
const defaultStatus = behavior?.isInstalled?.()
2223
? CONNECT_STATUS.CONNECTING

packages/walletkit/src/evm/utils/defaultEvmConfig.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
binanceWallet,
55
coinbaseWallet,
66
EvmWallet,
7+
EvmWalletBehavior,
78
isWalletConnect,
89
metaMask,
910
walletConnect,
@@ -13,7 +14,7 @@ import { setEvmGlobalData } from '../globalData';
1314
import { codexFieldWallet } from '../wallets/codexFieldWallet';
1415
import { ChainDisplayConfig } from '@/evm/chains/types';
1516
import { getChainDisplayConfigs } from '../chains';
16-
import { getEvmWalletPlatformBehavior } from './getEvmWalletPlatformBehavior';
17+
import { getWalletBehaviorOnPlatform } from '@/core/utils/common';
1718

1819
interface CustomizedEvmConfig
1920
extends Omit<CreateConfigParameters, 'chains' | 'connectors' | 'multiInjectedProviderDiscovery'> {
@@ -99,7 +100,7 @@ function getCreateConnectorFns(wallets: EvmWallet[]) {
99100
localStorage.removeItem(`wagmi.${w.id}.shimDisconnect`);
100101
}
101102

102-
const behavior = getEvmWalletPlatformBehavior(w);
103+
const behavior = getWalletBehaviorOnPlatform<EvmWalletBehavior>(w);
103104
if (behavior?.getCreateConnectorFn) {
104105
return behavior.getCreateConnectorFn();
105106
}

0 commit comments

Comments
 (0)