Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🍪 Implement wagmi storage in cookies #82

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"sonner": "^1.4.41",
"styled-components": "^6.1.8",
"viem": "^2.9.3",
"wagmi": "^2.7.1",
"wagmi": "^2.10.2",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
7 changes: 6 additions & 1 deletion packages/frontend/src/config/wagmiConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { createConfig, webSocket } from 'wagmi'
import { cookieStorage, createConfig, createStorage, webSocket } from 'wagmi'
import { arbitrum, arbitrumSepolia, hardhat } from 'wagmi/chains'
import { coinbaseWallet, walletConnect } from 'wagmi/connectors'
import { environment } from '@/config/environment'
import { SupportedChains } from '@/blockchain/chain'

const storage = createStorage({
storage: cookieStorage,
})

export const wagmiConfig = createConfig({
chains: SupportedChains,
ssr: true,
Expand All @@ -16,6 +20,7 @@ export const wagmiConfig = createConfig({
walletConnect({ projectId: environment.walletConnectProjectId }),
coinbaseWallet({ appName: 'Devcon Auction/Raffle' }),
],
storage,
})

declare module 'wagmi' {
Expand Down
18 changes: 15 additions & 3 deletions packages/frontend/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { GlobalStyles } from '@/styles/GobalStyles'
import type { AppProps } from 'next/app'
import type { AppContext, AppProps } from 'next/app'
import { BlockchainProviders } from '@/providers/wagmi'
import { BidsProvider } from '@/providers/BidsProvider/provider'
import { ReactNode } from 'react'
import { TopBar } from '@/components/topBar/TopBar'
import { Footer } from '@/components/footer/Footer'
import { Toaster } from 'sonner'
import { cookieToInitialState } from 'wagmi'
import { wagmiConfig } from '@/config/wagmiConfig'

export default function App({ Component, pageProps, cookie }: AppProps & CustomProps) {
const initialState = cookieToInitialState(wagmiConfig, cookie)

export default function App({ Component, pageProps }: AppProps) {
return (
<BlockchainProviders>
<BlockchainProviders initialState={initialState}>
<BidsProvider>
<GlobalStyles />
<Layout>
Expand All @@ -28,3 +32,11 @@ const Layout = ({ children }: { children: ReactNode }) => (
<Footer />
</>
)

interface CustomProps {
cookie: string | undefined
}

App.getInitialProps = async (context: AppContext): Promise<CustomProps> => {
return { cookie: context.ctx.req?.headers.cookie }
}
6 changes: 3 additions & 3 deletions packages/frontend/src/pages/api/frames/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const handleRequest = frames(async (req) => {
image: `/images?status=ended`,
imageOptions: {
headers: {
"Cache-Control": "public, max-age=0",
'Cache-Control': 'public, max-age=0',
},
},
buttons: [
Expand All @@ -34,7 +34,7 @@ const handleRequest = frames(async (req) => {
image: `/images?status=withdrawal`,
imageOptions: {
headers: {
"Cache-Control": "public, max-age=0",
'Cache-Control': 'public, max-age=0',
},
},
buttons: [
Expand All @@ -52,7 +52,7 @@ const handleRequest = frames(async (req) => {
image: `/images?status=active`,
imageOptions: {
headers: {
"Cache-Control": "public, max-age=0",
'Cache-Control': 'public, max-age=0',
},
},
buttons: [
Expand Down
7 changes: 4 additions & 3 deletions packages/frontend/src/providers/wagmi.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WagmiProvider } from 'wagmi'
import { State, WagmiProvider } from 'wagmi'
import { ReactNode } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { wagmiConfig } from '@/config/wagmiConfig'
Expand All @@ -7,6 +7,7 @@ import { environment } from '@/config/environment'

interface ProviderProps {
children: ReactNode
initialState: State | undefined
}

const queryClient = new QueryClient()
Expand All @@ -16,9 +17,9 @@ createWeb3Modal({
wagmiConfig,
})

export const BlockchainProviders = ({ children }: ProviderProps) => {
export const BlockchainProviders = ({ children, initialState }: ProviderProps) => {
return (
<WagmiProvider config={wagmiConfig}>
<WagmiProvider config={wagmiConfig} initialState={initialState} reconnectOnMount>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</WagmiProvider>
)
Expand Down
Loading