Skip to content

Commit

Permalink
add finished scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Dec 19, 2024
1 parent daa1ad7 commit 837d354
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 66 deletions.
5 changes: 5 additions & 0 deletions contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ remappings = [
"@openzeppelin-contracts-upgradeable/=dependencies/@openzeppelin-contracts-upgradeable-5.1.0/",
"forge-std/=dependencies/forge-std-1.9.4/",
]
fs_permissions = [
{ access = "read-write", path = "script/output/31337"},
{ access = "read-write", path = "script/output/11155111"},
{ access = "read-write", path = "script/output/241320161"}
]

[soldeer]
recursive_deps = true
Expand Down
55 changes: 41 additions & 14 deletions contracts/script/DeployGroupMessages.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,59 @@ pragma solidity 0.8.28;

import "forge-std/src/Script.sol";
import "forge-std/src/Vm.sol";
import "./utils/Utils.sol";
import "./utils/Environment.sol";
import "src/GroupMessages.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

contract DeployGroupMessages is Script {
contract DeployGroupMessages is Script, Utils, Environment {
GroupMessages groupMessagesImpl;
ERC1967Proxy proxy;

address admin;
address deployer;

function run() external {
admin = vm.envAddress("XMTP_GROUP_MESSAGES_ADMIN_ADDRESS");
require(admin != address(0), "XMTP_GROUP_MESSAGES_ADMIN_ADDRESS not set");
require(admin.code.length == 0, "admin address is a contract, not an EOA");

uint256 privateKey = vm.envUint("PRIVATE_KEY");
address deployer = vm.addr(privateKey);
deployer = vm.addr(privateKey);
vm.startBroadcast(privateKey);

// Step 1: Deploy the implementation contract.
GroupMessages groupMessagesImpl = new GroupMessages();
// Deploy the implementation contract.
groupMessagesImpl = new GroupMessages();
require(address(groupMessagesImpl) != address(0), "Implementation deployment failed");

// Step 2: Deploy the proxy contract.
ERC1967Proxy proxy =
// Deploy the proxy contract.
proxy =
new ERC1967Proxy(
address(groupMessagesImpl),
abi.encodeWithSelector(GroupMessages.initialize.selector, deployer)
);

console.log(
'{"deployer":"%s","proxy":"%s","implementation":"%s"}',
deployer,
address(proxy),
address(groupMessagesImpl)
abi.encodeWithSelector(GroupMessages.initialize.selector, admin)
);

vm.stopBroadcast();

_serializeDeploymentData();
}

function _serializeDeploymentData() internal {
string memory parent_object = "parent object";
string memory addresses = "addresses";

string memory addressesOutput;

addressesOutput = vm.serializeAddress(addresses, "groupMessagesDeployer", deployer);
addressesOutput = vm.serializeAddress(addresses, "groupMessagesProxyAdmin", admin);
addressesOutput = vm.serializeAddress(addresses, "groupMessagesProxy", address(proxy));
addressesOutput = vm.serializeAddress(addresses, "groupMessagesImpl", address(groupMessagesImpl));

string memory finalJson;
finalJson = vm.serializeString(parent_object, addresses, addressesOutput);
finalJson = vm.serializeUint(parent_object, "deploymentBlock", block.number);
finalJson = vm.serializeUint(parent_object, "latestUpgradeBlock", block.number);

writeOutput(finalJson, XMTP_GROUP_MESSAGES_OUTPUT_JSON);
}
}
55 changes: 41 additions & 14 deletions contracts/script/DeployIdentityUpdates.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,59 @@ pragma solidity 0.8.28;

import "forge-std/src/Script.sol";
import "forge-std/src/Vm.sol";
import "./utils/Utils.sol";
import "./utils/Environment.sol";
import "src/IdentityUpdates.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

contract DeployIdentityUpdates is Script {
contract DeployIdentityUpdates is Script, Utils, Environment {
IdentityUpdates idUpdatesImpl;
ERC1967Proxy proxy;

address admin;
address deployer;

function run() external {
admin = vm.envAddress("XMTP_IDENTITY_UPDATES_ADMIN_ADDRESS");
require(admin != address(0), "XMTP_IDENTITY_UPDATES_ADMIN_ADDRESS not set");
require(admin.code.length == 0, "admin address is a contract, not an EOA");

uint256 privateKey = vm.envUint("PRIVATE_KEY");
address deployer = vm.addr(privateKey);
deployer = vm.addr(privateKey);
vm.startBroadcast(privateKey);

// Step 1: Deploy the implementation contract
IdentityUpdates idUpdatesImpl = new IdentityUpdates();
// Deploy the implementation contract.
idUpdatesImpl = new IdentityUpdates();
require(address(idUpdatesImpl) != address(0), "Implementation deployment failed");

// Step 2: Deploy the proxy contract
ERC1967Proxy proxy =
// Deploy the proxy contract.
proxy =
new ERC1967Proxy(
address(idUpdatesImpl),
abi.encodeWithSelector(IdentityUpdates.initialize.selector, deployer)
);

console.log(
'{"deployer":"%s","proxy":"%s","implementation":"%s"}',
deployer,
address(proxy),
address(idUpdatesImpl)
abi.encodeWithSelector(IdentityUpdates.initialize.selector, admin)
);

vm.stopBroadcast();

_serializeDeploymentData();
}

function _serializeDeploymentData() internal {
string memory parent_object = "parent object";
string memory addresses = "addresses";

string memory addressesOutput;

addressesOutput = vm.serializeAddress(addresses, "identityUpdatesDeployer", deployer);
addressesOutput = vm.serializeAddress(addresses, "identityUpdatesProxyAdmin", admin);
addressesOutput = vm.serializeAddress(addresses, "identityUpdatesProxy", address(proxy));
addressesOutput = vm.serializeAddress(addresses, "identityUpdatesImpl", address(idUpdatesImpl));

string memory finalJson;
finalJson = vm.serializeString(parent_object, addresses, addressesOutput);
finalJson = vm.serializeUint(parent_object, "deploymentBlock", block.number);
finalJson = vm.serializeUint(parent_object, "latestUpgradeBlock", block.number);

writeOutput(finalJson, XMTP_IDENTITY_UPDATES_OUTPUT_JSON);
}
}
10 changes: 10 additions & 0 deletions contracts/script/output/31337/group_messages_deployment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"addresses": {
"groupMessagesDeployer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"groupMessagesImpl": "0xD0141E899a65C95a556fE2B27e5982A6DE7fDD7A",
"groupMessagesProxy": "0xD5ac451B0c50B9476107823Af206eD814a2e2580",
"groupMessagesProxyAdmin": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
},
"deploymentBlock": 104,
"latestUpgradeBlock": 110
}
10 changes: 10 additions & 0 deletions contracts/script/output/31337/identity_updates_deployment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"addresses": {
"identityUpdatesDeployer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"identityUpdatesImpl": "0x18E317A7D70d8fBf8e6E893616b52390EbBdb629",
"identityUpdatesProxy": "0x5067457698Fd6Fa1C6964e416b3f42713513B3dD",
"identityUpdatesProxyAdmin": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
},
"deploymentBlock": 100,
"latestUpgradeBlock": 108
}
55 changes: 39 additions & 16 deletions contracts/script/upgrades/UpgradeGroupMessages.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,57 @@ pragma solidity 0.8.28;

