|
| 1 | +import { getSyncEntities } from "@dojoengine/state"; |
| 2 | +import * as torii from "@dojoengine/torii-client"; |
| 3 | + |
| 4 | +import { models } from "./models.ts"; |
| 5 | +import { systems } from "./systems.ts"; |
| 6 | +import { defineContractComponents } from "./defineContractComponents.ts"; |
| 7 | +import { world } from "./world.ts"; |
| 8 | +import { Config } from "../../dojoConfig.ts"; |
| 9 | +import { setupWorld } from "./defineContractSystems.ts"; |
| 10 | + |
| 11 | +import { DojoProvider } from "@dojoengine/core"; |
| 12 | +import { BurnerManager } from "@dojoengine/create-burner"; |
| 13 | +import { Account, RpcProvider } from "starknet"; |
| 14 | +import { |
| 15 | + ClientComponents, |
| 16 | + createClientComponents, |
| 17 | +} from "./createClientComponent.ts"; |
| 18 | + |
| 19 | +export type SetupResult = Awaited<ReturnType<typeof setup>>; |
| 20 | +export type IDojo = Awaited<ReturnType<typeof setup>>; |
| 21 | + |
| 22 | +export async function setup({ ...config }: Config) { |
| 23 | + // torii client |
| 24 | + let toriiClient = null; |
| 25 | + try { |
| 26 | + toriiClient = await torii.createClient({ |
| 27 | + rpcUrl: config.rpcUrl, |
| 28 | + toriiUrl: config.toriiUrl, |
| 29 | + relayUrl: "", |
| 30 | + worldAddress: config.manifest.world.address || "", |
| 31 | + }); |
| 32 | + } catch (e) { |
| 33 | + console.error("Failed to create torii client:", e); |
| 34 | + throw e; |
| 35 | + } |
| 36 | + |
| 37 | + // create contract components |
| 38 | + let contractModels = null; |
| 39 | + try { |
| 40 | + contractModels = createClientComponents({ |
| 41 | + contractComponents: defineContractComponents(world), |
| 42 | + }); |
| 43 | + } catch (e) { |
| 44 | + console.error("Failed to create contract components:", e); |
| 45 | + throw e; |
| 46 | + } |
| 47 | + |
| 48 | + // create client components |
| 49 | + const { models: clientModels } = models({ contractModels }); |
| 50 | + |
| 51 | + // fetch all existing entities from torii |
| 52 | + let sync = null; |
| 53 | + try { |
| 54 | + sync = await getSyncEntities( |
| 55 | + toriiClient, |
| 56 | + contractModels as any, |
| 57 | + [], |
| 58 | + 1000 |
| 59 | + ); |
| 60 | + } catch (e) { |
| 61 | + console.error("Failed to fetch sync:", e); |
| 62 | + throw e; |
| 63 | + } |
| 64 | + |
| 65 | + let client = null; |
| 66 | + try { |
| 67 | + client = await setupWorld( |
| 68 | + new DojoProvider(config.manifest, config.rpcUrl), |
| 69 | + config |
| 70 | + ); |
| 71 | + } catch (e) { |
| 72 | + console.error("Failed to create client:", e); |
| 73 | + throw e; |
| 74 | + } |
| 75 | + |
| 76 | + const rpcProvider = new RpcProvider({ |
| 77 | + nodeUrl: config.rpcUrl, |
| 78 | + }); |
| 79 | + |
| 80 | + let burnerManager = null; |
| 81 | + try { |
| 82 | + burnerManager = new BurnerManager({ |
| 83 | + masterAccount: new Account( |
| 84 | + rpcProvider, |
| 85 | + config.masterAddress, |
| 86 | + config.masterPrivateKey |
| 87 | + ), |
| 88 | + feeTokenAddress: config.feeTokenAddress, |
| 89 | + accountClassHash: config.accountClassHash, |
| 90 | + |
| 91 | + rpcProvider, |
| 92 | + }); |
| 93 | + } catch (e) { |
| 94 | + console.log("Failed to create burner manager:", e); |
| 95 | + throw e; |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + await burnerManager.init(); |
| 100 | + if (burnerManager.list().length === 0) { |
| 101 | + await burnerManager.create(); |
| 102 | + } |
| 103 | + } catch (e) { |
| 104 | + console.error(e); |
| 105 | + } |
| 106 | + const actions = systems({ |
| 107 | + client, |
| 108 | + clientModels: clientModels as ClientComponents, |
| 109 | + contractComponents: contractModels, |
| 110 | + }); |
| 111 | + const account = burnerManager.getActiveAccount(); |
| 112 | + if (null === account || undefined === account) { |
| 113 | + throw new Error("failed to get active account"); |
| 114 | + } |
| 115 | + |
| 116 | + return { |
| 117 | + client, |
| 118 | + clientModels, |
| 119 | + contractComponents: clientModels, |
| 120 | + systemCalls: actions.actions, |
| 121 | + config, |
| 122 | + world, |
| 123 | + burnerManager, |
| 124 | + rpcProvider, |
| 125 | + sync, |
| 126 | + account, |
| 127 | + }; |
| 128 | +} |
0 commit comments