Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script Enhancements #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions l1-contracts/script/BaseScript.s.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;

import "forge-std/Script.sol";
import "./utils/ContractAddressManager.s.sol";

/**
* @title Base Script
* @author Puffer Finance
*/
abstract contract BaseScript is Script {
abstract contract BaseScript is ContractAddressManager {
uint256 internal PK = 55358659325830545179143827536745912452716312441367500916455484419538098489698; // makeAddr("pufferDeployer")

// Anvil and `forge test` environment share the same chainId
Expand Down
43 changes: 43 additions & 0 deletions l1-contracts/script/utils/ContractAddressManager.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "forge-std/Script.sol";
import "forge-std/console.sol";

contract ContractAddressManager is Script {
using stdJson for string;

string private constant OUTPUT_DIR = "output/";
string private constant FILE_NAME = "contractDeployments.json";

function writeAddress(string memory contractName, address contractAddress) public {
string memory filePath = getFilePath();
string memory json = vm.readFile(filePath);

if (bytes(json).length == 0) {
json = "{}";
}

json = json.serialize(contractName, contractAddress);
vm.writeFile(filePath, json);

console.log("Wrote address for", contractName, ":", contractAddress);
}

function readAddress(string memory contractName) public view returns (address) {
string memory filePath = getFilePath();
string memory json = vm.readFile(filePath);

if (bytes(json).length == 0) {
revert("Contract address file is empty or doesn't exist");
}

address contractAddress = json.readAddress(string(abi.encodePacked(".", contractName)));
return contractAddress;
}

function getFilePath() private view returns (string memory) {
uint256 chainId = block.chainid;
return string(abi.encodePacked(OUTPUT_DIR, vm.toString(chainId), "/", FILE_NAME));
}
}
48 changes: 48 additions & 0 deletions l1-contracts/script/utils/DeploymentScript.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "forge-std/Script.sol";
import "./ContractAddressManager.s.sol";

abstract contract DeploymentScript is Script, ContractAddressManager {
// Add any common state variables here

function run() public {
vm.startBroadcast();

// Pre-deployment actions
preDeploymentActions();

// Deployment
deploy();

// Post-deployment actions
postDeploymentActions();

vm.stopBroadcast();
}

function preDeploymentActions() internal virtual {
// Read necessary contract addresses
// Example:
// address someContract = readAddress("SomeContract");
}

function deploy() internal virtual {
// Implement the actual deployment logic
// This function should be overridden in the specific deployment scripts
}

function postDeploymentActions() internal virtual {
// Write new contract addresses
// Example:
// writeAddress("NewContract", address(newContract));

// Setup ACL for the contract selectors
setupAccessControl();
}

function setupAccessControl() internal virtual {
// Setup ACL for the deployed contracts
}
}