Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bowd committed Aug 18, 2024
1 parent b07f2b0 commit 015de76
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 222 deletions.
16 changes: 3 additions & 13 deletions src/CVS.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ library CVS {
* @param args Arguments to pass to the contract constructor.
* @return addr Address of the deployed contracts.
*/
function deploy(
string memory artifact,
bytes memory args
) internal returns (address addr) {
function deploy(string memory artifact, bytes memory args) internal returns (address addr) {
bytes memory bytecode = abi.encodePacked(vm.getCode(artifact), args);

// solhint-disable-next-line no-inline-assembly
Expand All @@ -54,18 +51,11 @@ library CVS {
* @param artifact Artifact to deploy, must be compatbile with vm.getCode.
* @param args Arguments to pass to the contract constructor.
*/
function deployTo(
address target,
string memory artifact,
bytes memory args
) internal {
function deployTo(address target, string memory artifact, bytes memory args) internal {
bytes memory creationCode = vm.getCode(artifact);
vm.etch(target, abi.encodePacked(creationCode, args));
(bool success, bytes memory runtimeBytecode) = target.call("");
require(
success,
"deployTo(address,string,bytes): Failed to create runtime bytecode."
);
require(success, "deployTo(address,string,bytes): Failed to create runtime bytecode.");
vm.label(target, artifact);
vm.etch(target, runtimeBytecode);
}
Expand Down
18 changes: 4 additions & 14 deletions src/CeloChains.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ abstract contract CeloChains is StdChains {
/// @return private key of the deployer.
function deployerPrivateKey() internal returns (uint256) {
Chain memory chain = getChain(block.chainid);
string memory pkEnvVar = string(
abi.encodePacked(chain.chainAlias, "_DEPLOYER_PK")
);
string memory pkEnvVar = string(abi.encodePacked(chain.chainAlias, "_DEPLOYER_PK"));
return vm.envUint(pkEnvVar);
}

Expand Down Expand Up @@ -66,20 +64,12 @@ abstract contract CeloChains is StdChains {
celoChainsInitialized = true;

setChain("celo", StdChains.ChainData("Celo", CELO_ID, ""));
setChain(
"alfajores",
StdChains.ChainData("Celo Alfajores Testnet", ALFAJORES_ID, "")
);
setChain(
"baklava",
StdChains.ChainData("Celo Baklava Testnet", BAKLAVA_ID, "")
);
setChain("alfajores", StdChains.ChainData("Celo Alfajores Testnet", ALFAJORES_ID, ""));
setChain("baklava", StdChains.ChainData("Celo Baklava Testnet", BAKLAVA_ID, ""));
}

/// @dev Override getChain to initialize Celo chains before lookup.
function getChain(
uint256 chainId
) internal override returns (Chain memory) {
function getChain(uint256 chainId) internal override returns (Chain memory) {
initializeCeloChains();
return super.getChain(chainId);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Constants.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8;

address constant VM_ADDRESS = address(
uint160(uint256(keccak256("hevm cheat code")))
);
address constant VM_ADDRESS = address(uint160(uint256(keccak256("hevm cheat code"))));

address constant CELO_REGISTRY_ADDRESS = 0x000000000000000000000000000000000000ce10;

Expand Down
189 changes: 36 additions & 153 deletions src/ContractsLookup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ abstract contract ContractsLookup is CeloChains {

/// @notice Stores contracts parsed from broadcast scripts.
/// @dev Is populated by the `load` function.
mapping(bytes32 contractName => address contractAddress)
private contractAddress;
mapping(bytes32 contractName => address contractAddress) private contractAddress;

/// @notice Stores the content of the `dependencies.json` file, as a string.
string private dependencies;

/// @dev keeps a mapping between hash(contractName) and the call data needed to read
/// the contract address from the GovernanceFactory contract. The mapping
/// is build in the constructor.
mapping(bytes32 contractNameHash => bytes getterFn)
private govFactoryLookupFn;
mapping(bytes32 contractNameHash => bytes getterFn) private govFactoryLookupFn;

/// @dev To be overriden by implementing contract in order to specify the location
/// of the `dependencies.json` file. It's defaulted to `./dependencies.json` at
Expand All @@ -57,22 +55,12 @@ abstract contract ContractsLookup is CeloChains {
string memory path = string(abi.encodePacked(root, dependenciesPath()));
dependencies = vm.readFile(path);

govFactoryLookupFn[keccak256("MentoToken")] = abi.encodeWithSignature(
"mentoToken()"
);
govFactoryLookupFn[keccak256("Emission")] = abi.encodeWithSignature(
"emission()"
);
govFactoryLookupFn[keccak256("GovernanceTimelock")] = abi
.encodeWithSignature("governanceTimelock()");
govFactoryLookupFn[keccak256("MentoGovernor")] = abi
.encodeWithSignature("mentoGovernor()");
govFactoryLookupFn[keccak256("Locking")] = abi.encodeWithSignature(
"locking()"
);
govFactoryLookupFn[keccak256("Airgrab")] = abi.encodeWithSignature(
"airgrab()"
);
govFactoryLookupFn[keccak256("MentoToken")] = abi.encodeWithSignature("mentoToken()");
govFactoryLookupFn[keccak256("Emission")] = abi.encodeWithSignature("emission()");
govFactoryLookupFn[keccak256("GovernanceTimelock")] = abi.encodeWithSignature("governanceTimelock()");
govFactoryLookupFn[keccak256("MentoGovernor")] = abi.encodeWithSignature("mentoGovernor()");
govFactoryLookupFn[keccak256("Locking")] = abi.encodeWithSignature("locking()");
govFactoryLookupFn[keccak256("Airgrab")] = abi.encodeWithSignature("airgrab()");
}

/**
Expand All @@ -94,55 +82,27 @@ abstract contract ContractsLookup is CeloChains {
* run-{timestamp}.json files in the broadcast folder for the current chain.
* @param silent Set eether to log indexed contracts or not.
*/
function load(
string memory script,
string memory timestamp,
bool silent
) internal {
function load(string memory script, string memory timestamp, bool silent) internal {
string memory root = vm.projectRoot();
string memory path = string(
abi.encodePacked(
root,
"/broadcast/",
script,
".sol/",
vm.toString(block.chainid),
"/",
"run-",
timestamp,
".json"
root, "/broadcast/", script, ".sol/", vm.toString(block.chainid), "/", "run-", timestamp, ".json"
)
);

string memory json = vm.readFile(path);
bytes memory contractAddressesRaw = vm.parseJson(
json,
"$.transactions[?(@.transactionType == 'CREATE')].contractAddress"
);
address[] memory contractAddresses = abi.decode(
contractAddressesRaw,
(address[])
);
bytes memory contractNamesRaw = vm.parseJson(
json,
"$.transactions[?(@.transactionType == 'CREATE')].contractName"
);
string[] memory contractNames = abi.decode(
contractNamesRaw,
(string[])
);
bytes memory contractAddressesRaw =
vm.parseJson(json, "$.transactions[?(@.transactionType == 'CREATE')].contractAddress");
address[] memory contractAddresses = abi.decode(contractAddressesRaw, (address[]));
bytes memory contractNamesRaw =
vm.parseJson(json, "$.transactions[?(@.transactionType == 'CREATE')].contractName");
string[] memory contractNames = abi.decode(contractNamesRaw, (string[]));
for (uint256 i = 0; i < contractAddresses.length; i++) {
contractAddress[
keccak256(bytes(contractNames[i]))
] = contractAddresses[i];
contractAddress[keccak256(bytes(contractNames[i]))] = contractAddresses[i];
vm.label(contractAddresses[i], contractNames[i]);

if (!silent) {
console.log(
"Loaded contract %s at %s",
contractNames[i],
contractAddresses[i]
);
console.log("Loaded contract %s at %s", contractNames[i], contractAddresses[i]);
}
}
}
Expand All @@ -155,9 +115,7 @@ abstract contract ContractsLookup is CeloChains {
* @param contractName The name of the contract to lookup.
* @return found The address of the contract.
*/
function lookup(
string memory contractName
) public returns (address payable found) {
function lookup(string memory contractName) public returns (address payable found) {
address payable[] memory results = new address payable[](4);
results[0] = _lookupDependencies(contractName);
results[1] = _lookupDeployed(contractName);
Expand All @@ -175,16 +133,7 @@ abstract contract ContractsLookup is CeloChains {
}
}

require(
!foundNone,
string(
abi.encodePacked(
"Contracts: ",
contractName,
" not found in any source."
)
)
);
require(!foundNone, string(abi.encodePacked("Contracts: ", contractName, " not found in any source.")));

if (foundMany) {
console.log("Contract appears in multiple sources: ");
Expand All @@ -198,24 +147,11 @@ abstract contract ContractsLookup is CeloChains {
console.log("celoRegistry(%s): %s", contractName, results[2]);
}
if (results[3] != address(0)) {
console.log(
"governanceFactory(%s): %s",
contractName,
results[3]
);
console.log("governanceFactory(%s): %s", contractName, results[3]);
}
}

require(
!foundMany,
string(
abi.encodePacked(
"Contracts: ",
contractName,
" found in multiple sources"
)
)
);
require(!foundMany, string(abi.encodePacked("Contracts: ", contractName, " found in multiple sources")));
vm.label(found, contractName);
}

Expand All @@ -225,27 +161,17 @@ abstract contract ContractsLookup is CeloChains {
* @param contractName The name of the contract to lookup.
* @return found The address of the contract.
*/
function lookupDeployed(
string memory contractName
) public returns (address payable found) {
function lookupDeployed(string memory contractName) public returns (address payable found) {
found = _lookupDeployed(contractName);
require(
found != address(0),
string(
abi.encodePacked(
"Contracts: ",
contractName,
" not found in loaded deployment scripts."
)
)
string(abi.encodePacked("Contracts: ", contractName, " not found in loaded deployment scripts."))
);
vm.label(found, contractName);
}

/// @dev Internal method that returns address(0) if contract is not found in the source.
function _lookupDeployed(
string memory contractName
) private view returns (address payable found) {
function _lookupDeployed(string memory contractName) private view returns (address payable found) {
found = payable(contractAddress[keccak256(bytes(contractName))]);
}

Expand All @@ -255,27 +181,16 @@ abstract contract ContractsLookup is CeloChains {
* @param contractName The name of the contract to lookup.
* @return found The address of the contract.
*/
function lookupCeloRegistry(
string memory contractName
) internal returns (address payable found) {
function lookupCeloRegistry(string memory contractName) internal returns (address payable found) {
found = _lookupCeloRegistry(contractName);
require(
found != address(0),
string(
abi.encodePacked(
"Contracts: ",
contractName,
" not found in Celo registry."
)
)
found != address(0), string(abi.encodePacked("Contracts: ", contractName, " not found in Celo registry."))
);
vm.label(found, contractName);
}

/// @dev Internal method that returns address(0) if contract is not found in the source.
function _lookupCeloRegistry(
string memory contractName
) private view returns (address payable) {
function _lookupCeloRegistry(string memory contractName) private view returns (address payable) {
return payable(celoRegistry.getAddressForString(contractName));
}

Expand All @@ -285,39 +200,19 @@ abstract contract ContractsLookup is CeloChains {
* @param contractName The name of the contract to lookup.
* @return found The address of the contract.
*/
function lookupDependencies(
string memory contractName
) public returns (address payable found) {
function lookupDependencies(string memory contractName) public returns (address payable found) {
found = _lookupDependencies(contractName);
require(
found != address(0),
string(
abi.encodePacked(
"Contracts: ",
contractName,
" not found in dependencies."
)
)
found != address(0), string(abi.encodePacked("Contracts: ", contractName, " not found in dependencies."))
);
vm.label(found, contractName);
}

/// @dev Internal method that returns address(0) if contract is not found in the source.
function _lookupDependencies(
string memory contractName
) private view returns (address payable) {
function _lookupDependencies(string memory contractName) private view returns (address payable) {
bytes memory contractAddressRaw = dependencies.parseRaw(
// solhint-disable-next-line quotes
string(
abi.encodePacked(
'["',
vm.toString(block.chainid),
'"]',
'["',
contractName,
'"]'
)
)
string(abi.encodePacked('["', vm.toString(block.chainid), '"]', '["', contractName, '"]'))
);

if (contractAddressRaw.length != 32) {
Expand All @@ -332,30 +227,18 @@ abstract contract ContractsLookup is CeloChains {
* @param contractName The name of the contract to lookup.
* @return found The address of the contract.
*/
function lookupGovernanceFactory(
string memory contractName
) public returns (address payable found) {
function lookupGovernanceFactory(string memory contractName) public returns (address payable found) {
found = _lookupGovernanceFactory(contractName);
require(
found != address(0),
string(
abi.encodePacked(
"Contracts: ",
contractName,
" not found in GovernanceFactory."
)
)
string(abi.encodePacked("Contracts: ", contractName, " not found in GovernanceFactory."))
);
vm.label(found, contractName);
}

/// @dev Internal method that returns address(0) if contract is not found in the source.
function _lookupGovernanceFactory(
string memory contractName
) private view returns (address payable found) {
bytes memory getter = govFactoryLookupFn[
keccak256(bytes(contractName))
];
function _lookupGovernanceFactory(string memory contractName) private view returns (address payable found) {
bytes memory getter = govFactoryLookupFn[keccak256(bytes(contractName))];
if (getter.length == 0) return payable(address(0));

address governanceFactory = _lookupDeployed("GovernanceFactory");
Expand Down
2 changes: 1 addition & 1 deletion src/Script.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract contract Script is ScriptBase, CeloChains, ContractsLookup, StdCheatsSa
// Note: IS_SCRIPT() must return true.
bool public IS_SCRIPT = true;

/// @notice Defaults dependenciesPath to "<project root>/dependencies.json",
/// @notice Defaults dependenciesPath to "<project root>/dependencies.json",
/// can be overriden by inheriting contracts.
function dependenciesPath() internal pure virtual override returns (string memory) {
return "/dependencies.json";
Expand Down
Loading

0 comments on commit 015de76

Please sign in to comment.