diff --git a/src/lib/crossbell.ts b/src/lib/crossbell.ts index fd339e8b..7221623a 100644 --- a/src/lib/crossbell.ts +++ b/src/lib/crossbell.ts @@ -1,12 +1,15 @@ import { Contract, Indexer } from "crossbell.js" const indexer = new Indexer() -const contract = - typeof window !== "undefined" - ? new Contract((window).ethereum) - : undefined -if (contract) { - contract.connect() + +let contract: Contract | undefined + +const getContract = () => { + if (!contract && typeof window !== "undefined") { + contract = new Contract((window).ethereum) + contract.connect() + } + return contract } -export { indexer, contract } +export { indexer, getContract } diff --git a/src/models/page.model.ts b/src/models/page.model.ts index a3944209..14e86734 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -13,7 +13,7 @@ import { PageVisibilityEnum, Notes } from "~/lib/types" import { isUUID } from "~/lib/uuid" import { stripHTML } from "~/lib/utils" import unidata from "~/lib/unidata" -import { indexer, contract } from "~/lib/crossbell" +import { indexer, getContract } from "~/lib/crossbell" const checkPageSlug = async ({ slug, @@ -305,7 +305,7 @@ export async function likePage({ if (!characterId) { throw notFound(`character not found`) } else { - return contract?.linkNote( + return getContract()?.linkNote( characterId, pageId.split("-")[0], pageId.split("-")[1], @@ -325,7 +325,7 @@ export async function unlikePage({ if (!characterId) { throw notFound(`character not found`) } else { - return contract?.unlinkNote( + return getContract()?.unlinkNote( characterId, pageId.split("-")[0], pageId.split("-")[1], @@ -371,7 +371,11 @@ export async function mintPage({ address: string pageId: string }) { - return contract?.mintNote(pageId.split("-")[0], pageId.split("-")[1], address) + return getContract()?.mintNote( + pageId.split("-")[0], + pageId.split("-")[1], + address, + ) } export async function getMints({ pageId }: { pageId: string }) {