diff --git a/.env.example b/.env.example index efe26bc..46b331d 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,3 @@ NEXT_PUBLIC_ALCHEMY_API_KEY='' -NEXT_PUBLIC_CHAIN_ID='' NEXT_PUBLIC_ENABLE_TESTNETS='' NEXT_PUBLIC_EVM_EXPLORER='' diff --git a/src/constants/constants.ts b/src/constants/constants.ts index e42e765..f39ba61 100644 --- a/src/constants/constants.ts +++ b/src/constants/constants.ts @@ -15,24 +15,34 @@ interface ContractsJson { meetdAppNFT: typeof meetdAppNFTJsonPolygon | typeof meetdAppNFTJsonMumbai } -export const CHAINID = process.env.NEXT_PUBLIC_CHAIN_ID +const mumbaiContracts: ContractsJson = { + meetdAppFactory: meetdAppFactoryJsonMumbai, + meetdAppEvent: meetdAppEventJsonMumbai, + meetdAppNFT: meetdAppNFTJsonMumbai +} + +const polygonContracts: ContractsJson = { + meetdAppFactory: meetdAppFactoryJsonPolygon, + meetdAppEvent: meetdAppEventJsonPolygon, + meetdAppNFT: meetdAppNFTJsonPolygon +} + export const PROVIDER = process.env.NEXT_PUBLIC_ALCHEMY_API_KEY + export const SCAN = ( address: string, scan: string | undefined = process.env.NEXT_PUBLIC_EVM_EXPLORER ) => `${scan}/${address}` -export const CONTRACTS_JSON = (): ContractsJson => { - if (process.env.NEXT_PUBLIC_ENABLE_TESTNETS === 'true') { - return { - meetdAppEvent: meetdAppEventJsonMumbai, - meetdAppFactory: meetdAppFactoryJsonMumbai, - meetdAppNFT: meetdAppNFTJsonMumbai - } - } - return { - meetdAppEvent: meetdAppEventJsonPolygon, - meetdAppFactory: meetdAppFactoryJsonPolygon, - meetdAppNFT: meetdAppNFTJsonPolygon - } +const setChainId = () => + process.env.NEXT_PUBLIC_ENABLE_TESTNETS === 'true' ? 80001 : 137 + +export const CHAINID = setChainId() + +const setContractsJson = (): ContractsJson => { + return process.env.NEXT_PUBLIC_ENABLE_TESTNETS === 'true' + ? mumbaiContracts + : polygonContracts } + +export const CONTRACTS_JSON = setContractsJson() diff --git a/src/pages/events/[id]/index.tsx b/src/pages/events/[id]/index.tsx index f97aaaa..d2317d4 100644 --- a/src/pages/events/[id]/index.tsx +++ b/src/pages/events/[id]/index.tsx @@ -39,7 +39,6 @@ import Loader from '@/components/shared/Loader' import { useTranslation } from 'react-i18next' export default function Event(): JSX.Element { - const contractsJson = CONTRACTS_JSON() const [attendees, setAttendees] = useState([]) const [event, setEvent] = useState(initialEvent) const [hasTicket, setHasTicket] = useState(false) @@ -189,14 +188,9 @@ export default function Event(): JSX.Element { const rpcProvider: ethers.providers.JsonRpcProvider = new ethers.providers.JsonRpcProvider(PROVIDER) - console.log( - 'contractsJson.meetdAppFactory.address: ', - contractsJson.meetdAppFactory.address - ) - const meetdAppFactoryContract: MeetdAppFactory = new ethers.Contract( - contractsJson.meetdAppFactory.address, - contractsJson.meetdAppFactory.abi, + CONTRACTS_JSON.meetdAppFactory.address, + CONTRACTS_JSON.meetdAppFactory.abi, rpcProvider ) as MeetdAppFactory @@ -208,7 +202,7 @@ export default function Event(): JSX.Element { const eventContract = new ethers.Contract( eventContractAdress, - contractsJson.meetdAppEvent.abi, + CONTRACTS_JSON.meetdAppEvent.abi, rpcProvider ) as MeetdAppEvent diff --git a/src/pages/index.tsx b/src/pages/index.tsx index dba0eb8..1dadba0 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -16,15 +16,17 @@ const metadata = { } export default function Home() { - const contractsJson = CONTRACTS_JSON() const [isLoading, setIsLoading] = useState(true) const fetchActiveEvents = async () => { setTimeout(() => { - const meetdAppFactoryContract: MeetdAppFactory = new ethers.Contract( - contractsJson.meetdAppFactory.address, - contractsJson.meetdAppFactory.abi, + const rpcProvider: ethers.providers.JsonRpcProvider = new ethers.providers.JsonRpcProvider(PROVIDER) + + const meetdAppFactoryContract: MeetdAppFactory = new ethers.Contract( + CONTRACTS_JSON.meetdAppFactory.address, + CONTRACTS_JSON.meetdAppFactory.abi, + rpcProvider ) as MeetdAppFactory setIsLoading(false)