Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/implement-1step-kro-undeleg…
Browse files Browse the repository at this point in the history
…ation' into feat/implement-1step-kgh-undelegation

# Conflicts:
#	packages/contracts/contracts/L1/AssetManager.sol
  • Loading branch information
0xHansLee committed Jul 22, 2024
2 parents 94b692b + 32a4cbd commit f8747f8
Showing 1 changed file with 39 additions and 41 deletions.
80 changes: 39 additions & 41 deletions packages/contracts/contracts/L1/AssetManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,29 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager {
/**
* @notice Constructs the AssetManager contract.
*
* @param _assetToken Address of the KRO token.
* @param _kgh Address of the KGH token.
* @param _kghManager Address of the KGHManager contract.
* @param _securityCouncil Address of the SecurityCouncil contract.
* @param _validatorManager Address of the ValidatorManager contract.
* @param _undelegationPeriod Period that should wait to finalize the undelegation.
* @param _bondAmount Amount to bond.
* @param _assetToken Address of the KRO token.
* @param _kgh Address of the KGH token.
* @param _kghManager Address of the KGHManager contract.
* @param _securityCouncil Address of the SecurityCouncil contract.
* @param _validatorManager Address of the ValidatorManager contract.
* @param _minDelegationPeriod Minimum delegation period.
* @param _bondAmount Amount to bond.
*/
constructor(
IERC20 _assetToken,
IERC721 _kgh,
IKGHManager _kghManager,
address _securityCouncil,
IValidatorManager _validatorManager,
uint128 _undelegationPeriod,
uint128 _minDelegationPeriod,
uint128 _bondAmount
) {
ASSET_TOKEN = _assetToken;
KGH = _kgh;
KGH_MANAGER = _kghManager;
SECURITY_COUNCIL = _securityCouncil;
VALIDATOR_MANAGER = _validatorManager;
UNDELEGATION_PERIOD = _undelegationPeriod;
MIN_DELEGATION_PERIOD = _minDelegationPeriod;
BOND_AMOUNT = _bondAmount;
}

Expand Down Expand Up @@ -172,6 +172,31 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager {
return _convertToKroAssets(validator, shares);
}

/**
* @inheritdoc IAssetManager
*/
function canUndelegateKroAt(
address validator,
address delegator
) external view returns (uint128) {
return
_vaults[validator].kroDelegators[delegator].lastDelegatedAt +
uint128(MIN_DELEGATION_PERIOD);
}

/**
* @inheritdoc IAssetManager
*/
function canUndelegateKghAt(
address validator,
address delegator,
uint256 tokenId
) external view returns (uint128) {
return
_vaults[validator].kghDelegators[delegator].delegatedAt[tokenId] +
uint128(MIN_DELEGATION_PERIOD);
}

/**
* @inheritdoc IAssetManager
*/
Expand Down Expand Up @@ -241,31 +266,6 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager {
return _vaults[validator].asset.validatorRewardKro;
}

/**
* @inheritdoc IAssetManager
*/
function canUndelegateKroAt(
address validator,
address delegator
) external view returns (uint128) {
return
_vaults[validator].kroDelegators[delegator].lastDelegatedAt +
uint128(MIN_DELEGATION_PERIOD);
}

/**
* @inheritdoc IAssetManager
*/
function canUndelegateKghAt(
address validator,
address delegator,
uint256 tokenId
) external view returns (uint128) {
return
_vaults[validator].kghDelegators[delegator].delegatedAt[tokenId] +
uint128(MIN_DELEGATION_PERIOD);
}

/**
* @notice Returns the reflective weight of given validator. It can be different from the actual
* current weight of the validator in validator tree since it includes all accumulated
Expand Down Expand Up @@ -417,14 +417,14 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager {
/**
* @inheritdoc IAssetManager
*/
function undelegate(address validator, uint128 shares) external {
function undelegate(address validator, uint128 assets) external {
if (assets == 0) revert InsufficientAsset();
uint128 shares = _convertToKroShares(validator, assets);
if (shares == 0) revert NotAllowedZeroInput();
if (shares > _vaults[validator].kroDelegators[msg.sender].shares)
revert InsufficientShare();

uint128 assets = _convertToKroAssets(validator, shares);
if (assets == 0) revert InsufficientAsset();
if (canUndelegateKroAt(validator, msg.sender) > uint128(block.timestamp))
if (canUndelegateKroAt(validator, msg.sender) > block.timestamp)
revert NotElapsedMinDelegationPeriod();

_undelegate(validator, msg.sender, assets, shares);
Expand Down Expand Up @@ -867,8 +867,6 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager {
* @param delegator Address of the delegator.
* @param assets The amount of KRO to delegate.
* @param shares The amount of shares to delegate.
*
* @return The amount of shares that the Vault would exchange for the amount of assets provided.
*/
function _delegate(
address validator,
Expand Down Expand Up @@ -964,8 +962,8 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager {

unchecked {
vault.asset.totalKroShares -= shares;
vault.kroDelegators[delegator].shares -= shares;
vault.asset.totalKro -= assets;
vault.kroDelegators[delegator].shares -= shares;
}
}

Expand Down

0 comments on commit f8747f8

Please sign in to comment.