Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the Dynamic(xyz) wallet library #2448

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
1056385
Update: `@web3-onboard` to latest release
rdig Dec 4, 2024
f24837f
Fix: `AppContextProvider` types after @web3-onboard updates
rdig Dec 4, 2024
d441de7
Add: introduce `dynamic` wallet framework
rdig Dec 4, 2024
8164b25
Chore: update package log after rebase
rdig Jan 17, 2025
d975ef9
Chore: update and fix package/package lock
rdig Jan 21, 2025
e517fdf
Add: dynamic env file var
rdig Jan 22, 2025
9eb9613
Add: `UserNavigation` show debug dynamic wallet controls
rdig Jan 22, 2025
11e84a4
Add: `WalletConnectedTopMenu` manage embedded wallet button
rdig Jan 22, 2025
aaa5de5
Fix: `getGasPrices` dynamic transport RPC call
rdig Jan 22, 2025
010a0c9
Remove: un-needed `isFullWallet` checks
rdig Jan 22, 2025
8fbf9c8
WIP: refactor `AppContext` to support dynamic wallets
rdig Jan 22, 2025
c8cd9c6
Refactor: RetryProvider to extend dynamic transport
rdig Jan 22, 2025
51c1dac
Add: `Entry` initialize dynamic wallet context
rdig Jan 22, 2025
7bd1973
Add: `Entry` introduce top level app dark mode
rdig Jan 22, 2025
572c416
Chore: update phospor icons to match types
rdig Jan 22, 2025
d770f26
Fix: various type complaints brought on by dynamic
rdig Jan 22, 2025
ec32087
Fix: `estimateGasCost` tx options update logic
rdig Jan 21, 2025
58753f4
Refactor: currency utils `tokenPrice` debug logging
rdig Jan 21, 2025
0651c63
Fix: Dockerfile add required runtimes for the new deps
rdig Jan 22, 2025
9cadfb5
Add: enable dynamic logs on non-local environments
rdig Jan 22, 2025
0fc5829
Fix: 'estimateGasCost` after rebase
rdig Jan 22, 2025
9773b53
Fix: Do not close action sidebar when clicking within dynamic wallet …
jakubcolony Jan 22, 2025
8e174b8
Fix: Also do not close the sidebar when clicking on other Dynamic modals
jakubcolony Jan 22, 2025
195f21f
Try: Disable Dynamic library shadow DOM
jakubcolony Jan 22, 2025
a718c9d
Revert "Fix: Also do not close the sidebar when clicking on other Dyn…
jakubcolony Jan 22, 2025
e3c280e
Revert "Fix: Do not close action sidebar when clicking within dynamic…
jakubcolony Jan 22, 2025
df0244d
Fix: somewhat propert solution for Dynamic modals closing action sidebar
jakubcolony Jan 22, 2025
6b6ae8a
Fix: `estimateGasCost` tx options update logic
rdig Jan 21, 2025
7c20d9c
Fix: show embedded wallet controls as long as one connected
rdig Jan 23, 2025
a5b5395
Fix: `AppContextProvider` handle wallet network change
rdig Jan 23, 2025
53e5473
Add: `AppContextProvider` handle wallet out of sync
rdig Jan 23, 2025
16435be
Add: `AppContextProvider` handle wallet address change
rdig Jan 23, 2025
263240f
Add: `AppContextProvider` handle wallet connect/sign cancel
rdig Jan 23, 2025
2bd97b4
Refactor: `Entry` use hook for dark move state
rdig Jan 23, 2025
76ff355
Add: minor dynamic wallet styles overrides
rdig Jan 23, 2025
0bb5bb8
Remove: onboard wallet saga leftover comments
rdig Jan 23, 2025
a38b466
Change: enable Dynamic wallet usage tracking
rdig Jan 23, 2025
49f5d8d
Add: dynamic wallet modal light mode tag bg override
rdig Jan 23, 2025
2fd4ed6
Refactor action sidebar before close callbacks, fix closing on Dynami…
jakubcolony Jan 28, 2025
0808123
Refactor: `WalletConnectedTopMenu` manage wallet copy
rdig Jan 28, 2025
565967d
Fix: `AppContextProvider` handle embedded wallet logout
rdig Jan 28, 2025
d2bf934
Add: automatically switch wallet network upon connect
rdig Jan 28, 2025
ee49c3a
Fix: `AppContext` wallet recover after traumatic event
rdig Jan 28, 2025
7eaf4b2
Fix: `LandingPage` wallet trigger multiple loading states
rdig Jan 28, 2025
426d0f1
Fix: remove dynamic wallet shadow dom disable
rdig Jan 29, 2025
c7a76c2
Fix: prevent redirect to landing if wallet will autoconnect
rdig Jan 29, 2025
609b172
Fix: `AppContextProvider` handle dynamic local storage
rdig Jan 29, 2025
d13a035
Refactor: dynamic wallet css overrides for non shadow dom
rdig Jan 29, 2025
9c1f3f1
Refactor: dynamic CSS overrides as strings
rdig Jan 29, 2025
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
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ MAGICBELL_DEV_KEY=

