From ef53c48d8b8af2c4f6ea7317c3006b0701ef4527 Mon Sep 17 00:00:00 2001 From: Erno Wever Date: Wed, 9 Feb 2022 17:57:32 +0000 Subject: [PATCH 1/4] feat: return results on async requests (#198) * feat: return results on async requests * fix: correct typings --- src/context/MoralisContext/MoralisContext.ts | 12 ++++++++---- src/hooks/core/useMoralis/_useMoralisAuth.ts | 7 +++++-- src/hooks/core/useMoralis/_useMoralisUser.ts | 2 ++ src/hooks/core/useMoralis/_useMoralisWeb3.ts | 13 ++++++++++--- .../_useResolveAsyncCall/_useResolveAsyncCall.ts | 1 + 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/context/MoralisContext/MoralisContext.ts b/src/context/MoralisContext/MoralisContext.ts index bdac111..8fc79c1 100644 --- a/src/context/MoralisContext/MoralisContext.ts +++ b/src/context/MoralisContext/MoralisContext.ts @@ -25,7 +25,9 @@ export interface MoralisContextValue { isInitializing: boolean; initialize: (options?: { serverUrl?: string; appId?: string }) => void; - authenticate: (options?: AuthenticateOptions) => Promise; + authenticate: ( + options?: AuthenticateOptions, + ) => Promise; logout: () => Promise; signup: Signup; login: Login; @@ -39,14 +41,16 @@ export interface MoralisContextValue { isLoggingOut: boolean; isAuthUndefined: boolean; - setUserData: (data: SetUserData) => Promise; + setUserData: (data: SetUserData) => Promise; user: MoralisType.User | null; _setUser: (user: MoralisType.User) => void; userError: null | Error; isUserUpdating: boolean; - refetchUserData: () => Promise; + refetchUserData: () => Promise; - enableWeb3: (options?: Web3EnableOptions) => void; + enableWeb3: ( + options?: Web3EnableOptions, + ) => Promise; deactivateWeb3: () => Promise; web3: MoralisType.MoralisWeb3Provider | null; isWeb3Enabled: boolean; diff --git a/src/hooks/core/useMoralis/_useMoralisAuth.ts b/src/hooks/core/useMoralis/_useMoralisAuth.ts index 87176a5..feba59b 100644 --- a/src/hooks/core/useMoralis/_useMoralisAuth.ts +++ b/src/hooks/core/useMoralis/_useMoralisAuth.ts @@ -82,7 +82,7 @@ export type Login = ( username: string, password: string, options?: LoginOptions, -) => Promise; +) => Promise | undefined>; export type Signup = ( username: string, @@ -90,7 +90,7 @@ export type Signup = ( email?: string, otherFields?: SetUserData, options?: SignupOptions, -) => Promise; +) => Promise | undefined>; export type OnAccountChanged = (account: string) => void; @@ -172,6 +172,7 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => { if (onSuccess) { onSuccess(user); } + return user; } catch (error) { setAuth({ state: AuthenticationState.ERROR, error }); setUser(null); @@ -231,6 +232,7 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => { if (onSuccess) { onSuccess(user); } + return user; } catch (error) { setAuth({ state: AuthenticationState.ERROR, error }); if (throwOnError) { @@ -274,6 +276,7 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => { if (onSuccess) { onSuccess(user); } + return user; } catch (error) { setAuth({ state: AuthenticationState.ERROR, error }); if (throwOnError) { diff --git a/src/hooks/core/useMoralis/_useMoralisUser.ts b/src/hooks/core/useMoralis/_useMoralisUser.ts index a0176c5..87fd76f 100644 --- a/src/hooks/core/useMoralis/_useMoralisUser.ts +++ b/src/hooks/core/useMoralis/_useMoralisUser.ts @@ -63,6 +63,7 @@ export const _useMoralisUser = (Moralis: MoralisType) => { if (onSuccess) { onSuccess(user); } + return user; } catch (error) { if (userHasLocallyUpdated) { user.revert(); @@ -116,6 +117,7 @@ export const _useMoralisUser = (Moralis: MoralisType) => { if (onSuccess) { onSuccess(newUserData); } + return newUserData; } catch (error) { setError(error); if (throwOnError) { diff --git a/src/hooks/core/useMoralis/_useMoralisWeb3.ts b/src/hooks/core/useMoralis/_useMoralisWeb3.ts index 4b50325..5d17525 100644 --- a/src/hooks/core/useMoralis/_useMoralisWeb3.ts +++ b/src/hooks/core/useMoralis/_useMoralisWeb3.ts @@ -12,13 +12,17 @@ export interface Web3EnableOptions { anyNetwork?: boolean; } +type EnableWeb3 = ( + options?: Web3EnableOptions | undefined, +) => Promise; + /** * Handles enabling of web3 and providing it, as soon as the user is authenticated */ export const _useMoralisWeb3 = ( Moralis: MoralisType, ): { - enableWeb3: (options?: Web3EnableOptions) => Promise; + enableWeb3: EnableWeb3; web3: null | MoralisType.MoralisWeb3Provider; isWeb3Enabled: boolean; web3EnableError: Error | null; @@ -99,7 +103,7 @@ export const _useMoralisWeb3 = ( /** * Enable web3 with the browsers web3Provider (only available when a user has been authenticated) */ - const enableWeb3 = useCallback( + const enableWeb3 = useCallback( async ({ throwOnError, onComplete, @@ -113,13 +117,16 @@ export const _useMoralisWeb3 = ( try { // TODO: fix typechecking when passing ...rest // @ts-ignore - const currentWeb3 = await Moralis.enableWeb3(rest); + const currentWeb3: MoralisType.Web3Provider = await Moralis.enableWeb3( + rest, + ); _setIsWeb3Enabled(true); if (onSuccess) { onSuccess(currentWeb3); } + return currentWeb3; } catch (error) { setEnableWeb3Error(error); if (throwOnError) { diff --git a/src/hooks/internal/_useResolveAsyncCall/_useResolveAsyncCall.ts b/src/hooks/internal/_useResolveAsyncCall/_useResolveAsyncCall.ts index 5cc3341..5ed02d4 100644 --- a/src/hooks/internal/_useResolveAsyncCall/_useResolveAsyncCall.ts +++ b/src/hooks/internal/_useResolveAsyncCall/_useResolveAsyncCall.ts @@ -75,6 +75,7 @@ export const _useResolveCall = ( if (onSuccess) { onSuccess(results); } + return results; } catch (error) { setData(initialData); setError(error); From 0c0ebd48c8ed05fe8fc7e8d6d5bb06a88c122a9d Mon Sep 17 00:00:00 2001 From: Erno Wever Date: Wed, 9 Feb 2022 17:57:46 +0000 Subject: [PATCH 2/4] feat: resolve ens (#197) --- README.md | 33 ++++++++++++++++++ src/hooks/index.ts | 1 + src/hooks/utils/index.ts | 1 + src/hooks/utils/useEns/index.ts | 2 ++ src/hooks/utils/useEns/useEnsAddress.ts | 27 +++++++++++++++ src/hooks/utils/useEns/useEnsName.ts | 45 +++++++++++++++++++++++++ 6 files changed, 109 insertions(+) create mode 100644 src/hooks/utils/index.ts create mode 100644 src/hooks/utils/useEns/index.ts create mode 100644 src/hooks/utils/useEns/useEnsAddress.ts create mode 100644 src/hooks/utils/useEns/useEnsName.ts diff --git a/README.md b/README.md index d78e2b2..1b95de3 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,10 @@ function App() { - [`Web3`](#web3) - [Enable web3 via metamask](#enable-web3-via-metamask) - [Enable web3 via Walletconnect](#enable-web3-via-walletconnect) + - [Utils](#utils) + - [Resolve/Lookup ens names](#resolvelookup-ens-names) + - [`useEnsName`](#useensname) + - [`useEnsAddress`](#useensaddress) - [Components](#components) - [Handling responses](#handling-responses) - [Webpack v5 support](#webpack-v5-support) @@ -1595,6 +1599,35 @@ const EnableWeb3 = ({user, score}) => { } ``` +## Utils + +### Resolve/Lookup ens names +If you want to resolve/lookup ENS names, then you can use `useEnsName` or `useEnsAddress`. + +#### `useEnsName` +Resolve the ENS name and lookup the address, avatar, email and url details (if they are set) + +*example:* +```javascript + const { + address, + avatar, + email, + url, + isLoading, + error + } = useEnsName("ricmoo.eth"); + +``` + +#### `useEnsAddress` +Lookup if an ENS name is registered for the address +*example:* +```javascript + const { name, isLoading, error } = useEnsAddress("0x5555763613a12D8F3e73be831DFf8598089d3dCa"); +``` + + ## Components If you want to support Moralis, you can use the `ByMoralis` component, to render our logo 🙏: diff --git a/src/hooks/index.ts b/src/hooks/index.ts index e11c5b1..c061fe1 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -1,3 +1,4 @@ export * from "./core"; export * from "./plugins"; export * from "./web3ApiWrappers"; +export * from "./utils"; diff --git a/src/hooks/utils/index.ts b/src/hooks/utils/index.ts new file mode 100644 index 0000000..6ec338b --- /dev/null +++ b/src/hooks/utils/index.ts @@ -0,0 +1 @@ +export * from "./useEns"; diff --git a/src/hooks/utils/useEns/index.ts b/src/hooks/utils/useEns/index.ts new file mode 100644 index 0000000..4632038 --- /dev/null +++ b/src/hooks/utils/useEns/index.ts @@ -0,0 +1,2 @@ +export * from "./useEnsAddress"; +export * from "./useEnsName"; diff --git a/src/hooks/utils/useEns/useEnsAddress.ts b/src/hooks/utils/useEns/useEnsAddress.ts new file mode 100644 index 0000000..e18f47c --- /dev/null +++ b/src/hooks/utils/useEns/useEnsAddress.ts @@ -0,0 +1,27 @@ +import { useState, useEffect } from "react"; +import { useMoralis } from "../../core/useMoralis"; + +export const useEnsAddress = (ensAddress: string) => { + const { web3 } = useMoralis(); + const [resolved, setResolved] = useState(null); + const [isLoading, setIsLoading] = useState(false); + const [error, setError] = useState(null); + + useEffect(() => { + if (web3) { + setIsLoading(true); + setError(null); + web3 + .lookupAddress(ensAddress) + .then((result) => { + setResolved(result); + }) + .catch(setError) + .finally(() => { + setIsLoading(false); + }); + } + }, [ensAddress, web3]); + + return { name: resolved, isLoading, error }; +}; diff --git a/src/hooks/utils/useEns/useEnsName.ts b/src/hooks/utils/useEns/useEnsName.ts new file mode 100644 index 0000000..9263266 --- /dev/null +++ b/src/hooks/utils/useEns/useEnsName.ts @@ -0,0 +1,45 @@ +import { useState, useEffect } from "react"; +import { useMoralis } from "../../core/useMoralis"; + +export const useEnsName = (ensName: string) => { + const { web3 } = useMoralis(); + const [address, setAddress] = useState(null); + const [avatar, setAvatar] = useState(null); + const [url, setUrl] = useState(null); + const [email, setEmail] = useState(null); + const [isLoading, setIsLoading] = useState(false); + const [error, setError] = useState(null); + + useEffect(() => { + if (web3) { + setIsLoading(true); + setError(null); + web3 + .getResolver(ensName) + .then((resolver) => { + if (!resolver) { + return; + } + Promise.all([ + resolver.getAddress().catch(() => null), + resolver.getAvatar().catch(() => null), + resolver.getText("email").catch(() => null), + resolver.getText("url").catch(() => null), + ]).then( + ([resolvedAddress, resolvedAvatar, resolvedEmail, resolvedUrl]) => { + setAddress(resolvedAddress); + setAvatar(resolvedAvatar?.url ?? null); + setEmail(resolvedEmail); + setUrl(resolvedUrl); + }, + ); + }) + .catch(setError) + .finally(() => { + setIsLoading(false); + }); + } + }, [ensName, web3]); + + return { address, avatar, email, url, isLoading, error }; +}; From f82057ae682e0b0144e01b49201b107bb59a506d Mon Sep 17 00:00:00 2001 From: Erno Wever Date: Wed, 9 Feb 2022 17:57:59 +0000 Subject: [PATCH 3/4] fix: import of AuthError from src (#199) --- src/hooks/core/useMoralis/_useMoralisAuth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/core/useMoralis/_useMoralisAuth.ts b/src/hooks/core/useMoralis/_useMoralisAuth.ts index feba59b..011f1f5 100644 --- a/src/hooks/core/useMoralis/_useMoralisAuth.ts +++ b/src/hooks/core/useMoralis/_useMoralisAuth.ts @@ -1,6 +1,6 @@ import { useState, useCallback, useEffect } from "react"; import { setMultipleDataToUser, SetUserData } from "./utils/setUserData"; -import { AuthError } from "src"; +import { AuthError } from "../../../context/MoralisContext/MoralisContext"; import MoralisType from "moralis"; import { Environment } from "./_useMoralisInit"; From e5412d324b26bd2d4794a275d7c5c871313064eb Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Wed, 9 Feb 2022 17:59:28 +0000 Subject: [PATCH 4/4] chore(release): set `package.json` to 1.3.0 [skip ci] # [1.3.0](https://github.com/MoralisWeb3/react-moralis/compare/v1.2.5...v1.3.0) (2022-02-09) ### Bug Fixes * import of AuthError from src ([#199](https://github.com/MoralisWeb3/react-moralis/issues/199)) ([f82057a](https://github.com/MoralisWeb3/react-moralis/commit/f82057ae682e0b0144e01b49201b107bb59a506d)) ### Features * resolve ens ([#197](https://github.com/MoralisWeb3/react-moralis/issues/197)) ([0c0ebd4](https://github.com/MoralisWeb3/react-moralis/commit/0c0ebd48c8ed05fe8fc7e8d6d5bb06a88c122a9d)) * return results on async requests ([#198](https://github.com/MoralisWeb3/react-moralis/issues/198)) ([ef53c48](https://github.com/MoralisWeb3/react-moralis/commit/ef53c48d8b8af2c4f6ea7317c3006b0701ef4527)) --- CHANGELOG.md | 13 +++++++++++++ package.json | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8db4827..f2bbf9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +# [1.3.0](https://github.com/MoralisWeb3/react-moralis/compare/v1.2.5...v1.3.0) (2022-02-09) + + +### Bug Fixes + +* import of AuthError from src ([#199](https://github.com/MoralisWeb3/react-moralis/issues/199)) ([f82057a](https://github.com/MoralisWeb3/react-moralis/commit/f82057ae682e0b0144e01b49201b107bb59a506d)) + + +### Features + +* resolve ens ([#197](https://github.com/MoralisWeb3/react-moralis/issues/197)) ([0c0ebd4](https://github.com/MoralisWeb3/react-moralis/commit/0c0ebd48c8ed05fe8fc7e8d6d5bb06a88c122a9d)) +* return results on async requests ([#198](https://github.com/MoralisWeb3/react-moralis/issues/198)) ([ef53c48](https://github.com/MoralisWeb3/react-moralis/commit/ef53c48d8b8af2c4f6ea7317c3006b0701ef4527)) + ## [1.2.5](https://github.com/MoralisWeb3/react-moralis/compare/v1.2.4...v1.2.5) (2022-02-07) diff --git a/package.json b/package.json index 6c232e7..07c065f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-moralis", - "version": "1.2.5", + "version": "1.3.0", "description": "Hooks and components to use Moralis in a React app", "keywords": [ "moralis",