Skip to content

Commit

Permalink
✨ migrate to wagmi+viem v2
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Nov 24, 2023
1 parent 387092a commit ea8abd6
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 186 deletions.
1 change: 1 addition & 0 deletions app/+html.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-expect-error -- missing types
import { ScrollViewStyleReset } from "expo-router/html";
import React, { type ReactNode } from "react";

Expand Down
34 changes: 22 additions & 12 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import "expo-webauthn";

import { AlchemyProvider } from "@alchemy/aa-alchemy";
import FontAwesome from "@expo/vector-icons/FontAwesome";
import InterBold from "@tamagui/font-inter/otf/Inter-Bold.otf";
import Inter from "@tamagui/font-inter/otf/Inter-Medium.otf";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { reconnect } from "@wagmi/core";
import { type FontSource, useFonts } from "expo-font";
import { Slot, SplashScreen } from "expo-router";
import { StatusBar } from "expo-status-bar";
import React, { useEffect } from "react";
import * as Sentry from "sentry-expo";
import { TamaguiProvider } from "tamagui";
import { TextEncoder } from "text-encoding";
import { WagmiConfig, configureChains, createConfig } from "wagmi";
import { alchemyProvider } from "wagmi/providers/alchemy";
import { publicProvider } from "wagmi/providers/public";
import { WagmiProvider, createConfig, custom } from "wagmi";

import metadata from "../package.json";
import tamaguiConfig from "../tamagui.config";
import AlchemyConnector from "../utils/AlchemyConnector";
import alchemyConnector from "../utils/alchemyConnector";
import { alchemyAPIKey, chain } from "../utils/constants";
import handleError from "../utils/handleError";

export { ErrorBoundary } from "expo-router";

Expand All @@ -35,11 +37,13 @@ Sentry.init({
autoSessionTracking: true,
});

const { publicClient, webSocketPublicClient } = configureChains(
[chain],
[alchemyAPIKey ? alchemyProvider({ apiKey: alchemyAPIKey }) : publicProvider()],
);
const wagmiConfig = createConfig({ connectors: [new AlchemyConnector()], publicClient, webSocketPublicClient });
const provider = new AlchemyProvider({ apiKey: alchemyAPIKey, chain });
const wagmiConfig = createConfig({
chains: [chain],
connectors: [alchemyConnector(provider)],
transports: { [chain.id]: custom(provider) },
});
const queryClient = new QueryClient();

