Skip to content

Commit

Permalink
Add test for delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
vdrg committed Nov 22, 2024
1 parent 572fe5b commit 05ea70f
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/world/ts/node/render-solidity/renderSystemLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export function renderSystemLibrary(options: RenderSystemLibraryOptions) {
symbol: "IWorldCall",
path: `${worldImportPath}/IWorldKernel.sol`,
},
{
symbol: "Systems",
path: `${worldImportPath}/codegen/tables/Systems.sol`,
},
{
symbol: "ResourceId",
path: `${storeImportPath}/ResourceId.sol`,
Expand Down Expand Up @@ -82,6 +86,10 @@ export function renderSystemLibrary(options: RenderSystemLibraryOptions) {
return ${userTypeName}.wrap(resourceId.unwrap());
}
function getAddress(${userTypeName} self) internal view returns(address) {
return Systems.getSystem(self.toResourceId());
}
function _world() private view returns (IWorldCall) {
return IWorldCall(StoreSwitch.getStoreAddress());
}
Expand Down
13 changes: 13 additions & 0 deletions test/system-libraries/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export default defineWorld({
},
key: [],
},
AddressValue: {
schema: {
value: "address",
},
key: [],
},
},
},

Expand All @@ -18,4 +24,11 @@ export default defineWorld({
codegen: {
generateSystemLibraries: true,
},
modules: [
{
artifactPath: "@latticexyz/world-modules/out/StandardDelegationsModule.sol/StandardDelegationsModule.json",
root: true,
args: [],
},
],
});
2 changes: 2 additions & 0 deletions test/system-libraries/src/codegen/world/IASystem.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/system-libraries/src/namespaces/a/ASystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity >=0.8.28;

import { System } from "@latticexyz/world/src/System.sol";
import { Value } from "./codegen/tables/Value.sol";
import { AddressValue } from "./codegen/tables/AddressValue.sol";

contract ASystem is System {
function setValue(uint256 value) external {
Expand All @@ -16,4 +17,10 @@ contract ASystem is System {
function getTwoValues() external view returns (uint256, uint256) {
return (Value.get(), Value.get());
}

function setAddress() external returns (address) {
address addr = _msgSender();
AddressValue.set(addr);
return addr;
}
}
1 change: 1 addition & 0 deletions test/system-libraries/src/namespaces/a/codegen/index.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

187 changes: 187 additions & 0 deletions test/system-libraries/src/namespaces/a/codegen/tables/AddressValue.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions test/system-libraries/test/Libraries.t.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;

import { console } from "forge-std/console.sol";
import { ResourceIds } from "@latticexyz/store/src/codegen/index.sol";

import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";
import { ResourceId, WorldResourceIdLib } from "@latticexyz/world/src/WorldResourceId.sol";
import { ResourceIds } from "@latticexyz/store/src/codegen/tables/ResourceIds.sol";
import { MudTest } from "@latticexyz/world/test/MudTest.t.sol";

import { SystemboundDelegationControl } from "@latticexyz/world-modules/src/modules/std-delegations/SystemboundDelegationControl.sol";
import { SYSTEMBOUND_DELEGATION } from "@latticexyz/world-modules/src/modules/std-delegations/StandardDelegationsModule.sol";

import { Value } from "../src/namespaces/a/codegen/tables/Value.sol";
import { AddressValue } from "../src/namespaces/a/codegen/tables/AddressValue.sol";
import { aSystem } from "../src/namespaces/a/codegen/libraries/ASystemLib.sol";
import { bSystem } from "../src/namespaces/b/codegen/libraries/BSystemLib.sol";

Expand All @@ -30,4 +36,21 @@ contract LibrariesTest is MudTest {
assertEq(Value.get(), value, "Value.get");
assertEq(bSystem.getValueFromA(), value, "getValueFromA");
}

function testCallFrom() public {
address alice = address(0xDEADBEEF);

console.log(address(this));
// Alice delegates control to the test contract to call the aSystem on her behalf
vm.prank(alice);
IBaseWorld(worldAddress).registerDelegation(
address(this),
SYSTEMBOUND_DELEGATION,
abi.encodeCall(SystemboundDelegationControl.initDelegation, (address(this), aSystem.toResourceId(), 2))
);

address sender = aSystem.callFrom(alice).setAddress();
assertEq(sender, alice);
assertEq(AddressValue.get(), alice);
}
}

0 comments on commit 05ea70f

Please sign in to comment.