# Needed for Arbitrary transaction feature
ARBISCAN_API_KEY=

# Needed so that we have a wallet provided by Dynamic.xyz
DYNAMIC_ENV_ID=
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# From https://github.com/runtime-env/import-meta-env/blob/main/packages/examples/docker-starter-example/Dockerfile
FROM node:20.11-alpine3.19 as build-stage
RUN apk add git
RUN apk add git python3 make g++
WORKDIR /app
COPY package.json package-lock.json ./
RUN SKIP_HOOKS=true npm ci
Expand Down
16,705 changes: 12,393 additions & 4,312 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@
"@colony/redux-promise-listener": "^1.2.0",
"@colony/sdk": "^3.0.0-next.2",
"@colony/unicode-confusables-noascii": "^0.1.2",
"@dynamic-labs/ethereum": "^4.2.3",
"@dynamic-labs/ethers-v5": "^2.6.2",
"@dynamic-labs/sdk-react-core": "^4.2.3",
"@hookform/resolvers": "^2.9.10",
"@magicbell/react-headless": "^5.0.1",
"@nivo/arcs": "^0.87.0",
Expand Down
131 changes: 103 additions & 28 deletions src/Entry.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { ApolloProvider } from '@apollo/client';
import { EthereumWalletConnectors } from '@dynamic-labs/ethereum';
import {
DynamicContextProvider,
mergeNetworks,
} from '@dynamic-labs/sdk-react-core';
import { PostHogProvider } from 'posthog-js/react';
import React from 'react';
import { HelmetProvider } from 'react-helmet-async';
import { IntlProvider } from 'react-intl';
import { Provider as ReduxProvider } from 'react-redux';
import { BrowserRouter as Router } from 'react-router-dom';
import useLocalStorage from 'use-local-storage';

import { GANACHE_NETWORK, GANACHE_LOCAL_RPC_URL } from '~constants/index.ts';
import AnalyticsContextProvider from '~context/AnalyticsContext/AnalyticsContextProvider.tsx';
import BreadcrumbsContextProvider from '~context/BreadcrumbsContext/BreadcrumbsContextProvider.tsx';
import { getContext, ContextModule } from '~context/index.ts';
Expand All @@ -17,6 +24,10 @@ import motionStatesMessages from './i18n/en-motion-states.ts';
import systemMessages from './i18n/en-system-messages.ts';
import messages from './i18n/en.json';
import Routes from './routes/index.ts';
import {
dynamicCssOverridesLight,
dynamicCssOverridesDark,
} from './styles/dynamicOverrides.ts';

