-
Notifications
You must be signed in to change notification settings - Fork 19
Add SDRewardManager (ETHx Merkle Automation) #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
534f7c5
feat: add sd reward manager contract
blockgroot bdf19c4
test: add tests and scripts
blockgroot cacf569
fix: permissions
blockgroot b82cdf2
chore: allow manager to grant and revoke permissions
blockgroot 73a1415
Revert "chore: allow manager to grant and revoke permissions"
blockgroot 0dbfe22
fix: call internal function and allow manager to grant and revoke per…
blockgroot 64a43a8
feat: allow overriding last non approved entry
blockgroot 49d6513
refactor: test file name
blockgroot 27fc0bc
refactor: use legacy mechanism to check access control
blockgroot 837cf25
feat: fetch cycle number from pool
blockgroot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
pragma solidity 0.8.16; | ||
|
||
import { Initializable } from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; | ||
import { SafeERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol"; | ||
import { IERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol"; | ||
import { IStaderConfig } from "./interfaces/IStaderConfig.sol"; | ||
import { ISocializingPool } from "./interfaces/ISocializingPool.sol"; | ||
import { UtilLib } from "./library/UtilLib.sol"; | ||
|
||
/** | ||
* @title SDRewardManager | ||
* @notice This contract is responsible to add SD rewards to the socializing pool | ||
*/ | ||
contract SDRewardManager is Initializable { | ||
using SafeERC20Upgradeable for IERC20Upgradeable; | ||
|
||
struct SDRewardEntry { | ||
uint256 cycleNumber; | ||
uint256 amount; | ||
bool approved; | ||
} | ||
|
||
///@notice Address of the Stader Config contract | ||
IStaderConfig public staderConfig; | ||
|
||
///@notice Cycle number of the last added entry | ||
uint256 public lastEntryCycleNumber; | ||
|
||
// Mapping of cycle numbers to reward entries | ||
mapping(uint256 => SDRewardEntry) public rewardEntries; | ||
|
||
// Event emitted when a new reward entry is created | ||
event NewRewardEntry(uint256 indexed cycleNumber, uint256 amount); | ||
|
||
// Event emitted when a reward entry is approved | ||
event RewardEntryApproved(uint256 indexed cycleNumber, uint256 amount); | ||
|
||
error AccessDenied(address account); | ||
error EntryNotFound(uint256 cycleNumber); | ||
error EntryAlreadyApproved(uint256 cycleNumber); | ||
|
||
/// @custom:oz-upgrades-unsafe-allow constructor | ||
constructor() { | ||
_disableInitializers(); | ||
} | ||
|
||
/** | ||
* @notice Initializes the contract with a Stader configuration address | ||
* @param _staderConfig Address of the StaderConfig contract | ||
*/ | ||
function initialize(address _staderConfig) external initializer { | ||
UtilLib.checkNonZeroAddress(_staderConfig); | ||
staderConfig = IStaderConfig(_staderConfig); | ||
} | ||
|
||
/** | ||
* @notice Adds a new reward entry for the current cycle (fetched from socializing pool) | ||
* @param _amount The amount of SD to be rewarded | ||
*/ | ||
function addRewardEntry(uint256 _amount) external { | ||
if (!staderConfig.onlySDRewardEntryRole(msg.sender)) { | ||
revert AccessDenied(msg.sender); | ||
} | ||
uint256 cycleNumber = getCurrentCycleNumber(); | ||
SDRewardEntry memory rewardEntry = rewardEntries[cycleNumber]; | ||
|
||
if (rewardEntry.approved) { | ||
revert EntryAlreadyApproved(cycleNumber); | ||
} | ||
|
||
rewardEntry.cycleNumber = cycleNumber; | ||
rewardEntry.amount = _amount; | ||
lastEntryCycleNumber = cycleNumber; | ||
rewardEntries[cycleNumber] = rewardEntry; | ||
|
||
emit NewRewardEntry(cycleNumber, _amount); | ||
} | ||
|
||
/** | ||
* @notice Approves a reward entry for the current cycle (fetched from socializing pool) and transfers the reward amount. | ||
*/ | ||
function approveEntry() external { | ||
if (!staderConfig.onlySDRewardApproverRole(msg.sender)) { | ||
revert AccessDenied(msg.sender); | ||
} | ||
|
||
uint256 cycleNumber = getCurrentCycleNumber(); | ||
|
||
SDRewardEntry storage rewardEntry = rewardEntries[cycleNumber]; | ||
|
||
if (rewardEntry.cycleNumber == 0) { | ||
revert EntryNotFound(cycleNumber); | ||
} | ||
|
||
if (rewardEntry.approved) { | ||
revert EntryAlreadyApproved(cycleNumber); | ||
} | ||
|
||
rewardEntry.approved = true; | ||
|
||
if (rewardEntry.amount > 0) { | ||
IERC20Upgradeable(staderConfig.getStaderToken()).safeTransferFrom( | ||
msg.sender, | ||
staderConfig.getPermissionlessSocializingPool(), | ||
rewardEntry.amount | ||
); | ||
emit RewardEntryApproved(cycleNumber, rewardEntry.amount); | ||
} | ||
} | ||
|
||
/** | ||
* @notice Returns the latest reward entry | ||
* @return The latest SDRewardEntry struct for the most recent cycle | ||
*/ | ||
function viewLatestEntry() external view returns (SDRewardEntry memory) { | ||
return rewardEntries[lastEntryCycleNumber]; | ||
} | ||
|
||
/** | ||
* @notice Fetch the current cycle number from permissionless socializing pool | ||
* @return Current cycle number | ||
*/ | ||
function getCurrentCycleNumber() public view returns (uint256) { | ||
return ISocializingPool(staderConfig.getPermissionlessSocializingPool()).getCurrentRewardsIndex(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ethers, upgrades } from 'hardhat' | ||
|
||
async function main() { | ||
const [owner] = await ethers.getSigners() | ||
const staderConfigAddr = process.env.STADER_CONFIG ?? '' | ||
|
||
const sdRewardManagerFactory = await ethers.getContractFactory('SDRewardManager') | ||
const sdRewardManager = await upgrades.deployProxy(sdRewardManagerFactory, [staderConfigAddr]) | ||
console.log('SDRewardManager deployed to: ', sdRewardManager.address) | ||
} | ||
|
||
main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.