Skip to content

Commit

Permalink
[wallet, react] Add Crypto.com Defi wallet (thirdweb-dev#1928)
Browse files Browse the repository at this point in the history
Co-authored-by: Manan Tank <[email protected]>
  • Loading branch information
Kien Ngo and MananTank authored Nov 27, 2023
1 parent e79b293 commit 2b4f1c8
Show file tree
Hide file tree
Showing 20 changed files with 623 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/wicked-ladybugs-repeat.md
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
2 changes: 2 additions & 0 deletions packages/react-core/src/core/hooks/wallet-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
Coin98Wallet,
CoinbaseWallet,
CoreWallet,
CryptoDefiWallet,
EmbeddedWallet,
FrameWallet,
LocalWallet,
Expand Down Expand Up @@ -46,6 +47,7 @@ type WalletIdToWalletTypeMap = {
phantom: PhantomWallet;
walletConnectV1: WalletConnect;
okx: OKXWallet;
cryptoDefiWallet: CryptoDefiWallet;
rabby: RabbyWallet;
};

Expand Down
1 change: 1 addition & 0 deletions packages/react/src/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export {
InjectedWallet,
setWalletAnalyticsEnabled,
CoreWallet,
CryptoDefiWallet,
RabbyWallet,
Coin98Wallet,
} from "@thirdweb-dev/wallets";
1 change: 1 addition & 0 deletions packages/react/src/evm/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export function enDefault() {
trustWallet: extensionAndQRScanScreens("Trust"),
zerionWallet: extensionAndQRScanScreens("Zerion"),
coreWallet: extensionAndQRScanScreens("Core"),
cryptoDefiWallet: extensionAndQRScanScreens("Crypto Defi"),
rabbyWallet: extensionAndQRScanScreens("Rabby"),
coin98Wallet: extensionAndQRScanScreens("Coin98"),
paperWallet: {
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/evm/locales/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export function esDefault(): ThirdwebLocale {
rainbowWallet: extensionAndQRScanScreens("Rainbow"),
trustWallet: extensionAndQRScanScreens("Trust"),
zerionWallet: extensionAndQRScanScreens("Zerion"),
cryptoDefiWallet: extensionAndQRScanScreens("Crypto Defi"),
rabbyWallet: extensionAndQRScanScreens("Rabby"),
paperWallet: {
signIn: "Iniciar sesión",
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/evm/locales/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export function jaDefault(): ThirdwebLocale {
rainbowWallet: extensionAndQRScanScreens("Rainbow"),
trustWallet: extensionAndQRScanScreens("Trust"),
zerionWallet: extensionAndQRScanScreens("Zerion"),
cryptoDefiWallet: extensionAndQRScanScreens("Crypto Defi"),
rabbyWallet: extensionAndQRScanScreens("Rabby"),
paperWallet: {
signIn: "サインイン",
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export { walletConnectV1 } from "./wallet/wallets/walletConnectV1";
export { zerionWallet } from "./wallet/wallets/zerion/zerionWallet";
export { okxWallet } from "./wallet/wallets/okx/okxWallet";
export { coreWallet } from "./wallet/wallets/coreWallet/coreWallet";
export { cryptoDefiWallet } from "./wallet/wallets/defiWallet/cryptoDefiWallet";
export { rabbyWallet } from "./wallet/wallets/rabby/rabbyWallet";
export { coin98Wallet } from "./wallet/wallets/coin98/coin98Wallet";
export { darkTheme, lightTheme } from "./design-system/index";
Expand Down
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;
};
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 packages/react/src/wallet/wallets/defiWallet/cryptoDefiWallet.tsx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const cryptoDefiWalletUris = {
ios: "dfw://",
android: "dfw://",
other: "dfw://",
};
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"
}
}
7 changes: 7 additions & 0 deletions packages/wallets/evm/wallets/crypto-defi-wallet/package.json
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"
}
}
14 changes: 14 additions & 0 deletions packages/wallets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@
},
"default": "./evm/wallets/wallet-connect-v1/dist/thirdweb-dev-wallets-evm-wallets-wallet-connect-v1.cjs.js"
},
"./evm/wallets/crypto-defi-wallet": {
"module": {
"browser": "./evm/wallets/crypto-defi-wallet/dist/thirdweb-dev-wallets-evm-wallets-crypto-defi-wallet.browser.esm.js",
"default": "./evm/wallets/crypto-defi-wallet/dist/thirdweb-dev-wallets-evm-wallets-crypto-defi-wallet.esm.js"
},
"default": "./evm/wallets/crypto-defi-wallet/dist/thirdweb-dev-wallets-evm-wallets-crypto-defi-wallet.cjs.js"
},
"./evm/wallets/aws-secrets-manager": {
"module": {
"browser": "./evm/wallets/aws-secrets-manager/dist/thirdweb-dev-wallets-evm-wallets-aws-secrets-manager.browser.esm.js",
Expand Down Expand Up @@ -406,6 +413,13 @@
},
"default": "./evm/connectors/wallet-connect-v1/dist/thirdweb-dev-wallets-evm-connectors-wallet-connect-v1.cjs.js"
},
"./evm/connectors/crypto-defi-wallet": {
"module": {
"browser": "./evm/connectors/crypto-defi-wallet/dist/thirdweb-dev-wallets-evm-connectors-crypto-defi-wallet.browser.esm.js",
"default": "./evm/connectors/crypto-defi-wallet/dist/thirdweb-dev-wallets-evm-connectors-crypto-defi-wallet.esm.js"
},
"default": "./evm/connectors/crypto-defi-wallet/dist/thirdweb-dev-wallets-evm-connectors-crypto-defi-wallet.cjs.js"
},
"./evm/connectors/token-bound-smart-wallet": {
"module": {
"browser": "./evm/connectors/token-bound-smart-wallet/dist/thirdweb-dev-wallets-evm-connectors-token-bound-smart-wallet.browser.esm.js",
Expand Down
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;
}
}
}
Loading

0 comments on commit 2b4f1c8

Please sign in to comment.