Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(contracts): add REZ token to Hyperlane AVS #4786

Open
wants to merge 5 commits into
base: audit-q3-2024
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions solidity/contracts/interfaces/avs/vendored/IStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,10 @@ interface IStrategy {

/// @notice Returns either a brief string explaining the strategy's goal & purpose, or a link to metadata that explains in more detail.
function explanation() external view returns (string memory);

function strategyManager() external view returns (address);
}

interface IStrategyFactory {
function deployedStrategies(IERC20 token) external view returns (IStrategy);
}
54 changes: 53 additions & 1 deletion solidity/script/avs/DeployAVS.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pragma solidity >=0.8.0;

import "forge-std/Script.sol";

import {IStrategy} from "../../contracts/interfaces/avs/vendored/IStrategy.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IStrategy, IStrategyFactory} from "../../contracts/interfaces/avs/vendored/IStrategy.sol";
import {IAVSDirectory} from "../../contracts/interfaces/avs/vendored/IAVSDirectory.sol";
import {IPaymentCoordinator} from "../../contracts/interfaces/avs/vendored/IPaymentCoordinator.sol";
import {IDelegationManager} from "../../contracts/interfaces/avs/vendored/IDelegationManager.sol";
Expand Down Expand Up @@ -226,4 +227,55 @@ contract DeployAVS is Script {
require(strategies[i] != address(0), "Strategy address is 0");
}
}

function addREZStrategy() external {
IERC20 rezToken = IERC20(0x3B50805453023a91a8bf641e279401a0b23FA6F9);
IStrategyFactory strategyFactory = IStrategyFactory(
0x5e4C39Ad7A3E881585e383dB9827EB4811f6F647
);

IStrategy rezStrategy = strategyFactory.deployedStrategies(rezToken);

ECDSAStakeRegistry stakeRegistry = ECDSAStakeRegistry(
0x272CF0BB70D3B4f79414E0823B426d2EaFd48910
);

// get current quorum
Quorum memory currentQuorum = stakeRegistry.quorum();
Quorum memory newQuorum;
newQuorum.strategies = new StrategyParams[](
currentQuorum.strategies.length + 1
);

uint256 totalStrategies = newQuorum.strategies.length;
uint96 baseMultiplier = uint96(10000 / totalStrategies);

uint96 remainder = 10000 % uint96(totalStrategies);

for (uint256 i = 0; i < currentQuorum.strategies.length; i++) {
newQuorum.strategies[i] = currentQuorum.strategies[i];
newQuorum.strategies[i].multiplier = baseMultiplier;
}
Comment on lines +257 to +265
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really want to apply a prorate quorum for REZ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter, we'll read it manually for ISM config.


newQuorum.strategies[newQuorum.strategies.length - 1] = StrategyParams({
strategy: rezStrategy,
multiplier: baseMultiplier + remainder
});

console.log("New REZ Strategy: ", address(rezStrategy));
console.log(
"New REZ Strategy Multiplier: ",
baseMultiplier + remainder
);

// add strategy to quorum
bytes memory encodedCall = abi.encodeWithSelector(
stakeRegistry.updateQuorumConfig.selector,
newQuorum,
new address[](0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats this param?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update operator weights, if we give an empty array, it doesn't update it.

);

console.log("Encoded call: ");
console.logBytes(encodedCall);
aroralanuk marked this conversation as resolved.
Show resolved Hide resolved
}
}
140 changes: 140 additions & 0 deletions solidity/test/avs/TestREZUpgrade.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;

import {Test} from "forge-std/Test.sol";
import {console} from "forge-std/console.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import {ECDSAStakeRegistry} from "../../contracts/avs/ECDSAStakeRegistry.sol";
import {HyperlaneServiceManager} from "../../contracts/avs/HyperlaneServiceManager.sol";
import {IStrategy, IStrategyFactory} from "../../contracts/interfaces/avs/vendored/IStrategy.sol";
import {IDelegationManager} from "../../contracts/interfaces/avs/vendored/IDelegationManager.sol";
import "../../contracts/interfaces/avs/vendored/ISignatureUtils.sol";
import {Quorum, StrategyParams} from "../../contracts/interfaces/avs/vendored/IECDSAStakeRegistryEventsAndErrors.sol";

