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

Rename DelegationTokenStorage #53

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Additionally, it ships a wrapper contract to simplify the migration of assets fr

## Upgradability

The Morpho token complies with the EIP-1967 to support upgradeability.
The Morpho token complies with the [EIP-1967](https://eips.ethereum.org/EIPS/eip-1967) to support upgradeability.

## Delegation

Expand All @@ -22,7 +22,6 @@ The `Wrapper` contract will hold the migrated legacy tokens.

### Migration Flow


During contract intialization, 1 billion tokens will be minted for the `Wrapper` contract, which will initially hold the entire supply.
Any legacy token holder will then be able to migrate their tokens provided that the migration amount is the approved for the wrapper.
Migrated legacy tokens may be recovered in order to revert a migration.
Expand Down
23 changes: 11 additions & 12 deletions src/DelegationToken.sol
QGarchery marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ abstract contract DelegationToken is IDelegation, ERC20PermitUpgradeable, Ownabl
keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)");

// keccak256(abi.encode(uint256(keccak256("DelegationToken")) - 1)) & ~bytes32(uint256(0xff))
bytes32 internal constant ERC20DelegatesStorageLocation =
bytes32 internal constant DelegationTokenStorageLocation =
0xd583ef41af40c9ecf9cd08176e1b50741710eaecf057b22e93a6b99fa47a6400;

/* STORAGE LAYOUT */

/// @custom:storage-location erc7201:DelegationToken
struct ERC20DelegatesStorage {
struct DelegationTokenStorage {
Copy link
Contributor

@Jean-Grimal Jean-Grimal Oct 23, 2024

Choose a reason for hiding this comment

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

As this storage is only about delegation

Suggested change
struct DelegationTokenStorage {
struct DelegationStorage {

Or we can just do like OZ and stay with the exact contract name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It matches the name of the contract though.

mapping(address => address) _delegatee;
mapping(address => uint256) _delegatedVotingPower;
mapping(address => uint256) _delegationNonce;
Expand Down Expand Up @@ -75,19 +75,19 @@ abstract contract DelegationToken is IDelegation, ERC20PermitUpgradeable, Ownabl

/// @dev Returns the delegatee that `account` has chosen.
function delegatee(address account) public view returns (address) {
ERC20DelegatesStorage storage $ = _getERC20DelegatesStorage();
DelegationTokenStorage storage $ = _getDelegationTokenStorage();
return $._delegatee[account];
}

/// @dev Returns the current voting power delegated to `account`.
function delegatedVotingPower(address account) external view returns (uint256) {
ERC20DelegatesStorage storage $ = _getERC20DelegatesStorage();
DelegationTokenStorage storage $ = _getDelegationTokenStorage();
return $._delegatedVotingPower[account];
}

/// @dev Returns the current delegation nonce of `account`.
function delegationNonce(address account) external view returns (uint256) {
ERC20DelegatesStorage storage $ = _getERC20DelegatesStorage();
DelegationTokenStorage storage $ = _getDelegationTokenStorage();
return $._delegationNonce[account];
}

Expand All @@ -104,12 +104,11 @@ abstract contract DelegationToken is IDelegation, ERC20PermitUpgradeable, Ownabl
external
{
require(block.timestamp <= expiry, DelegatesExpiredSignature(expiry));

address delegator = ECDSA.recover(
_hashTypedDataV4(keccak256(abi.encode(DELEGATION_TYPEHASH, newDelegatee, nonce, expiry))), v, r, s
);

ERC20DelegatesStorage storage $ = _getERC20DelegatesStorage();
DelegationTokenStorage storage $ = _getDelegationTokenStorage();
require(nonce == $._delegationNonce[delegator]++, InvalidDelegationNonce());

_delegate(delegator, newDelegatee);
Expand All @@ -119,7 +118,7 @@ abstract contract DelegationToken is IDelegation, ERC20PermitUpgradeable, Ownabl

/// @dev Delegates the balance of the `delegator` to `newDelegatee`.
function _delegate(address delegator, address newDelegatee) internal {
ERC20DelegatesStorage storage $ = _getERC20DelegatesStorage();
DelegationTokenStorage storage $ = _getDelegationTokenStorage();
address oldDelegatee = $._delegatee[delegator];
$._delegatee[delegator] = newDelegatee;

Expand All @@ -136,7 +135,7 @@ abstract contract DelegationToken is IDelegation, ERC20PermitUpgradeable, Ownabl

/// @dev Moves delegated votes from one delegate to another.
function _moveDelegateVotes(address from, address to, uint256 amount) internal {
ERC20DelegatesStorage storage $ = _getERC20DelegatesStorage();
DelegationTokenStorage storage $ = _getDelegationTokenStorage();
if (from != to && amount > 0) {
if (from != address(0)) {
uint256 oldValue = $._delegatedVotingPower[from];
Expand All @@ -153,10 +152,10 @@ abstract contract DelegationToken is IDelegation, ERC20PermitUpgradeable, Ownabl
}
}

/// @dev Returns the ERC20DelegatesStorage struct.
function _getERC20DelegatesStorage() internal pure returns (ERC20DelegatesStorage storage $) {
/// @dev Returns the DelegationTokenStorage struct.
function _getDelegationTokenStorage() internal pure returns (DelegationTokenStorage storage $) {
assembly {
$.slot := ERC20DelegatesStorageLocation
$.slot := DelegationTokenStorageLocation
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/MorphoTokenTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ contract MorphoTokenEthereumTest is BaseTest {
assertEq(newMorpho.delegatedVotingPower(delegatee2), transferredAmount);
}

function testERC20DelegatesStorageLocation() public pure {
function testDelegationTokenStorageLocation() public pure {
bytes32 expected =
keccak256(abi.encode(uint256(keccak256("morpho.storage.ERC20Delegates")) - 1)) & ~bytes32(uint256(0xff));
assertEq(expected, 0x1dc92b2c6e971ab6e08dfd7dcec0e9496d223ced663ba2a06543451548549500);
Expand Down