@multiplatform.one/web3 6.0.4 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/cjs/providers/WalletProvider.cjs +7 -11
  2. package/dist/cjs/providers/WalletProvider.native.js +10 -9
  3. package/dist/cjs/providers/WalletProvider.native.js.map +1 -1
  4. package/dist/cjs/providers/walletConfig.cjs +23 -0
  5. package/dist/cjs/providers/walletConfig.native.js +27 -0
  6. package/dist/cjs/providers/walletConfig.native.js.map +1 -1
  7. package/dist/cjs/providers/walletConfig.spec.cjs +44 -0
  8. package/dist/cjs/providers/walletConfig.spec.native.js +57 -0
  9. package/dist/cjs/providers/walletConfig.spec.native.js.map +1 -0
  10. package/dist/esm/providers/WalletProvider.mjs +10 -14
  11. package/dist/esm/providers/WalletProvider.mjs.map +1 -1
  12. package/dist/esm/providers/WalletProvider.native.js +14 -13
  13. package/dist/esm/providers/WalletProvider.native.js.map +1 -1
  14. package/dist/esm/providers/walletConfig.mjs +24 -2
  15. package/dist/esm/providers/walletConfig.mjs.map +1 -1
  16. package/dist/esm/providers/walletConfig.native.js +28 -2
  17. package/dist/esm/providers/walletConfig.native.js.map +1 -1
  18. package/dist/esm/providers/walletConfig.spec.mjs +45 -0
  19. package/dist/esm/providers/walletConfig.spec.mjs.map +1 -0
  20. package/dist/esm/providers/walletConfig.spec.native.js +55 -0
  21. package/dist/esm/providers/walletConfig.spec.native.js.map +1 -0
  22. package/dist/jsx/providers/WalletProvider.mjs +10 -14
  23. package/dist/jsx/providers/WalletProvider.mjs.map +1 -1
  24. package/dist/jsx/providers/WalletProvider.native.js +10 -9
  25. package/dist/jsx/providers/WalletProvider.native.js.map +1 -1
  26. package/dist/jsx/providers/walletConfig.mjs +24 -2
  27. package/dist/jsx/providers/walletConfig.mjs.map +1 -1
  28. package/dist/jsx/providers/walletConfig.native.js +27 -0
  29. package/dist/jsx/providers/walletConfig.native.js.map +1 -1
  30. package/dist/jsx/providers/walletConfig.spec.mjs +45 -0
  31. package/dist/jsx/providers/walletConfig.spec.mjs.map +1 -0
  32. package/dist/jsx/providers/walletConfig.spec.native.js +57 -0
  33. package/dist/jsx/providers/walletConfig.spec.native.js.map +1 -0
  34. package/package.json +4 -4
  35. package/src/providers/WalletProvider.native.tsx +22 -12
  36. package/src/providers/WalletProvider.tsx +11 -12
  37. package/src/providers/walletConfig.spec.ts +42 -0
  38. package/src/providers/walletConfig.ts +38 -1
@@ -6,7 +6,7 @@ import { useMemo } from "react";
6
6
  import type { Chain } from "viem";
7
7
  import { WagmiProvider, createConfig, http } from "wagmi";
8
8
  import { createMockConnector } from "./mockConnector";
9
- import { type Web3Config, Web3ConfigContext, defaultChains } from "./walletConfig";
9
+ import { type Web3Config, Web3ConfigContext, resolveChains } from "./walletConfig";
10
10
 
