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 dfb866f
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 118 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
1 change: 1 addition & 0 deletions examples/react-native/src/components/MyProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function ProfileAvatar({ profile }: ProfileAvatarProps) {
source={{
uri,
}}
alt="profile image"
/>
)}
</Avatar>
Expand Down
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;
});
}
}
32 changes: 32 additions & 0 deletions examples/react-native/src/utils/bindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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;

console.log('getProvider', provider);

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

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

console.log('getSigner', provider);

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.

58 changes: 16 additions & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dfb866f

Please sign in to comment.