-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): link and deploy public libraries (#1910)
Co-authored-by: Fraser Scott <[email protected]> Co-authored-by: Kevin Ingersoll <[email protected]>
- Loading branch information
1 parent
3c0f11e
commit 5554b19
Showing
25 changed files
with
386 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@latticexyz/cli": minor | ||
--- | ||
|
||
`mud deploy` now supports public/linked libraries. | ||
|
||
This helps with cases where system contracts would exceed the EVM bytecode size limit and logic would need to be split into many smaller systems. | ||
|
||
Instead of the overhead and complexity of system-to-system calls, this logic can now be moved into public libraries that will be deployed alongside your systems and automatically `delegatecall`ed. |
15 changes: 15 additions & 0 deletions
15
e2e/packages/contracts/src/codegen/world/ILibWrapperSystem.sol
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.24; | ||
|
||
import { Lib3 } from "../systems/LibWrapperSystem.sol"; | ||
|
||
/** | ||
* @title Library 2 | ||
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz) | ||
* @dev Testing that the deployer can handle nesting of 2 libraries | ||
* Included in a separate file to test handling libraries in different files | ||
*/ | ||
library Lib2 { | ||
function call() public pure returns (string memory) { | ||
return Lib3.call(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.24; | ||
|
||
/** | ||
* @title Library 4 | ||
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz) | ||
* @dev Testing that the deployer can handle nesting of 4 libraries | ||
* Included in a separate file to test handling libraries in different files | ||
*/ | ||
library Lib4 { | ||
function call() public pure returns (string memory) { | ||
return Lib5.call(); | ||
} | ||
} | ||
|
||
/** | ||
* @title Library 5 | ||
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz) | ||
* @dev Testing that the deployer can handle nesting of 4 libraries | ||
* Included in a separate file to test handling libraries in different files | ||
*/ | ||
library Lib5 { | ||
function call() public pure returns (string memory) { | ||
return "success"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.24; | ||
|
||
import { System } from "@latticexyz/world/src/System.sol"; | ||
import { Lib2 } from "../libraries/Lib2.sol"; | ||
import { Lib4 } from "../libraries/Lib4and5.sol"; | ||
|
||
/** | ||
* @dev For calling a library using a free function. | ||
*/ | ||
function freeLibWrapper() pure returns (string memory) { | ||
return Lib1.call(); | ||
} | ||
|
||
/** | ||
* @title Library 1 | ||
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz) | ||
* @dev Used for testing that the deployer can handle a single library call | ||
*/ | ||
library Lib1 { | ||
function call() public pure returns (string memory) { | ||
return Lib2.call(); | ||
} | ||
} | ||
|
||
/** | ||
* @title Library 3 | ||
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz) | ||
* @dev Testing that the deployer can handle nesting of 3 libraries | ||
*/ | ||
library Lib3 { | ||
function call() public pure returns (string memory) { | ||
return Lib4.call(); | ||
} | ||
} | ||
|
||
/** | ||
* @title Library Wrapper System | ||
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz) | ||
* @dev This contract is used for testing that the deployer can handle deeply nested public libraries | ||
*/ | ||
contract LibWrapperSystem is System { | ||
function callLib() public pure returns (string memory) { | ||
return Lib1.call(); | ||
} | ||
|
||
function callFreeFunc() public pure returns (string memory) { | ||
return freeLibWrapper(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.24; | ||
|
||
import { MudTest } from "@latticexyz/world/test/MudTest.t.sol"; | ||
|
||
import { IWorld } from "../src/codegen/world/IWorld.sol"; | ||
|
||
contract PublicLibraryTest is MudTest { | ||
/** | ||
* @dev Test that the deployer can handle deeply nested public libraries. | ||
*/ | ||
function testNesting() public { | ||
assertEq(IWorld(worldAddress).callLib(), "success"); | ||
assertEq(IWorld(worldAddress).callFreeFunc(), "success"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { DeterministicContract, Library, LibraryPlaceholder, salt } from "./common"; | ||
import { spliceHex } from "@latticexyz/common"; | ||
import { Hex, getCreate2Address, Address } from "viem"; | ||
|
||
export function createPrepareDeploy( | ||
bytecodeWithPlaceholders: Hex, | ||
placeholders: readonly LibraryPlaceholder[], | ||
): DeterministicContract["prepareDeploy"] { | ||
return function prepareDeploy(deployer: Address, libraries: readonly Library[]) { | ||
let bytecode = bytecodeWithPlaceholders; | ||
for (const placeholder of placeholders) { | ||
const library = libraries.find((lib) => lib.path === placeholder.path && lib.name === placeholder.name); | ||
if (!library) { | ||
throw new Error(`Could not find library for bytecode placeholder ${placeholder.path}:${placeholder.name}`); | ||
} | ||
bytecode = spliceHex( | ||
bytecode, | ||
placeholder.start, | ||
placeholder.length, | ||
library.prepareDeploy(deployer, libraries).address, | ||
); | ||
} | ||
return { | ||
bytecode, | ||
address: getCreate2Address({ from: deployer, bytecode, salt }), | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.