contract REZAdditionTest is Test {
string MAINNET_RPC_URL = "https://eth.llamarpc.com";
uint256 FORK_BLOCK_NUMBER = 21061545;

address AW_SAFE = 0xa7ECcdb9Be08178f896c26b7BbD8C3D4E844d9Ba;
address BITGET_6 = 0x1AB4973a48dc892Cd9971ECE8e01DcC7688f8F23;
address PIER2 = 0x5dCdf02a7188257b7c37dD3158756dA9Ccd4A9Cb;

ECDSAStakeRegistry stakeRegistry =
ECDSAStakeRegistry(0x272CF0BB70D3B4f79414E0823B426d2EaFd48910);
IDelegationManager delegationManager =
IDelegationManager(0x39053D51B77DC0d36036Fc1fCc8Cb819df8Ef37A);
HyperlaneServiceManager serviceManager =
HyperlaneServiceManager(0xe8E59c6C8B56F2c178f63BCFC4ce5e5e2359c8fc);
IERC20 rezToken = IERC20(0x3B50805453023a91a8bf641e279401a0b23FA6F9);
IStrategyFactory strategyFactory =
IStrategyFactory(0x5e4C39Ad7A3E881585e383dB9827EB4811f6F647);

function setUp() public {
vm.createSelectFork(MAINNET_RPC_URL, FORK_BLOCK_NUMBER);
}

function test_addREZStrategy() external {
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
vm.startPrank(AW_SAFE);

// STEP 1: update the stakeRegistry quorum with the new REZ strategy and adjust the weights accordingly to sum up to 10_000
IStrategy rezStrategy = strategyFactory.deployedStrategies(rezToken);
require(address(rezStrategy).code.length > 0, "Strategy not deployed");
_setQuorum(rezStrategy);

address[] memory restakeableStrategies = serviceManager
.getRestakeableStrategies();

bool rezStrategyFound = false;
for (uint256 i = 0; i < restakeableStrategies.length; i++) {
if (restakeableStrategies[i] == address(rezStrategy)) {
rezStrategyFound = true;
}
}
require(
rezStrategyFound,
"REZ strategy not found in total restakeable strategies"
);

// STEP 2: check if a staker's REZ deposit gets accounted for
uint256 amountToStake = rezToken.balanceOf(BITGET_6);

IStrategy[] memory strategiesToQuery = new IStrategy[](1);
strategiesToQuery[0] = rezStrategy;
uint256 sharesBefore = delegationManager.getOperatorShares(
PIER2,
strategiesToQuery
)[0];

// compute the storage slot
address[] memory operatorStrategies1 = serviceManager
.getOperatorRestakedStrategies(PIER2);

// directly update the operator shares instead of calling
bytes32 operatorSharesSlot = keccak256(
abi.encode(
address(rezStrategy),
keccak256(abi.encode(PIER2, bytes32(uint256(152))))
)
);
vm.store(
address(delegationManager),
operatorSharesSlot,
bytes32(amountToStake)
);

uint256 sharesAfter = delegationManager.getOperatorShares(
PIER2,
strategiesToQuery
)[0];
assertEq(sharesAfter, sharesBefore + amountToStake);

address[] memory operatorStrategies = serviceManager
.getOperatorRestakedStrategies(PIER2);

rezStrategyFound = false;
for (uint256 i = 0; i < operatorStrategies.length; i++) {
if (operatorStrategies[i] == address(rezStrategy)) {
rezStrategyFound = true;
}
}
require(
rezStrategyFound,
"REZ strategy not found in operator restaked strategies"
);

vm.stopPrank();
}

function _setQuorum(IStrategy rezStrategy) internal {
// get current quorum
Quorum memory currentQuorum = stakeRegistry.quorum();
Quorum memory newQuorum;
newQuorum.strategies = new StrategyParams[](
currentQuorum.strategies.length + 1
);

uint256 totalStrategies = newQuorum.strategies.length;
// split the 10000 base multiplier evenly
uint96 baseMultiplier = uint96(10000 / totalStrategies);

uint96 remainder = 10000 % uint96(totalStrategies);

for (uint256 i = 0; i < currentQuorum.strategies.length; i++) {
newQuorum.strategies[i] = currentQuorum.strategies[i];
newQuorum.strategies[i].multiplier = baseMultiplier;
}

// add the remainder to the last strategy
newQuorum.strategies[newQuorum.strategies.length - 1] = StrategyParams({
strategy: rezStrategy,
multiplier: baseMultiplier + remainder
});

console.log("REZ Strategy: ", address(rezStrategy));
console.log("REZ Strategy Multiplier: ", baseMultiplier + remainder);

// add strategy to quorum
stakeRegistry.updateQuorumConfig(newQuorum, new address[](0));
}
}
Loading