Skip to content

Commit

Permalink
fix(pg): stop requiring the user to override the run() function in …
Browse files Browse the repository at this point in the history
…their script.
  • Loading branch information
sam-goldman committed Jan 3, 2024
1 parent 05d2c5f commit ecededa
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-seas-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sphinx-labs/plugins': patch
---

Stop requiring the user to override the `run()` function in their script.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ tsconfig.tsbuildinfo

# vim
*.sw*

# env
.env
2 changes: 1 addition & 1 deletion docs/cli-existing-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ The entry point of your deployment script must be a `run()` function; it cannot
Then, add a `sphinx` modifier to your `run` function:

```sol
function run() sphinx public override {
function run() sphinx public {
...
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/writing-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ address safe = sphinxSafe();
The entry point for your deployment must always be a `run()` function that has a `sphinx` modifier:

```sol
function run() public sphinx override {
function run() public sphinx {
...
}
```
Expand Down
17 changes: 11 additions & 6 deletions packages/plugins/contracts/foundry/Sphinx.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,14 @@ abstract contract Sphinx {
deploymentInfo.requireSuccess = true;

isCollecting = true;
vm.startBroadcast(safe);
run();
vm.stopBroadcast();

// Delegatecall the `run()` function on this contract to collect the transactions. This
// pattern gives us flexibility to support function names other than `run()` in the future.
(bool success, ) = address(this).delegatecall(abi.encodeWithSignature("run()"));
// Throw an error if the deployment script fails. The error message in the user's script is
// displayed by Foundry's stack trace, so it'd be redundant to include the data returned by
// the delegatecall in our error message.
require(success, "Sphinx: Deployment script failed.");

// Set the labels. We do this after running the user's script because the user may assign
// labels in their deployment. We use a for-loop instead of directly assigning the labels to
Expand Down Expand Up @@ -243,11 +248,13 @@ abstract contract Sphinx {

if (isCollecting) {
// Execute the user's 'run()' function.
vm.startBroadcast(sphinxSafe());
_;
vm.stopBroadcast();
} else {
// Prank the Gnosis Safe then execute the user's `run()` function. We prank the Gnosis
// Safe to replicate the deployment process on live networks.
vm.startPrank(address(sphinxSafe()));
vm.startPrank(sphinxSafe());
_;
vm.stopPrank();
}
Expand All @@ -257,8 +264,6 @@ abstract contract Sphinx {
sphinxModifierEnabled = false;
}

function run() public virtual;

/**
* @notice Get the address of the SphinxModule. Before calling this function, the
* `sphinxConfig.owners` array and `sphinxConfig.threshold` must be set.
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/contracts/test/script/Cases.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ contract Simple is Script, Sphinx {
sphinxConfig.orgId = "test-org-id";
}

function run() public override sphinx {
function run() public sphinx {
// Deploy with Create2
Fallback fallbackCreate2 = new Fallback{ salt: 0 }(-1);
// Perform low level call to fallback function
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/contracts/test/script/Empty.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ contract Empty is Script, Sphinx {
sphinxConfig.orgId = "test-org-id";
}

function run() public override sphinx {}
function run() public sphinx {}
}
2 changes: 1 addition & 1 deletion packages/plugins/contracts/test/script/Large.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract Simple is Script, Sphinx {
sphinxConfig.orgId = "test-org-id";
}

function run() public override sphinx {
function run() public sphinx {
new MyLargeContract{ salt: bytes32(uint(0)) }();
new MyLargeContract{ salt: bytes32(uint(1)) }();
new MyLargeContract{ salt: bytes32(uint(2)) }();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract PartiallyEmpty is Script, Sphinx {
sphinxConfig.orgId = "test-org-id";
}

function run() public override sphinx {
function run() public sphinx {
// Deploy a contract on Ethereum and don't deploy anything on Optimism.
if (block.chainid == 1) {
new MyContract2{ salt: 0 }();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract RevertDuringSimulation_Script is Script, Sphinx {
sphinxConfig.orgId = "test-org-id";
}

function run() public override sphinx {
function run() public sphinx {
RevertDuringSimulation reverter = new RevertDuringSimulation{ salt: 0 }(sphinxModule());
reverter.revertDuringSimulation();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/contracts/test/script/Simple.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract Simple1 is Script, Sphinx {
sphinxConfig.orgId = "test-org-id";
}

function run() public override sphinx {
function run() public sphinx {
MyContract2 myContract;
Network network = getSphinxNetwork(block.chainid);
if (network == Network.ethereum || network == Network.sepolia) {
Expand All @@ -39,7 +39,7 @@ contract Simple2 is Script, Sphinx {
sphinxConfig.orgId = "test-org-id";
}

function run() public override sphinx {
function run() public sphinx {
new MyContract2{ salt: bytes32(uint(2)) }();
}
}
2 changes: 1 addition & 1 deletion packages/plugins/script/Sample.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract Sample is Sphinx {
sphinxConfig.saltNonce = 0;
}

function run() public override sphinx {
function run() public sphinx {
new MyContract1{ salt: bytes32(uint(1)) }(
-1,
2,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/src/sample-project/sample-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ contract HelloSphinxScript is Sphinx {
];
}
function run() public override sphinx {
function run() public sphinx {
helloSphinx = new HelloSphinx{ salt: bytes32(0) }("Hi", 2);
helloSphinx.add(8);
}
Expand Down

0 comments on commit ecededa

Please sign in to comment.