interface Props {
store: any;
Expand All @@ -38,35 +49,99 @@ if (__COMMIT_HASH__) {
const Entry = ({ store }: Props) => {
const apolloClient = getContext(ContextModule.ApolloClient);

const [isDarkMode] = useLocalStorage(
'isDarkMode',
window.matchMedia('(prefers-color-scheme: dark)').matches,
);

return (
<PostHogProvider {...posthogConfig}>
<IntlProvider
locale="en"
defaultLocale="en"
messages={{
...messages,
...actionMessages,
...eventsMessages,
...systemMessages,
...motionStatesMessages,
}}
>
<ApolloProvider client={apolloClient}>
<ReduxProvider store={store}>
<HelmetProvider>
<AnalyticsContextProvider>
<Router>
<BreadcrumbsContextProvider>
<RouteTracker />
<Routes />
</BreadcrumbsContextProvider>
</Router>
</AnalyticsContextProvider>
</HelmetProvider>
</ReduxProvider>
</ApolloProvider>
</IntlProvider>
</PostHogProvider>
<DynamicContextProvider
theme={isDarkMode ? 'dark' : 'light'}
settings={{
debugError: import.meta.env.DEV,
logLevel: import.meta.env.DEV ? 'WARN' : 'ERROR',
mobileExperience: 'redirect',
environmentId: import.meta.env.DYNAMIC_ENV_ID,
walletConnectors: [EthereumWalletConnectors],
initialAuthenticationMode: 'connect-only',
networkValidationMode: 'always',
recommendedWallets: [
{
walletKey: 'metamask',
label: 'Recommended',
},
],
// Since we can't use the shadow Dom override for css styling, since that breaks the skeleton loader
// we have to resort to this method to override the css, which is very verbose
cssOverrides: isDarkMode
? dynamicCssOverridesDark
: dynamicCssOverridesLight,
overrides: {
evmNetworks: (networks) => {
// Only needed for embedded wallets
// All the others have their own interface to manage the chain
if (import.meta.env.DEV) {
return mergeNetworks(
[
{
blockExplorerUrls: [
GANACHE_NETWORK.blockExplorerUrl as string,
],
chainId: parseInt(GANACHE_NETWORK.chainId, 10),
chainName: 'Local Hardhat Instance',
iconUrls: ['/src/images/icons/hardhat.svg'],
name: 'Local Hardhat Instance',
nativeCurrency: {
decimals: 18,
name: 'Ether',
symbol: 'ETH',
iconUrl:
'https://app.dynamic.xyz/assets/networks/eth.svg',
},
networkId: parseInt(GANACHE_NETWORK.chainId, 10),

rpcUrls: [GANACHE_LOCAL_RPC_URL],
vanityName: 'Hardhat',
},
],
networks,
);
}
return networks;
},
},
shadowDOMEnabled: true,
}}
>
<PostHogProvider {...posthogConfig}>
<IntlProvider
locale="en"
defaultLocale="en"
messages={{
...messages,
...actionMessages,
...eventsMessages,
...systemMessages,
...motionStatesMessages,
}}
>
<ApolloProvider client={apolloClient}>
<ReduxProvider store={store}>
<HelmetProvider>
<AnalyticsContextProvider>
<Router>
<BreadcrumbsContextProvider>
<RouteTracker />
<Routes />
</BreadcrumbsContextProvider>
</Router>
</AnalyticsContextProvider>
</HelmetProvider>
</ReduxProvider>
</ApolloProvider>
</IntlProvider>
</PostHogProvider>
</DynamicContextProvider>
);
};

Expand Down
5 changes: 0 additions & 5 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { SiweMessage } from 'siwe';

import { APP_URL } from '~constants/index.ts';
import { ContextModule, getContext } from '~context/index.ts';
import { isFullWallet } from '~types/wallet.ts';

const authProxyRequest = async (urlPartial: string, options?: RequestInit) => {
try {
Expand Down Expand Up @@ -38,10 +37,6 @@ export const deauthenticateWallet = async (): Promise<void> => {
export const authenticateWallet = async (): Promise<void> => {
const wallet = getContext(ContextModule.Wallet);

if (!isFullWallet(wallet)) {
throw new Error('Background login not yet completed.');
}

const signer = wallet.ethersProvider.getSigner();

const authCheck = await authProxyRequest('check');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DynamicWidget } from '@dynamic-labs/sdk-react-core';
import { Cardholder, GearSix, List, X } from '@phosphor-icons/react';
import React, { useState, type FC } from 'react';
import { defineMessages } from 'react-intl';
Expand Down Expand Up @@ -27,6 +28,8 @@ const MSG = defineMessages({
},
});

const localStorageKey = '__DYNAMIC_WALLET_DEBUG_ENABLED__';

// @TODO: Rename this to something more explanatory
const UserNavigation: FC<UserNavigationProps> = ({
extra = null,
Expand Down Expand Up @@ -79,6 +82,15 @@ const UserNavigation: FC<UserNavigationProps> = ({

return (
<div data-tour={TourTargets.UserMenu} className="flex gap-1 md:relative">
<div
className={
localStorage.getItem(localStorageKey) === 'true'
? 'visible'
: 'hidden'
}
>
<DynamicWidget />
</div>
{txButton}
{isWalletConnected ? (
<div className="flex gap-1">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UserCircleGear } from '@phosphor-icons/react';
import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { UserCircleGear, Wallet } from '@phosphor-icons/react';
import React, { type PropsWithChildren, type FC } from 'react';
import { useIntl } from 'react-intl';
import { useIntl, defineMessages } from 'react-intl';

import { useAppContext } from '~context/AppContext/AppContext.ts';
import { useMobile } from '~hooks/index.ts';
Expand All @@ -14,10 +15,18 @@ import Link from '~v5/shared/Link/index.ts';
const displayName =
'common.Extensions.UserNavigation.partials.WalletConnectedTopMenu';

const MSG = defineMessages({
manageEmbedded: {
id: `${displayName}.manageEmbedded`,
defaultMessage: 'Manage wallet',
},
});

const WalletConnectedTopMenu: FC<PropsWithChildren> = ({ children }) => {
const { formatMessage } = useIntl();
const { user } = useAppContext();
const { user, wallet } = useAppContext();
const isMobile = useMobile();
const { setShowAuthFlow } = useDynamicContext();

const iconSize = isMobile ? 18 : 16;

Expand Down Expand Up @@ -45,6 +54,22 @@ const WalletConnectedTopMenu: FC<PropsWithChildren> = ({ children }) => {
</p>
</Link>
)}
{/*
* Only show the manage embedded wallet link if the wallet is embedded / custodial
* All the other wallet types have their own interface
*/}
{wallet?.label === 'turnkeyhd' && (
<div className="navigation-link -ml-4 w-[calc(100%+2rem)] rounded hover:bg-gray-50">
<Wallet size={iconSize} />
<button
type="button"
className="ml-2"
onClick={() => setShowAuthFlow(true)}
>
{formatMessage(MSG.manageEmbedded)}
</button>
</div>
)}
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/frame/LandingPage/LandingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const LandingPage = () => {
bottomComponent={
isContentLoading ? (
<LoadingSkeleton
isLoading
// isLoading
className="m-6 h-[2.125rem] w-full rounded md:hidden"
/>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ const MSG = defineMessages({
});

const UserAdvancedPage = () => {
const { user, userLoading, walletConnecting } = useAppContext();
const { user, userLoading, walletConnecting, willWalletAutoConnect } =
useAppContext();

useSetPageHeadingTitle(formatText({ id: 'advancedSettings.title' }));

if (userLoading || walletConnecting) {
if (userLoading || walletConnecting || (!user && willWalletAutoConnect)) {
return <LoadingTemplate loadingText={MSG.loadingText} />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const MSG = defineMessages({
});

const UserCryptoToFiatPage = () => {
const { user, userLoading, walletConnecting } = useAppContext();
const { user, userLoading, walletConnecting, willWalletAutoConnect } =
useAppContext();
const featureFlags = useContext(FeatureFlagsContext);
const cryptoToFiatFeatureFlag = featureFlags[FeatureFlag.CRYPTO_TO_FIAT];

Expand All @@ -45,7 +46,8 @@ const UserCryptoToFiatPage = () => {
return <Navigate to={`${USER_HOME_ROUTE}/${USER_EDIT_PROFILE_ROUTE}`} />;
}

const isLoadingUserAndWalletInfo = userLoading || walletConnecting;
const isLoadingUserAndWalletInfo =
userLoading || walletConnecting || (!user && willWalletAutoConnect);

if (!isLoadingUserAndWalletInfo && !user) {
return <Navigate to={LANDING_PAGE_ROUTE} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ const MSG = defineMessages({
});

const UserPreferencesPage: FC = () => {
const { user, userLoading, walletConnecting } = useAppContext();
const { user, userLoading, walletConnecting, willWalletAutoConnect } =
useAppContext();

useSetPageHeadingTitle(formatText({ id: 'userPreferencesPage.title' }));

if (userLoading || walletConnecting) {
if (userLoading || walletConnecting || (!user && willWalletAutoConnect)) {
return <LoadingTemplate loadingText={MSG.loadingText} />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ const MSG = defineMessages({
});

const UserAccountPage: FC = () => {
const { user, userLoading, walletConnecting } = useAppContext();
const { user, userLoading, walletConnecting, willWalletAutoConnect } =
useAppContext();
const { handleSubmit } = useUserProfile();

useSetPageHeadingTitle(formatText({ id: 'userProfileTab.title' }));

if (userLoading || walletConnecting) {
if (userLoading || walletConnecting || (!user && willWalletAutoConnect)) {
return <LoadingTemplate loadingText={MSG.loadingText} />;
}

Expand Down
Loading
Loading