export default function RootLayout() {
const [loaded, error] = useFonts({
Expand All @@ -56,15 +60,21 @@ export default function RootLayout() {
if (loaded) SplashScreen.hideAsync().catch(() => {});
}, [loaded]);

useEffect(() => {
reconnect(wagmiConfig).catch(handleError);
}, []);

if (!loaded) return;

return (
<>
<StatusBar translucent={false} />
<TamaguiProvider config={tamaguiConfig}>
<WagmiConfig config={wagmiConfig}>
<Slot />
</WagmiConfig>
<WagmiProvider config={wagmiConfig}>
<QueryClientProvider client={queryClient}>
<Slot />
</QueryClientProvider>
</WagmiProvider>
</TamaguiProvider>
</>
);
Expand Down
14 changes: 7 additions & 7 deletions app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ import { deviceName } from "expo-device";
import React, { useCallback } from "react";
import { Button, Spinner, Text, XStack, YStack } from "tamagui";
import { UAParser } from "ua-parser-js";
import { useAccount, useConnect, useDisconnect, usePrepareSendTransaction, useSendTransaction } from "wagmi";
import { useAccount, useConnect, useDisconnect, useSendTransaction } from "wagmi";

import base64URLEncode from "../utils/base64URLEncode";
import { rpId, turnkeyAPIPublicKey, turnkeyAPIPrivateKey, turnkeyOrganizationId } from "../utils/constants";
import { rpId, turnkeyAPIPrivateKey, turnkeyAPIPublicKey, turnkeyOrganizationId } from "../utils/constants";
import generateRandomBuffer from "../utils/generateRandomBuffer";
import handleError from "../utils/handleError";

export default function Home() {
const {
connect,
isLoading: isConnecting,
isPending: isConnecting,
connectors: [connector],
} = useConnect();
const { address } = useAccount();
const { disconnect } = useDisconnect();
const { config: sendConfig } = usePrepareSendTransaction({ to: "0xE72185a9f4Ce3500d6dC7CCDCfC64cf66D823bE8" });
const { sendTransaction, data: txHash, isLoading: isSending } = useSendTransaction(sendConfig);
const { sendTransaction, data: txHash, isPending: isSending } = useSendTransaction();

const createAccount = useCallback(() => {
const name = `exactly, ${new Date().toISOString()}`;
Expand Down Expand Up @@ -81,6 +80,7 @@ export default function Home() {
}, []);

const connectAccount = useCallback(() => {
if (!connector) throw new Error("no connector");
connect({ connector });
}, [connect, connector]);

Expand All @@ -89,15 +89,15 @@ export default function Home() {
}, [disconnect]);

const send = useCallback(() => {
sendTransaction?.();
sendTransaction({ to: "0xE72185a9f4Ce3500d6dC7CCDCfC64cf66D823bE8" });
}, [sendTransaction]);

return (
<XStack flex={1} alignItems="center" space>
<YStack flex={1} alignItems="center" space>
<Text textAlign="center">{txHash}</Text>
<Button onPress={createAccount}>create</Button>
<Button disabled={isConnecting} onPress={address ? disconnectAccount : connectAccount}>
<Button disabled={!connector || isConnecting} onPress={address ? disconnectAccount : connectAccount}>
{isConnecting ? <Spinner size="small" /> : address ?? "connect"}
</Button>
<Button disabled={!address || isSending} onPress={send}>
Expand Down
15 changes: 6 additions & 9 deletions app/pomelo+api.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { configureChains, createConfig, fetchBlockNumber } from "@wagmi/core";
import { alchemyProvider } from "@wagmi/core/providers/alchemy";
import { publicProvider } from "@wagmi/core/providers/public";
import { createConfig, getBlockNumber, http } from "@wagmi/core";
import { type ExpoRequest, ExpoResponse } from "expo-router/server";

import { alchemyAPIKey, chain } from "../utils/constants";

const { publicClient, webSocketPublicClient } = configureChains(
[chain],
[alchemyAPIKey ? alchemyProvider({ apiKey: alchemyAPIKey }) : publicProvider()],
);
createConfig({ publicClient, webSocketPublicClient });
const httpConfig = createConfig({
chains: [chain],
transports: { [chain.id]: http(`${chain.rpcUrls.alchemy.http[0]}/${alchemyAPIKey}`) },
});

// eslint-disable-next-line import/prefer-default-export
export async function GET(_: ExpoRequest) {
const blockNumber = await fetchBlockNumber();
const blockNumber = await getBlockNumber(httpConfig);
return ExpoResponse.json(Number(blockNumber));
}
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function (api) {
plugins: [
"expo-router/babel",
"transform-inline-environment-variables",
["@babel/plugin-transform-private-methods", { loose: true }],
["@tamagui/babel-plugin", { components: ["tamagui"], config: "tamagui.config.ts", logTimings: true }],
],
};
Expand Down
Binary file modified bun.lockb
Binary file not shown.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"@alchemy/aa-core": "^1.2.0",
"@expo/vector-icons": "^13.0.0",
"@react-native-async-storage/async-storage": "1.18.2",
"@sentry/react-native": "^5.14.0",
"@sentry/react-native": "^5.14.1",
"@tamagui/config": "^1.79.1",
"@tanstack/react-query": "^5.8.4",
"@turnkey/api-key-stamper": "^0.2.0",
"@turnkey/http": "^2.3.1",
"@turnkey/viem": "^0.3.4",
Expand Down Expand Up @@ -55,12 +56,13 @@
"tamagui": "^1.79.1",
"text-encoding": "^0.7.0",
"ua-parser-js": "^1.0.37",
"viem": "^1.19.7",
"wagmi": "^1.4.7",
"viem": "2.0.0-beta.6",
"wagmi": "2.0.0-beta.5",
"zustand": "^4.4.6"
},
"devDependencies": {
"@babel/core": "^7.23.3",
"@babel/plugin-transform-private-methods": "^7.23.3",
"@tamagui/babel-plugin": "^1.79.1",
"@types/babel__core": "^7.20.5",
"@types/eslint": "^8.44.7",
Expand Down
153 changes: 0 additions & 153 deletions utils/AlchemyConnector.ts

This file was deleted.

Loading

0 comments on commit ea8abd6

Please sign in to comment.