RebasedStakedCelo.sol (wrap & unwrap)
This is a wrapper token (ticker: rstCELO) around stCELO that, instead of accruing value to each token as staking rewards accrue in the pool, rebases balances, such that an account's balance always represents the amount of CELO that could be withdrawn for the underlying stCELO. Thus, the value of one unit of rstCELO and one unit of CELO should be approximately equivalent.
Methods
Initialize
Replaces the constructor for proxy implementation.
Copy function initialize(
address _stakedCelo,
address _account,
address _owner
) external initializer {
__ERC20_init("Rebased Staked CELO", "rstCELO");
_transferOwnership(_owner);
stakedCelo = IStakedCelo(_stakedCelo);
account = IAccount(_account);
}
Deposit
Deposit stCELO in return for rstCELO. Although rstCELO is never minted to any account, the rstCELO balance is calculated based on the account's deposited stCELO. See `balanceOf()` function below.
Copy function deposit(uint256 stCeloAmount) external {
if (stCeloAmount == 0) {
revert ZeroAmount();
}
totalDeposit += stCeloAmount;
stakedCeloBalance[msg.sender] += stCeloAmount;
emit StakedCeloDeposited(msg.sender, stCeloAmount);
if (!stakedCelo.transferFrom(msg.sender, address(this), stCeloAmount)) {
revert FailedDeposit(msg.sender, stCeloAmount);
}
}
Withdraw
Withdraws stCELO. This function transfers back some or all of the sender's. previously deposited stCELO.
Copy function withdraw(uint256 stCeloAmount) external {
if (stCeloAmount > stakedCeloBalance[msg.sender]) {
revert InsufficientBalance(stCeloAmount);
}
totalDeposit -= stCeloAmount;
unchecked {
stakedCeloBalance[msg.sender] -= stCeloAmount;
}
emit StakedCeloWithdrawn(msg.sender, stCeloAmount);
if (!stakedCelo.transfer(msg.sender, stCeloAmount)) {
revert FailedWithdrawal(msg.sender, stCeloAmount);
}
}
totalSupply
Used to query the total supply of rstCELO. The calculated total supply of rstCELO.
Copy function totalSupply() public view virtual override returns (uint256) {
return toRebasedStakedCelo(totalDeposit);
}
balanceOf
Used to query the rstCELO balance of an address.
Copy function balanceOf(address _account) public view override returns (uint256) {
return toRebasedStakedCelo(stakedCeloBalance[_account]);
}
toStakedCelo
Computes the amount of stCELO that is represented by an amount of rstCELO.
Copy function toStakedCelo(uint256 rstCeloAmount) public view returns (uint256) {
uint256 stCeloSupply = stakedCelo.totalSupply();
uint256 celoBalance = account.getTotalCelo();
if (stCeloSupply == 0 || celoBalance == 0) {
return rstCeloAmount;
}
return (rstCeloAmount * stCeloSupply) / celoBalance;
}
toRebasedStakedCelo
Computes the amount of rstCELO that is represented by an amount of stCELO.
Copy function toRebasedStakedCelo(uint256 stCeloAmount) public view returns (uint256) {
uint256 stCeloSupply = stakedCelo.totalSupply();
uint256 celoBalance = account.getTotalCelo();
if (stCeloSupply == 0 || celoBalance == 0) {
return stCeloAmount;
}
return (stCeloAmount * celoBalance) / stCeloSupply;
}
_transfer
Moves `amount` of rstCELO from `sender` to `recipient`.
Copy function _transfer(
address from,
address to,
uint256 amount
) internal override {
if (from == address(0)) {
revert NullAddress();
}
if (to == address(0)) {
revert NullAddress();
}
uint256 fromBalance = stakedCeloBalance[from];
uint256 equivalentStakedCeloAmount = toStakedCelo(amount);
if (fromBalance < equivalentStakedCeloAmount) {
revert InsufficientBalance(amount);
}
unchecked {
stakedCeloBalance[from] = fromBalance - equivalentStakedCeloAmount;
}
stakedCeloBalance[to] += equivalentStakedCeloAmount;
emit Transfer(from, to, amount);
}
}