Skip to content

Commit

Permalink
Save addresses of contracts deployed with Forge #39 (#55)
Browse files Browse the repository at this point in the history
* Save addresses of contracts deployed with Forge #39

* updated PR

* updated PR

* updated PR

* updated PR

* updated PR
  • Loading branch information
Ugo-X authored Sep 3, 2024
1 parent bc47d8c commit 2aac42d
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export MSG_CLAIM_VALUE=0
export MSG_CLAIM_NONCE=0
export MSG_CLAIM_CALLDATA="YOUR_CALL_DATA"
export MSG_CLAIM_FEE_RECIPIENT=0x0

export PRIVATE_KEY="YOUR_PRIVATE_KEY"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ yarn.lock
broadcast/*
broadcast/*/31337/

src/script/.deploy-config.json
.deploy-config.json
Binary file modified bun.lockb
Binary file not shown.
5 changes: 2 additions & 3 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ script = "script"
solc = "0.8.19"
src = "src"
test = "test"
fs_permissions = [{ access = "read-write", path = "./src/script/.deploy-config.json" }]

fs_permissions = [{ access = "read-write", path = "src/script/.deploy-config.json" }]

[profile.ci]
fuzz = { runs = 10_000 }
Expand All @@ -40,4 +39,4 @@ sepolia = "https://sepolia.infura.io/v3/${API_KEY_INFURA}"
mainnet = "https://mainnet.infura.io/v3/${API_KEY_INFURA}"
linea_sepolia = "https://linea-sepolia.infura.io/v3/${API_KEY_INFURA}"
linea_mainnet = "https://linea-mainnet.infura.io/v3/${API_KEY_INFURA}"
localhost = "http://localhost:8545"
localhost = "http://localhost:8545"
19 changes: 15 additions & 4 deletions src/script/DeployLineaStateBridge.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@ contract DeployLineaStateBridge is Script {
string public path = "src/script/.deploy-config.json";
string public json = vm.readFile(path);

error ConfigError();

function setUp() public {
// https://docs.worldcoin.org/reference/address-book

messageServiceAddress = abi.decode(vm.parseJson(json, ".messageServiceAddressL1"), (address));
lineaWorldIDAddress = abi.decode(vm.parseJson(json, ".lineaWorldIDAddress"), (address));
worldIDIdentityManagerAddress = abi.decode(vm.parseJson(json, ".worldIDIdentityManagerAddress"), (address));
bytes memory messageServiceJson = vm.parseJson(json, ".messageServiceAddressL1");
bytes memory lineaWorldIDJson = vm.parseJson(json, ".lineaWorldIDAddress");
bytes memory worldIDIdentityManagerJson = vm.parseJson(json, ".worldIDIdentityManagerAddress");

if (messageServiceJson.length != 32 || lineaWorldIDJson.length != 32 || worldIDIdentityManagerJson.length != 32)
{
console.log("Config Error: required addresses not set");
revert ConfigError();
}
messageServiceAddress = abi.decode(messageServiceJson, (address));
lineaWorldIDAddress = abi.decode(lineaWorldIDJson, (address));
worldIDIdentityManagerAddress = abi.decode(worldIDIdentityManagerJson, (address));
}

function run() external {
Expand All @@ -34,7 +45,7 @@ contract DeployLineaStateBridge is Script {
// Check if lineaStateBridgeAddress is already in the JSON config
bytes memory encodedAddress = vm.parseJson(json, ".lineaStateBridgeAddress");

if (encodedAddress.length > 0) {
if (encodedAddress.length == 32) {
// If the address exists, load it
address existingBridgeAddress = abi.decode(encodedAddress, (address));
bridge = LineaStateBridge(existingBridgeAddress);
Expand Down
8 changes: 4 additions & 4 deletions src/script/DeployLineaWorldID.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ contract DeployLineaWorldID is Script {
vm.startBroadcast(privateKey);

// Check if lineaWorldIdAddress is already in the JSON config
bytes memory encodedAddress = vm.parseJson(json, ".lineaWorldIDAddress");
bytes memory lineaWorldIDAddressJson = vm.parseJson(json, ".lineaWorldIDAddress");

if (encodedAddress.length > 0) {
if (lineaWorldIDAddressJson.length == 32) {
address lineaWorldIDAddress = abi.decode(bytes(lineaWorldIDAddressJson), (address));
// If the address exists, load it
address existingWorldIdAddress = abi.decode(encodedAddress, (address));
lineaWorldId = LineaWorldID(existingWorldIdAddress);
lineaWorldId = LineaWorldID(lineaWorldIDAddress);
console.log("Loaded existing LineaWorldID at:", address(lineaWorldId));
} else {
// If the address doesn't exist, deploy a new contract
Expand Down
175 changes: 136 additions & 39 deletions src/script/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { execSync } from "child_process";
// === Constants ==================================================================================
const CONFIG_FILENAME = "src/script/.deploy-config.json";
const DEFAULT_RPC_URL = "http://localhost:8545";
const DEFAULT_TREE_DEPTH = 30;
const DEFAULT_MESSENGER_L1 = "0xB218f8A4Bc926cF1cA7b3423c154a0D627Bdb7E5";
const DEFAULT_MESSENGER_L2 = "0x971e727e956690b9957be6d51Ec16E73AcAC83A7";
const DEFAULT_WORLD_ID_MANAGER = "0x928a514350A403e2f5e3288C102f6B1CCABeb37C";
const addressRegex = /0x[a-fA-F0-9]{40}/;

// === Implementation =============================================================================

Expand Down Expand Up @@ -75,6 +80,30 @@ async function getPrivateKey(config) {
}
}

async function getMessageServiceAddressL1(config) {
if (!config.messageServiceAddressL1) {
config.messageServiceAddressL1 = process.env.MESSENGER_SERVICE_ADDRESS_L1;
}
if (!config.messageServiceAddressL1) {
config.messageServiceAddressL1 = await ask(`Enter L1 message service address: (${DEFAULT_MESSENGER_L1}) `);
}
if (!config.messageServiceAddressL1) {
config.messageServiceAddressL1 = DEFAULT_MESSENGER_L1;
}
}

async function getMessageServiceAddressL2(config) {
if (!config.messageServiceAddressL2) {
config.messageServiceAddressL2 = process.env.MESSENGER_SERVICE_ADDRESS_L2;
}
if (!config.messageServiceAddressL2) {
config.messageServiceAddressL2 = await ask(`Enter L2 message service address: (${DEFAULT_MESSENGER_L2}) `);
}
if (!config.messageServiceAddressL2) {
config.messageServiceAddressL2 = DEFAULT_MESSENGER_L2;
}
}

async function getEthereumRpcUrl(config) {
if (!config.ethereumRpcUrl) {
config.ethereumRpcUrl = process.env.ETH_RPC_URL;
Expand Down Expand Up @@ -123,7 +152,10 @@ async function getLineaEtherscanApiKey(config) {

async function getTreeDepth(config) {
if (!config.treeDepth) {
config.treeDepth = await ask("Enter WorldID tree depth: ");
config.treeDepth = await ask(`Enter WorldID tree depth: (${DEFAULT_TREE_DEPTH}) `);
}
if (!config.treeDepth) {
config.treeDepth = DEFAULT_TREE_DEPTH;
}
}

Expand All @@ -145,6 +177,20 @@ async function getLineaStateBridgeAddress(config) {
}
}

async function getWorldIDIdentityManagerAddress(config) {
if (!config.worldIDIdentityManagerAddress) {
config.worldIDIdentityManagerAddress = process.env.WORLD_ID_IDENTITY_MANAGER_ADDRESS;
}
if (!config.worldIDIdentityManagerAddress) {
config.worldIDIdentityManagerAddress = await ask(
`Enter WorldID Identity Manager Address: (${DEFAULT_WORLD_ID_MANAGER}) `,
);
}
if (!config.worldIDIdentityManagerAddress) {
config.worldIDIdentityManagerAddress = DEFAULT_WORLD_ID_MANAGER;
}
}

///////////////////////////////////////////////////////////////////
/// UTILS ///
///////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -188,6 +234,8 @@ async function loadConfiguration(useConfig, environment) {
}
} else {
spinner.succeed("Configuration not loaded");
const data = JSON.stringify({});
fs.writeFileSync(CONFIG_FILENAME, data);
return {};
}
}
Expand All @@ -205,6 +253,22 @@ async function saveConfiguration(config) {
fs.writeFileSync(CONFIG_FILENAME, data);
}

export function parseJson(data) {
let jsonStartIndex = data.indexOf("{");
let jsonEndIndex = data.lastIndexOf("}");
if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
const jsonString = data.substring(jsonStartIndex, jsonEndIndex + 1);
try {
const jsonData = JSON.parse(jsonString);
return jsonData;
} catch (error) {
throw error;
}
} else {
return {};
}
}

///////////////////////////////////////////////////////////////////
/// DEPLOYMENTS ///
///////////////////////////////////////////////////////////////////
Expand All @@ -213,16 +277,27 @@ async function deployLineaWorldID(config) {
const spinner = ora("Deploying LineaID on Linea...").start();

try {
const data = execSync(
`forge script src/script/DeployLineaWorldID.s.sol:DeployOpWorldID --fork-url ${config.lineaRpcUrl} \
--etherscan-api-key ${config.lineaEtherscanApiKey} --broadcast --verify -vvvv`,
);
console.log(data.toString());
let command = `forge script src/script/DeployLineaWorldID.s.sol:DeployLineaWorldID --fork-url ${config.lineaRpcUrl} --broadcast --json`;
if (config.lineaEtherscanApiKey) {
command += ` --etherscan-api-key ${config.lineaEtherscanApiKey} --verify`;
}
const output = execSync(command);
const data = output.toString();
const jsonData = parseJson(data);
if (jsonData.success) {
for (const log of jsonData.logs) {
if (!log.includes("LineaWorldID")) continue;
const match = data.match(addressRegex);
if (!match) continue;
const contractAddress = match[0];
config.lineaWorldIDAddress = contractAddress;
}
}
spinner.succeed("DeployLineaWorldID.s.sol ran successfully!");
} catch (err) {
console.error(err);
spinner.fail("DeployLineaWorldID.s.sol failed!");
throw err;
}

spinner.succeed("DeployLineaWorldID.s.sol ran successfully!");
}

///////////////////////////////////////////////////////////////////
Expand All @@ -233,34 +308,48 @@ async function deployLineaStateBridgeMainnet(config) {
const spinner = ora("Deploying Linea State Bridge...").start();

try {
const data =
execSync(`forge script src/script/DeployLineaStateBridgeMainnet.s.sol:DeployLineaStateBridgeMainnet --fork-url ${config.ethereumRpcUrl} \
--etherscan-api-key ${config.ethereumEtherscanApiKey} --broadcast --verify -vvvv`);
console.log(data.toString());
let command = `forge script src/script/DeployLineaStateBridge.s.sol:DeployLineaStateBridge --fork-url ${config.ethereumRpcUrl} --broadcast -vvvv --json`;
if (config.ethereumEtherscanApiKey) {
command += ` --etherscan-api-key ${config.lineaEtherscanApiKey} --verify`;
}
const output = execSync(command);
const data = output.toString();
const jsonData = parseJson(data);
if (jsonData.success) {
for (const log of jsonData.logs) {
if (!log.includes("LineaStateBridge")) continue;
const match = data.match(addressRegex);
if (!match) continue;
const contractAddress = match[0];
config.lineaStateBridgeAddress = contractAddress;
}
}
spinner.succeed("DeployLineaStateBridge.s.sol ran successfully!");
} catch (err) {
console.error(err);
spinner.fail("DeployLineaStateBridge.s.sol failed!");
throw err;
}

spinner.succeed("DeployLineaStateBridgeMainnet.s.sol ran successfully!");
}

///////////////////////////////////////////////////////////////////
/// INITIALIZE ///
///////////////////////////////////////////////////////////////////

async function InitializeLineaStateBridge(config) {
const spinner = ora("Initializing LineaStateBridge...").start();
async function InitializeLineaWorldID(config) {
const spinner = ora("Initializing LineaWorldId...").start();

try {
const data = execSync(
`forge script src/script/InitializeLineaStateBridge.s.sol:LineaStateBridge --fork-url ${config.lineaRpcUrl} --broadcast -vvvv --legacy`,
`forge script src/script/InitializeLineaWorldID.s.sol:InitializeLineaWorldID --fork-url ${config.lineaRpcUrl} --broadcast -vvvv --legacy --json`,
);
console.log(data.toString());
const jsonData = parseJson(data.toString());
if (jsonData.success) {
spinner.succeed("InitializeLineaStateBridge.s.sol ran successfully!");
}
} catch (err) {
console.error(err);
spinner.fail("InitializeLineaStateBridge.s.sol failed!");
throw err;
}

spinner.succeed("InitializeLineaStateBridge.s.sol ran successfully!");
}

///////////////////////////////////////////////////////////////////
Expand All @@ -269,21 +358,29 @@ async function InitializeLineaStateBridge(config) {

async function deploymentMainnet(config) {
dotenv.config();

await getPrivateKey(config);
await getEthereumRpcUrl(config);
await getLineaRpcUrl(config);
await getEthereumEtherscanApiKey(config);
await getLineaEtherscanApiKey(config);
await getTreeDepth(config);
await saveConfiguration(config);
await deployLineaWorldID(config);
await getLineaWorldIDAddress(config);
await saveConfiguration(config);
await deployLineaStateBridgeMainnet(config);
await getLineaStateBridgeAddress(config);
await saveConfiguration(config);
await InitializeLineaStateBridge(config);
try {
await getPrivateKey(config);
await getEthereumRpcUrl(config);
await getLineaRpcUrl(config);
await getEthereumEtherscanApiKey(config);
await getLineaEtherscanApiKey(config);
await getTreeDepth(config);
await getMessageServiceAddressL1(config);
await getMessageServiceAddressL2(config);
await saveConfiguration(config);
await deployLineaWorldID(config);
await saveConfiguration(config);
await getWorldIDIdentityManagerAddress(config);
await getLineaWorldIDAddress(config);
await saveConfiguration(config);
await deployLineaStateBridgeMainnet(config);
await saveConfiguration(config);
await getLineaStateBridgeAddress(config);
await saveConfiguration(config);
await InitializeLineaWorldID(config);
} catch (err) {
throw err;
}
}

///////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -313,4 +410,4 @@ async function main() {
await program.parseAsync();
}

main().then(() => process.exit(0));
main().then(() => process.exit(0));
2 changes: 1 addition & 1 deletion src/script/js/claimMessageModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ async function claimPendingMessage(sender, destination, fee, value, messageHash,
}

// Export the module functions
export { claimPendingMessage };
export { claimPendingMessage };
2 changes: 1 addition & 1 deletion src/services/lineaStateBridgeEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ export const lineaStateBridgeEventListener = async ({
console.log(`Transaction Hash: ${event.transactionHash}`);
console.log('---------------------------');
});
};
};

0 comments on commit 2aac42d

Please sign in to comment.