Skip to content

Commit 22d9aea

Browse files
committed
feat: Implement EnvUtils library for updating environment variables in .env file
1 parent 3b71cff commit 22d9aea

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed

foundry.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
src = "src"
33
out = "out"
44
libs = ["lib"]
5+
fs_permissions = [{ access = "read-write", path = "./" }]
56

67
remappings = [
78
'@layerzerolabs/oft-evm/=lib/devtools/packages/oft-evm/',

script/RLCAdapter.s.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pragma solidity ^0.8.22;
33

44
import {Script, console} from "forge-std/Script.sol";
55
import {RLCAdapter} from "../src/RLCAdapter.sol";
6+
import {EnvUtils} from "./UpdateEnvUtils.sol";
67

78
contract DeployRLCAdapter is Script {
89
RLCAdapter public rlcAdapter;
@@ -20,7 +21,7 @@ contract DeployRLCAdapter is Script {
2021
console.log("RLCAdapter deployed at:", address(rlcAdapter));
2122

2223
vm.stopBroadcast();
24+
25+
EnvUtils.updateEnvVariable("SEPOLIA_ADAPTER_ADDRESS", address(rlcAdapter));
2326
}
2427
}
25-
26-
// 0x607F4C5BB672230e8672085532f7e901544a7375 => mainnet

script/RLCOFT.s.sol

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pragma solidity ^0.8.22;
33

44
import {Script, console} from "forge-std/Script.sol";
55
import {RLCOFT} from "../src/RLCOFT.sol";
6+
import {EnvUtils} from "./UpdateEnvUtils.sol";
67

78
contract DeployRLCOFT is Script {
89
RLCOFT public rlcOFT;
@@ -18,8 +19,10 @@ contract DeployRLCOFT is Script {
1819
address delegate = vm.envAddress("DELEGATE_ADDRESS");
1920

2021
rlcOFT = new RLCOFT(name, symbol, lzEndpoint, delegate);
21-
console.log("rlcAOFT deployed at:", address(rlcOFT));
22+
console.log("rlcOFT deployed at:", address(rlcOFT));
2223

2324
vm.stopBroadcast();
25+
26+
EnvUtils.updateEnvVariable("ARBITRUM_SEPOLIA_OFT_ADDRESS", address(rlcOFT));
2427
}
2528
}

script/UpdateEnvUtils.sol

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.22;
3+
4+
import {console} from "forge-std/console.sol";
5+
import {Vm} from "forge-std/Vm.sol";
6+
7+
library EnvUtils {
8+
Vm constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));
9+
10+
function updateEnvVariable(string memory variableName, address value) internal {
11+
string memory envPath = ".env";
12+
string memory addressString = vm.toString(value);
13+
string memory targetVar = string.concat(variableName, "=");
14+
15+
// Check if file exists
16+
if (!vm.exists(envPath)) {
17+
console.log(".env file not found");
18+
return;
19+
}
20+
21+
// Read the entire file
22+
string memory content = vm.readFile(envPath);
23+
string[] memory lines = splitLines(content);
24+
25+
// Process lines and make updates
26+
string memory newContent = "";
27+
bool found = false;
28+
29+
for (uint i = 0; i < lines.length; i++) {
30+
string memory currentLine = lines[i];
31+
32+
// Check if this is the line we want to replace
33+
if (startsWith(currentLine, targetVar)) {
34+
currentLine = string.concat(targetVar, addressString);
35+
found = true;
36+
}
37+
38+
// Add line to new content
39+
if (i > 0) {
40+
newContent = string.concat(newContent, "\n");
41+
}
42+
newContent = string.concat(newContent, currentLine);
43+
}
44+
45+
// If not found, append it
46+
if (!found) {
47+
newContent = string.concat(newContent, "\n", targetVar, addressString);
48+
}
49+
50+
// Write back to file
51+
vm.writeFile(envPath, newContent);
52+
console.log("Updated .env file with new", variableName, ":", addressString);
53+
}
54+
55+
// Helper function to split a string by newlines
56+
function splitLines(string memory _content) internal pure returns (string[] memory) {
57+
bytes memory content = bytes(_content);
58+
59+
// Count the number of lines
60+
uint lineCount = 1;
61+
for (uint i = 0; i < content.length; i++) {
62+
if (content[i] == bytes1("\n")) {
63+
lineCount++;
64+
}
65+
}
66+
67+
// Split the content
68+
string[] memory lines = new string[](lineCount);
69+
uint lineStart = 0;
70+
uint currentLine = 0;
71+
72+
for (uint i = 0; i < content.length; i++) {
73+
if (content[i] == bytes1("\n") || i == content.length - 1) {
74+
uint lineEnd = i;
75+
if (i == content.length - 1 && content[i] != bytes1("\n")) {
76+
lineEnd = i + 1;
77+
}
78+
79+
// Extract line
80+
bytes memory line = new bytes(lineEnd - lineStart);
81+
for (uint j = lineStart; j < lineEnd; j++) {
82+
line[j - lineStart] = content[j];
83+
}
84+
85+
lines[currentLine] = string(line);
86+
currentLine++;
87+
lineStart = i + 1;
88+
}
89+
}
90+
91+
return lines;
92+
}
93+
94+
// Helper function to check if a string starts with a prefix
95+
function startsWith(string memory _str, string memory _prefix) internal pure returns (bool) {
96+
bytes memory str = bytes(_str);
97+
bytes memory prefix = bytes(_prefix);
98+
99+
if (str.length < prefix.length) {
100+
return false;
101+
}
102+
103+
for (uint i = 0; i < prefix.length; i++) {
104+
if (str[i] != prefix[i]) {
105+
return false;
106+
}
107+
}
108+
109+
return true;
110+
}
111+
}

0 commit comments

Comments
 (0)