From 5afee8b5940465c1dc34f6035a4aab2a7a19957f Mon Sep 17 00:00:00 2001 From: alvrs Date: Tue, 31 Oct 2023 22:02:21 +0100 Subject: [PATCH] use system switch in createPuppet --- .../src/modules/puppet/createPuppet.sol | 16 ++++++++++++---- packages/world-modules/test/PuppetModule.t.sol | 4 +++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/world-modules/src/modules/puppet/createPuppet.sol b/packages/world-modules/src/modules/puppet/createPuppet.sol index 54311284c5..0ca446e522 100644 --- a/packages/world-modules/src/modules/puppet/createPuppet.sol +++ b/packages/world-modules/src/modules/puppet/createPuppet.sol @@ -1,23 +1,31 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.21; import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; -import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol"; +import { IWorldRegistrationSystem } from "@latticexyz/world/src/codegen/interfaces/IWorldRegistrationSystem.sol"; import { WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol"; import { PUPPET_DELEGATION, PUPPET_FACTORY } from "./constants.sol"; import { PuppetDelegationControl } from "./PuppetDelegationControl.sol"; import { Puppet } from "./Puppet.sol"; import { PuppetFactorySystem } from "./PuppetFactorySystem.sol"; +import { SystemSwitch } from "../../utils/SystemSwitch.sol"; using WorldResourceIdInstance for ResourceId; /** * This free function can be used to create a puppet and register it with the puppet delegation control. * Since it is inlined in the caller's context, the calls originate from the caller's address. + * The function expects to be called from a World context + * (i.e. `StoreSwitch.setStoreAddress(world)` is set up in the calling contract) */ -function createPuppet(IBaseWorld world, ResourceId systemId) returns (address puppet) { +function createPuppet(ResourceId systemId) returns (address puppet) { puppet = abi.decode( - world.call(PUPPET_FACTORY, abi.encodeCall(PuppetFactorySystem.createPuppet, (systemId))), + SystemSwitch.call(PUPPET_FACTORY, abi.encodeCall(PuppetFactorySystem.createPuppet, (systemId))), (address) ); - world.registerNamespaceDelegation(systemId.getNamespaceId(), PUPPET_DELEGATION, new bytes(0)); + SystemSwitch.call( + abi.encodeCall( + IWorldRegistrationSystem.registerNamespaceDelegation, + (systemId.getNamespaceId(), PUPPET_DELEGATION, new bytes(0)) + ) + ); } diff --git a/packages/world-modules/test/PuppetModule.t.sol b/packages/world-modules/test/PuppetModule.t.sol index 7e29308620..d601da0b30 100644 --- a/packages/world-modules/test/PuppetModule.t.sol +++ b/packages/world-modules/test/PuppetModule.t.sol @@ -3,6 +3,7 @@ pragma solidity >=0.8.21; import { Test } from "forge-std/Test.sol"; import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; +import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; import { World } from "@latticexyz/world/src/World.sol"; import { ResourceId, WorldResourceIdLib, WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol"; @@ -50,13 +51,14 @@ contract PuppetModuleTest is Test, GasReporter { world = IBaseWorld(address(new World())); world.initialize(new CoreModule()); world.installModule(new PuppetModule(), new bytes(0)); + StoreSwitch.setStoreAddress(address(world)); // Register a new system PuppetTestSystem system = new PuppetTestSystem(); world.registerSystem(systemId, system, true); // Connect the puppet - puppet = PuppetTestSystem(createPuppet(world, systemId)); + puppet = PuppetTestSystem(createPuppet(systemId)); } function testEmitOnPuppet() public {