-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Factory Deployment Script (#318)
- Loading branch information
Showing
5 changed files
with
304 additions
and
18 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,134 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.26; | ||
|
||
import {console} from "forge-std/console.sol"; | ||
|
||
import {ModularAccount} from "../src/account/ModularAccount.sol"; | ||
import {SemiModularAccountBytecode} from "../src/account/SemiModularAccountBytecode.sol"; | ||
import {Artifacts} from "./Artifacts.sol"; | ||
import {ScriptBase} from "./ScriptBase.sol"; | ||
import {IEntryPoint} from "@eth-infinitism/account-abstraction/interfaces/IEntryPoint.sol"; | ||
|
||
// Deploys the Account Factory. This requires the following env vars to be set: | ||
// - ENTRY_POINT | ||
// - MODULAR_ACCOUNT_IMPL | ||
// - SEMI_MODULAR_ACCOUNT_BYTECODE_IMPL | ||
// - SINGLE_SIGNER_VALIDATION_MODULE | ||
// - WEBAUTHN_VALIDATION_MODULE | ||
// - FACTORY_OWNER | ||
contract DeployFactoryScript is ScriptBase, Artifacts { | ||
// State vars for expected addresses and salts. | ||
|
||
address public expectedFactoryAddr; | ||
uint256 public factorySalt; | ||
|
||
// State vars for factory dependencies | ||
|
||
IEntryPoint public entryPoint; | ||
ModularAccount public modularAccountImpl; | ||
SemiModularAccountBytecode public semiModularAccountBytecodeImpl; | ||
address public singleSignerValidationModule; | ||
address public webAuthnValidationModule; | ||
address public factoryOwner; | ||
|
||
function setUp() public { | ||
// Load the required addresses for the factory deployment from env vars. | ||
entryPoint = _getEntryPoint(); | ||
modularAccountImpl = _getModularAccountImpl(); | ||
semiModularAccountBytecodeImpl = _getSemiModularAccountBytecodeImpl(); | ||
singleSignerValidationModule = _getSingleSignerValidationModule(); | ||
webAuthnValidationModule = _getWebAuthnValidationModule(); | ||
factoryOwner = _getFactoryOwner(); | ||
|
||
// Load the expected address and salt from env vars. | ||
expectedFactoryAddr = vm.envOr("ACCOUNT_FACTORY", address(0)); | ||
factorySalt = vm.envOr("ACCOUNT_FACTORY_SALT", uint256(0)); | ||
} | ||
|
||
function run() public onlyProfile("optimized-build") { | ||
console.log("******** Deploying Factory *********"); | ||
|
||
vm.startBroadcast(); | ||
|
||
_safeDeploy( | ||
"Account Factory", | ||
expectedFactoryAddr, | ||
factorySalt, | ||
_getAccountFactoryInitcode( | ||
entryPoint, | ||
modularAccountImpl, | ||
semiModularAccountBytecodeImpl, | ||
singleSignerValidationModule, | ||
webAuthnValidationModule, | ||
factoryOwner | ||
), | ||
_deployFactory | ||
); | ||
|
||
vm.stopBroadcast(); | ||
|
||
console.log("******** Factory Deployed *********"); | ||
} | ||
|
||
// Wrapper function to be called within _safeDeploy using the context in this contract. | ||
function _deployFactory(bytes32 salt) internal returns (address) { | ||
_ensureNonzeroFactoryArgs(); | ||
return _deployAccountFactory( | ||
salt, | ||
entryPoint, | ||
modularAccountImpl, | ||
semiModularAccountBytecodeImpl, | ||
singleSignerValidationModule, | ||
webAuthnValidationModule, | ||
factoryOwner | ||
); | ||
} | ||
|
||
function _ensureNonzeroFactoryArgs() internal view { | ||
bool shouldRevert; | ||
if (address(modularAccountImpl) == address(0)) { | ||
console.log("Env Variable 'MODULAR_ACCOUNT_IMPL' not found or invalid during factory deployment"); | ||
shouldRevert = true; | ||
} else { | ||
console.log("Using user-defined ModularAccount at: %x", address(modularAccountImpl)); | ||
} | ||
|
||
if (address(semiModularAccountBytecodeImpl) == address(0)) { | ||
console.log( | ||
"Env Variable 'SEMI_MODULAR_ACCOUNT_BYTECODE_IMPL' not found or invalid during factory deployment" | ||
); | ||
shouldRevert = true; | ||
} else { | ||
console.log( | ||
"Using user-defined SemiModularAccountBytecode at: %x", address(semiModularAccountBytecodeImpl) | ||
); | ||
} | ||
|
||
if (singleSignerValidationModule == address(0)) { | ||
console.log( | ||
"Env Variable 'SINGLE_SIGNER_VALIDATION_MODULE' not found or invalid during factory deployment" | ||
); | ||
shouldRevert = true; | ||
} else { | ||
console.log("Using user-defined SingleSignerValidationModule at: %x", singleSignerValidationModule); | ||
} | ||
|
||
if (webAuthnValidationModule == address(0)) { | ||
console.log("Env Variable 'WEBAUTHN_VALIDATION_MODULE' not found or invalid during factory deployment"); | ||
shouldRevert = true; | ||
} else { | ||
console.log("Using user-defined WebAuthnValidationModule at: %x", webAuthnValidationModule); | ||
} | ||
|
||
if (factoryOwner == address(0)) { | ||
console.log("Env Variable 'FACTORY_OWNER' not found or invalid during factory deployment"); | ||
shouldRevert = true; | ||
} else { | ||
console.log("Using user-defined factory owner at: %x", factoryOwner); | ||
} | ||
|
||
if (shouldRevert) { | ||
revert("Missing or invalid env variables during factory deployment"); | ||
} | ||
} | ||
} |
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
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,83 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.26; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
|
||
import {DeployFactoryScript} from "../../script/DeployFactory.s.sol"; | ||
import {AccountFactory} from "../../src/factory/AccountFactory.sol"; | ||
import {Create2} from "@openzeppelin/contracts/utils/Create2.sol"; | ||
|
||
contract DeployFactoryTest is Test { | ||
DeployFactoryScript internal _deployFactoryScript; | ||
|
||
address public entryPoint; | ||
address public modularAccountImpl; | ||
address public semiModularAccountBytecodeImpl; | ||
address public singleSignerValidationModule; | ||
address public webAuthnValidationModule; | ||
address public factoryOwner; | ||
|
||
AccountFactory public factory; | ||
|
||
function setUp() public { | ||
_deployFactoryScript = new DeployFactoryScript(); | ||
|
||
bytes32 zeroSalt = bytes32(0); | ||
|
||
entryPoint = makeAddr("Entrypoint"); | ||
modularAccountImpl = makeAddr("Modular Account Impl"); | ||
semiModularAccountBytecodeImpl = makeAddr("Semi Modular Account Bytecode Impl"); | ||
singleSignerValidationModule = makeAddr("Single Signer Validation Module"); | ||
webAuthnValidationModule = makeAddr("Webauthn Validation Module"); | ||
factoryOwner = makeAddr("Factory Owner"); | ||
|
||
vm.setEnv("ENTRYPOINT", vm.toString(entryPoint)); | ||
vm.setEnv("MODULAR_ACCOUNT_IMPL", vm.toString(modularAccountImpl)); | ||
vm.setEnv("SEMI_MODULAR_ACCOUNT_BYTECODE_IMPL", vm.toString(semiModularAccountBytecodeImpl)); | ||
vm.setEnv("SINGLE_SIGNER_VALIDATION_MODULE", vm.toString(singleSignerValidationModule)); | ||
vm.setEnv("WEBAUTHN_VALIDATION_MODULE", vm.toString(webAuthnValidationModule)); | ||
vm.setEnv("ACCOUNT_FACTORY_OWNER", vm.toString(factoryOwner)); | ||
|
||
factory = AccountFactory( | ||
Create2.computeAddress( | ||
zeroSalt, | ||
keccak256( | ||
bytes.concat( | ||
type(AccountFactory).creationCode, | ||
abi.encode( | ||
entryPoint, | ||
modularAccountImpl, | ||
semiModularAccountBytecodeImpl, | ||
singleSignerValidationModule, | ||
webAuthnValidationModule, | ||
factoryOwner | ||
) | ||
) | ||
), | ||
CREATE2_FACTORY | ||
) | ||
); | ||
|
||
vm.setEnv("ACCOUNT_FACTORY", vm.toString(address(factory))); | ||
|
||
string memory zeroSaltString = vm.toString(zeroSalt); | ||
|
||
vm.setEnv("ACCOUNT_FACTORY_SALT", zeroSaltString); | ||
|
||
// Spoof as though the profile is set to "optimized-build". | ||
vm.setEnv("FOUNDRY_PROFILE", "optimized-build"); | ||
} | ||
|
||
function test_deployFactoryScript() public { | ||
assertEq(address(factory).code.length, 0); | ||
|
||
_deployFactoryScript.setUp(); | ||
|
||
_deployFactoryScript.run(); | ||
|
||
assertGt(address(factory).code.length, 0); | ||
|
||
// Test an arbitrary function, ensuring the factory doesn't revert. | ||
factory.createSemiModularAccount(address(this), 1); | ||
} | ||
} |