forked from thirdweb-dev/js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wallet, react] Add Crypto.com Defi wallet (thirdweb-dev#1928)
Co-authored-by: Manan Tank <[email protected]>
- Loading branch information
Showing
20 changed files
with
623 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@thirdweb-dev/react-core": patch | ||
"@thirdweb-dev/wallets": patch | ||
"@thirdweb-dev/react": patch | ||
--- | ||
|
||
Add Crypto.com Defi wallet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
packages/react/src/wallet/wallets/defiWallet/CryptoDefiWalletConnectUI.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { ConnectUIProps, useConnect } from "@thirdweb-dev/react-core"; | ||
import { ConnectingScreen } from "../../ConnectWallet/screens/ConnectingScreen"; | ||
import { isMobile } from "../../../evm/utils/isMobile"; | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import { CryptoDefiWalletScan } from "./CryptoDefiWalletScan"; | ||
import { GetStartedScreen } from "../../ConnectWallet/screens/GetStartedScreen"; | ||
import type { CryptoDefiWallet } from "@thirdweb-dev/wallets"; | ||
import { wait } from "../../../utils/wait"; | ||
import { useTWLocale } from "../../../evm/providers/locale-provider"; | ||
import { WCOpenURI } from "../../ConnectWallet/screens/WCOpenUri"; | ||
import { cryptoDefiWalletUris } from "./cryptoDefiWalletUris"; | ||
|
||
export const CryptoDefiWalletConnectUI = ( | ||
props: ConnectUIProps<CryptoDefiWallet>, | ||
) => { | ||
const [screen, setScreen] = useState< | ||
"connecting" | "scanning" | "get-started" | "open-wc-uri" | ||
>("connecting"); | ||
const locale = useTWLocale().wallets.cryptoDefiWallet; | ||
const connectingLocale = { | ||
getStartedLink: locale.getStartedLink, | ||
instruction: locale.connectionScreen.instruction, | ||
tryAgain: locale.connectionScreen.retry, | ||
inProgress: locale.connectionScreen.inProgress, | ||
failed: locale.connectionScreen.failed, | ||
}; | ||
const { walletConfig, connected } = props; | ||
const connect = useConnect(); | ||
const [errorConnecting, setErrorConnecting] = useState(false); | ||
|
||
const hideBackButton = props.supportedWallets.length === 1; | ||
|
||
const connectToExtension = useCallback(async () => { | ||
try { | ||
connectPrompted.current = true; | ||
setErrorConnecting(false); | ||
setScreen("connecting"); | ||
await wait(1000); | ||
await connect(walletConfig); | ||
connected(); | ||
} catch (e) { | ||
setErrorConnecting(true); | ||
console.error(e); | ||
} | ||
}, [connected, connect, walletConfig]); | ||
|
||
const connectPrompted = useRef(false); | ||
useEffect(() => { | ||
if (connectPrompted.current) { | ||
return; | ||
} | ||
|
||
const isInstalled = walletConfig.isInstalled | ||
? walletConfig.isInstalled() | ||
: false; | ||
|
||
// if loading | ||
(async () => { | ||
if (isInstalled) { | ||
connectToExtension(); | ||
} | ||
|
||
// if wallet is not injected | ||
else { | ||
// on mobile, open the Defi Mobile via wallet connect | ||
if (isMobile()) { | ||
setScreen("open-wc-uri"); | ||
} else { | ||
// on desktop, show the Defi app scan qr code | ||
setScreen("scanning"); | ||
} | ||
} | ||
})(); | ||
}, [connectToExtension, walletConfig]); | ||
|
||
if (screen === "connecting") { | ||
return ( | ||
<ConnectingScreen | ||
locale={{ | ||
getStartedLink: locale.getStartedLink, | ||
instruction: locale.connectionScreen.instruction, | ||
tryAgain: locale.connectionScreen.retry, | ||
inProgress: locale.connectionScreen.inProgress, | ||
failed: locale.connectionScreen.failed, | ||
}} | ||
errorConnecting={errorConnecting} | ||
onGetStarted={() => { | ||
setScreen("get-started"); | ||
}} | ||
onRetry={connectToExtension} | ||
hideBackButton={hideBackButton} | ||
onBack={props.goBack} | ||
walletName={walletConfig.meta.name} | ||
walletIconURL={walletConfig.meta.iconURL} | ||
/> | ||
); | ||
} | ||
|
||
if (screen === "open-wc-uri") { | ||
return ( | ||
<WCOpenURI | ||
locale={connectingLocale} | ||
onRetry={() => { | ||
// NOOP - TODO make onRetry optional | ||
}} | ||
errorConnecting={errorConnecting} | ||
onGetStarted={() => { | ||
setScreen("get-started"); | ||
}} | ||
hideBackButton={hideBackButton} | ||
onBack={props.goBack} | ||
onConnected={connected} | ||
walletConfig={walletConfig} | ||
appUriPrefix={cryptoDefiWalletUris} | ||
/> | ||
); | ||
} | ||
|
||
if (screen === "get-started") { | ||
return ( | ||
<GetStartedScreen | ||
locale={{ | ||
scanToDownload: locale.getStartedScreen.instruction, | ||
}} | ||
walletIconURL={walletConfig.meta.iconURL} | ||
walletName={walletConfig.meta.name} | ||
chromeExtensionLink={walletConfig.meta.urls?.chrome} | ||
googlePlayStoreLink={walletConfig.meta.urls?.android} | ||
appleStoreLink={walletConfig.meta.urls?.ios} | ||
onBack={props.goBack} | ||
/> | ||
); | ||
} | ||
|
||
if (screen === "scanning") { | ||
return ( | ||
<CryptoDefiWalletScan | ||
onBack={props.goBack} | ||
onConnected={props.connected} | ||
onGetStarted={() => { | ||
setScreen("get-started"); | ||
}} | ||
hideBackButton={hideBackButton} | ||
walletConfig={walletConfig} | ||
/> | ||
); | ||
} | ||
|
||
return null; | ||
}; |
65 changes: 65 additions & 0 deletions
65
packages/react/src/wallet/wallets/defiWallet/CryptoDefiWalletScan.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { ScanScreen } from "../../ConnectWallet/screens/ScanScreen"; | ||
import { | ||
useCreateWalletInstance, | ||
useWalletContext, | ||
} from "@thirdweb-dev/react-core"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import type { CryptoDefiWallet } from "@thirdweb-dev/wallets"; | ||
import type { WalletConfig } from "@thirdweb-dev/react-core"; | ||
import { useTWLocale } from "../../../evm/providers/locale-provider"; | ||
|
||
export const CryptoDefiWalletScan: React.FC<{ | ||
onBack: () => void; | ||
onGetStarted: () => void; | ||
onConnected: () => void; | ||
walletConfig: WalletConfig<CryptoDefiWallet>; | ||
hideBackButton: boolean; | ||
}> = ({ onBack, onConnected, onGetStarted, walletConfig, hideBackButton }) => { | ||
const locale = useTWLocale().wallets.cryptoDefiWallet; | ||
const createInstance = useCreateWalletInstance(); | ||
const [qrCodeUri, setQrCodeUri] = useState<string | undefined>(); | ||
const { setConnectedWallet, chainToConnect, setConnectionStatus } = | ||
useWalletContext(); | ||
|
||
const scanStarted = useRef(false); | ||
useEffect(() => { | ||
if (scanStarted.current) { | ||
return; | ||
} | ||
scanStarted.current = true; | ||
|
||
const wallet = createInstance(walletConfig); | ||
|
||
setConnectionStatus("connecting"); | ||
wallet.connectWithQrCode({ | ||
chainId: chainToConnect?.chainId, | ||
onQrCodeUri(uri) { | ||
setQrCodeUri(uri); | ||
}, | ||
onConnected() { | ||
setConnectedWallet(wallet); | ||
onConnected(); | ||
}, | ||
}); | ||
}, [ | ||
createInstance, | ||
setConnectedWallet, | ||
chainToConnect, | ||
onConnected, | ||
walletConfig, | ||
setConnectionStatus, | ||
]); | ||
|
||
return ( | ||
<ScanScreen | ||
qrScanInstruction={locale.scanScreen.instruction} | ||
onBack={onBack} | ||
onGetStarted={onGetStarted} | ||
qrCodeUri={qrCodeUri} | ||
walletName={walletConfig.meta.name} | ||
walletIconURL={walletConfig.meta.iconURL} | ||
hideBackButton={hideBackButton} | ||
getStartedLink={locale.getStartedLink} | ||
/> | ||
); | ||
}; |
55 changes: 55 additions & 0 deletions
55
packages/react/src/wallet/wallets/defiWallet/cryptoDefiWallet.tsx
Large diffs are not rendered by default.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
packages/react/src/wallet/wallets/defiWallet/cryptoDefiWalletUris.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const cryptoDefiWalletUris = { | ||
ios: "dfw://", | ||
android: "dfw://", | ||
other: "dfw://", | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/wallets/evm/connectors/crypto-defi-wallet/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"main": "dist/thirdweb-dev-wallets-evm-connectors-crypto-defi-wallet.cjs.js", | ||
"module": "dist/thirdweb-dev-wallets-evm-connectors-crypto-defi-wallet.esm.js", | ||
"browser": { | ||
"./dist/thirdweb-dev-wallets-evm-connectors-crypto-defi-wallet.esm.js": "./dist/thirdweb-dev-wallets-evm-connectors-crypto-defi-wallet.browser.esm.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"main": "dist/thirdweb-dev-wallets-evm-wallets-crypto-defi-wallet.cjs.js", | ||
"module": "dist/thirdweb-dev-wallets-evm-wallets-crypto-defi-wallet.esm.js", | ||
"browser": { | ||
"./dist/thirdweb-dev-wallets-evm-wallets-crypto-defi-wallet.esm.js": "./dist/thirdweb-dev-wallets-evm-wallets-crypto-defi-wallet.browser.esm.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ages/wallets/src/evm/connectors/crypto-defi-wallet/getInjectedCryptoDefiWalletProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Ethereum } from "../injected/types"; | ||
import { assertWindowEthereum } from "../../utils/assertWindowEthereum"; | ||
|
||
declare global { | ||
interface Window { | ||
deficonnectProvider?: Ethereum; | ||
} | ||
} | ||
|
||
export function getInjectedCryptoDefiWalletProvider(): Ethereum | undefined { | ||
if (typeof window === "undefined") { | ||
return; | ||
} | ||
|
||
if (assertWindowEthereum(globalThis.window)) { | ||
if (globalThis.window.ethereum && window.deficonnectProvider) { | ||
return window.deficonnectProvider; | ||
} | ||
} | ||
} |
Oops, something went wrong.