11
11
  export interface WalletProviderProps extends PropsWithChildren {
12
12
  config: Web3Config;
@@ -34,21 +34,19 @@ export function WalletProvider({
34
34
  appDescription = "Connect your wallet",
35
35
  appUrl = isWeb && typeof window !== "undefined" ? window.location.origin : "",
36
36
  appIcon,
37
- chains = defaultChains,
37
+ chains,
38
38
  enableTestnets = false,
39
+ localnet = false,
40
+ localnetRpcUrl,
39
41
  mockWallet = false,
40
42
  } = web3Config;
41
43
 
42
- const activeChains = useMemo(() => {
43
- if (enableTestnets) {
44
- return chains;
45
- }
46
- const filtered = chains.filter((chain) => !chain.testnet);
47
- if (filtered.length === 0) {
48
- return chains;
49
- }
50
- return filtered as unknown as readonly [Chain, ...Chain[]];
51
- }, [chains, enableTestnets]);
44
+ // Testnet filter + optional localnet dev chain (prepended -> default chain).
45
+ const activeChains = useMemo(
46
+ () => resolveChains(web3Config),
47
+ // eslint-disable-next-line react-hooks/exhaustive-deps
48
+ [chains, enableTestnets, localnet, localnetRpcUrl],
49
+ );
52
50
 
53
51
  const wagmiConfig = useMemo(() => {
54
52
  const chainIds = activeChains.map((chain) => chain.id).join(",");
@@ -59,6 +57,7 @@ export function WalletProvider({
59
57
  appIcon,
60
58
  walletConnectProjectId,
61
59
  chainIds,
60
+ localnetRpcUrl,
62
61
  mockWallet,
63
62
  });
64
63
  const cached = wagmiConfigCache.get(cacheKey);
@@ -0,0 +1,42 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { hardhat, mainnet, sepolia } from "wagmi/chains";
3
+ import { defaultChains, resolveChains, type Web3Config } from "./walletConfig";
4
+
5
+ const base: Web3Config = { walletConnectProjectId: "", appName: "test" };
6
+
7
+ describe("resolveChains", () => {
8
+ it("filters testnets by default", () => {
9
+ const chains = resolveChains(base);
10
+ expect(chains.map((c) => c.id)).not.toContain(sepolia.id);
11
+ expect(chains[0].id).toBe(mainnet.id);
12
+ });
13
+
14
+ it("keeps testnets when enableTestnets is on", () => {
15
+ const chains = resolveChains({ ...base, enableTestnets: true });
16
+ expect(chains.map((c) => c.id)).toContain(sepolia.id);
17
+ expect(chains).toHaveLength(defaultChains.length);
18
+ });
19
+
20
+ it("prepends the hardhat dev chain as the default when localnet is on", () => {
21
+ const chains = resolveChains({ ...base, localnet: true });
22
+ expect(chains[0].id).toBe(hardhat.id);
23
+ expect(chains[0].rpcUrls.default.http[0]).toBe("http://127.0.0.1:8545");
24
+ // still exactly one localnet entry ahead of the regular chains
25
+ expect(chains.filter((c) => c.id === hardhat.id)).toHaveLength(1);
26
+ expect(chains.map((c) => c.id)).toContain(mainnet.id);
27
+ });
28
+
29
+ it("honours a custom localnet RPC url (emulator host addresses)", () => {
30
+ const chains = resolveChains({
31
+ ...base,
32
+ localnet: true,
33
+ localnetRpcUrl: "http://10.0.2.2:8545",
34
+ });
35
+ expect(chains[0].id).toBe(hardhat.id);
36
+ expect(chains[0].rpcUrls.default.http[0]).toBe("http://10.0.2.2:8545");
37
+ });
38
+
39
+ it("does not add the dev chain when localnet is off", () => {
40
+ expect(resolveChains(base).map((c) => c.id)).not.toContain(hardhat.id);
41
+ });
42
+ });
@@ -1,6 +1,6 @@
1
1
  import { createContext, useContext } from "react";
2
2
  import type { Chain } from "viem";
3
- import { arbitrum, base, mainnet, optimism, polygon, sepolia } from "wagmi/chains";
3
+ import { arbitrum, base, hardhat, mainnet, optimism, polygon, sepolia } from "wagmi/chains";
4
4
 
5
5
  /**
6
6
  * Shared config seam for both platform variants of WalletProvider. Keeping the
@@ -25,14 +25,51 @@ export interface Web3Config {
25
25
  appIcon?: string;
26
26
  chains?: readonly [Chain, ...Chain[]];
27
27
  enableTestnets?: boolean;
28
+ /**
29
+ * Add the local dev chain (hardhat, chain 31337, `make localnet`) and make it
30
+ * the DEFAULT chain, so dev builds point at the local node out of the box.
31
+ */
32
+ localnet?: boolean;
33
+ /**
34
+ * Local EVM JSON-RPC endpoint when `localnet` is on. Defaults to hardhat's
35
+ * http://127.0.0.1:8545 -- emulators need the host address instead (e.g.
36
+ * http://10.0.2.2:8545 on Android).
37
+ */
38
+ localnetRpcUrl?: string;
28
39
  /** Solana JSON-RPC endpoint (native bridge + web). */
29
40
  solanaRpcUrl?: string;
41
+ /** Sui JSON-RPC endpoint (native bridge + web). */
42
+ suiRpcUrl?: string;
30
43
  /** E2E only (native): route connect through the in-WebView mock wallet. */
31
44
  mockWallet?: boolean;
32
45
  /** Native: URL serving the wallet WebView HTML (preferred over the inline string). */
33
46
  webViewUrl?: string;
34
47
  }
35
48
 
49
+ /**
50
+ * Resolve the active chain list for a Web3Config: apply the testnet filter,
51
+ * then (when `localnet` is on) prepend the hardhat dev chain so it becomes
52
+ * wagmi's default chain. Shared by the web and native WalletProviders so both
53
+ * platforms point at the same networks.
54
+ */
55
+ export function resolveChains(config: Web3Config): readonly [Chain, ...Chain[]] {
56
+ const {
57
+ chains = defaultChains,
58
+ enableTestnets = false,
59
+ localnet = false,
60
+ localnetRpcUrl,
61
+ } = config;
62
+ let active: Chain[] = enableTestnets ? [...chains] : chains.filter((chain) => !chain.testnet);
63
+ if (active.length === 0) active = [...chains];
64
+ if (localnet) {
65
+ const local: Chain = localnetRpcUrl
66
+ ? { ...hardhat, rpcUrls: { default: { http: [localnetRpcUrl] } } }
67
+ : hardhat;
68
+ active = [local, ...active.filter((chain) => chain.id !== hardhat.id)];
69
+ }
70
+ return active as unknown as readonly [Chain, ...Chain[]];
71
+ }
72
+
36
73
  export const Web3ConfigContext = createContext<Web3Config | null>(null);
37
74
 
38
75
  export function useWeb3Config(): Web3Config {