Skip to content

Commit de8a1d7

Browse files
authored
Merge pull request #74 from vanilladefi/fix/locked-mask
Wallet refactoring and fixes.
2 parents d11700c + f734a60 commit de8a1d7

1 file changed

Lines changed: 20 additions & 31 deletions

File tree

state/actions/wallet.tsx

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ import {
66
import { BigNumber, providers } from "ethers";
77
import { snapshot } from "valtio";
88
import { toast } from "react-toastify";
9-
import { persistedKeys, ref, state, subscribeKey, VanillaEvents } from "..";
10-
import {
11-
correctNetwork,
12-
getHexaDecimalChainId,
13-
} from "../../lib/config";
9+
import { ref, state, subscribeKey, VanillaEvents } from "..";
10+
import { correctNetwork, getHexaDecimalChainId } from "../../lib/config";
1411
import { emitEvent, formatJuice, parseJuice } from "../../utils/helpers";
1512
import { showDialog } from "./dialog";
1613
import { parseUnits } from "ethers/lib/utils";
@@ -20,14 +17,18 @@ let lockedWalletToast: any;
2017
interface ConnectOptions {
2118
skipLockedWalletCheck?: boolean;
2219
}
23-
let window_ethereum: any
20+
let window_ethereum: any;
2421

2522
export const connectWallet = async (opts?: ConnectOptions) => {
2623
const { skipLockedWalletCheck } = opts || {};
2724
const { modal } = snapshot(state);
2825
try {
2926
// Handle metamask locked state
30-
if (typeof window !== undefined && !skipLockedWalletCheck) {
27+
if (
28+
typeof window !== undefined &&
29+
!skipLockedWalletCheck &&
30+
modal?.cachedProvider === "injected"
31+
) {
3132
const isUnlocked = window.ethereum?._metamask?.isUnlocked;
3233
if (isUnlocked && (await isUnlocked()) === false) {
3334
const isToastActive = toast.isActive(lockedWalletToast);
@@ -46,7 +47,7 @@ export const connectWallet = async (opts?: ConnectOptions) => {
4647

4748
const polygonProvider = await modal?.connect();
4849

49-
window_ethereum = window.ethereum
50+
window_ethereum = window.ethereum;
5051
window.ethereum = polygonProvider;
5152

5253
const web3Provider = new providers.Web3Provider(polygonProvider);
@@ -58,7 +59,7 @@ export const connectWallet = async (opts?: ConnectOptions) => {
5859
state.walletAddress = await signer?.getAddress();
5960

6061
updateBalances();
61-
updateProviderName()
62+
updateProviderName();
6263
} catch (error) {
6364
console.warn("Connection error: ", error);
6465
}
@@ -68,17 +69,17 @@ export const disconnect = (soft?: boolean) => {
6869
const { modal } = snapshot(state);
6970
if (!soft) {
7071
modal?.clearCachedProvider();
72+
state.providerName = null;
7173
if (window_ethereum) {
72-
window.ethereum = window_ethereum
74+
window.ethereum = window_ethereum;
7375
}
7476
}
7577

7678
state.signer = null;
77-
if (!soft) state.walletAddress = null;
79+
state.walletAddress = null;
7880
state.walletOpen = false;
7981
state.truncatedWalletAddress = null;
8082

81-
!soft && localStorage.removeItem(persistedKeys.walletAddress);
8283
state.balances = {};
8384
state.rawBalances = {};
8485
};
@@ -99,11 +100,10 @@ export const ensureCorrectChain = (force?: true): boolean => {
99100
try {
100101
const { signer, walletAddress } = snapshot(state);
101102
const _chainId: number | string | undefined = window.ethereum?.chainId;
102-
let chainId: string
103-
if (typeof _chainId === 'string' && _chainId.startsWith('0x')) {
104-
chainId = _chainId
105-
}
106-
else {
103+
let chainId: string;
104+
if (typeof _chainId === "string" && _chainId.startsWith("0x")) {
105+
chainId = _chainId;
106+
} else {
107107
chainId = getHexaDecimalChainId(Number(_chainId || 0));
108108
}
109109
if (chainId !== correctNetwork.chainId) {
@@ -144,8 +144,6 @@ export const initWalletSubscriptions = () => {
144144
updateBalances();
145145
updateTruncatedAddress();
146146

147-
persistWalletAddress();
148-
149147
const { signer, polygonProvider } = snapshot(state);
150148

151149
// subscribe to juice transactions
@@ -178,22 +176,13 @@ export const initWalletSubscriptions = () => {
178176
if (modal?.cachedProvider) {
179177
// TODO see if we can remove this or make run only once
180178
connectWallet({ skipLockedWalletCheck: true });
181-
updateProviderName()
179+
updateProviderName();
182180
}
183181
});
184182
};
185183

186-
export const persistWalletAddress = () => {
187-
const persistedAddress = localStorage.getItem(persistedKeys.walletAddress);
188-
if (state.walletAddress !== persistedAddress)
189-
localStorage.setItem(
190-
persistedKeys.walletAddress,
191-
JSON.stringify(state.walletAddress)
192-
);
193-
};
194-
195184
export const updateProviderName = async () => {
196-
const { modal } = snapshot(state)
185+
const { modal } = snapshot(state);
197186
let name = null;
198187
switch (modal?.cachedProvider) {
199188
case "injected": {
@@ -209,7 +198,7 @@ export const updateProviderName = async () => {
209198
}
210199
}
211200
state.providerName = name;
212-
}
201+
};
213202

214203
export const updateBalances = async () => {
215204
const { polygonProvider, ethereumProvider, walletAddress } = snapshot(state);

0 commit comments

Comments
 (0)