-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: state mgt - add contract and wallet stores
- Loading branch information
Showing
11 changed files
with
2,286 additions
and
10,407 deletions.
There are no files selected for viewing
12,207 changes: 2,022 additions & 10,185 deletions
12,207
contracts/src/bridge/frontend/package-lock.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
contracts/src/bridge/frontend/src/components/modules/common/connect-wallet.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 8 additions & 175 deletions
183
contracts/src/bridge/frontend/src/components/providers/wallet-provider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,199 +1,32 @@ | ||
import React, { createContext, useState, useContext, useEffect } from "react"; | ||
import { toast } from "../ui/use-toast"; | ||
import React, { createContext, useContext, useEffect } from "react"; | ||
import useWalletStore from "@/src/stores/wallet-store"; | ||
import { | ||
ToastType, | ||
WalletConnectionContextType, | ||
WalletConnectionProviderProps, | ||
WalletNetwork, | ||
} from "@/src/types"; | ||
import { requestMethods } from "@/src/routes"; | ||
import { | ||
getEthereumProvider, | ||
handleStorage, | ||
} from "@/src/lib/utils/walletUtils"; | ||
import { | ||
setupEventListeners, | ||
initializeSigner, | ||
} from "@/src/lib/utils/walletEvents"; | ||
|
||
const WalletContext = createContext<WalletConnectionContextType | null>(null); | ||
|
||
const WalletProvider = ({ children }: WalletConnectionProviderProps) => { | ||
const [provider, setProvider] = useState<any>(null); | ||
const [isWalletConnected, setIsWalletConnected] = useState<boolean>(false); | ||
const [signer, setSigner] = useState<any>(null); | ||
const [address, setAddress] = useState<string>(""); | ||
const [isL1ToL2, setIsL1ToL2] = useState<boolean>(true); | ||
|
||
const initializeProvider = async () => { | ||
const detectedProvider = await getEthereumProvider(); | ||
setProvider(detectedProvider); | ||
|
||
const newSigner = initializeSigner(provider); | ||
setSigner(newSigner); | ||
|
||
const chainId = await detectedProvider.send(requestMethods.getChainId, []); | ||
|
||
const isL1 = chainId === WalletNetwork.L1_SEPOLIA; | ||
setIsL1ToL2(isL1); | ||
}; | ||
|
||
const connectWallet = async () => { | ||
try { | ||
const detectedProvider = await getEthereumProvider(); | ||
setProvider(detectedProvider); | ||
|
||
const accounts = await detectedProvider.send( | ||
requestMethods.connectAccounts, | ||
[] | ||
); | ||
|
||
const newSigner = initializeSigner(detectedProvider); | ||
setSigner(newSigner); | ||
|
||
setIsWalletConnected(true); | ||
setAddress(accounts[0]); | ||
handleStorage.save("walletAddress", accounts[0]); | ||
handleStorage.save("isL1ToL2", isL1ToL2.toString()); | ||
|
||
toast({ | ||
title: "Connected", | ||
description: "Connected to wallet! Account: " + accounts[0], | ||
variant: ToastType.SUCCESS, | ||
}); | ||
} catch (error) { | ||
console.error("Error connecting to wallet:", error); | ||
toast({ | ||
title: "Error", | ||
description: "Error connecting to wallet", | ||
variant: ToastType.DESTRUCTIVE, | ||
}); | ||
} | ||
}; | ||
|
||
const disconnectWallet = () => { | ||
try { | ||
if (provider) { | ||
provider.removeAllListeners(); | ||
setProvider(null); | ||
setSigner(null); | ||
setAddress(""); | ||
setIsWalletConnected(false); | ||
handleStorage.remove("walletAddress"); | ||
handleStorage.remove("isL1ToL2"); | ||
handleStorage.remove("tenBridgeReceiver"); | ||
window.location.reload(); | ||
|
||
toast({ | ||
title: "Disconnected", | ||
description: "Disconnected from wallet", | ||
variant: ToastType.INFO, | ||
}); | ||
} | ||
} catch (error) { | ||
console.error("Error disconnecting from wallet:", error); | ||
toast({ | ||
title: "Error", | ||
description: "Error disconnecting from wallet", | ||
variant: ToastType.DESTRUCTIVE, | ||
}); | ||
} | ||
}; | ||
const { restoreWalletState } = useWalletStore(); | ||
|
||
useEffect(() => { | ||
const storedAddress = handleStorage.get("walletAddress"); | ||
const storedIsL1ToL2 = handleStorage.get("isL1ToL2") === "true"; | ||
|
||
if (storedAddress) { | ||
setAddress(storedAddress); | ||
setIsWalletConnected(true); | ||
setIsL1ToL2(storedIsL1ToL2); | ||
} | ||
|
||
initializeProvider(); | ||
restoreWalletState(); | ||
}, []); | ||
|
||
const switchNetwork = async () => { | ||
if (!provider) { | ||
toast({ | ||
title: "Error", | ||
description: "Please connect to wallet first", | ||
variant: ToastType.DESTRUCTIVE, | ||
}); | ||
return; | ||
} | ||
try { | ||
const desiredNetwork = isL1ToL2 | ||
? WalletNetwork.L2_TEN_TESTNET | ||
: WalletNetwork.L1_SEPOLIA; | ||
|
||
await provider.request(requestMethods.switchNetwork, [ | ||
{ chainId: desiredNetwork }, | ||
]); | ||
|
||
const isL1 = desiredNetwork === WalletNetwork.L1_SEPOLIA; | ||
setIsL1ToL2(isL1); | ||
handleStorage.save("isL1ToL2", isL1.toString()); | ||
|
||
toast({ | ||
title: "Network Switched", | ||
description: `Switched to ${ | ||
desiredNetwork === WalletNetwork.L2_TEN_TESTNET | ||
? "L2 TEN Testnet" | ||
: "L1 Sepolia" | ||
}`, | ||
variant: ToastType.SUCCESS, | ||
}); | ||
} catch (error: any) { | ||
console.error("Error switching network:", error); | ||
if (error.code === 4902) { | ||
toast({ | ||
title: "Network Not Found", | ||
description: "Network not found in wallet", | ||
variant: ToastType.INFO, | ||
}); | ||
} else { | ||
toast({ | ||
title: "Error", | ||
description: error.message || "Error switching network", | ||
variant: ToastType.DESTRUCTIVE, | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (provider) { | ||
const newSigner = initializeSigner(provider); | ||
setSigner(newSigner); | ||
|
||
const cleanup = setupEventListeners(provider, setAddress); | ||
return cleanup; | ||
} | ||
}, [provider]); | ||
|
||
const value = { | ||
provider, | ||
signer, | ||
address, | ||
walletConnected: isWalletConnected, | ||
isL1ToL2, | ||
connectWallet, | ||
disconnectWallet, | ||
switchNetwork, | ||
}; | ||
const value = {}; | ||
|
||
return ( | ||
<WalletContext.Provider value={value}>{children}</WalletContext.Provider> | ||
); | ||
}; | ||
|
||
const useWalletStore = () => { | ||
const useWallet = () => { | ||
const context = useContext(WalletContext); | ||
if (!context) { | ||
throw new Error("useWalletStore must be used within a WalletProvider"); | ||
throw new Error("useWallet must be used within a WalletProvider"); | ||
} | ||
return context; | ||
}; | ||
|
||
export { WalletProvider, useWalletStore }; | ||
export { WalletProvider, useWallet }; |
Oops, something went wrong.