import "forge-std/src/Script.sol";
import "forge-std/src/Vm.sol";
import "../utils/Utils.sol";
import "../utils/Environment.sol";
import "src/GroupMessages.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

contract UpgradeGroupMessages is Script {
function run() external {
address proxyAddress = vm.envAddress("XMTP_GROUP_MESSAGES_PROXY_ADDRESS");
require(proxyAddress != address(0), "XMTP_GROUP_MESSAGES_PROXY_ADDRESS not set");
contract UpgradeGroupMessages is Script, Utils, Environment {
GroupMessages newImplementation;
GroupMessages proxy;

address upgrader;

function run() external {
uint256 privateKey = vm.envUint("PRIVATE_KEY");
address upgrader = vm.addr(privateKey);
upgrader = vm.addr(privateKey);

vm.startBroadcast(privateKey);

// Step 1: Deploy the new implementation contract.
GroupMessages newImplementation = new GroupMessages();
_initializeProxy();

// Step 2: Initialize the proxy.
GroupMessages proxy = GroupMessages(proxyAddress);
// Deploy the new implementation contract.
newImplementation = new GroupMessages();
require(address(newImplementation) != address(0), "Implementation deployment failed");

// Step 3: Upgrade the proxy pointer to the new implementation.
// Upgrade the proxy pointer to the new implementation.
proxy.upgradeToAndCall(address(newImplementation), "");

console.log(
'{"upgrader":"%s","proxy":"%s","newImplementation":"%s"}',
upgrader,
address(proxy),
address(newImplementation)
vm.stopBroadcast();

_serializeUpgradeData();
}

function _initializeProxy() internal {
string memory fileContent = readOutput(XMTP_GROUP_MESSAGES_OUTPUT_JSON);
proxy = GroupMessages(stdJson.readAddress(fileContent, ".addresses.groupMessagesProxy"));
require(address(proxy) != address(0), "proxy address not set");
require(
proxy.hasRole(proxy.DEFAULT_ADMIN_ROLE(), upgrader),
"Upgrader must have admin role"
);
}

vm.stopBroadcast();
function _serializeUpgradeData() internal {
vm.writeJson(
vm.toString(address(newImplementation)),
getOutputPath(XMTP_GROUP_MESSAGES_OUTPUT_JSON),
".addresses.groupMessagesImpl"
);
vm.writeJson(
vm.toString(block.number),
getOutputPath(XMTP_GROUP_MESSAGES_OUTPUT_JSON),
".latestUpgradeBlock"
);
}
}
57 changes: 40 additions & 17 deletions contracts/script/upgrades/UpgradeIdentityUpdates.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,57 @@ pragma solidity 0.8.28;

import "forge-std/src/Script.sol";
import "forge-std/src/Vm.sol";
import "../utils/Utils.sol";
import "../utils/Environment.sol";
import "src/IdentityUpdates.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

contract UpgradeGroupMessages is Script {
function run() external {
address proxyAddress = vm.envAddress("XMTP_IDENTITY_UPDATES_PROXY_ADDRESS");
require(proxyAddress != address(0), "XMTP_IDENTITY_UPDATES_PROXY_ADDRESS not set");
contract UpgradeIdentityUpdates is Script, Utils, Environment {
IdentityUpdates newImplementation;
IdentityUpdates proxy;

address upgrader;

function run() external {
uint256 privateKey = vm.envUint("PRIVATE_KEY");
address upgrader = vm.addr(privateKey);
upgrader = vm.addr(privateKey);

vm.startBroadcast(privateKey);

// Step 1: Deploy the new implementation contract.
IdentityUpdates newImplementation = new IdentityUpdates();

// Step 2: Initialize the proxy.
IdentityUpdates proxy = IdentityUpdates(proxyAddress);
_initializeProxy();

// Step 3: Upgrade the proxy pointer to the new implementation.
// Deploy the new implementation contract.
newImplementation = new IdentityUpdates();
require(address(newImplementation) != address(0), "Implementation deployment failed");

// Upgrade the proxy pointer to the new implementation.
proxy.upgradeToAndCall(address(newImplementation), "");

console.log(
'{"upgrader":"%s","proxy":"%s","newImplementation":"%s"}',
upgrader,
address(proxy),
address(newImplementation)
vm.stopBroadcast();

_serializeUpgradeData();
}

function _initializeProxy() internal {
string memory fileContent = readOutput(XMTP_IDENTITY_UPDATES_OUTPUT_JSON);
proxy = IdentityUpdates(stdJson.readAddress(fileContent, ".addresses.identityUpdatesProxy"));
require(address(proxy) != address(0), "proxy address not set");
require(
proxy.hasRole(proxy.DEFAULT_ADMIN_ROLE(), upgrader),
"Upgrader must have admin role"
);
}

vm.stopBroadcast();
function _serializeUpgradeData() internal {
vm.writeJson(
vm.toString(address(newImplementation)),
getOutputPath(XMTP_IDENTITY_UPDATES_OUTPUT_JSON),
".addresses.identityUpdatesImpl"
);
vm.writeJson(
vm.toString(block.number),
getOutputPath(XMTP_IDENTITY_UPDATES_OUTPUT_JSON),
".latestUpgradeBlock"
);
}
}
9 changes: 9 additions & 0 deletions contracts/script/utils/Environment.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

import "forge-std/src/Script.sol";

contract Environment is Script {
string public constant XMTP_GROUP_MESSAGES_OUTPUT_JSON = "group_messages_deployment";
string public constant XMTP_IDENTITY_UPDATES_OUTPUT_JSON = "identity_updates_deployment";
}
36 changes: 36 additions & 0 deletions contracts/script/utils/Utils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

import "forge-std/src/Script.sol";
import "forge-std/src/StdJson.sol";

contract Utils is Script {
function readInput(string memory inputFileName) internal view returns (string memory) {
string memory file = getInputPath(inputFileName);
return vm.readFile(file);
}

function getInputPath(string memory inputFileName) internal view returns (string memory) {
string memory inputDir = string.concat(vm.projectRoot(), "/script/input/");
string memory chainDir = string.concat(vm.toString(block.chainid), "/");
string memory file = string.concat(inputFileName, ".json");
return string.concat(inputDir, chainDir, file);
}

function readOutput(string memory outputFileName) internal view returns (string memory) {
string memory file = getOutputPath(outputFileName);
return vm.readFile(file);
}

function writeOutput(string memory outputJson, string memory outputFileName) internal {
string memory outputFilePath = getOutputPath(outputFileName);
vm.writeJson(outputJson, outputFilePath);
}

function getOutputPath(string memory outputFileName) internal view returns (string memory) {
string memory outputDir = string.concat(vm.projectRoot(), "/script/output/");
string memory chainDir = string.concat(vm.toString(block.chainid), "/");
string memory outputFilePath = string.concat(outputDir, chainDir, outputFileName, ".json");
return outputFilePath;
}
}
Loading

0 comments on commit 837d354

Please sign in to comment.