Skip to content

Commit

Permalink
use system switch in createPuppet
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Oct 31, 2023
1 parent 276b1f0 commit 5afee8b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 12 additions & 4 deletions packages/world-modules/src/modules/puppet/createPuppet.sol
Original file line number Diff line number Diff line change
@@ -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))
)
);
}
4 changes: 3 additions & 1 deletion packages/world-modules/test/PuppetModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5afee8b

Please sign in to comment.