Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default function App() {
});
},
onAuthFailure: (method, reason) => {
console.log("Auth failed ", reason);
dispatch({
type: "SET_EXTERNAL_WALLET_AUTH_LOAD_STATE",
payload: "rejected",
Expand Down
2 changes: 1 addition & 1 deletion src/common/Common.constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const walletCategories: WalletCategoriesType[] = [
icon: <Arbitrum width={24} height={24} />,
},
{
value: "binance",
value: "bsc",
label: "Connect Binance Wallet",
icon: <BnbIcon width={24} height={24} />,
},
Expand Down
26 changes: 23 additions & 3 deletions src/context/AppContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { createContext, ReactNode, useContext, useReducer } from "react";
import {
createContext,
ReactNode,
useContext,
useReducer,
useState,
} from "react";

// Define the shape of the app state
export type AppState = {
Expand All @@ -7,7 +13,8 @@ export type AppState = {
| "success"
| "loading"
| "rejected"
| "timeout";
| "timeout"
| "check_network";
};

// Define actions for state management
Expand Down Expand Up @@ -38,9 +45,13 @@ function appReducer(state: AppState, action: AppAction): AppState {
const AppContext = createContext<{
state: AppState;
dispatch: React.Dispatch<AppAction>;
selectedWalletCategory: string;
setSelectedWalletCategory: (selectedWalletCategory: string) => void;
}>({
state: initialState,
dispatch: () => null, // Placeholder function for initial context
selectedWalletCategory: "",
setSelectedWalletCategory: () => {},
});

// Custom hook to use the AppContext
Expand All @@ -57,9 +68,18 @@ export const AppProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const [state, dispatch] = useReducer(appReducer, initialState);
const [selectedWalletCategory, setSelectedWalletCategory] =
useState<string>("");

return (
<AppContext.Provider value={{ state, dispatch }}>
<AppContext.Provider
value={{
state,
dispatch,
selectedWalletCategory,
setSelectedWalletCategory,
}}
>
{children}
</AppContext.Provider>
);
Expand Down
27 changes: 27 additions & 0 deletions src/context/EventEmitterContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
useReinitialize,
} from "@dynamic-labs/sdk-react-core";
import { getAuthWindowConfig } from "../modules/Authentication/Authentication.utils";
import { arbitrum, base, bsc, mainnet } from "viem/chains";
import { useAppState } from "./AppContext";

// Define the shape of the app state
export type EventEmitterState = {
Expand Down Expand Up @@ -63,6 +65,7 @@ export const EventEmitterProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const { dispatch, state } = useGlobalState();
const { selectedWalletCategory } = useAppState();

const [isLoggedEmitterCalled, setLoginEmitterStatus] = useState(false);

Expand All @@ -88,9 +91,33 @@ export const EventEmitterProvider: React.FC<{ children: ReactNode }> = ({
// for external wallets
const externalWalletRef = useRef<Signer | null>(null);

let networkToSwitch;
switch (selectedWalletCategory) {
case "mainnet":
networkToSwitch = mainnet;
break;
case "arbitrum":
networkToSwitch = arbitrum;
break;
case "base":
networkToSwitch = base;
break;
case "bsc":
networkToSwitch = bsc;
break;
default:
networkToSwitch = mainnet;
break;
}

useEffect(() => {
if (state.dynamicWallet && !isLoggedEmitterCalled) {
(async () => {
const chainId = await state.dynamicWallet.getNetwork();
if (chainId !== networkToSwitch.id) {
await state.dynamicWallet.switchNetwork(networkToSwitch.id);
}

externalWalletRef.current = await PushSigner.initialize(
state.dynamicWallet,
"DYNAMIC"
Expand Down
11 changes: 7 additions & 4 deletions src/modules/Authentication/components/WalletSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ type WalletSelectionProps = {
};

const WalletSelection: FC<WalletSelectionProps> = ({ setConnectMethod }) => {
const [selectedWalletCategory, setSelectedWalletCategory] =
useState<string>("");
// const [selectedWalletCategory, setSelectedWalletCategory] =
// useState<string>("");
const { primaryWallet } = useDynamicContext();
const { walletOptions, selectWalletOption } = useWalletOptions();
const navigate = useNavigate();

const {
dispatch,
state: { externalWalletAuthState },
selectedWalletCategory,
setSelectedWalletCategory,
} = useAppState();
const persistQuery = usePersistedQuery();

Expand Down Expand Up @@ -93,7 +95,7 @@ const WalletSelection: FC<WalletSelectionProps> = ({ setConnectMethod }) => {
? wallets.solanaWallets
: selectedWalletCategory === "arbitrum"
? wallets.arbitrumWallets
: selectedWalletCategory === "binance" && wallets.binanceWallets;
: selectedWalletCategory === "bsc" && wallets.binanceWallets;

const handleBack = () => {
if (selectedWalletCategory) setSelectedWalletCategory("");
Expand Down Expand Up @@ -150,7 +152,7 @@ const WalletSelection: FC<WalletSelectionProps> = ({ setConnectMethod }) => {
customScrollbar
>
{!primaryWallet &&
(!selectedWalletCategory ? (
(selectedWalletCategory === "" ? (
<WalletCategories
setSelectedWalletCategory={setSelectedWalletCategory}
/>
Expand Down Expand Up @@ -222,6 +224,7 @@ const WalletSelection: FC<WalletSelectionProps> = ({ setConnectMethod }) => {
/>
</DrawerWrapper>
)}

{externalWalletAuthState === "rejected" && (
<DrawerWrapper>
<ErrorContent
Expand Down
8 changes: 6 additions & 2 deletions src/modules/wallet/Wallet.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC, useEffect, useState } from "react";
import { Box, Info } from "../../blocks";
import { Box, Button, Info } from "../../blocks";
import {
BoxLayout,
ContentLayout,
Expand All @@ -18,7 +18,10 @@ import { PushWallet } from "../../services/pushWallet/pushWallet";
import { APP_ROUTES, ENV } from "../../constants";
import secrets from "secrets.js-grempe";
import { useGlobalState } from "../../context/GlobalContext";
import { useDynamicContext } from "@dynamic-labs/sdk-react-core";
import {
useDynamicContext,
useSwitchNetwork,
} from "@dynamic-labs/sdk-react-core";
import { getWalletlist } from "./Wallet.utils";
import { WalletListType } from "./Wallet.types";
import { PushWalletAppConnection } from "../../common";
Expand All @@ -36,6 +39,7 @@ const Wallet: FC<WalletProps> = () => {
const params = new URLSearchParams(location.search);
const externalOrigin = params.get("app");
const { primaryWallet } = useDynamicContext();
const switchNetwork = useSwitchNetwork();
const [showConnectionSuccess, setConnectionSuccess] =
useState<boolean>(false);

Expand Down
4 changes: 4 additions & 0 deletions src/modules/wallet/components/WalletProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { useDynamicContext } from "@dynamic-labs/sdk-react-core";
import { WalletListType } from "../Wallet.types";
import { APP_ROUTES } from "../../../constants";
import { useEventEmitterContext } from "../../../context/EventEmitterContext";
import { useAppState } from "../../../context/AppContext";

export type WalletProfileProps = {
selectedWallet: WalletListType;
Expand All @@ -35,6 +36,7 @@ const WalletProfile: FC<WalletProfileProps> = ({ selectedWallet }) => {
const walletName = selectedWallet?.name ?? "External Wallet";
const [copied, setCopied] = useState(false);
const { dispatch } = useGlobalState();
const { setSelectedWalletCategory } = useAppState();

const { handleLogOutEvent } = useEventEmitterContext();

Expand All @@ -53,6 +55,8 @@ const WalletProfile: FC<WalletProfileProps> = ({ selectedWallet }) => {
navigate(persistQuery(APP_ROUTES.AUTH));

handleLogOutEvent();

setSelectedWalletCategory("");
};

return (
Expand Down