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

Deploy #10

Merged
merged 3 commits into from
Sep 22, 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
30 changes: 30 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ COPY . .
RUN npm install -g pnpm && export NEXT_PUBLIC_IS_BASE=${NEXT_PUBLIC_IS_BASE} && pnpm build

FROM nginx
COPY --from=builder /app/out /usr/share/nginx/html
COPY --from=builder /app/out /usr/share/nginx/html
RUN apt-get update && apt-get install -y jq && apt-get clean
COPY ./nginx.conf.example /etc/nginx/nginx.conf
COPY ./start.sh /start.sh
RUN chmod +x /start.sh
CMD ["/bin/bash", "/start.sh"]
25 changes: 3 additions & 22 deletions nginx.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,17 @@ http {
}

location ~ /index/ {
proxy_pass http://Sepolia/;
proxy_pass http://meiliSearch:7700/;
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
proxy_set_header X-Forward-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Authorization "Bearer $TOKEN";
}
location ~ /indexMain/ {
proxy_pass http://main/;
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
proxy_set_header X-Forward-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Authorization "Bearer $TOKEN";
proxy_set_header Authorization "Bearer TOKEN";
}
location ~ /api/ {
proxy_pass http://Sepolia/;
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
proxy_set_header X-Forward-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location ~ /apiMain/ {
proxy_pass http://main/;
proxy_pass http://node1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
proxy_set_header X-Forward-Proto $scheme;
Expand Down
10 changes: 6 additions & 4 deletions src/components/Cards/SurfaceCards/ClaimCard.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { ClaimData } from "@/types";
import React, { FC } from "react";
import React, { FC, useContext } from "react";
import { SurfaceCardBase } from "./SurfaceCardBase";
import { Link } from "@/components/Link";
import { Skeleton } from "@/components/Skeleton";
import { CardField } from "../Card";
import { shortenAddress } from "@/utils";
import { useNetworkConfig } from "@/hooks/useNetworkConfig";
import { NetworkConfigContext } from "@/components/NetworkConfigContext";

type ClaimCardProps = {
claimData: ClaimData;
};

const ClaimCard: FC<Partial<ClaimCardProps>> = ({ claimData = {} }) => {
const ClaimCard: FC<Partial<ClaimCardProps>> = ({
claimData = {} as ClaimData,
}) => {
const { claim, claimant, output_block, event_id } = claimData;
const { explorer_l1: EXPLORER_L1, explorer_l2: EXPLORER_L2 } =
useNetworkConfig();
useContext(NetworkConfigContext);
return (
<SurfaceCardBase>
<div className="flex justify-between gap-2 text-sm">
Expand Down
6 changes: 3 additions & 3 deletions src/components/Cards/SurfaceCards/CreditCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FC } from "react";
import { useContext, type FC } from "react";

import { Skeleton } from "@/components/Skeleton";
import { Link } from "../../Link";
Expand All @@ -7,7 +7,7 @@ import { SurfaceCardBase } from "./SurfaceCardBase";
import { Credit } from "@/types";
import { shortenAddress } from "@/utils";
import { EtherUnitDisplay } from "@/components/Displays/EtherUnitDisplay";
import { useNetworkConfig } from "@/hooks/useNetworkConfig";
import { NetworkConfigContext } from "@/components/NetworkConfigContext";

type CreditCardProps = {
credit: Credit;
Expand All @@ -18,7 +18,7 @@ const CreditCard: FC<Partial<CreditCardProps>> = function ({
credit: { address, amount } = {},
index,
}) {
const { explorer_l1: EXPLORER_L1 } = useNetworkConfig();
const { explorer_l1: EXPLORER_L1 } = useContext(NetworkConfigContext);
return (
<SurfaceCardBase>
<div className="flex justify-between gap-2 text-sm">
Expand Down
6 changes: 3 additions & 3 deletions src/components/Cards/SurfaceCards/EventCard.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { FC } from "react";
import { useContext, type FC } from "react";

import dayjs from "@/utils/dayjs";
import { Skeleton } from "@/components/Skeleton";
import { Link } from "../../Link";
import { SurfaceCardBase } from "./SurfaceCardBase";
import { LatestEvents } from "@/types";
import { shortenAddress } from "@/utils";
import { useNetworkConfig } from "@/hooks/useNetworkConfig";
import { NetworkConfigContext } from "@/components/NetworkConfigContext";

type EventCardProps = {
events: LatestEvents;
Expand All @@ -22,7 +22,7 @@ const EventCard: FC<Partial<EventCardProps>> = function ({
tx_hash,
} = {},
}) {
const { explorer_l1: EXPLORER_L1 } = useNetworkConfig();
const { explorer_l1: EXPLORER_L1 } = useContext(NetworkConfigContext);

return (
<SurfaceCardBase>
Expand Down
6 changes: 3 additions & 3 deletions src/components/Cards/SurfaceCards/GameCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FC } from "react";
import { useContext, type FC } from "react";

import dayjs from "@/utils/dayjs";
import { Skeleton } from "@/components/Skeleton";
Expand All @@ -10,7 +10,7 @@ import { shortenAddress } from "@/utils";
import shield from "@/icons/shield.png";
import sword from "@/icons/sword.png";
import Image from "next/image";
import { useNetworkConfig } from "@/hooks/useNetworkConfig";
import { NetworkConfigContext } from "@/components/NetworkConfigContext";

type GameCardProps = {
game: Game;
Expand All @@ -26,7 +26,7 @@ const GameCard: FC<Partial<GameCardProps>> = function ({
} = {},
}) {
const { explorer_l1: EXPLORER_L1, explorer_l2: EXPLORER_L2 } =
useNetworkConfig();
useContext(NetworkConfigContext);
return (
<SurfaceCardBase>
<div className="flex justify-between gap-2 text-sm">
Expand Down
20 changes: 13 additions & 7 deletions src/components/DisputeGameLogo.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { useNetworkConfig } from "@/hooks/useNetworkConfig";
import { NetworkConfigContext } from "@/components/NetworkConfigContext";
import Link from "next/link";
import { useContext } from "react";


const DisputeGameLogo: React.FC<{ className?: string }> = ({ }) => {
const { network } = useNetworkConfig()
return <Link href="/">
<img src={network === 'base-sepolia' ? '/logo_base.svg' : '/logo.svg'} className="w-48" alt="logo" />
</Link>
const DisputeGameLogo: React.FC<{ className?: string }> = ({}) => {
const { network } = useContext(NetworkConfigContext);
return (
<Link href="/">
<img
src={network === "base-sepolia" ? "/logo_base.svg" : "/logo.svg"}
className="w-48"
alt="logo"
/>
</Link>
);
};

export default DisputeGameLogo;
6 changes: 3 additions & 3 deletions src/components/ExplorerDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { useMemo } from "react";
import React, { useContext, useMemo } from "react";

import "react-loading-skeleton/dist/skeleton.css";
import Skeleton from "react-loading-skeleton";

import { capitalize } from "@/utils";
import { useNetworkConfig } from "@/hooks/useNetworkConfig";
import { NetworkConfigContext } from "@/components/NetworkConfigContext";

type ExplorerDetailsItemProps = {
name: string;
Expand All @@ -31,7 +31,7 @@ function ExplorerDetailsItem({
}

export function ExplorerDetails() {
const { network } = useNetworkConfig();
const { network } = useContext(NetworkConfigContext);
const explorerDetailsItems: ExplorerDetailsItemProps[] = useMemo(() => {
return [{ name: "Network", value: capitalize(network!) }];
}, [network]);
Expand Down
14 changes: 9 additions & 5 deletions src/components/Logo.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useNetworkConfig } from "@/hooks/useNetworkConfig";
import { useTheme } from "next-themes";
import { NetworkConfigContext } from "@/components/NetworkConfigContext";
import Link from "next/link";
import { useContext } from "react";

export const Logo: React.FC<{ className?: string }> = ({ className = "" }) => {
const { network } = useNetworkConfig()
const { network } = useContext(NetworkConfigContext);
return (
<Link href="/">
<img src={network === 'base-sepolia' ? '/logo_base.svg' : '/logo.svg'} className="w-40" alt="logo" />
<img
src={network === "base-sepolia" ? "/logo_base.svg" : "/logo.svg"}
className="w-40"
alt="logo"
/>
</Link>
);
}
};
42 changes: 42 additions & 0 deletions src/components/NetworkConfigContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NetworkConfig, networkConfigs, Network } from "@/utils/env";
import { createContext, ReactNode, useEffect, useState } from "react";

const defaultNetwork = networkConfigs["sepolia"];
export const NetworkConfigContext =
createContext<NetworkConfig>(defaultNetwork);

export default function NetworkConfigProvider({
children,
}: {
children: ReactNode;
}) {
const [config, setConfig] = useState<NetworkConfig>(defaultNetwork);
useEffect(() => {
const getChain = async () => {
try {
const res = await fetch("/api/disputegames/chainname");
const data = await res.json();
let network = data?.blockchain as string;
if (network) {
if (network.startsWith("eth-")) {
network = network.substring(4);
}
const fetchedConfig = networkConfigs[network as Network];
if (fetchedConfig) {
setConfig(fetchedConfig); // 更新上下文状态
}
}
} catch (error) {
console.error("Failed to fetch network:", error);
}
};

getChain();
}, []);

return (
<NetworkConfigContext.Provider value={config}>
{children}
</NetworkConfigContext.Provider>
);
}
25 changes: 12 additions & 13 deletions src/hooks/useAutoSwitchNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { useAccount, useSwitchChain } from "wagmi"
import { useNetworkConfig } from "./useNetworkConfig"
import { useEffect } from "react"
import { mainnet, sepolia } from "viem/chains"

import { useAccount, useSwitchChain } from "wagmi";
import { useContext, useEffect } from "react";
import { mainnet, sepolia } from "viem/chains";
import { NetworkConfigContext } from "@/components/NetworkConfigContext";

const useAutoSwitchNetwork = () => {
const { network } = useNetworkConfig()
const { switchChain } = useSwitchChain()
const { isConnected, chain } = useAccount()
const { network } = useContext(NetworkConfigContext);
const { switchChain } = useSwitchChain();
const { isConnected, chain } = useAccount();
useEffect(() => {
if (!isConnected || !chain) return;
const targetChainId = network === 'mainnet' ? mainnet.id : sepolia.id;
const targetChainId = network === "mainnet" ? mainnet.id : sepolia.id;
if (chain.id !== targetChainId) {
switchChain({ chainId: targetChainId })
switchChain({ chainId: targetChainId });
}
}, [isConnected, network, chain])
}
}, [isConnected, network, chain]);
};

export default useAutoSwitchNetwork
export default useAutoSwitchNetwork;
46 changes: 23 additions & 23 deletions src/hooks/useEthersProvider.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import { type Config, getClient } from '@wagmi/core'
import { FallbackProvider, JsonRpcProvider } from 'ethers'
import type { Client, Chain, Transport } from 'viem'
import { wagmiConfig } from './useWagmiConfig'
import { useMemo } from 'react'
import { useNetworkConfig } from './useNetworkConfig'
import { mainnet, sepolia } from 'viem/chains'
import { type Config, getClient } from "@wagmi/core";
import { FallbackProvider, JsonRpcProvider } from "ethers";
import type { Client, Chain, Transport } from "viem";
import { wagmiConfig } from "./useWagmiConfig";
import { useContext, useMemo } from "react";
import { NetworkConfigContext } from "@/components/NetworkConfigContext";
import { mainnet, sepolia } from "viem/chains";

export function clientToProvider(client: Client<Transport, Chain>) {
const { chain, transport } = client
const { chain, transport } = client;
const network = {
chainId: chain.id,
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
}
if (transport.type === 'fallback') {
};
if (transport.type === "fallback") {
const providers = (transport.transports as ReturnType<Transport>[]).map(
({ value }) => new JsonRpcProvider(value?.url, network),
)
if (providers.length === 1) return providers[0]
return new FallbackProvider(providers)
({ value }) => new JsonRpcProvider(value?.url, network)
);
if (providers.length === 1) return providers[0];
return new FallbackProvider(providers);
}
return new JsonRpcProvider(transport.url, network)
return new JsonRpcProvider(transport.url, network);
}

/** Action to convert a viem Client to an ethers.js Provider. */
export function useEthersProvider() {
const { network } = useNetworkConfig()
const { network } = useContext(NetworkConfigContext);
const chainId = useMemo(() => {
return network === 'mainnet' ? mainnet.id : sepolia.id
}, [network])
const client = useMemo(() => getClient(wagmiConfig, { chainId }), [chainId])
return network === "mainnet" ? mainnet.id : sepolia.id;
}, [network]);
const client = useMemo(() => getClient(wagmiConfig, { chainId }), [chainId]);
return useMemo(() => {
if (!client) return
return clientToProvider(client)
}, [client])
}
if (!client) return;
return clientToProvider(client);
}, [client]);
}
Loading