Skip to content

Commit

Permalink
Merge pull request #18 from devlancer412/test/minter
Browse files Browse the repository at this point in the history
Added test for RewardsReceiver and SharedDepositMinterV2
  • Loading branch information
chimera-defi authored Jun 12, 2024
2 parents f807bbb + 135139e commit a999e52
Show file tree
Hide file tree
Showing 8 changed files with 647 additions and 67 deletions.
128 changes: 64 additions & 64 deletions contracts/lib/xERC4626.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,95 +12,95 @@ import {IxERC4626} from "../interfaces/IxERC4626.sol";

// Rewards logic inspired by xERC20 (https://github.com/ZeframLou/playpen/blob/main/src/xERC20.sol)

/**
/**
@title An xERC4626 Single Staking Contract
@notice This contract allows users to autocompound rewards denominated in an underlying reward token.
@notice This contract allows users to autocompound rewards denominated in an underlying reward token.
It is fully compatible with [ERC4626](https://eips.ethereum.org/EIPS/eip-4626) allowing for DeFi composability.
It maintains balances using internal accounting to prevent instantaneous changes in the exchange rate.
NOTE: an exception is at contract creation, when a reward cycle begins before the first deposit. After the first deposit, exchange rate updates smoothly.
Operates on "cycles" which distribute the rewards surplus over the internal balance to users linearly over the remainder of the cycle window.
*/
abstract contract xERC4626 is IxERC4626, ERC4626 {
using SafeCastLib for *;
using SafeCastLib for *;

/// @notice the maximum length of a rewards cycle
uint32 public immutable rewardsCycleLength;
/// @notice the maximum length of a rewards cycle
uint32 public immutable rewardsCycleLength;

/// @notice the effective start of the current cycle
uint32 public lastSync;
/// @notice the effective start of the current cycle
uint32 public lastSync;

/// @notice the end of the current cycle. Will always be evenly divisible by `rewardsCycleLength`.
uint32 public rewardsCycleEnd;
/// @notice the end of the current cycle. Will always be evenly divisible by `rewardsCycleLength`.
uint32 public rewardsCycleEnd;

/// @notice the amount of rewards distributed in a the most recent cycle.
uint192 public lastRewardAmount;
/// @notice the amount of rewards distributed in a the most recent cycle.
uint192 public lastRewardAmount;

uint256 internal storedTotalAssets;
uint256 internal storedTotalAssets;

constructor(uint32 _rewardsCycleLength) {
rewardsCycleLength = _rewardsCycleLength;
// seed initial rewardsCycleEnd
rewardsCycleEnd = (block.timestamp.safeCastTo32() / rewardsCycleLength) * rewardsCycleLength;
}
constructor(uint32 _rewardsCycleLength) {
rewardsCycleLength = _rewardsCycleLength;
// seed initial rewardsCycleEnd
rewardsCycleEnd = (block.timestamp.safeCastTo32() / rewardsCycleLength) * rewardsCycleLength;
}

/// @notice Compute the amount of tokens available to share holders.
/// Increases linearly during a reward distribution period from the sync call, not the cycle start.
function totalAssets() public view override returns (uint256) {
// cache global vars
uint256 storedTotalAssets_ = storedTotalAssets;
uint192 lastRewardAmount_ = lastRewardAmount;
uint32 rewardsCycleEnd_ = rewardsCycleEnd;
uint32 lastSync_ = lastSync;
/// @notice Compute the amount of tokens available to share holders.
/// Increases linearly during a reward distribution period from the sync call, not the cycle start.
function totalAssets() public view override returns (uint256) {
// cache global vars
uint256 storedTotalAssets_ = storedTotalAssets;
uint192 lastRewardAmount_ = lastRewardAmount;
uint32 rewardsCycleEnd_ = rewardsCycleEnd;
uint32 lastSync_ = lastSync;

if (block.timestamp >= rewardsCycleEnd_) {
// no rewards or rewards fully unlocked
// entire reward amount is available
return storedTotalAssets_ + lastRewardAmount_;
}

// rewards not fully unlocked
// add unlocked rewards to stored total
uint256 unlockedRewards = (lastRewardAmount_ * (block.timestamp - lastSync_)) / (rewardsCycleEnd_ - lastSync_);
return storedTotalAssets_ + unlockedRewards;
}

if (block.timestamp >= rewardsCycleEnd_) {
// no rewards or rewards fully unlocked
// entire reward amount is available
return storedTotalAssets_ + lastRewardAmount_;
// Update storedTotalAssets on withdraw/redeem
function beforeWithdraw(uint256 amount, uint256 shares) internal virtual override {
super.beforeWithdraw(amount, shares);
storedTotalAssets -= amount;
}

// rewards not fully unlocked
// add unlocked rewards to stored total
uint256 unlockedRewards = (lastRewardAmount_ * (block.timestamp - lastSync_)) / (rewardsCycleEnd_ - lastSync_);
return storedTotalAssets_ + unlockedRewards;
}
// Update storedTotalAssets on deposit/mint
function afterDeposit(uint256 amount, uint256 shares) internal virtual override {
storedTotalAssets += amount;
super.afterDeposit(amount, shares);
}

// Update storedTotalAssets on withdraw/redeem
function beforeWithdraw(uint256 amount, uint256 shares) internal virtual override {
super.beforeWithdraw(amount, shares);
storedTotalAssets -= amount;
}
/// @notice Distributes rewards to xERC4626 holders.
/// All surplus `asset` balance of the contract over the internal balance becomes queued for the next cycle.
function syncRewards() public virtual {
uint192 lastRewardAmount_ = lastRewardAmount;
uint32 timestamp = block.timestamp.safeCastTo32();

// Update storedTotalAssets on deposit/mint
function afterDeposit(uint256 amount, uint256 shares) internal virtual override {
storedTotalAssets += amount;
super.afterDeposit(amount, shares);
}
if (timestamp < rewardsCycleEnd) revert SyncError();

/// @notice Distributes rewards to xERC4626 holders.
/// All surplus `asset` balance of the contract over the internal balance becomes queued for the next cycle.
function syncRewards() public virtual {
uint192 lastRewardAmount_ = lastRewardAmount;
uint32 timestamp = block.timestamp.safeCastTo32();
uint256 storedTotalAssets_ = storedTotalAssets;
uint256 nextRewards = asset.balanceOf(address(this)) - storedTotalAssets_ - lastRewardAmount_;

if (timestamp < rewardsCycleEnd) revert SyncError();
storedTotalAssets = storedTotalAssets_ + lastRewardAmount_; // SSTORE

uint256 storedTotalAssets_ = storedTotalAssets;
uint256 nextRewards = asset.balanceOf(address(this)) - storedTotalAssets_ - lastRewardAmount_;
uint32 end = ((timestamp + rewardsCycleLength) / rewardsCycleLength) * rewardsCycleLength;

storedTotalAssets = storedTotalAssets_ + lastRewardAmount_; // SSTORE
if (end - timestamp < rewardsCycleLength / 20) {
end += rewardsCycleLength;
}

uint32 end = ((timestamp + rewardsCycleLength) / rewardsCycleLength) * rewardsCycleLength;
// Combined single SSTORE
lastRewardAmount = nextRewards.safeCastTo192();
lastSync = timestamp;
rewardsCycleEnd = end;

if (end - timestamp < rewardsCycleLength / 20) {
end += rewardsCycleLength;
emit NewRewardsCycle(end, nextRewards);
}

// Combined single SSTORE
lastRewardAmount = nextRewards.safeCastTo192();
lastSync = timestamp;
rewardsCycleEnd = end;

emit NewRewardsCycle(end, nextRewards);
}
}
2 changes: 1 addition & 1 deletion contracts/v2/core/WSGEth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ contract WSGETH is xERC4626, ReentrancyGuard {
uint8 v,
bytes32 r,
bytes32 s
) external nonReentrant returns (uint256 shares) {
) external returns (uint256 shares) {
uint256 amount = approveMax ? type(uint256).max : assets;
asset.permit(msg.sender, address(this), amount, deadline, v, r, s);
return (deposit(assets, receiver));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"@fei-protocol/erc4626": "^0.0.0",
"@nomicfoundation/hardhat-network-helpers": "^1.0.11",
"@openzeppelin/contracts": "^4.8.0",
"@openzeppelin/contracts-upgradeable": "^4.3.1",
"dotenv": "^16.1.4",
Expand Down
2 changes: 1 addition & 1 deletion test/v2/core/e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("e2e test", () => {
const adminFee = 0;

const addresses = [
paymentSplitter.address, // fee splitter
ethers.constants.AddressZero, // fee splitter
sgEth.address, // sgETH address
wsgETH.address, // wsgETH address
multiSig.address, // government address
Expand Down
Loading

0 comments on commit a999e52

Please sign in to comment.