-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thirdweb V5 Migration : Initial Setup and Deprize (#301)
* Setup thirdweb v5 provider * Config provider and chains for thirdwebV5 * Migrated Deprize to Thirdweb V5 * Removed log * Updated env var
- Loading branch information
1 parent
ca115fd
commit b4e6e92
Showing
13 changed files
with
879 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { usePrivy, useWallets } from '@privy-io/react-auth' | ||
import { signIn, signOut } from 'next-auth/react' | ||
import { useEffect, useState } from 'react' | ||
import { defineChain } from 'thirdweb' | ||
import { ethers5Adapter } from 'thirdweb/adapters/ethers5' | ||
import { useSetActiveWallet } from 'thirdweb/react' | ||
import { createWalletAdapter } from 'thirdweb/wallets' | ||
import client from '@/lib/thirdweb/client' | ||
import PrivyWalletContext from './privy-wallet-context' | ||
|
||
export function PrivyThirdwebV5Provider({ selectedChain, children }: any) { | ||
const { user, ready, authenticated, getAccessToken } = usePrivy() | ||
const [selectedWallet, setSelectedWallet] = useState<number>(0) | ||
const { wallets } = useWallets() | ||
const setActiveWallet = useSetActiveWallet() | ||
|
||
useEffect(() => { | ||
async function setActive() { | ||
try { | ||
const wallet = wallets[selectedWallet] | ||
const provider = await wallet?.getEthersProvider() | ||
const signer = provider?.getSigner() | ||
|
||
const walletClientType = wallet?.walletClientType | ||
if ( | ||
walletClientType === 'coinbase_wallet' || | ||
walletClientType === 'privy' | ||
) | ||
await wallet?.switchChain(selectedChain.chainId) | ||
|
||
const adaptedAccount = await ethers5Adapter.signer.fromEthers({ | ||
signer, | ||
}) | ||
|
||
const thirdwebWallet = createWalletAdapter({ | ||
adaptedAccount, | ||
chain: defineChain(selectedChain.chainId), | ||
client, | ||
onDisconnect: () => {}, | ||
switchChain: () => {}, | ||
}) | ||
|
||
await thirdwebWallet.connect({ client }) | ||
setActiveWallet(thirdwebWallet) | ||
} catch (err: any) { | ||
console.log(err.message) | ||
} | ||
} | ||
|
||
setActive() | ||
}, [wallets, selectedWallet, selectedChain]) | ||
|
||
useEffect(() => { | ||
async function handleAuth() { | ||
if (ready && authenticated && user) { | ||
try { | ||
// Sign in to NextAuth with the Privy token | ||
const accessToken = await getAccessToken() | ||
const result = await signIn('credentials', { | ||
accessToken: accessToken, | ||
redirect: false, // Prevent automatic redirect | ||
}) | ||
|
||
if (result?.error) { | ||
console.error('NextAuth sign in failed:', result.error) | ||
} | ||
} catch (error) { | ||
console.error('Auth error:', error) | ||
} | ||
} | ||
} | ||
|
||
handleAuth() | ||
}, [ready, authenticated, user, getAccessToken]) | ||
|
||
useEffect(() => { | ||
if (ready && !authenticated) { | ||
signOut({ redirect: false }) | ||
} | ||
}, [ready, authenticated, user]) | ||
|
||
return ( | ||
<PrivyWalletContext.Provider value={{ selectedWallet, setSelectedWallet }}> | ||
{children} | ||
</PrivyWalletContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createContext } from 'react' | ||
import { arbitrum, sepolia, Chain } from 'thirdweb/chains' | ||
|
||
const ChainContextV5 = createContext({ | ||
selectedChain: | ||
process.env.NEXT_PUBLIC_CHAIN === 'mainnet' ? arbitrum : sepolia, | ||
setSelectedChain: (chain: Chain) => {}, | ||
}) | ||
|
||
export default ChainContextV5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Chain } from 'thirdweb/chains' | ||
|
||
export function getChainSlug(chain: Chain) { | ||
let slug | ||
if (chain.name === 'Arbitrum One') { | ||
slug = 'arbitrum' | ||
} else { | ||
slug = chain.name?.toLowerCase().replace(/\s+/g, '-') ?? '' | ||
} | ||
return slug | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createThirdwebClient } from 'thirdweb' | ||
|
||
const client = createThirdwebClient({ | ||
clientId: process.env.NEXT_PUBLIC_THIRDWEB_CLIENT_ID as string, | ||
}) | ||
|
||
export const serverClient = createThirdwebClient({ | ||
secretKey: process.env.NEXT_PUBLIC_THIRDWEB_CLIENT_SECRET as string, | ||
}) | ||
|
||
export default client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.