Skip to content

Commit

Permalink
feat(contracts): apply PR reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
seolaoh committed Jul 18, 2024
1 parent 738760a commit e8ab665
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 81 deletions.
27 changes: 22 additions & 5 deletions packages/contracts/contracts/L1/AssetManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager {
return kghAssets + kroAssets;
}

/**
* @inheritdoc IAssetManager
*/
function getWithdrawAccount(address validator) external view returns (address) {
return _vaults[validator].withdrawAccount;
}

/**
* @inheritdoc IAssetManager
*/
Expand Down Expand Up @@ -304,14 +311,23 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager {
}

/**
* @notice Deposit KRO to register as a validator.
* This function is only called by the ValidatorManager contract.
* @notice Deposit KRO to register as a validator. This function is only called by the
* ValidatorManager contract.
*
* @param validator Address of the validator.
* @param assets The amount of KRO to deposit.
* @param validator Address of the validator.
* @param assets The amount of KRO to deposit.
* @param withdrawAccount An account where assets can be withdrawn to. Only this account can
* withdraw the assets.
*/
function depositToRegister(address validator, uint128 assets) external onlyValidatorManager {
function depositToRegister(
address validator,
uint128 assets,
address withdrawAccount
) external onlyValidatorManager {
if (assets == 0) revert NotAllowedZeroInput();
if (withdrawAccount == address(0)) revert ZeroAddress();

_vaults[validator].withdrawAccount = withdrawAccount;
_deposit(validator, assets, false);
emit Deposited(validator, assets);
}
Expand All @@ -323,6 +339,7 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager {
if (assets == 0) revert NotAllowedZeroInput();
if (VALIDATOR_MANAGER.getStatus(msg.sender) == IValidatorManager.ValidatorStatus.NONE)
revert ImproperValidatorStatus();

_deposit(msg.sender, assets, true);
emit Deposited(msg.sender, assets);
}
Expand Down
26 changes: 6 additions & 20 deletions packages/contracts/contracts/L1/ValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,11 @@ contract ValidatorManager is ISemver, IValidatorManager {

if (commissionRate > COMMISSION_RATE_DENOM) revert MaxCommissionRateExceeded();

if (withdrawAccount == address(0)) revert ZeroAddress();

Validator storage validatorInfo = _validatorInfo[msg.sender];
validatorInfo.isInitiated = true;
validatorInfo.commissionRate = commissionRate;
validatorInfo.withdrawAccount = withdrawAccount;

ASSET_MANAGER.depositToRegister(msg.sender, assets);
ASSET_MANAGER.depositToRegister(msg.sender, assets, withdrawAccount);

bool ready = assets >= MIN_ACTIVATE_AMOUNT;
if (ready) {
Expand Down Expand Up @@ -243,7 +240,7 @@ contract ValidatorManager is ISemver, IValidatorManager {
if (newCommissionRate == oldCommissionRate) revert SameCommissionRate();

validatorInfo.pendingCommissionRate = newCommissionRate;
validatorInfo.commissionChangeInitTime = uint128(block.timestamp);
validatorInfo.commissionChangeInitiatedAt = uint128(block.timestamp);

emit ValidatorCommissionChangeInitiated(msg.sender, oldCommissionRate, newCommissionRate);
}
Expand All @@ -264,7 +261,7 @@ contract ValidatorManager is ISemver, IValidatorManager {

validatorInfo.commissionRate = newCommissionRate;
validatorInfo.pendingCommissionRate = 0;
validatorInfo.commissionChangeInitTime = 0;
validatorInfo.commissionChangeInitiatedAt = 0;

emit ValidatorCommissionChangeFinalized(msg.sender, oldCommissionRate, newCommissionRate);
}
Expand Down Expand Up @@ -348,18 +345,6 @@ contract ValidatorManager is ISemver, IValidatorManager {
return _validatorInfo[validator].pendingCommissionRate;
}

// TODO: move this to AssetManager
/**
* @notice Returns the address of withdraw account of given validator.
*
* @param validator Address of the validator.
*
* @return The address of withdraw account of given validator.
*/
function getWithdrawAccount(address validator) external view returns (address) {
return _validatorInfo[validator].withdrawAccount;
}

/**
* @inheritdoc IValidatorManager
*/
Expand Down Expand Up @@ -470,8 +455,9 @@ contract ValidatorManager is ISemver, IValidatorManager {
/**
* @inheritdoc IValidatorManager
*/
function canFinalizeCommissionChangeAt(address validator) public view returns (uint256) {
return _validatorInfo[validator].commissionChangeInitTime + COMMISSION_CHANGE_DELAY_SECONDS;
function canFinalizeCommissionChangeAt(address validator) public view returns (uint128) {
return
_validatorInfo[validator].commissionChangeInitiatedAt + COMMISSION_CHANGE_DELAY_SECONDS;
}

/**
Expand Down
63 changes: 35 additions & 28 deletions packages/contracts/contracts/L1/interfaces/IAssetManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ interface IAssetManager {
/**
* @notice Represents the asset information of the vault of a validator.
*
* @custom:field validatorKro Total amount of KRO that deposited by the validator and
* accumulated as validator reward (including validatorKroReserved).
* @custom:field validatorKroReserved Total amount of validator KRO that reserved during output
* submission or challenge creation.
* @custom:field totalKro Total amount of KRO that delegated by the delegators and
* accumulated as KRO delegation reward (including totalKroInKgh).
* @custom:field totalKroShares Total shares for KRO delegation in the vault.
* @custom:field totalKgh Total number of KGH in the vault.
* @custom:field totalKroInKgh Total amount of KRO which KGHs in the vault have.
* @custom:field rewardPerKghStored Accumulated boosted reward per 1 KGH.
* @custom:field validatorKro Total amount of KRO that deposited by the validator and
* accumulated as validator reward (including validatorKroBonded).
* @custom:field validatorKroBonded Total amount of validator KRO that bonded during output
* submission or challenge creation.
* @custom:field totalKro Total amount of KRO that delegated by the delegators and
* accumulated as KRO delegation reward (including totalKroInKgh).
* @custom:field totalKroShares Total shares for KRO delegation in the vault.
* @custom:field totalKgh Total number of KGH in the vault.
* @custom:field totalKroInKgh Total amount of KRO which KGHs in the vault have.
* @custom:field rewardPerKghStored Accumulated boosted reward per 1 KGH.
*/
struct Asset {
uint128 validatorKro;
uint128 validatorKroReserved;
uint128 validatorKroBonded;
uint128 totalKro;
uint128 totalKroShares;
uint128 totalKgh;
Expand All @@ -45,18 +45,17 @@ interface IAssetManager {
/**
* @notice Constructs the delegator of KGH in the vault of a validator.
*
* @custom:field lastDelegatedAt Last timestamp when the delegator delegated. The delegator
* can undelegate after UNDELEGATION_DELAY_SECONDS elapsed.
* @custom:field rewardPerKghPaid Accumulated paid boosted reward per 1 KGH.
* @custom:field kghNum Total number of KGH delegated.
* @custom:field delegationHistory A mapping of tokenId to the delegation timestamp.
* @custom:field kroShares A mapping of tokenId to the amount of shares for KRO in KGH.
* @custom:field rewardPerKghPaid Accumulated paid boosted reward per 1 KGH.
* @custom:field kghNum Total number of KGH delegated.
* @custom:field delegatedAt A mapping of tokenId to the delegation timestamp. The
* delegator can undelegate after UNDELEGATION_DELAY_SECONDS
* elapsed from each delegation timestamp.
* @custom:field kroShares A mapping of tokenId to the amount of shares for KRO in KGH.
*/
struct KghDelegator {
uint128 lastDelegatedAt;
uint128 rewardPerKghPaid;
uint256 kghNum;
mapping(uint256 => uint128) delegationHistory;
mapping(uint256 => uint128) delegatedAt;
mapping(uint256 => uint128) kroShares;
}

Expand Down Expand Up @@ -228,6 +227,11 @@ interface IAssetManager {
*/
error NotAllowedZeroInput();

/**
* @notice Reverts when the address is zero address.
*/
error ZeroAddress();

/**
* @notice Reverts when the asset is insufficient.
*/
Expand Down Expand Up @@ -260,14 +264,14 @@ interface IAssetManager {
*
* @return When the validator can withdraw KRO.
*/
function canWithdrawAt(address validator) external view returns (uint256);
function canWithdrawAt(address validator) external view returns (uint128);

/**
* @notice Returns the total amount of KRO a validator has deposited and rewarded.
* @notice Returns the total amount of KRO a validator has deposited and been rewarded.
*
* @param validator Address of the validator.
*
* @return The total amount of KRO a validator has deposited and rewarded.
* @return The total amount of KRO a validator has deposited and been rewarded.
*/
function totalValidatorKro(address validator) external view returns (uint128);

Expand Down Expand Up @@ -325,7 +329,7 @@ interface IAssetManager {
function canUndelegateKroAt(
address validator,
address delegator
) external view returns (uint256);
) external view returns (uint128);

/**
* @notice Returns the number of KGH delegated by the given delegator.
Expand Down Expand Up @@ -353,18 +357,21 @@ interface IAssetManager {
) external view returns (uint128);

/**
* @notice Returns when the KGH delegators can undelegate KGH. The delegators can undelegate
* after UNDELEGATION_DELAY_SECONDS elapsed from lastDelegatedAt.
* @notice Returns when the KGH delegators can undelegate KGH. The delegators can undelegate KGH
* for the given token id after UNDELEGATION_DELAY_SECONDS elapsed from delegation
* timestamp.
*
* @param validator Address of the validator.
* @param delegator Address of the KGH delegator.
* @param tokenId The token id of KGH to undelegate.
*
* @return When the KGH delegators can undelegate KGH.
* @return When the KGH delegators can undelegate KGH for the given token id.
*/
function canUndelegateKghAt(
address validator,
address delegator
) external view returns (uint256);
address delegator,
uint256 tokenId
) external view returns (uint128);

/**
* @notice Allows an on-chain or off-chain user to simulate the effects of their KRO delegation
Expand Down
43 changes: 15 additions & 28 deletions packages/contracts/contracts/L1/interfaces/IValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ interface IValidatorManager {
/**
* @notice Constructs the information of a validator.
*
* @custom:field isInitiated Whether the validator is initiated.
* @custom:field noSubmissionCount Number of counts that the validator did not submit the
* output in priority round.
* @custom:field commissionRate Commission rate of validator.
* @custom:field pendingCommissionRate Pending commission rate of validator.
* @custom:field commissionChangeInitTime Timestamp of commission change initialization.
* @custom:field isInitiated Whether the validator is initiated.
* @custom:field noSubmissionCount Number of counts that the validator did not submit
* the output in priority round.
* @custom:field commissionRate Commission rate of validator.
* @custom:field pendingCommissionRate Pending commission rate of validator.
* @custom:field commissionChangeInitiatedAt Timestamp of commission change initialization.
*/
struct Validator {
bool isInitiated;
uint8 noSubmissionCount;
uint8 commissionRate;
uint8 pendingCommissionRate;
uint128 commissionChangeInitTime;
uint128 commissionChangeInitiatedAt;
}

/**
Expand Down Expand Up @@ -145,21 +145,13 @@ interface IValidatorManager {
);

/**
* @notice Emitted when a validator is jailed when it does not submit outputs in priority round.
* @notice Emitted when a validator is jailed.
*
* @param validator Address of the validator.
* @param expiresAt The expiration timestamp of the jail.
*/
event ValidatorJailed(address indexed validator, uint128 expiresAt);

/**
* @notice Emitted when a validator is jailed by slashing.
*
* @param validator Address of the validator.
* @param expiresAt The expiration timestamp of the jail.
*/
event ValidatorJailedBySlashing(address indexed validator, uint128 expiresAt);

/**
* @notice Emitted when a validator is unjailed.
*
Expand All @@ -168,20 +160,20 @@ interface IValidatorManager {
event ValidatorUnjailed(address indexed validator);

/**
* @notice Emitted when validator KRO is reserved during output submission or challenge creation.
* @notice Emitted when validator KRO is bonded during output submission or challenge creation.
*
* @param validator Address of the validator.
* @param amount The amount of KRO reserved.
* @param amount The amount of KRO bonded.
*/
event ValidatorKroReserved(address indexed validator, uint128 amount);
event ValidatorKroBonded(address indexed validator, uint128 amount);

/**
* @notice Emitted when validator KRO is unreserved during output finalization or slashing.
* @notice Emitted when validator KRO is unbonded during output finalization or slashing.
*
* @param validator Address of the validator.
* @param amount The amount of KRO unreserved.
* @param amount The amount of KRO unbonded.
*/
event ValidatorKroUnreserved(address indexed validator, uint128 amount);
event ValidatorKroUnbonded(address indexed validator, uint128 amount);

/**
* @notice Emitted when the output reward is distributed.
Expand Down Expand Up @@ -257,11 +249,6 @@ interface IValidatorManager {
*/
error NotElapsedCommissionChangeDelay();

/**
* @notice Reverts when the address is zero address.
*/
error ZeroAddress();

/**
* @notice Reverts when try to unjail before jail period elapsed.
*/
Expand Down Expand Up @@ -380,7 +367,7 @@ interface IValidatorManager {
*
* @return When commission change of given validator can be finalized.
*/
function canFinalizeCommissionChangeAt(address validator) external view returns (uint256);
function canFinalizeCommissionChangeAt(address validator) external view returns (uint128);

/**
* @notice Checks the eligibility to submit L2 checkpoint output during output submission.
Expand Down

0 comments on commit e8ab665

Please sign in to comment.