Skip to content

Commit

Permalink
fix: small fix browser wallet logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Argeare5 committed Feb 13, 2023
1 parent ab059fe commit 88cb87a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
14 changes: 8 additions & 6 deletions src/utils/wallets/getBrowserWalletLabelAndIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { wallets } from './wallets';
import { defaultWallet } from './wallets/defaultWallet';

export function getBrowserWalletLabelAndIcon() {
const defaultBrowserWallet = {
label: defaultWallet.label,
icon: defaultWallet.icon,
};

if (typeof window !== 'undefined') {
if (!!window.ethereum) {
const userBrowserWallets = wallets.filter(
Expand All @@ -10,20 +15,17 @@ export function getBrowserWalletLabelAndIcon() {
(wallet) => !!window.ethereum[wallet.identityFlag]
);
if (userBrowserWallets.length > 1 || userBrowserWallets.length === 0) {
return {
label: defaultWallet.label,
icon: defaultWallet.icon,
};
return defaultBrowserWallet;
} else {
return {
label: userBrowserWallets[0].label,
icon: userBrowserWallets[0].icon,
};
}
} else {
return undefined;
return defaultBrowserWallet;
}
} else {
return undefined;
return defaultBrowserWallet;
}
}
8 changes: 2 additions & 6 deletions src/utils/wallets/wallets/defaultWallet.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
export const defaultWallet = {
label: 'Browser wallet',
icon: `<svg width="25" height="25" viewBox="0 0 25 25" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="0.5" y="0.5" width="24" height="24" rx="2.5" stroke="#1B2030"/>
<path d="M0 7.14282H25" stroke="#1B2030"/>
<rect x="6.5" y="11.5" width="12.0952" height="8.52381" stroke="#1B2030"/>
<rect x="6.25" y="10.25" width="11.5" height="0.5" stroke="#1B2030" stroke-width="0.5"/>
<circle cx="16" cy="16" r="0.5" stroke="#1B2030"/>
icon: `<svg width="100%" height="100%" viewBox="0 0 27 27" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1 8.14286H26M7 12H20.0952V21.5238H7V12ZM7 12H19V11H7V12ZM20.5 4L23 6.5M23 4L20.5 6.5M15 6H13M4 26H23C24.6569 26 26 24.6569 26 23V4C26 2.34315 24.6569 1 23 1H4C2.34315 1 1 2.34315 1 4V23C1 24.6569 2.34315 26 4 26ZM18 17C18 17.5523 17.5523 18 17 18C16.4477 18 16 17.5523 16 17C16 16.4477 16.4477 16 17 16C17.5523 16 18 16.4477 18 17ZM16.5 4H19V6.5H16.5V4Z" stroke="#1B2030"/>
</svg>
`,
};
37 changes: 21 additions & 16 deletions src/web3/store/walletSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface Wallet {
}

export type IWalletSlice = {
isContractWalletRecord: Record<string, boolean>
isContractWalletRecord: Record<string, boolean>;
activeWallet?: Wallet;
getActiveAddress: () => string | undefined;
connectWallet: (walletType: WalletType, chainID?: number) => Promise<void>;
Expand All @@ -43,7 +43,7 @@ export type IWalletSlice = {
setConnectors: (connectors: Connector[]) => void;
_impersonatedAddress?: string;
setImpersonatedAddress: (address: string) => void;
checkIsContractWallet: (wallet: Omit<Wallet, 'signer'>) => Promise<boolean>
checkIsContractWallet: (wallet: Omit<Wallet, 'signer'>) => Promise<boolean>;
};

export function createWalletSlice({
Expand Down Expand Up @@ -132,10 +132,13 @@ export function createWalletSlice({
}
} catch (e) {
if (e instanceof Error) {
let errorMessage = e.message ? e.message.toString() : e.toString();
if (errorMessage === 'MetaMask not installed') {
errorMessage = 'Browser wallet not installed';
}

set({
walletConnectionError: e.message
? e.message.toString()
: e.toString(),
walletConnectionError: errorMessage,
});
}
console.error(e);
Expand Down Expand Up @@ -166,19 +169,21 @@ export function createWalletSlice({
clearWalletConnectLocalStorage();
},
checkIsContractWallet: async (wallet: Omit<Wallet, 'signer'>) => {
const account = wallet.accounts[0]
const walletRecord = get().isContractWalletRecord[account]
const account = wallet.accounts[0];
const walletRecord = get().isContractWalletRecord[account];
if (walletRecord !== undefined) {
return walletRecord
return walletRecord;
}
const codeOfWalletAddress = await wallet.provider.getCode(
wallet.accounts[0]
);
const isContractWallet = codeOfWalletAddress !== '0x'
set((state) => produce(state, (draft) => {
draft.isContractWalletRecord[account] = isContractWallet
}))
return isContractWallet
const isContractWallet = codeOfWalletAddress !== '0x';
set((state) =>
produce(state, (draft) => {
draft.isContractWalletRecord[account] = isContractWallet;
})
);
return isContractWallet;
},
/**
* setActiveWallet is separate from connectWallet for a reason, after metaMask.activate()
Expand All @@ -197,7 +202,7 @@ export function createWalletSlice({
wallet.provider as StaticJsonRpcBatchProvider
);
}
const isContractAddress = await get().checkIsContractWallet(wallet)
const isContractAddress = await get().checkIsContractWallet(wallet);
set({
activeWallet: {
...wallet,
Expand All @@ -212,14 +217,14 @@ export function createWalletSlice({
},
changeActiveWalletChainId: (chainId?: number) => {
if (chainId !== undefined) {
set((state) =>
set((state) =>
produce(state, (draft) => {
if (draft.activeWallet) {
draft.activeWallet.chainId = chainId;
}
})
);
setLocalStorageWalletChainId(chainId.toString());
setLocalStorageWalletChainId(chainId.toString());
}
},

Expand Down

0 comments on commit 88cb87a

Please sign in to comment.