-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the deployment of the libraries from the
sismo-connect-solidity repository
- Loading branch information
Showing
9 changed files
with
404 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.17; | ||
|
||
import {Script} from "forge-std/Script.sol"; | ||
import "forge-std/console.sol"; | ||
import "@openzeppelin/contracts/utils/Strings.sol"; | ||
|
||
struct DeploymentConfig { | ||
address authRequestBuilder; | ||
address claimRequestBuilder; | ||
address requestBuilder; | ||
address signatureBuilder; | ||
} | ||
|
||
contract BaseDeploymentConfig is Script { | ||
DeploymentConfig public config; | ||
|
||
string public _chainName; | ||
bool public _checkIfEmpty; | ||
|
||
address immutable SISMO_ADDRESSES_PROVIDER_V2 = 0x3Cd5334eB64ebBd4003b72022CC25465f1BFcEe6; | ||
address immutable ZERO_ADDRESS = 0x0000000000000000000000000000000000000000; | ||
|
||
error ChainNameNotFound(string chainName); | ||
|
||
function _setConfig(string memory chainName) internal { | ||
if ( | ||
_compareStrings(chainName, "mainnet") || | ||
_compareStrings(chainName, "gnosis") || | ||
_compareStrings(chainName, "polygon") || | ||
_compareStrings(chainName, "optimism") || | ||
_compareStrings(chainName, "arbitrum-one") || | ||
_compareStrings(chainName, "testnet-goerli") || | ||
_compareStrings(chainName, "testnet-sepolia") || | ||
_compareStrings(chainName, "testnet-mumbai") || | ||
_compareStrings(chainName, "optimism-goerli") || | ||
_compareStrings(chainName, "arbitrum-goerli") || | ||
_compareStrings(chainName, "scroll-testnet-goerli") || | ||
_compareStrings(chainName, "staging-goerli") || | ||
_compareStrings(chainName, "staging-mumbai") | ||
) { | ||
config = _readDeploymentConfig( | ||
string.concat(vm.projectRoot(), "/deployments/", chainName, ".json") | ||
); | ||
} else if (_compareStrings(chainName, "test")) { | ||
config = DeploymentConfig(address(0), address(0), address(0), address(0)); | ||
} else { | ||
revert ChainNameNotFound(chainName); | ||
} | ||
} | ||
|
||
function _readDeploymentConfig( | ||
string memory filePath | ||
) internal view returns (DeploymentConfig memory) { | ||
string memory file = _tryReadingFile(filePath); | ||
return | ||
DeploymentConfig({ | ||
authRequestBuilder: _tryReadingAddressFromFileAtKey(file, ".authRequestBuilder"), | ||
claimRequestBuilder: _tryReadingAddressFromFileAtKey(file, ".claimRequestBuilder"), | ||
requestBuilder: _tryReadingAddressFromFileAtKey(file, ".requestBuilder"), | ||
signatureBuilder: _tryReadingAddressFromFileAtKey(file, ".signatureBuilder") | ||
}); | ||
} | ||
|
||
function _tryReadingFile(string memory filePath) internal view returns (string memory) { | ||
try vm.readFile(filePath) returns (string memory file) { | ||
return file; | ||
} catch { | ||
return ""; | ||
} | ||
} | ||
|
||
function _tryReadingAddressFromFileAtKey( | ||
string memory file, | ||
string memory key | ||
) internal view returns (address) { | ||
try vm.parseJson(file, key) returns (bytes memory encodedAddress) { | ||
return | ||
keccak256(encodedAddress) == keccak256(abi.encodePacked((""))) | ||
? address(0) | ||
: abi.decode(encodedAddress, (address)); | ||
} catch { | ||
return ZERO_ADDRESS; | ||
} | ||
} | ||
|
||
function _saveDeploymentConfig(string memory chainName) internal { | ||
_createFolderIfItDoesNotExists(string.concat(vm.projectRoot(), "/deployments")); | ||
_createFolderIfItDoesNotExists(string.concat(vm.projectRoot(), "/deployments/tmp")); | ||
_createFolderIfItDoesNotExists(string.concat(vm.projectRoot(), "/deployments/tmp/", chainName)); | ||
|
||
vm.serializeAddress(chainName, "authRequestBuilder", address(config.authRequestBuilder)); | ||
vm.serializeAddress(chainName, "claimRequestBuilder", address(config.claimRequestBuilder)); | ||
vm.serializeAddress(chainName, "requestBuilder", address(config.requestBuilder)); | ||
string memory finalJson = vm.serializeAddress( | ||
chainName, | ||
"signatureBuilder", | ||
address(config.signatureBuilder) | ||
); | ||
|
||
vm.writeJson( | ||
finalJson, | ||
string.concat( | ||
vm.projectRoot(), | ||
"/deployments/tmp/", | ||
chainName, | ||
"/logs", | ||
"-", | ||
Strings.toString(block.timestamp), | ||
".json" | ||
) | ||
); | ||
} | ||
|
||
function _createFolderIfItDoesNotExists(string memory folderPath) internal { | ||
string[] memory inputs = new string[](3); | ||
inputs[0] = "mkdir"; | ||
inputs[1] = "-p"; | ||
inputs[2] = folderPath; | ||
vm.ffi(inputs); | ||
} | ||
|
||
function _compareStrings(string memory a, string memory b) internal pure returns (bool) { | ||
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b)))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.17; | ||
|
||
import "forge-std/Script.sol"; | ||
import "forge-std/console.sol"; | ||
import {BaseDeploymentConfig, DeploymentConfig} from "script/BaseConfig.sol"; | ||
import {IAddressesProvider} from "src/interfaces/IAddressesProvider.sol"; | ||
import {AuthRequestBuilder} from "src/utils/AuthRequestBuilder.sol"; | ||
import {ClaimRequestBuilder} from "src/utils/ClaimRequestBuilder.sol"; | ||
import {SignatureBuilder} from "src/utils/SignatureBuilder.sol"; | ||
import {RequestBuilder} from "src/utils/RequestBuilder.sol"; | ||
|
||
/** | ||
* @title DeployLibraries Script | ||
* @notice To test this script locally, run: | ||
* in a first terminal: `anvil fork-url https://rpc.ankr.com/polygon_mumbai` | ||
* in a second terminal: `cast rpc anvil_impersonateAccount` | ||
* and finally: `CHAIN_NAME=test forge script DeployLibraries --rpc-url localhost --sender 0xbb8fca8f2381cfeede5d7541d7bf76343ef6c67b --unlocked -vvvv` | ||
* You should see that the libraries are being deployed and successfully set in the AddressesProvider contract. | ||
* You can then run the `yarn save-deployments` command to save the deployment addresses in the `deployments` folder. You will see the libraries addresses in the `deployments/test/logs-*.json` file. | ||
*/ | ||
|
||
contract DeployLibraries is Script, BaseDeploymentConfig { | ||
struct DeployedLibraries { | ||
AuthRequestBuilder authRequestBuilder; | ||
ClaimRequestBuilder claimRequestBuilder; | ||
SignatureBuilder signatureBuilder; | ||
RequestBuilder requestBuilder; | ||
} | ||
|
||
struct DeployedLibrary { | ||
address addr; | ||
string name; | ||
} | ||
|
||
// useful variables to set libraries addresses in AddressesProvider in batch | ||
address[] public contractAddresses; | ||
string[] public contractNames; | ||
|
||
function run() public { | ||
runFor(vm.envString("CHAIN_NAME")); | ||
} | ||
|
||
function runFor(string memory chainName) public returns (DeploymentConfig memory) { | ||
console.log("Run for CHAIN_NAME:", chainName); | ||
console.log("Deployer:", msg.sender); | ||
|
||
vm.startBroadcast(); | ||
|
||
_setConfig({chainName: chainName}); | ||
|
||
// deploy external libraries | ||
DeployedLibraries memory deployedLibraries; | ||
deployedLibraries.authRequestBuilder = _deployAuthRequestBuilder(); | ||
deployedLibraries.claimRequestBuilder = _deployClaimRequestBuilder(); | ||
deployedLibraries.signatureBuilder = _deploySignatureBuilder(); | ||
deployedLibraries.requestBuilder = _deployRequestBuilder(); | ||
|
||
// associate libraries addresses to names | ||
DeployedLibrary[] memory deployedLibrariesArray = new DeployedLibrary[](4); | ||
deployedLibrariesArray[0] = DeployedLibrary({ | ||
addr: address(deployedLibraries.authRequestBuilder), | ||
name: "authRequestBuilder-v1.1" | ||
}); | ||
deployedLibrariesArray[1] = DeployedLibrary({ | ||
addr: address(deployedLibraries.claimRequestBuilder), | ||
name: "claimRequestBuilder-v1.1" | ||
}); | ||
deployedLibrariesArray[2] = DeployedLibrary({ | ||
addr: address(deployedLibraries.signatureBuilder), | ||
name: "signatureBuilder-v1.1" | ||
}); | ||
deployedLibrariesArray[3] = DeployedLibrary({ | ||
addr: address(deployedLibraries.requestBuilder), | ||
name: "requestBuilder-v1.1" | ||
}); | ||
|
||
// update addresses provider with deployed libraries addresses | ||
_setLibrariesAddresses(deployedLibrariesArray); | ||
|
||
// update deployment config with deployed libraries addresses | ||
// and save it in `deployments` folder | ||
// only for a successful broadcast | ||
config = DeploymentConfig({ | ||
authRequestBuilder: address(deployedLibraries.authRequestBuilder), | ||
claimRequestBuilder: address(deployedLibraries.claimRequestBuilder), | ||
signatureBuilder: address(deployedLibraries.signatureBuilder), | ||
requestBuilder: address(deployedLibraries.requestBuilder) | ||
}); | ||
vm.stopBroadcast(); | ||
_saveDeploymentConfig(chainName); | ||
|
||
return config; | ||
} | ||
|
||
function _deployAuthRequestBuilder() private returns (AuthRequestBuilder) { | ||
address authRequestBuilderAddress = config.authRequestBuilder; | ||
if (authRequestBuilderAddress != address(0)) { | ||
console.log("Using existing authrequestBuilder:", authRequestBuilderAddress); | ||
return AuthRequestBuilder(authRequestBuilderAddress); | ||
} | ||
AuthRequestBuilder authRequestBuilder = new AuthRequestBuilder(); | ||
console.log("authRequestBuilder Deployed:", address(authRequestBuilder)); | ||
return authRequestBuilder; | ||
} | ||
|
||
function _deployClaimRequestBuilder() private returns (ClaimRequestBuilder) { | ||
address claimRequestBuilderAddress = config.claimRequestBuilder; | ||
if (claimRequestBuilderAddress != address(0)) { | ||
console.log("Using existing claimRequestBuilder:", claimRequestBuilderAddress); | ||
return ClaimRequestBuilder(claimRequestBuilderAddress); | ||
} | ||
ClaimRequestBuilder claimRequestBuilder = new ClaimRequestBuilder(); | ||
console.log("claimRequestBuilder Deployed:", address(claimRequestBuilder)); | ||
return claimRequestBuilder; | ||
} | ||
|
||
function _deploySignatureBuilder() private returns (SignatureBuilder) { | ||
address signatureBuilderAddress = config.signatureBuilder; | ||
if (signatureBuilderAddress != address(0)) { | ||
console.log("Using existing signatureBuilder:", signatureBuilderAddress); | ||
return SignatureBuilder(signatureBuilderAddress); | ||
} | ||
SignatureBuilder signatureBuilder = new SignatureBuilder(); | ||
console.log("signatureBuilder Deployed:", address(signatureBuilder)); | ||
return signatureBuilder; | ||
} | ||
|
||
function _deployRequestBuilder() private returns (RequestBuilder) { | ||
address requestBuilderAddress = config.requestBuilder; | ||
if (requestBuilderAddress != address(0)) { | ||
console.log("Using existing requestBuilder:", requestBuilderAddress); | ||
return RequestBuilder(requestBuilderAddress); | ||
} | ||
RequestBuilder requestBuilder = new RequestBuilder(); | ||
console.log("requestBuilder Deployed:", address(requestBuilder)); | ||
return requestBuilder; | ||
} | ||
|
||
function _setLibrariesAddresses(DeployedLibrary[] memory deployedLibrariesArray) private { | ||
console.log("== Updating Addresses Provider =="); | ||
IAddressesProvider sismoAddressProvider = IAddressesProvider(SISMO_ADDRESSES_PROVIDER_V2); | ||
|
||
for (uint256 i = 0; i < deployedLibrariesArray.length; i++) { | ||
DeployedLibrary memory deployedLibrary = deployedLibrariesArray[i]; | ||
address currentContractAddress = sismoAddressProvider.get(deployedLibrary.name); | ||
|
||
if (currentContractAddress != deployedLibrary.addr) { | ||
console.log( | ||
"current contract address for", | ||
deployedLibrary.name, | ||
"is different. Updating address to", | ||
deployedLibrary.addr | ||
); | ||
// save address to update in batch after | ||
contractAddresses.push(deployedLibrary.addr); | ||
contractNames.push(deployedLibrary.name); | ||
} else { | ||
console.log( | ||
"current contract address for", | ||
deployedLibrary.name, | ||
"is already the expected one. skipping update" | ||
); | ||
} | ||
} | ||
|
||
if (contractAddresses.length > 0) { | ||
console.log("Updating Addresses Provider in batch..."); | ||
sismoAddressProvider.setBatch(contractAddresses, contractNames); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/bin/bash | ||
|
||
source_folder="$PWD/deployments/tmp" | ||
destination_root="$PWD/deployments" | ||
|
||
# Find all subfolders containing JSON files and process them | ||
find "$source_folder" -type f -name "*.json" | while read -r json_file; do | ||
subfolder=$(dirname "$json_file") | ||
subfolder_name=$(basename "$subfolder") | ||
destination_folder="$destination_root/$subfolder_name" | ||
|
||
# Create the destination folder after deleting the previous one if it exists | ||
if [ -d "$destination_folder" ]; then rm -Rf $destination_folder; fi | ||
mkdir "$destination_folder" | ||
|
||
# Find the latest JSON file in the subfolder | ||
latest_file=$(ls -t "$subfolder"/*.json | head -n 1) | ||
|
||
if [ -n "$latest_file" ]; then | ||
# Copy the latest JSON file to the destination folder to keep track of the latest deployment | ||
cp "$latest_file" "$destination_folder" | ||
else | ||
echo "No matching files found in $subfolder_name." | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.