refactor(react-dapp-v2): replaces legacy qrcodeModal with web3modalV2 (#78)

This commit is contained in:
Ben Kremer 2022-11-15 10:44:02 +01:00 committed by GitHub
parent 9ffdf676a1
commit 99ef432c76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 684 additions and 28 deletions

View File

@ -16,10 +16,12 @@
"@polkadot/util-crypto": "^10.1.2", "@polkadot/util-crypto": "^10.1.2",
"@solana/web3.js": "^1.36.0", "@solana/web3.js": "^1.36.0",
"@walletconnect/encoding": "^1.0.1", "@walletconnect/encoding": "^1.0.1",
"@walletconnect/qrcode-modal": "^1.7.8",
"@walletconnect/sign-client": "2.1.3", "@walletconnect/sign-client": "2.1.3",
"@walletconnect/types": "2.1.3", "@walletconnect/types": "2.1.3",
"@walletconnect/utils": "2.1.3", "@walletconnect/utils": "2.1.3",
"@web3modal/core": "2.0.0-beta.4",
"@web3modal/ethereum": "2.0.0-beta.4",
"@web3modal/ui": "2.0.0-beta.4",
"axios": "^0.21.1", "axios": "^0.21.1",
"blockies-ts": "^1.0.0", "blockies-ts": "^1.0.0",
"bs58": "^5.0.0", "bs58": "^5.0.0",

View File

@ -1,3 +1,6 @@
if (!process.env.NEXT_PUBLIC_PROJECT_ID)
throw new Error("`NEXT_PUBLIC_PROJECT_ID` env variable is missing.");
export const DEFAULT_MAIN_CHAINS = [ export const DEFAULT_MAIN_CHAINS = [
// mainnets // mainnets
"eip155:1", "eip155:1",

View File

@ -1,6 +1,9 @@
import Client from "@walletconnect/sign-client"; import Client from "@walletconnect/sign-client";
import { PairingTypes, SessionTypes } from "@walletconnect/types"; import { PairingTypes, SessionTypes } from "@walletconnect/types";
import QRCodeModal from "@walletconnect/qrcode-modal"; import { ConfigCtrl as ModalConfigCtrl, ModalCtrl } from "@web3modal/core";
import type { W3mModal } from "@web3modal/ui";
import "@web3modal/ui";
import { import {
createContext, createContext,
ReactNode, ReactNode,
@ -49,6 +52,14 @@ interface IContext {
*/ */
export const ClientContext = createContext<IContext>({} as IContext); export const ClientContext = createContext<IContext>({} as IContext);
/**
* Web3Modal Config
*/
ModalConfigCtrl.setConfig({
projectId: DEFAULT_PROJECT_ID,
theme: "light" as const,
});
/** /**
* Provider * Provider
*/ */
@ -142,9 +153,12 @@ export function ClientContextProvider({
// Open QRCode modal if a URI was returned (i.e. we're not connecting an existing pairing). // Open QRCode modal if a URI was returned (i.e. we're not connecting an existing pairing).
if (uri) { if (uri) {
QRCodeModal.open(uri, () => { // Create a flat array of all requested chains across namespaces.
console.log("EVENT", "QR Code Modal closed"); const standaloneChains = Object.values(requiredNamespaces)
}); .map((namespace) => namespace.chains)
.flat();
ModalCtrl.open({ uri, standaloneChains });
} }
const session = await approval(); const session = await approval();
@ -157,7 +171,7 @@ export function ClientContextProvider({
// ignore rejection // ignore rejection
} finally { } finally {
// close modal in case it was open // close modal in case it was open
QRCodeModal.close(); ModalCtrl.close();
} }
}, },
[chains, client, onSessionConnected] [chains, client, onSessionConnected]
@ -306,11 +320,23 @@ export function ClientContextProvider({
...value, ...value,
}} }}
> >
{children} <>
{children}
<w3m-modal></w3m-modal>
</>
</ClientContext.Provider> </ClientContext.Provider>
); );
} }
// Let Typescript know about the custom w3m-modal dom / webcomponent element
declare global {
namespace JSX {
interface IntrinsicElements {
"w3m-modal": Partial<W3mModal>;
}
}
}
export function useWalletConnectClient() { export function useWalletConnectClient() {
const context = useContext(ClientContext); const context = useContext(ClientContext);
if (context === undefined) { if (context === undefined) {

File diff suppressed because it is too large Load Diff