Skip to content

Commit

Permalink
refactor: create transferInitialFunds function for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
nlecoufl committed Nov 22, 2024
1 parent 0c2b2ec commit b760b03
Showing 1 changed file with 53 additions and 41 deletions.
94 changes: 53 additions & 41 deletions deploy/foundry/merklDeploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ contract MainDeployScript is Script, JsonReader, TokensUtils, CreateXConstants {
address public MERKL_DEPLOYER_ADDRESS;
address public DISPUTE_TOKEN;

JsonReader public reader;

struct DeploymentAddresses {
address proxy;
address implementation;
Expand Down Expand Up @@ -130,33 +128,11 @@ contract MainDeployScript is Script, JsonReader, TokensUtils, CreateXConstants {
// 1. Deploy using DEPLOYER_PRIVATE_KEY
vm.startBroadcast(DEPLOYER_PRIVATE_KEY);

// Check if deployer has enough balance to transfer to KEEPER, DUMPER, DISPUTER_WHITELIST and CreateX deployer
if (
DEPLOYER_ADDRESS.balance <
FUND_AMOUNT * (2 + DISPUTER_WHITELIST.length + (CREATEX.code.length == 0 ? 1 : 0))
) {
revert(
"DEPLOYER_ADDRESS does not have enough balance to transfer to KEEPER, DUMPER, DISPUTER_WHITELIST and CreateX deployer, please fund the deployer and check FUND_AMOUNT if needed"
);
}

// Transfer funds to KEEPER, DUMPER and DISPUTER_WHITELIST
address[] memory recipients = new address[](2 + DISPUTER_WHITELIST.length);
recipients[0] = KEEPER;
recipients[1] = DUMPER;
for (uint256 i = 0; i < DISPUTER_WHITELIST.length; i++) {
recipients[i + 2] = DISPUTER_WHITELIST[i];
}

uint256[] memory amounts = new uint256[](2 + DISPUTER_WHITELIST.length);
amounts[0] = FUND_AMOUNT;
amounts[1] = FUND_AMOUNT;
for (uint256 i = 0; i < DISPUTER_WHITELIST.length; i++) {
amounts[i + 2] = FUND_AMOUNT;
}
// Transfer initial funds to required addresses
transferInitialFunds();

console.log("Transferring to KEEPER, DUMPER and DISPUTER_WHITELIST native tokens:", FUND_AMOUNT);
transferNativeTokens(recipients, amounts);
// Deploy CreateX (if contract not found on this chain)
deployCreateX();

// Deploy ProxyAdmin
address proxyAdmin = deployProxyAdmin();
Expand All @@ -165,12 +141,6 @@ contract MainDeployScript is Script, JsonReader, TokensUtils, CreateXConstants {
// Deploy AglaMerkl
address aglaMerkl = deployAglaMerkl();

// If needed, send funds to CreateX deployer and deploy CreateX
if (CREATEX.code.length == 0) {
transferNativeTokens(CREATEX_DEPLOYER, FUND_AMOUNT); // recommended by https://github.com/pcaversaccio/createx to send FUND_AMOUNT to the deployer
deployCreateX();
}

vm.stopBroadcast();

// 2. Deploy using MERKL_DEPLOYER_PRIVATE_KEY
Expand Down Expand Up @@ -365,13 +335,15 @@ contract MainDeployScript is Script, JsonReader, TokensUtils, CreateXConstants {
}

function deployCreateX() public returns (address) {
console.log("\n=== Deploying CreateX ===");
// Deploy the contract using the provided bytecode (see https://github.com/pcaversaccio/createx/blob/main/scripts/presigned-createx-deployment-transactions/signed_serialised_transaction_gaslimit_3000000_.json)
// Broadcast the raw transaction
vm.broadcastRawTransaction(CREATEX_RAW_RX);
if (CREATEX.code.length != 0) {
console.log("CreateX:", CREATEX);
} else console.log("Failed to deploy CreateX");
if (CREATEX.code.length == 0) {
console.log("\n=== Deploying CreateX ===");
// Deploy the contract using the provided bytecode (see https://github.com/pcaversaccio/createx/blob/main/scripts/presigned-createx-deployment-transactions/signed_serialised_transaction_gaslimit_3000000_.json)
// Broadcast the raw transaction
vm.broadcastRawTransaction(CREATEX_RAW_RX);
if (CREATEX.code.length != 0) {
console.log("CreateX:", CREATEX);
} else console.log("Failed to deploy CreateX");
}
}

/*//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -455,4 +427,44 @@ contract MainDeployScript is Script, JsonReader, TokensUtils, CreateXConstants {
if (EXPECTED_DISTRIBUTION_CREATOR_PROXY_ADDRESS != vm.computeCreateAddress(MERKL_DEPLOYER_ADDRESS, 4))
revert("DISTRIBUTION_CREATOR_PROXY_ADDRESS_MISMATCH");
}

function transferInitialFunds() internal {
console.log("\n=== Transferring initial funds ===");

// Calculate total recipients including KEEPER, DUMPER, DISPUTER_WHITELIST and optionally CreateX deployer
uint256 transferLength = 2 + DISPUTER_WHITELIST.length + (CREATEX.code.length == 0 ? 1 : 0);

// Check deployer balance
if (DEPLOYER_ADDRESS.balance < FUND_AMOUNT * transferLength) {
revert(
"DEPLOYER_ADDRESS does not have enough balance to transfer to KEEPER, DUMPER, DISPUTER_WHITELIST and CreateX deployer, please fund the deployer and check FUND_AMOUNT if needed"
);
}

// Prepare recipient and amount arrays
address[] memory recipients = new address[](transferLength);
uint256[] memory amounts = new uint256[](transferLength);

// Add KEEPER and DUMPER
recipients[0] = KEEPER;
recipients[1] = DUMPER;
amounts[0] = FUND_AMOUNT;
amounts[1] = FUND_AMOUNT;

// Add DISPUTER_WHITELIST
for (uint256 i = 0; i < DISPUTER_WHITELIST.length; i++) {
recipients[i + 2] = DISPUTER_WHITELIST[i];
amounts[i + 2] = FUND_AMOUNT;
}

// Add CreateX deployer if needed
if (CREATEX.code.length == 0) {
recipients[transferLength - 1] = CREATEX_DEPLOYER;
amounts[transferLength - 1] = FUND_AMOUNT;
}

console.log("Transferring funds to required addresses:", FUND_AMOUNT);
console.log("Total amount transferred:", FUND_AMOUNT * transferLength);
transferNativeTokens(recipients, amounts);
}
}

0 comments on commit b760b03

Please sign in to comment.