diff --git a/client/apps/game/src/dojo/sync.ts b/client/apps/game/src/dojo/sync.ts index 3fcf6e6eb..1296a9f53 100644 --- a/client/apps/game/src/dojo/sync.ts +++ b/client/apps/game/src/dojo/sync.ts @@ -1,9 +1,7 @@ import { AppStore } from "@/hooks/store/use-ui-store"; import { LoadingStateKey } from "@/hooks/store/use-world-loading"; -import { ETERNUM_CONFIG } from "@/utils/config"; import { BUILDING_CATEGORY_POPULATION_CONFIG_ID, - configManager, HYPERSTRUCTURE_CONFIG_ID, SetupResult, WORLD_CONFIG_ID, @@ -212,10 +210,6 @@ export const initialSync = async (setup: SetupResult, state: AppStore) => { setLoading(LoadingStateKey.SingleKey, false); }); - const eternumConfig = await ETERNUM_CONFIG(); - console.log({ eternumConfig }); - configManager.setDojo(setup.components, eternumConfig); - setLoading(LoadingStateKey.Events, true); await getEvents( diff --git a/client/apps/game/src/main.tsx b/client/apps/game/src/main.tsx index a9c457f9c..9745b6632 100644 --- a/client/apps/game/src/main.tsx +++ b/client/apps/game/src/main.tsx @@ -1,6 +1,6 @@ /// -import { setup } from "@bibliothecadao/eternum"; +import { configManager, setup } from "@bibliothecadao/eternum"; import { inject } from "@vercel/analytics"; import { Buffer } from "buffer"; import React from "react"; @@ -20,6 +20,7 @@ import GameRenderer from "./three/game-renderer"; import { PWAUpdatePopup } from "./ui/components/pwa-update-popup"; import { LoadingScreen } from "./ui/modules/loading-screen"; import { getRandomBackgroundImage } from "./ui/utils/utils"; +import { ETERNUM_CONFIG } from "./utils/config"; declare global { interface Window { @@ -71,6 +72,9 @@ async function init() { { vrfProviderAddress: env.VITE_PUBLIC_VRF_PROVIDER_ADDRESS, useBurner: env.VITE_PUBLIC_CHAIN === "local" }, ); + const eternumConfig = await ETERNUM_CONFIG(); + configManager.setDojo(setupResult.components, eternumConfig); + await initialSync(setupResult, state); const graphic = new GameRenderer(setupResult); diff --git a/client/apps/game/src/ui/components/structures/construction/structure-construction-menu.tsx b/client/apps/game/src/ui/components/structures/construction/structure-construction-menu.tsx index b5a5c1036..5757ddf38 100644 --- a/client/apps/game/src/ui/components/structures/construction/structure-construction-menu.tsx +++ b/client/apps/game/src/ui/components/structures/construction/structure-construction-menu.tsx @@ -15,7 +15,6 @@ import { import { useDojo } from "@bibliothecadao/react"; import React from "react"; -// const eternumConfig = await ETERNUM_CONFIG(); const STRUCTURE_IMAGE_PREFIX = "/images/buildings/thumb/"; export const STRUCTURE_IMAGE_PATHS = { [StructureType.Bank]: STRUCTURE_IMAGE_PREFIX + "mine.png", diff --git a/client/apps/landing/src/dojo/createSystemCalls.ts b/client/apps/landing/src/dojo/createSystemCalls.ts deleted file mode 100644 index 474c121de..000000000 --- a/client/apps/landing/src/dojo/createSystemCalls.ts +++ /dev/null @@ -1,195 +0,0 @@ -import type * as SystemProps from "@bibliothecadao/eternum"; -import { toast } from "sonner"; -import { type SetupNetworkResult } from "./setupNetwork"; - -class PromiseQueue { - private readonly queue: Array<() => Promise> = []; - private processing = false; - - async enqueue(task: () => Promise): Promise { - return new Promise((resolve, reject) => { - this.queue.push(async () => { - try { - const result = await task(); - resolve(result); - } catch (error) { - reject(error); - } - }); - this.processQueue(); - }); - } - - private async processQueue() { - if (this.processing) return; - this.processing = true; - - while (this.queue.length > 0) { - const task = this.queue.shift(); - if (task) { - try { - await task(); - } catch (error) { - console.error("Error processing task:", error); - } - } - } - - this.processing = false; - } -} - -//type SystemCallFunctions = ReturnType; -//type SystemCallFunction = (...args: any[]) => any; -//type WrappedSystemCalls = Record; - -/*const withErrorHandling = - (fn: any) => - async (...args: any[]) => { - try { - return await fn(...args); - } catch (error: any) { - toast(error.message); - } - };*/ - -export function createSystemCalls({ provider }: SetupNetworkResult) { - const promiseQueue = new PromiseQueue(); - - const withQueueing = Promise>(fn: T) => { - return async (...args: Parameters): Promise> => { - return await promiseQueue.enqueue(async () => await fn(...args)); - }; - }; - - const withErrorHandling = Promise>(fn: T) => { - return async (...args: Parameters): Promise> => { - try { - const resp = await fn(...args); - return resp; - } catch (error: any) { - let errorMessage = error.message; - - if (error.message.includes("Failure reason:")) { - const match = error.message.match(/Failure reason: \\"(.*?)"/); - if (match?.[1]) { - errorMessage = match[1].slice(0, -1); - } else { - const matchOther = error.message.match(/Failure reason: "(.*?)"/); - if (matchOther?.[1]) { - errorMessage = matchOther[1].slice(0, -1); - } else { - const matchHex = error.message.match(/Failure reason: (0x[0-9a-f]+) \('(.*)'\)/); - if (matchHex?.[2]) { - errorMessage = matchHex[2]; - } - } - } - } - toast(errorMessage); - throw error; - } - }; - }; - - const uuid = async () => { - return await provider.uuid(); - }; - - const create_order = async (props: SystemProps.CreateOrderProps) => { - await provider.create_order(props); - }; - - const accept_order = async (props: SystemProps.AcceptOrderProps) => { - await provider.accept_order(props); - }; - - const accept_partial_order = async (props: SystemProps.AcceptPartialOrderProps) => { - await provider.accept_partial_order(props); - }; - - const cancel_order = async (props: SystemProps.CancelOrderProps) => { - await provider.cancel_order(props); - }; - const mint_test_realm = async (props: SystemProps.MintTestRealmProps) => { - await provider.mint_test_realm(props); - }; - - const mint_season_passes = async (props: SystemProps.MintSeasonPassesProps) => { - await provider.mint_season_passes(props); - }; - - const attach_lords = async (props: SystemProps.AttachLordsProps) => { - await provider.attach_lords(props); - }; - - const detach_lords = async (props: SystemProps.DetachLordsProps) => { - await provider.detach_lords(props); - }; - - const mint_test_lords = async (props: SystemProps.MintTestLordsProps) => { - await provider.mint_test_lords(props); - }; - - const bridge_resources_into_realm = async (props: SystemProps.BridgeResourcesIntoRealmProps) => { - return await provider.bridge_resources_into_realm(props); - }; - - const bridge_start_withdraw_from_realm = async (props: SystemProps.BridgeStartWithdrawFromRealmProps) => { - return await provider.bridge_start_withdraw_from_realm(props); - }; - - const bridge_finish_withdraw_from_realm = async (props: SystemProps.BridgeFinishWithdrawFromRealmProps) => { - return await provider.bridge_finish_withdraw_from_realm(props); - }; - - const register_to_leaderboard = async (props: SystemProps.RegisterToLeaderboardProps) => { - await provider.register_to_leaderboard(props); - }; - - const end_game = async (props: SystemProps.EndGameProps) => { - await provider.end_game(props); - }; - - const claim_leaderboard_rewards = async (props: SystemProps.ClaimLeaderboardRewardsProps) => { - await provider.claim_leaderboard_rewards(props); - }; - - const isLive = async () => { - try { - await provider.uuid(); - return true; - } catch { - return false; - } - }; - - const systemCalls = { - isLive: withQueueing(withErrorHandling(isLive)), - create_order: withQueueing(withErrorHandling(create_order)), - accept_order: withQueueing(withErrorHandling(accept_order)), - cancel_order: withQueueing(withErrorHandling(cancel_order)), - accept_partial_order: withQueueing(withErrorHandling(accept_partial_order)), - - uuid: withQueueing(withErrorHandling(uuid)), - - mint_test_realm: withQueueing(withErrorHandling(mint_test_realm)), - mint_season_passes: withQueueing(withErrorHandling(mint_season_passes)), - attach_lords: withQueueing(withErrorHandling(attach_lords)), - detach_lords: withQueueing(withErrorHandling(detach_lords)), - mint_test_lords: withQueueing(withErrorHandling(mint_test_lords)), - bridge_resources_into_realm: withQueueing(withErrorHandling(bridge_resources_into_realm)), - bridge_start_withdraw_from_realm: withQueueing(withErrorHandling(bridge_start_withdraw_from_realm)), - bridge_finish_withdraw_from_realm: withQueueing(withErrorHandling(bridge_finish_withdraw_from_realm)), - register_to_leaderboard: withQueueing(withErrorHandling(register_to_leaderboard)), - end_game: withQueueing(withErrorHandling(end_game)), - claim_leaderboard_rewards: withQueueing(withErrorHandling(claim_leaderboard_rewards)), - }; - - // TODO: Fix Type - /*const wrappedSystemCalls = Object.fromEntries( - Object.entries(systemCalls).map(([key, fn]) => [key, withErrorHandling(fn)]), - );*/ - - return systemCalls; -} diff --git a/client/apps/landing/src/dojo/setup.ts b/client/apps/landing/src/dojo/setup.ts deleted file mode 100644 index 91ac392b6..000000000 --- a/client/apps/landing/src/dojo/setup.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { ClientConfigManager, createClientComponents } from "@bibliothecadao/eternum"; -import { DojoConfig } from "@dojoengine/core"; -import { createSystemCalls } from "./createSystemCalls"; -import { setupNetwork } from "./setupNetwork"; -import { ETERNUM_CONFIG } from "../utils/config"; - -export type SetupResult = Awaited>; - -export async function setup({ ...config }: DojoConfig) { - const network = await setupNetwork(config); - const components = createClientComponents(network); - const systemCalls = createSystemCalls(network); - - const configManager = ClientConfigManager.instance(); - - const eternumConfig = await ETERNUM_CONFIG(); - configManager.setDojo(components, eternumConfig); - - return { - network, - components, - systemCalls, - }; -} diff --git a/client/apps/landing/src/dojo/setupNetwork.ts b/client/apps/landing/src/dojo/setupNetwork.ts deleted file mode 100644 index bcbecf362..000000000 --- a/client/apps/landing/src/dojo/setupNetwork.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { defineContractComponents, EternumProvider } from "@bibliothecadao/eternum"; -import { DojoConfig } from "@dojoengine/core"; -import { world } from "./world"; - -import { BurnerManager } from "@dojoengine/create-burner"; -import * as torii from "@dojoengine/torii-client"; -import { Account } from "starknet"; - -export type SetupNetworkResult = Awaited>; - -export async function setupNetwork({ ...config }: DojoConfig) { - const provider = new EternumProvider(config.manifest, config.rpcUrl); - - const toriiClient = await torii.createClient({ - rpcUrl: config.rpcUrl, - toriiUrl: config.toriiUrl, - relayUrl: config.relayUrl, - worldAddress: config.manifest.world.address || "", - }); - - const burnerManager = new BurnerManager({ - masterAccount: new Account(provider.provider, config.masterAddress, config.masterPrivateKey), - accountClassHash: config.accountClassHash, - rpcProvider: provider.provider, - feeTokenAddress: config.feeTokenAddress, - }); - await burnerManager.init(); - - try { - if (import.meta.env.VITE_PUBLIC_CHAIN === "local") { - if (burnerManager.list().length === 0) { - await burnerManager.create(); - } - } else { - await burnerManager.clear(); - } - } catch (e) { - console.error(e); - } - - return { - toriiClient, - contractComponents: defineContractComponents(world), - provider, - world, - burnerManager, - }; -} diff --git a/client/apps/landing/src/dojo/world.ts b/client/apps/landing/src/dojo/world.ts deleted file mode 100644 index 960676e5e..000000000 --- a/client/apps/landing/src/dojo/world.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { createWorld } from "@dojoengine/recs"; - -export const world = createWorld(); diff --git a/client/apps/landing/src/main.tsx b/client/apps/landing/src/main.tsx index d98758b80..79e8ec35b 100644 --- a/client/apps/landing/src/main.tsx +++ b/client/apps/landing/src/main.tsx @@ -6,15 +6,16 @@ import "./index.css"; // Import the generated route tree //import { ArkProvider } from '@ark-project/react'; +import { ClientConfigManager, setup } from "@bibliothecadao/eternum"; import { NuqsAdapter } from "nuqs/adapters/react"; import { dojoConfig } from "../dojoConfig"; import { StarknetProvider } from "./components/providers/starknet-provider"; import { ThemeProvider } from "./components/providers/theme-provider"; import { TypeH1 } from "./components/typography/type-h1"; -import { setup } from "./dojo/setup"; import { DojoProvider } from "./hooks/context/DojoContext"; import { DojoEventListener } from "./hooks/subscriptions.tsx/dojo-event-listener"; import { routeTree } from "./routeTree.gen"; +import { ETERNUM_CONFIG } from "./utils/config"; // Create a new router instance const router = createRouter({ routeTree }); @@ -25,6 +26,16 @@ declare module "@tanstack/react-router" { } } +const initializeApp = async () => { + const setupResult = await setup(dojoConfig, { + vrfProviderAddress: import.meta.env.VITE_PUBLIC_VRF_PROVIDER_ADDRESS, + useBurner: import.meta.env.VITE_PUBLIC_CHAIN === "local", + }); + const eternumConfig = await ETERNUM_CONFIG(); + ClientConfigManager.instance().setDojo(setupResult.components, eternumConfig); + return setupResult; +}; + // Render the app const rootElement = document.getElementById("root")!; if (!rootElement.innerHTML) { @@ -40,7 +51,7 @@ if (!rootElement.innerHTML) { , ); - const setupResult = await setup(dojoConfig); + const setupResult = await initializeApp(); root.render( diff --git a/packages/core/src/dojo/create-system-calls.ts b/packages/core/src/dojo/create-system-calls.ts index 21daf1992..5d432c748 100644 --- a/packages/core/src/dojo/create-system-calls.ts +++ b/packages/core/src/dojo/create-system-calls.ts @@ -22,6 +22,38 @@ export function createSystemCalls({ provider }: { provider: EternumProvider }) { await provider.cancel_order(props); }; + const mint_test_realm = async (props: SystemProps.MintTestRealmProps) => { + await provider.mint_test_realm(props); + }; + + const mint_season_passes = async (props: SystemProps.MintSeasonPassesProps) => { + await provider.mint_season_passes(props); + }; + + const attach_lords = async (props: SystemProps.AttachLordsProps) => { + await provider.attach_lords(props); + }; + + const detach_lords = async (props: SystemProps.DetachLordsProps) => { + await provider.detach_lords(props); + }; + + const mint_test_lords = async (props: SystemProps.MintTestLordsProps) => { + await provider.mint_test_lords(props); + }; + + const bridge_resources_into_realm = async (props: SystemProps.BridgeResourcesIntoRealmProps) => { + return await provider.bridge_resources_into_realm(props); + }; + + const bridge_start_withdraw_from_realm = async (props: SystemProps.BridgeStartWithdrawFromRealmProps) => { + return await provider.bridge_start_withdraw_from_realm(props); + }; + + const bridge_finish_withdraw_from_realm = async (props: SystemProps.BridgeFinishWithdrawFromRealmProps) => { + return await provider.bridge_finish_withdraw_from_realm(props); + }; + const upgrade_realm = async (props: SystemProps.UpgradeRealmProps) => { await provider.upgrade_realm(props); }; @@ -303,6 +335,15 @@ export function createSystemCalls({ provider }: { provider: EternumProvider }) { battle_pillage, battle_leave_and_claim, battle_leave_and_pillage, + + mint_test_realm, + mint_season_passes, + attach_lords, + detach_lords, + mint_test_lords, + bridge_resources_into_realm, + bridge_start_withdraw_from_realm, + bridge_finish_withdraw_from_realm, }; return systemCalls; diff --git a/packages/core/src/dojo/index.ts b/packages/core/src/dojo/index.ts index d901e93f4..738b631a6 100644 --- a/packages/core/src/dojo/index.ts +++ b/packages/core/src/dojo/index.ts @@ -1,5 +1,5 @@ export * from "./contract-components"; export * from "./create-client-components"; export * from "./setup"; -export * from "./setupNetwork"; +export * from "./setup-network"; export * from "./world"; diff --git a/packages/core/src/dojo/setupNetwork.ts b/packages/core/src/dojo/setup-network.ts similarity index 100% rename from packages/core/src/dojo/setupNetwork.ts rename to packages/core/src/dojo/setup-network.ts diff --git a/packages/core/src/dojo/setup.ts b/packages/core/src/dojo/setup.ts index 613fac1d1..7bbac75e4 100644 --- a/packages/core/src/dojo/setup.ts +++ b/packages/core/src/dojo/setup.ts @@ -1,7 +1,7 @@ import { DojoConfig } from "@dojoengine/core"; import { createClientComponents } from ".."; import { createSystemCalls } from "./create-system-calls"; -import { setupNetwork } from "./setupNetwork"; +import { setupNetwork } from "./setup-network"; export type SetupResult = Awaited>;