-
Notifications
You must be signed in to change notification settings - Fork 79
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
Sdk-dapp-core migration #264
base: development
Are you sure you want to change the base?
Changes from all commits
4721146
909c58d
2e891ee
8b44297
d0e74e8
1347c1a
51731f8
45b67c7
67893ec
7063ab9
bf45f4e
dae68f4
e7e0abd
e2f9072
37f11bc
cdc8f15
f26a5d6
2b25485
34b4eb7
9ff7517
f8bb725
f85cc39
dbcdd4b
675c8a2
aaa76e9
8ace13f
28b71f7
85fcc56
2406187
d2bce86
768aed0
85326cc
092bda2
6cdd470
2c7cee3
48e5532
8caa0a5
b0a8ba1
1ee026f
7df59ca
dfe11c2
9c48535
da161fa
187c7d1
c9e7783
97ca156
dc5f52f
415b8ef
afab2a2
5b91d19
9bbdb0f
3d2ed82
5667113
bc806b8
fee1781
e243a6e
5dd6f8f
87db609
c17be28
68aed91
f4954e7
4099be3
e873509
e8eeb9f
2ccb759
f2814ae
2fa946e
dc51e44
1258568
e886857
cb96ca1
60a18ab
91dde85
99b1ed5
bd95e23
3886c39
40277be
2936b50
289a178
198c894
73a92ac
973a5ee
14bc613
5de2dc2
45b833a
b6805e0
88dd8bd
7420888
6a06d9a
51ee890
ca1a82a
34c307c
9848014
114d233
5c3a1bd
77a0f00
687245b
cb2ec7b
4b6f917
e73457d
cd8e631
5dd0773
29834bb
03aeea0
b07f1a6
73c3b74
daf36d0
c007402
fd23313
fedff79
a73aeb0
2bf0a2d
434c596
9ca1fcd
1335f5b
5d9430a
3a757c2
50271da
a11280e
9439832
98cfc6d
33e46b3
017714d
0101c02
43512ca
53ac3f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# @multiversx/template-dapp | ||
|
||
Migrating your application to the new SDK will probably require more code removal than insertion. Of course, each application is different, so this guide will have a generic character, pointing to you the main areas where you might need to change your code. | ||
The main areas that might need code restructuring are: | ||
- package.json library dependencies | ||
- index.tsx file where the app bootstraps | ||
- App.tsx, the main file of your application | ||
- logging in and out | ||
- sending and signing transactions | ||
- component imports related to displaying data | ||
- types | ||
|
||
A typical migration can be seen in this [pull request](https://github.com/multiversx/mx-template-dapp/pull/264). | ||
|
||
Next, we will make a brief overview of the main changes required. | ||
|
||
## 1. Changes in [`package.json`](https://github.com/multiversx/mx-template-dapp/pull/264/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519) | ||
|
||
#### Dependencies | ||
- Removed: | ||
- `@multiversx/sdk-dapp` | ||
- Added: | ||
- `@multiversx/sdk-dapp-core` (version `0.0.0-alpha.12`) | ||
- `@multiversx/sdk-dapp-core-ui` (version `0.0.0-alpha.2`) | ||
- `@multiversx/sdk-dapp-utils` (version `1.0.5`) | ||
|
||
## 2. Changes in [`index.tsx`](https://github.com/multiversx/mx-template-dapp/pull/264/files#diff-0b5adbfe7b36e4ae2f479291e20152e33e940f7f265162d77f40f6bdb5da7405) | ||
|
||
You will need to wrap your application in a call to `initApp`: | ||
|
||
```tsx | ||
initApp(config).then(() => { | ||
ReactDOM.createRoot(document.getElementById('root')!).render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode> | ||
); | ||
}); | ||
``` | ||
|
||
## 2. Summary of Changes in [`App.tsx`](https://github.com/multiversx/mx-template-dapp/pull/264/files#diff-26ad4b834941d9b19ebf9db8082bd202aaf72ea0ddea85f5a8a0cb3c729cc6f2) | ||
|
||
- AxiosInterceptorContext is now created as a local component | ||
- DappProvider was removed and initialization is now made in initApp | ||
- NotificationModal, SignTransactionsModals and TransactionsToastList are removed and functionality is handled under the hood | ||
|
||
## 3. Logging in and out: | ||
|
||
Login buttons have been removed and there is a universal Unlock side panel, which can be inserted in the DOM at a desired location. | ||
|
||
Example of how to use the Unlock side panel: | ||
|
||
```tsx | ||
import { Button } from 'components'; | ||
import { useState } from 'react'; | ||
import { ProviderFactory } from 'lib'; | ||
export { UnlockPanel, UnlockButton } from '@multiversx/sdk-dapp-core-ui/react'; | ||
import { ExtendedProviders } from 'initConfig'; | ||
import { IProviderFactory } from '@multiversx/sdk-dapp-core/out/core/providers/types/providerFactory.types'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { RouteNamesEnum } from 'localConstants'; | ||
|
||
export const ConnectButton = () => { | ||
const [open, setOpen] = useState(false); | ||
const navigate = useNavigate(); | ||
|
||
const handleLogin = async ({ type, anchor }: IProviderFactory) => { | ||
const provider = await ProviderFactory.create({ | ||
type, | ||
anchor | ||
}); | ||
await provider?.login(); | ||
navigate(RouteNamesEnum.dashboard); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button onClick={() => setOpen(true)}>Connect</Button> | ||
<UnlockPanel | ||
open={open} | ||
onLogin={(options) => | ||
handleLogin({ | ||
type: options.detail.provider, | ||
anchor: options.detail.anchor | ||
}) | ||
} | ||
onClose={() => { | ||
setOpen(false); | ||
}} | ||
> | ||
<UnlockButton | ||
label='In Memory Provider' | ||
onClick={() => | ||
handleLogin({ | ||
type: ExtendedProviders.inMemoryProvider | ||
}) | ||
} | ||
/> | ||
</UnlockButton> | ||
</> | ||
); | ||
}; | ||
``` | ||
|
||
## 4. [Sending transactions](https://github.com/multiversx/mx-template-dapp/pull/264/files#diff-1eadd6ccf43da9c2a8c30f5dfddbb56f3daeec28f04e43b41c134811bec478fb) | ||
- `sendTransactions` has been replaced by `provider.signTransactions`, `txManager.send` and `txManager.track` | ||
- `newTransaction` function has been removed and you need to create `Transaction` objects directly | ||
|
||
## 5. [UI components get imported from sdk-dapp-core-ui](https://github.com/multiversx/mx-template-dapp/pull/264/files#diff-e07cb98fcda2927e31f2a0f6cc5db5cb9a364c8da43d8df70597321bb1558336) | ||
- see `src/lib/sdkDapp/components/CopyButton/CopyButton.tsx` | ||
|
||
### URL login is no longer supported | ||
- see TODO how to login with PostMessage | ||
|
||
## 6. [Types](https://github.com/multiversx/mx-template-dapp/pull/264/files#diff-bd634780b000031ebfffb83de483b66a93ed12fde321950128e31a794ce96057) | ||
- `LoginMethodsEnum` is replaced by `ProviderTypeEnum` | ||
- `RawTransactionType` was removed | ||
- some new types were added, related to UI elements, like `FormatAmountControllerPropsType` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,28 @@ | ||
import { Route, BrowserRouter as Router, Routes } from 'react-router-dom'; | ||
import { Layout } from 'components'; | ||
import { | ||
apiTimeout, | ||
environment, | ||
sampleAuthenticatedDomains, | ||
walletConnectV2ProjectId | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use this walletConnectV2ProjectId @razvantomegea |
||
} from 'config'; | ||
import { | ||
AxiosInterceptorContext, // using this is optional | ||
DappProvider, | ||
NotificationModal, | ||
SignTransactionsModals, | ||
TransactionsToastList | ||
// uncomment this to use the custom transaction tracker | ||
// TransactionsTracker | ||
} from 'lib'; | ||
import { RouteNamesEnum } from 'localConstants'; | ||
import { PageNotFound } from 'pages'; | ||
import { PageNotFound } from 'pages/PageNotFound/PageNotFound'; | ||
import { routes } from 'routes'; | ||
import { BatchTransactionsContextProvider } from 'wrappers'; | ||
|
||
const AppContent = () => { | ||
return ( | ||
<DappProvider | ||
environment={environment} | ||
customNetworkConfig={{ | ||
name: 'customConfig', | ||
apiTimeout, | ||
walletConnectV2ProjectId | ||
}} | ||
dappConfig={{ | ||
shouldUseWebViewProvider: true, | ||
logoutRoute: RouteNamesEnum.unlock | ||
}} | ||
customComponents={{ | ||
transactionTracker: { | ||
// uncomment this to use the custom transaction tracker | ||
// component: TransactionsTracker, | ||
props: { | ||
onSuccess: (sessionId: string) => { | ||
console.log(`Session ${sessionId} successfully completed`); | ||
}, | ||
onFail: (sessionId: string, errorMessage: string) => { | ||
console.log(`Session ${sessionId} failed. ${errorMessage ?? ''}`); | ||
} | ||
} | ||
} | ||
}} | ||
> | ||
<AxiosInterceptorContext.Listener> | ||
<Layout> | ||
<TransactionsToastList /> | ||
<NotificationModal /> | ||
<SignTransactionsModals /> | ||
<Routes> | ||
{routes.map((route) => ( | ||
<Route | ||
path={route.path} | ||
key={`route-key-'${route.path}`} | ||
element={<route.component />} | ||
/> | ||
))} | ||
<Route path='*' element={<PageNotFound />} /> | ||
arhtudormorar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</Routes> | ||
</Layout> | ||
</AxiosInterceptorContext.Listener> | ||
</DappProvider> | ||
); | ||
}; | ||
import { AxiosInterceptors, BatchTransactionsContextProvider } from 'wrappers'; | ||
import { Layout } from './components'; | ||
|
||
export const App = () => { | ||
return ( | ||
<AxiosInterceptorContext.Provider> | ||
<AxiosInterceptorContext.Interceptor | ||
authenticatedDomains={sampleAuthenticatedDomains} | ||
> | ||
<Router> | ||
<BatchTransactionsContextProvider> | ||
<AppContent /> | ||
</BatchTransactionsContextProvider> | ||
</Router> | ||
</AxiosInterceptorContext.Interceptor> | ||
</AxiosInterceptorContext.Provider> | ||
<Router> | ||
<AxiosInterceptors> | ||
<BatchTransactionsContextProvider> | ||
<Layout> | ||
<Routes> | ||
{routes.map((route) => ( | ||
<Route | ||
key={`route-key-'${route.path}`} | ||
path={route.path} | ||
element={<route.component />} | ||
/> | ||
))} | ||
<Route path='*' element={<PageNotFound />} /> | ||
</Routes> | ||
</Layout> | ||
</BatchTransactionsContextProvider> | ||
</AxiosInterceptors> | ||
</Router> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,39 @@ | ||
import { useMatch } from 'react-router-dom'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { Button, MxLink } from 'components'; | ||
import { environment } from 'config'; | ||
import { logout, useGetIsLoggedIn } from 'lib'; | ||
import { | ||
getAccountProvider, | ||
ProviderFactory, | ||
ProviderTypeEnum, | ||
useGetIsLoggedIn, | ||
UnlockButtonSDK, | ||
UnlockPanelSDK | ||
} from 'lib'; | ||
import { RouteNamesEnum } from 'localConstants'; | ||
import MultiversXLogo from '../../../assets/img/multiversx-logo.svg?react'; | ||
|
||
const callbackUrl = `${window.location.origin}/unlock`; | ||
const onRedirect = undefined; // use this to redirect with useNavigate to a specific page after logout | ||
const shouldAttemptReLogin = false; // use for special cases where you want to re-login after logout | ||
const options = { | ||
/* | ||
* @param {boolean} [shouldBroadcastLogoutAcrossTabs=true] | ||
* @description If your dApp supports multiple accounts on multiple tabs, | ||
* this param will broadcast the logout event across all tabs. | ||
*/ | ||
shouldBroadcastLogoutAcrossTabs: true, | ||
/* | ||
* @param {boolean} [hasConsentPopup=false] | ||
* @description Set it to true if you want to perform async calls before logging out on Safari. | ||
* It will open a consent popup for the user to confirm the action before leaving the page. | ||
*/ | ||
hasConsentPopup: false | ||
}; | ||
import { useState } from 'react'; | ||
import { ExtendedProviders } from 'initConfig'; | ||
import { IProviderFactory } from '@multiversx/sdk-dapp-core/out/core/providers/types/providerFactory.types'; | ||
|
||
export const Header = () => { | ||
const isLoggedIn = useGetIsLoggedIn(); | ||
const isUnlockRoute = Boolean(useMatch(RouteNamesEnum.unlock)); | ||
const navigate = useNavigate(); | ||
const provider = getAccountProvider(); | ||
const [open, setOpen] = useState(false); | ||
|
||
const ConnectButton = isUnlockRoute ? null : ( | ||
<MxLink to={RouteNamesEnum.unlock}>Connect</MxLink> | ||
); | ||
const handleLogout = async () => { | ||
await provider.logout(); | ||
navigate(RouteNamesEnum.home); | ||
}; | ||
|
||
const handleLogout = () => { | ||
sessionStorage.clear(); | ||
logout( | ||
callbackUrl, | ||
/* | ||
* following are optional params. Feel free to remove them in your implementation | ||
*/ | ||
onRedirect, | ||
shouldAttemptReLogin, | ||
options | ||
); | ||
const handleLogin = async ({ type, anchor }: IProviderFactory) => { | ||
const provider = await ProviderFactory.create({ | ||
type, | ||
anchor | ||
Check failure on line 32 in src/components/Layout/Header/Header.tsx
|
||
}); | ||
await provider?.login(); | ||
setOpen(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check |
||
navigate(RouteNamesEnum.dashboard); | ||
}; | ||
|
||
return ( | ||
|
@@ -68,8 +60,29 @@ | |
Close | ||
</Button> | ||
) : ( | ||
ConnectButton | ||
<Button onClick={() => setOpen(true)}>Connect</Button> | ||
)} | ||
<UnlockPanelSDK | ||
open={open} | ||
onLogin={(options) => | ||
handleLogin({ | ||
type: options.detail.provider, | ||
anchor: options.detail.anchor | ||
}) | ||
} | ||
onClose={() => { | ||
setOpen(false); | ||
}} | ||
> | ||
<UnlockButtonSDK | ||
label='In Memory Provider' | ||
onClick={() => | ||
handleLogin({ | ||
type: ExtendedProviders.inMemoryProvider | ||
}) | ||
} | ||
/> | ||
</UnlockPanelSDK> | ||
</div> | ||
</nav> | ||
</header> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { faSpinner } from '@fortawesome/free-solid-svg-icons'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
|
||
export const Loader = () => { | ||
return ( | ||
<div className='flex justify-center items-center h-screen'> | ||
<FontAwesomeIcon | ||
icon={faSpinner} | ||
spin | ||
className='text-4xl text-blue-500' | ||
/> | ||
</div> | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bring back or use in promise.all @razvantomegea