Skip to content

Commit

Permalink
Added delegateAllTo function to HelioProviderV2
Browse files Browse the repository at this point in the history
  • Loading branch information
lawsonccy committed Sep 24, 2024
1 parent db70c00 commit e27c26e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
1 change: 1 addition & 0 deletions contracts/ceros/interfaces/IDao.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ interface IDao {

function collaterals(address token) external view returns (GemJoinLike, bytes32, uint32, address);

function locked(address token, address usr) external view returns (uint256);
}
2 changes: 1 addition & 1 deletion contracts/ceros/interfaces/IHelioProviderV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ interface IHelioProviderV2 {
// in BNB
function provide() external payable returns (uint256);
function provide(address _delegateTo) external payable returns (uint256);
function changeDelegatee(address _delegateTo) external;
function delegateAllTo(address _newDelegateTo) external;

// in aBNBc
// function provideInABNBc(uint256 amount) external returns (uint256);
Expand Down
4 changes: 4 additions & 0 deletions contracts/ceros/mocks/Dao.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ contract Dao is IDao {

function collaterals(address token) external view returns (GemJoinLike gem, bytes32 ilk, uint32 live, address clip) {}

function locked(address token, address usr) public view returns (uint256) {
return 0;
}

}
47 changes: 32 additions & 15 deletions contracts/ceros/upgrades/HelioProviderV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,45 @@ ReentrancyGuardUpgradeable
}

/**
* CHANGE DELEGATEE
* @notice By burning all the collateral tokens from the
* old delegatee and minting the same amount to the new delegatee,
* also replace delegateTo address by the new one to perform the change.
* @notice Delegate all clisBNB to a new delegatee or self
new delegatee can be self or anyone else, but not zero address
* @param _newDelegateTo address of the new delegatee
*/
function changeDelegatee(address _newDelegateTo) external override whenNotPaused {
function delegateAllTo(address _newDelegateTo) external override whenNotPaused {
require(_newDelegateTo != address(0), "delegateTo cannot be zero address");
require(
_delegation[msg.sender].amount > 0 && _delegation[msg.sender].delegateTo != _newDelegateTo,
"delegatee must differ from the current one"
);
// get user total deposit
uint256 totalLocked = _dao.locked(_ceToken, msg.sender);

Delegation storage delegation = _delegation[msg.sender];
address oldDelegateTo = delegation.delegateTo;
// burn old delegatee's token
_collateralToken.burn(oldDelegateTo, delegation.amount);
// mint to new delegatee
_collateralToken.mint(_newDelegateTo, delegation.amount);
// change delegation info
delegation.delegateTo = _newDelegateTo;

// Step 1. burn all tokens
if (delegation.amount > 0) {
// burn delegatee's token
_collateralToken.burn(oldDelegateTo, delegation.amount);
// burn self's token
_collateralToken.burn(msg.sender, totalLocked - delegation.amount);
} else {
_collateralToken.burn(msg.sender, totalLocked);
}

// Step 2. save new delegatee and mint all tokens to delegatee
if (_newDelegateTo == msg.sender) {
// mint all to self
_collateralToken.mint(msg.sender, totalLocked);
// remove delegatee
delete _delegation[msg.sender];
} else {
// mint all to new delegatee
_collateralToken.mint(_newDelegateTo, totalLocked);
// save delegatee's info
delegation.delegateTo = _newDelegateTo;
delegation.amount = totalLocked;
}

emit ChangeDelegateTo(msg.sender, oldDelegateTo, _newDelegateTo);
}

/**
* RELEASE
*/
Expand Down

0 comments on commit e27c26e

Please sign in to comment.