Skip to content

Commit

Permalink
WalletConnect wallet and other improvements in react
Browse files Browse the repository at this point in the history
  • Loading branch information
MananTank committed Feb 7, 2024
1 parent b5a03e9 commit 6a3459f
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type ModalConfig = {
isEmbed?: boolean;
onConnect?: () => void;
chainId?: bigint;
chains?: bigint[];
};

const WalletModalOpen = /* @__PURE__ */ createContext(false);
Expand Down Expand Up @@ -61,6 +62,7 @@ export const WalletUIStatesProvider = (
};
onConnect?: () => void;
chainId?: bigint;
chains?: bigint[];
}>,
) => {
const [isWalletModalOpen, setIsWalletModalOpen] = useState(false);
Expand All @@ -80,6 +82,7 @@ export const WalletUIStatesProvider = (
auth: props.auth,
onConnect: props.onConnect,
chainId: props.chainId,
chains: props.chains,
});

return (
Expand Down Expand Up @@ -235,6 +238,9 @@ export type ModalConfigOptions = {
* Note that this does not include the sign in, If you want to call a callback after user connects AND signs in with their wallet, use `auth.onLogin` instead
*/
onConnect?: () => void;

chainId?: bigint;
chains?: bigint[];
};

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/thirdweb/src/react/types/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type WalletConfig = {
*
* This is only required if your wallet requires a personal wallet to be connected such as a Safe Wallet or Smart Wallet
*
* providing the `personalWallets` ensures that autoconnect and ConnectWallet modal works properly for your wallet.
* providing the `personalWallets` ensures that auto connect and ConnectWallet modal works properly for your wallet.
* auto-connect will connect the last connected personal wallet first and then connect your wallet
* ConnectWallet modal will reopen once the personal wallet is connected so that you can render UI for connecting your wallet as the next step
*/
Expand Down Expand Up @@ -112,6 +112,11 @@ export type ConnectUIProps = {
*/
chainId?: bigint;

/**
* List of all chains supported by the app
*/
chains?: bigint[];

/**
* Create a wallet instance
* Calling this function uses calls the `WalletConfig.create` method with the required options
Expand Down
11 changes: 10 additions & 1 deletion packages/thirdweb/src/react/ui/ConnectWallet/ConnectWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ import {
} from "../../providers/wallet-ui-states-provider.js";

export type ConnectWalletProps = {
/**
* If specified, Wallet will be prompted to switch to this chain after connecting if it is not already connected to this chain.
*/
chainId?: bigint | number;

/**
* Array of chain ids that the app supports
*/
chains?: (bigint | number)[];

/**
* CSS class to apply to the button element
*
Expand Down Expand Up @@ -670,6 +678,7 @@ export function ConnectWallet(props: ConnectWalletProps) {
auth: props.auth,
onConnect: props.onConnect,
chainId: props.chainId ? BigInt(props.chainId) : undefined,
chains: props.chains?.map(BigInt),
});
setIsWalletModalOpen(true);
}}
Expand Down Expand Up @@ -748,7 +757,7 @@ export function ConnectWallet(props: ConnectWalletProps) {
}}
hideSwitchToPersonalWallet={props.hideSwitchToPersonalWallet}
hideDisconnect={props.hideDisconnect}
chainIds={props.networkSelector?.chains || []}
chains={props?.chains?.map(BigInt) || []}
/>
);
})()}
Expand Down
6 changes: 3 additions & 3 deletions packages/thirdweb/src/react/ui/ConnectWallet/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const ConnectedWalletDetails: React.FC<{
displayBalanceToken?: Record<number, string>;
hideSwitchToPersonalWallet?: boolean;
hideDisconnect?: boolean;
chainIds: number[];
chains: bigint[];
}> = (props) => {
const locale = useTWLocale().connectWallet;
const activeAccount = useActiveAccount();
Expand Down Expand Up @@ -564,8 +564,8 @@ export const ConnectedWalletDetails: React.FC<{
<NetworkSelectorContent
chains={
walletChainId
? [...new Set([Number(walletChainId), ...props.chainIds])]
: props.chainIds
? [...new Set([walletChainId, ...props.chains])]
: props.chains
}
open={true}
theme={props.theme}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export type ConnectEmbedProps = {
*/
chainId?: number | bigint;

/**
* List of all chains that the app supports
*/
chains?: bigint[];

/**
* Class name to be added to the root element of ConnectEmbed
*/
Expand Down Expand Up @@ -360,7 +365,6 @@ const ConnectEmbedContent = (
onShow={() => {
// no op
}}
chainId={props.chainId ? BigInt(props.chainId) : undefined}
/>
);
}
Expand All @@ -375,6 +379,7 @@ const ConnectEmbedContent = (
auth: props.auth,
onConnect: props.onConnect,
chainId: props.chainId ? BigInt(props.chainId) : undefined,
chains: props.chains,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ConnectModalContent } from "./ConnectModalContent.js";
* @internal
*/
export const ConnectModal = () => {
const { theme, modalSize, chainId } = useContext(ModalConfigCtx);
const { theme, modalSize } = useContext(ModalConfigCtx);

const { screen, setScreen, initialScreen } = useScreen();
const isWalletModalOpen = useIsWalletModalOpen();
Expand Down Expand Up @@ -127,7 +127,6 @@ export const ConnectModal = () => {
onShow={onShow}
isOpen={isWalletModalOpen}
onClose={closeModal}
chainId={chainId}
/>
</Modal>
</CustomThemeProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const ConnectModalContent = (props: {
onShow: () => void;
isOpen: boolean;
onClose: () => void;
chainId?: bigint;
}) => {
const { screen, setScreen, initialScreen, onHide, onShow, onClose } = props;
const { wallets, client, dappMetadata } = useThirdwebProviderProps();
Expand Down Expand Up @@ -171,13 +170,15 @@ export const ConnectModalContent = (props: {
// activeWalletConnectionStatus={activeWalletConnectionStatus}
// setActiveWalletConnectionStatus={setActiveWalletConnectionStatus}
// connect={walletConfig.connect}

createInstance={() => {
return walletConfig.create({
client,
dappMetadata,
});
}}
chainId={props.chainId}
chains={modalConfig.chains}
chainId={modalConfig.chainId}
/>
);
};
Expand Down Expand Up @@ -221,7 +222,7 @@ export const ConnectModalContent = (props: {
* @internal
*/
export const ConnectModal = () => {
const { theme, modalSize, chainId } = useContext(ModalConfigCtx);
const { theme, modalSize } = useContext(ModalConfigCtx);

const { screen, setScreen, initialScreen } = useScreen();
const isWalletModalOpen = useIsWalletModalOpen();
Expand Down Expand Up @@ -333,7 +334,6 @@ export const ConnectModal = () => {
onShow={onShow}
isOpen={isWalletModalOpen}
onClose={closeModal}
chainId={chainId}
/>
</Modal>
</CustomThemeProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export type ConnectModalInlineProps = {
*/
chainId?: bigint;

/**
* List of all chains that the app supports
*/
chains?: (bigint | number)[];

className?: string;

theme?: "dark" | "light" | Theme;
Expand Down Expand Up @@ -108,7 +113,6 @@ export const ConnectModalInline = (props: ConnectModalInlineProps) => {
onShow={() => {
// no op
}}
chainId={props.chainId}
/>

{/* close icon */}
Expand All @@ -135,6 +139,7 @@ export const ConnectModalInline = (props: ConnectModalInlineProps) => {
welcomeScreen: props.welcomeScreen,
titleIconUrl: props.modalTitleIconUrl,
chainId: props.chainId,
chains: props.chains?.map(BigInt),
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ export type NetworkSelectorProps = {
*/
open: boolean;

chains?: number[];
chains?: (number | bigint)[];

/**
* Array of chains to be displayed under "Popular" section
*/
popularChains?: number[];
popularChains?: (number | bigint)[];
/**
* Array of chains to be displayed under "Recent" section
*/
recentChains?: number[];
recentChains?: (number | bigint)[];
/**
* Override how the chain button is rendered in the Modal
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const WalletConnectConnection: React.FC<{
const { client, dappMetadata } = useThirdwebProviderProps();
const [qrCodeUri, setQrCodeUri] = useState<string | undefined>();
const [errorConnecting, setErrorConnecting] = useState(false);
const optionalChains = connectUIProps.chains;

const connect = useCallback(() => {
const wallet = walletConnect({
Expand Down Expand Up @@ -71,6 +72,7 @@ export const WalletConnectConnection: React.FC<{
}
},
onSessionRequestSent,
optionalChains,
})
.then((account) => {
done(account);
Expand All @@ -87,6 +89,7 @@ export const WalletConnectConnection: React.FC<{
platformUris,
projectId,
walletConfig.metadata,
optionalChains,
]);

const scanStarted = useRef(false);
Expand Down
1 change: 1 addition & 0 deletions packages/thirdweb/src/wallets/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function createConnectionManager(options?: ConnectionManagerOptions) {

const disconnect = (account: Account) => {
onAccountDisconnect(account);
account.wallet.disconnect();
};

const setActiveAccount = (account: Account) => {
Expand Down
15 changes: 8 additions & 7 deletions packages/thirdweb/src/wallets/manager/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ const CONNECT_PARAMS_MAP_KEY = "tw:connected-wallet-params";
* @param params
* @internal
*/
export async function saveConnectParamsToStorage<
T extends WalletConnectionOptions,
>(walletId: string, params: T) {
export async function saveConnectParamsToStorage<T extends object>(
walletId: string,
params: T,
) {
// params must be stringifiable
if (!isStringifiable(params)) {
console.debug("params", params);
Expand Down Expand Up @@ -89,9 +90,9 @@ export async function deleteConnectParamsFromStorage(walletId: string) {
* Get the saved connection params from storage for given wallet id
* @internal
*/
export async function getSavedConnectParamsFromStorage<
T extends WalletConnectionOptions,
>(walletId: string): Promise<T | null> {
export async function getSavedConnectParamsFromStorage<T extends object>(
walletId: string,
): Promise<T | null> {
const valueStr = await walletStorage.get(CONNECT_PARAMS_MAP_KEY);

if (!valueStr) {
Expand All @@ -102,7 +103,7 @@ export async function getSavedConnectParamsFromStorage<
const value = JSON.parse(valueStr);

if (value && value[walletId]) {
return value[walletId] as T;
return value[walletId];
}

return null;
Expand Down
Loading

0 comments on commit 6a3459f

Please sign in to comment.