Skip to content

Commit

Permalink
fix: the file creation if it already exists
Browse files Browse the repository at this point in the history
fix: handle non-set nameMap for unknown chain id
  • Loading branch information
andreivladbrg committed Aug 22, 2024
1 parent d011e43 commit 6697736
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 69 deletions.
58 changes: 29 additions & 29 deletions deploy-multi-chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ fn main() {

let deployment_path = get_deployment_path(is_deterministic, false);

// Create the parent directory if it doesn't exist
if let Some(parent) = Path::new(&deployment_path).parent() {
if parent.exists() {
_ = fs::copy(
&deployment_path,
get_deployment_path(is_deterministic, true),
);
}

fs::create_dir_all(parent).expect("Failed to create directories");
// Check if the deployment file exists
if Path::new(&deployment_path).exists() {
// Move the existing file to a new path with timestamp
_ = fs::rename(
&deployment_path,
get_deployment_path(is_deterministic, true),
);
} else {
// Create the parent directory
_ = fs::create_dir_all(Path::new(&deployment_path).parent().unwrap());
}

// Append the type of deployment at the start of the deployment file
Expand Down Expand Up @@ -113,53 +113,53 @@ fn main() {
let env_var_parts: Vec<&str> = env_var.split('=').collect();
env::set_var(env_var_parts[0], env_var_parts[1]);

// Create the CLI
let mut cmd = Command::new(command);
cmd.args(&command_args);

// Capture the command output
let output = cmd.output().expect("Failed to run command");
// Create the CLI and capture the command output
let output = Command::new(command)
.args(command_args)
.output()
.expect("Failed to run command");

// Check if the command executed successfully
// Process command output
let output_str = String::from_utf8_lossy(&output.stdout);
if output.status.success() {
let output_str = String::from_utf8_lossy(&output.stdout);
println!("Command output: {}", output_str);
} else {
let error_str = String::from_utf8_lossy(&output.stderr);
eprintln!("Command failed with error: {}", error_str);
eprintln!(
"Command failed with error: {}",
String::from_utf8_lossy(&output.stderr)
);
}

// Move broadcast file if needed
if cp_broadcasted_file {
move_broadcast_file(
&script_name,
&chain,
&String::from_utf8_lossy(&output.stdout),
&output_str,
broadcast_deployment.is_empty(),
);
}
}

// Run Prettier to format the deployment files
let _ = Command::new("bun")
_ = Command::new("bun")
.args(["prettier", "--write", "../deployments/**/*.md"])
.status()
.expect("Failed to run Prettier");
}

fn append_type_of_deployment(deployment_path: &str, is_broadcast_deployment: bool) {
let message = if is_broadcast_deployment {
" # This is a deployment simulation\n\n\n"
} else {
" # This deployment is broadcasted\n"
let message = match is_broadcast_deployment {
true => " # This deployment is a simulation\n\n",
false => " # This deployment is broadcasted\n\n",
};

OpenOptions::new()
.append(true)
.create(true)
.open(deployment_path)
.expect("Failed to open the file")
.write_all(message.as_bytes())
.expect("Failed to write to the file");
.and_then(|mut file| file.write_all(message.as_bytes()))
.expect("Failed to open or write to the file");
}

// Function that reads the TOML chain configurations and extracts them
Expand Down
85 changes: 45 additions & 40 deletions script/protocol/Protocol.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ abstract contract ProtocolScript is BaseScript {
/// @dev Admin address mapped by the chain Id.
mapping(uint256 chainId => address admin) internal adminMap;

/// @dev Chain names mapped by the chain Id.
mapping(uint256 chainId => string name) internal nameMap;

/// @dev Explorer URL mapped by the chain Id.
mapping(uint256 chainId => string explorerUrl) internal explorerMap;

/// @dev Chain names mapped by the chain Id.
mapping(uint256 chainId => string name) internal nameMap;

constructor(string memory deterministicOrNot) {
// Populate the admin map.
populateAdminMap();
Expand All @@ -46,6 +46,11 @@ abstract contract ProtocolScript is BaseScript {
explorerMap[block.chainid] = "<explorer_url_missing>";
}

// If there is no chain name set for a specific chain, use the chain ID.
if (nameMap[block.chainid].equal("")) {
nameMap[block.chainid] = block.chainid.toString();
}

// Set the deployment file path.
deploymentFile = string.concat("deployments/", deterministicOrNot, ".md");

Expand Down Expand Up @@ -117,43 +122,6 @@ abstract contract ProtocolScript is BaseScript {
_appendToFile(merkleFactoryLine);
}

/// @dev Append a line to the deployment file path.
function _appendToFile(string memory line) private {
vm.writeLine({ path: deploymentFile, data: line });
}

function _getContractLine(
string memory contractName,
string memory contractAddress,
string memory coreOrPeriphery
)
private
view
returns (string memory)
{
string memory version = getVersion();
version = string.concat("v", version);

return string.concat(
"| ",
contractName,
" | [",
contractAddress,
"](",
explorerMap[block.chainid],
contractAddress,
") | [",
coreOrPeriphery,
"-",
version,
"](https://github.com/sablier-labs/v2-deployments/tree/main/",
coreOrPeriphery,
"/",
version,
") |"
);
}

/// @dev Populates the admin map.
function populateAdminMap() internal {
adminMap[42_161] = 0xF34E41a6f6Ce5A45559B1D3Ee92E141a3De96376; // Arbitrum
Expand Down Expand Up @@ -225,4 +193,41 @@ abstract contract ProtocolScript is BaseScript {
explorerMap[167_009] = "https://explorer.hekla.taiko.xyz/address/";
explorerMap[167_000] = "https://taikoscan.io/address/";
}

/// @dev Append a line to the deployment file path.
function _appendToFile(string memory line) private {
vm.writeLine({ path: deploymentFile, data: line });
}

function _getContractLine(
string memory contractName,
string memory contractAddress,
string memory coreOrPeriphery
)
private
view
returns (string memory)
{
string memory version = getVersion();
version = string.concat("v", version);

return string.concat(
"| ",
contractName,
" | [",
contractAddress,
"](",
explorerMap[block.chainid],
contractAddress,
") | [",
coreOrPeriphery,
"-",
version,
"](https://github.com/sablier-labs/v2-deployments/tree/main/",
coreOrPeriphery,
"/",
version,
") |"
);
}
}

0 comments on commit 6697736

Please sign in to comment.