Skip to content

Commit

Permalink
drop viem as unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Apr 26, 2024
1 parent a83b824 commit 6340bd4
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 147 deletions.
3 changes: 1 addition & 2 deletions examples/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"react-native-modal": "^13.0.1",
"react-native-svg": "^13.14.0",
"react-native-web": "^0.19.11",
"react-native-windows": "^0.73.11",
"viem": "^2.9.26"
"react-native-windows": "^0.73.11"
},
"devDependencies": {
"@babel/core": "^7.24.4",
Expand Down
45 changes: 18 additions & 27 deletions examples/react-native/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,33 @@ import { config } from '@gluestack-ui/config';
import { Box, GluestackUIProvider, SafeAreaView } from '@gluestack-ui/themed';
import { LensConfig, LensProvider } from '@lens-protocol/react-native';
import { storage } from '@lens-protocol/react-native/storage/mmkv';
import { useWalletConnectModal, IProvider } from '@walletconnect/modal-react-native';
import React, { useMemo } from 'react';
import { createWalletClient, custom, WalletClient } from 'viem';
import { IProvider, useWalletConnectModal } from '@walletconnect/modal-react-native';
import React, { useEffect, useRef } from 'react';

import { ConnectButton } from './components/ConnectButton';
import { Main } from './components/Main';
import { WalletConnectModal } from './components/WalletConnectModal';
import { getLensEnvironment, getViemChain } from './utils/environment';
import { bindings } from './utils/wallet';

type RequestArguments = Parameters<IProvider['request']>[0];
import { Deferred } from './utils/Deferred';
import { bindings } from './utils/bindings';
import { getLensEnvironment } from './utils/environment';

export function App() {
const { provider } = useWalletConnectModal();

const walletClient: WalletClient = useMemo(
() =>
createWalletClient({
chain: getViemChain(),
transport: custom({
async request({ method, params }: RequestArguments) {
return provider?.request({ method, params });
},
}),
}),
[provider],
);
const deferred = useRef(new Deferred<IProvider>());

const lensConfig: LensConfig = useMemo(
() => ({
bindings: bindings(walletClient),
environment: getLensEnvironment(),
storage: storage(),
}),
[walletClient],
);
useEffect(() => {
if (provider) {
deferred.current.resolve(provider);
}
}, [provider]);

const lensConfig: LensConfig = {
bindings: bindings(deferred.current.promise),
environment: getLensEnvironment(),
storage: storage(),
origin: 'https://nativelens.com',
};

return (
<GluestackUIProvider config={config}>
Expand Down
3 changes: 3 additions & 0 deletions examples/react-native/src/components/ConnectButton.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import '@walletconnect/react-native-compat';
import { Button, ButtonText } from '@gluestack-ui/themed';
import { useLogout } from '@lens-protocol/react-native';
import { useWalletConnectModal } from '@walletconnect/modal-react-native';
import React from 'react';

export function ConnectButton() {
const { open, isConnected, provider } = useWalletConnectModal();
const { execute: logout } = useLogout();

const onPress = () => {
if (isConnected && provider) {
void provider.disconnect();
void logout();
} else {
void open();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/react-native/src/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function LoginForm({ address }: LoginFormProps) {
<Heading size="lg" textAlign="center">
Select a profile to log in
</Heading>
<VStack gap="$4">
<VStack gap="$4" mb="$4">
{profiles.map((profile) => (
<LoginButton key={profile.id} address={address} profile={profile} />
))}
Expand Down
21 changes: 0 additions & 21 deletions examples/react-native/src/components/LogoutButton.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions examples/react-native/src/components/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import React from 'react';

import { ErrorCallout } from './ErrorCallout';
import { LoginForm } from './LoginForm';
import { LogoutButton } from './LogoutButton';
import { MyProfile } from './MyProfile';

export function Main() {
Expand Down Expand Up @@ -44,7 +43,6 @@ export function Main() {
<Text>
Welcome <Text isTruncated>{session.address}</Text>, you need a Profile to use this app.
</Text>
<LogoutButton />
</VStack>
);
}
Expand Down
7 changes: 1 addition & 6 deletions examples/react-native/src/components/MyProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
import { Profile } from '@lens-protocol/react-native';
import React from 'react';

import { LogoutButton } from './LogoutButton';

type ProfileAvatarProps = {
profile: Profile;
};
Expand All @@ -34,6 +32,7 @@ function ProfileAvatar({ profile }: ProfileAvatarProps) {
source={{
uri,
}}
alt="profile image"
/>
)}
</Avatar>
Expand Down Expand Up @@ -94,10 +93,6 @@ export function MyProfile({ profile }: MyProfileProps) {
<Text size="xs">following</Text>
</Ticker>
</Box>

<Divider my="$6" />

<LogoutButton />
</Card>
);
}
15 changes: 15 additions & 0 deletions examples/react-native/src/utils/Deferred.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Unwraps the promise to allow resolving/rejecting outside the Promise constructor
*/
export class Deferred<T = void> {
readonly promise: Promise<T>;
resolve!: (value: T) => void;
reject!: (reason?: unknown) => void;

constructor() {
this.promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}
28 changes: 28 additions & 0 deletions examples/react-native/src/utils/bindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { JsonRpcProvider, JsonRpcSigner, Web3Provider } from '@ethersproject/providers';
import { IBindings } from '@lens-protocol/react-native';
import { IProvider } from '@walletconnect/modal-react-native';

export function bindings(providerPromise: Promise<IProvider>): IBindings {
return {
getProvider: async (): Promise<JsonRpcProvider> => {
const provider = await providerPromise;

if (!provider) {
throw new Error('Provider not defined');
}

return new Web3Provider(provider);
},
getSigner: async (): Promise<JsonRpcSigner> => {
const provider = await providerPromise;

if (!provider) {
throw new Error('Provider not defined');
}

const ethersProvider = new Web3Provider(provider);

return ethersProvider.getSigner();
},
};
}
12 changes: 0 additions & 12 deletions examples/react-native/src/utils/environment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EnvironmentConfig, development, production } from '@lens-protocol/react-native';
import Config from 'react-native-config';
import { polygon, polygonAmoy } from 'viem/chains';

const environment = Config.ENVIRONMENT || 'development';

Expand All @@ -25,14 +24,3 @@ export function getLensEnvironment(): EnvironmentConfig {
throw new Error(`Unknown environment ${environment}`);
}
}

export function getViemChain() {
switch (environment) {
case 'production':
return polygon;
case 'development':
return polygonAmoy;
default:
throw new Error(`Unknown environment ${environment}`);
}
}
34 changes: 0 additions & 34 deletions examples/react-native/src/utils/wallet.ts

This file was deleted.

Loading

0 comments on commit 6340bd4

Please sign in to comment.