Skip to content

Commit

Permalink
feat!: support for ethers refactor in moralis sdk
Browse files Browse the repository at this point in the history
BREAKING CHANGE: changes to accommodate for the breaking changes in the SDK, where web3 has been replaced for ethers
  • Loading branch information
ErnoW authored Jan 5, 2022
1 parent dc4a315 commit 32df645
Show file tree
Hide file tree
Showing 12 changed files with 569 additions and 2,085 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
![npm](https://img.shields.io/npm/v/react-moralis)
![node-current](https://img.shields.io/node/v/react-moralis)
![GitHub last commit](https://img.shields.io/github/last-commit/MoralisWeb3/react-moralis)
![David](https://img.shields.io/david/MoralisWeb3/react-moralis)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/react-moralis)
![npm type definitions](https://img.shields.io/npm/types/react-moralis)

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"husky": "^7.0.4",
"jest": "^27.4.5",
"lint-staged": "^12.1.2",
"moralis": ">=0.0.176",
"moralis": "^1.0.0-beta.1",
"prettier": "^2.5.1",
"rollup": "^2.61.1",
"rollup-plugin-cleaner": "^1.0.0",
Expand All @@ -77,7 +77,7 @@
"typescript": "^4.5.4"
},
"peerDependencies": {
"moralis": ">=0.0.176",
"moralis": ">=1.0.0-beta.1",
"react": ">=17.0.0",
"react-dom": ">=17.0.0"
},
Expand Down
14 changes: 10 additions & 4 deletions src/context/MoralisContext/MoralisContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { SetUserData } from "../../hooks/core/useMoralis/utils/setUserData";
import { Web3EnableOptions } from "../../hooks/core/useMoralis/_useMoralisWeb3";
import { Environment } from "../../hooks/core/useMoralis/_useMoralisInit";

export type MoralisWeb3 = unknown;
export interface AuthError extends Error {
code: number;
}
Expand Down Expand Up @@ -42,14 +43,19 @@ export interface MoralisContextValue {
isUserUpdating: boolean;
refetchUserData: () => Promise<void>;

account: null | string;
chainId: null | string;

enableWeb3: (options?: Web3EnableOptions) => void;
web3: MoralisType.Web3 | null;
deactivateWeb3: () => Promise<void>;
web3: MoralisWeb3 | null;
isWeb3Enabled: boolean;
web3EnableError: Error | null;
isWeb3EnableLoading: boolean;

chainId: string | null;
account: string | null;
network: string | null;
connector: unknown | null;
connectorType: string | null;
provider: unknown | null;
}

export const MoralisContext = createContext<null | MoralisContextValue>(null);
8 changes: 7 additions & 1 deletion src/context/MoralisProvider/MoralisProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface MoralisProviderProps {
options?: MoralisProviderOptions;
environment?: Environment;
getMoralis?: GetMoralis;
web3Library?: unknown;
}

export const MoralisProvider = ({
Expand All @@ -39,6 +40,7 @@ export const MoralisProvider = ({
environment,
getMoralis,
options: { onAccountChanged } = {},
web3Library,
}: MoralisProviderProps) => {
const moralisInit = _useMoralisInit({
appId,
Expand All @@ -48,15 +50,19 @@ export const MoralisProvider = ({
plugins,
environment,
getMoralis,
web3Library,
});
const { _setIsWeb3Enabled, _setIsWeb3EnableLoading, ...moralisWeb3 } =
_useMoralisWeb3(moralisInit.Moralis);
const { setUser, ...moralisUser } = _useMoralisUser(moralisInit.Moralis);
const moralisAuth = _useMoralisAuth({
onAccountChanged,
setUser,
Moralis: moralisInit.Moralis,
environment: moralisInit.environment,
_setIsWeb3Enabled,
_setIsWeb3EnableLoading,
});
const moralisWeb3 = _useMoralisWeb3(moralisInit.Moralis);

return (
<MoralisContext.Provider
Expand Down
23 changes: 20 additions & 3 deletions src/hooks/core/useChain/useChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { getChain } from "../../../functions/chains";
import { useMoralis } from "../useMoralis";

export const useChain = () => {
const { Moralis, chainId, account } = useMoralis();
const {
Moralis,
chainId,
account,
network,
provider,
connector,
connectorType,
} = useMoralis();

const switchNetwork = async (providedChainId: string) => {
try {
Expand All @@ -26,7 +34,7 @@ export const useChain = () => {
nativeCurrency.name,
nativeCurrency.symbol,
rpc[0],
blockExplorerUrl ?? "",
blockExplorerUrl ?? null,
);
} else {
throw error;
Expand All @@ -41,7 +49,16 @@ export const useChain = () => {
return getChain(chainId);
}, [chainId]);

return { switchNetwork, chainId, chain, account };
return {
switchNetwork,
chainId,
chain,
account,
network,
provider,
connector,
connectorType,
};
};

export default useChain;
24 changes: 22 additions & 2 deletions src/hooks/core/useMoralis/_useMoralisAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export interface UseMoralisAuthOptions {
setUser?: (user: MoralisType.User | null) => void;
Moralis: MoralisType;
environment: Environment;
_setIsWeb3Enabled?: (value: boolean) => void;
_setIsWeb3EnableLoading?: (value: boolean) => void;
}

const defaultUseMoralisAuthOptions = (
Expand All @@ -116,7 +118,13 @@ const defaultUseMoralisAuthOptions = (
* and its functions
*/
export const _useMoralisAuth = (options: UseMoralisAuthOptions) => {
const { onAccountChanged, Moralis, environment } = {
const {
onAccountChanged,
Moralis,
environment,
_setIsWeb3Enabled,
_setIsWeb3EnableLoading,
} = {
...defaultUseMoralisAuthOptions(options.Moralis),
...options,
};
Expand Down Expand Up @@ -144,11 +152,19 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => {
error: null,
});

if (_setIsWeb3EnableLoading) {
_setIsWeb3EnableLoading(true);
}

try {
// TODO: fix typechecking when passing ...rest
// @ts-ignore
const user = await Moralis.authenticate(rest);

setUser(user);
if (_setIsWeb3Enabled) {
_setIsWeb3Enabled(true);
}

setAuth({
state: AuthenticationState.AUTHENTICATED,
Expand All @@ -160,19 +176,23 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => {
}
} catch (error) {
setAuth({ state: AuthenticationState.ERROR, error });
setUser(null);
if (onError) {
onError(error);
}
if (throwOnError) {
throw error;
}
} finally {
if (_setIsWeb3EnableLoading) {
_setIsWeb3EnableLoading(false);
}
if (onComplete) {
onComplete();
}
}
},
[],
[_setIsWeb3Enabled, _setIsWeb3EnableLoading],
);

/**
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/core/useMoralis/_useMoralisInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const _useMoralisInit = ({
plugins,
environment = "browser",
getMoralis = () => MoralisImport,
web3Library,
}: {
appId: string;
serverUrl: string;
Expand All @@ -32,6 +33,7 @@ export const _useMoralisInit = ({
plugins?: PluginSpecs[];
environment?: "browser" | "native";
getMoralis?: GetMoralis;
web3Library?: unknown;
}) => {
const [isInitialized, setIsInitialized] = useState(false);
const [isInitializing, setIsInitializing] = useState(false);
Expand All @@ -44,12 +46,14 @@ export const _useMoralisInit = ({
javascriptKey,
masterKey,
plugins,
web3Library,
}: {
serverUrl: string;
appId: string;
javascriptKey?: string;
masterKey?: string;
plugins?: PluginSpecs[];
web3Library?: unknown;
}) => {
if (isInitialized) {
return;
Expand All @@ -74,6 +78,7 @@ export const _useMoralisInit = ({
javascriptKey,
masterKey,
plugins,
web3Library,
});
setIsInitializing(false);
setIsInitialized(true);
Expand All @@ -92,6 +97,7 @@ export const _useMoralisInit = ({
masterKey: dangerouslyUseOfMasterKey,
javascriptKey: jsKey,
plugins,
web3Library,
});

setIsInitialized(true);
Expand All @@ -102,6 +108,7 @@ export const _useMoralisInit = ({
jsKey,
plugins,
isInitialized,
web3Library,
]);

return {
Expand Down
Loading

0 comments on commit 32df645

Please sign in to comment.