Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-r4bbit committed May 3, 2024
1 parent 180dfd4 commit 8305c5e
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 113 deletions.
44 changes: 42 additions & 2 deletions contracts/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pragma solidity ^0.8.18;

import { console } from "forge-std/console.sol";
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
Expand Down Expand Up @@ -110,12 +111,22 @@ contract StakeManager is Ownable {
*/
modifier processEpoch() {
if (block.timestamp >= epochEnd() && address(migration) == address(0)) {
console.log("Processing epoch...");
console.log("--- currentEpoch: ", currentEpoch);

uint256 _epochReward = epochReward();
console.log("--- epochReward: ", _epochReward);
//finalize current epoch
epochs[currentEpoch].epochReward = epochReward();
epochs[currentEpoch].epochReward = _epochReward;
epochs[currentEpoch].totalSupply = totalSupply();
pendingReward += epochs[currentEpoch].epochReward;

console.log("--- totalSupply: ", totalSupply());
console.log("--- pendingReward: ", pendingReward);
//create new epoch
currentEpoch++;
console.log("Done. New epoch started: ", currentEpoch);
console.log("\n");
epochs[currentEpoch].startTime = block.timestamp;
}
_;
Expand All @@ -135,6 +146,7 @@ contract StakeManager is Ownable {
* @dev Reverts when resulting locked time is not in range of [MIN_LOCKUP_PERIOD, MAX_LOCKUP_PERIOD]
*/
function stake(uint256 _amount, uint256 _timeToIncrease) external onlyVault noMigration processEpoch {
console.log("--- Staking: ", _amount);
Account storage account = accounts[msg.sender];
if (account.lockUntil == 0) {
// account not initialized
Expand Down Expand Up @@ -226,6 +238,7 @@ contract StakeManager is Ownable {
* @param _limitEpoch Until what epoch it should be executed
*/
function executeAccount(address _vault, uint256 _limitEpoch) external onlyInitialized(_vault) processEpoch {
console.log("Processing account: ", _vault);
_processAccount(accounts[_vault], _limitEpoch);
}

Expand Down Expand Up @@ -345,14 +358,30 @@ contract StakeManager is Ownable {
uint256 userEpoch = account.epoch;
uint256 mpDifference = account.currentMP;
for (Epoch storage iEpoch = epochs[userEpoch]; userEpoch < _limitEpoch; userEpoch++) {
console.log("--- processing account epoch: ", userEpoch);
uint256 userSupply = account.balance + account.currentMP;
console.log("--- userSupply in epoch (before): ", userSupply);
console.log("------ account.balance: ", account.balance);
console.log("------ account.currentMP: ", account.currentMP);
console.log("--- epoch totalSupply (before): ", iEpoch.totalSupply);
console.log("------ Minting multiplier points for epoch...");
//mint multiplier points to that epoch
_mintMP(account, iEpoch.startTime + EPOCH_SIZE, iEpoch);
uint256 userSupply = account.balance + account.currentMP;
userSupply = account.balance + account.currentMP;
console.log("--- userSupply in epoch (after): ", userSupply);
console.log("------ account.balance: ", account.balance);
console.log("------ account.currentMP: ", account.currentMP);
console.log("--- epoch totalSupply (after): ", iEpoch.totalSupply);
uint256 userEpochReward = Math.mulDiv(userSupply, iEpoch.epochReward, iEpoch.totalSupply);
console.log("--- userEpochReward: ", userEpochReward);

userReward += userEpochReward;
iEpoch.epochReward -= userEpochReward;
console.log("--- removing epoch userSupply from epoch totalSupply: ", userSupply);
iEpoch.totalSupply -= userSupply;
console.log("--- New epoch epochReward: ", iEpoch.epochReward);
console.log("--- New epoch totalSupply: ", iEpoch.totalSupply);
console.log("\n");
}
account.epoch = userEpoch;
if (userReward > 0) {
Expand Down Expand Up @@ -392,6 +421,7 @@ contract StakeManager is Ownable {

//does not check for MAX_BOOST

console.log("--- Minting MP: ", increasedMP);
//update storage
totalSupplyMP += increasedMP;
account.initialMP += increasedMP;
Expand All @@ -413,6 +443,8 @@ contract StakeManager is Ownable {
account.currentMP
);

console.log("--------- increasedMP: ", increasedMP);

//update storage
account.lastMint = processTime;
account.currentMP += increasedMP;
Expand Down Expand Up @@ -482,4 +514,12 @@ contract StakeManager is Ownable {
function epochEnd() public view returns (uint256 _epochEnd) {
return epochs[currentEpoch].startTime + EPOCH_SIZE;
}

function getEpoch(uint256 _epoch) public view returns (Epoch memory) {
return epochs[_epoch];
}

function getAccount(address _account) public view returns (Account memory) {
return accounts[_account];
}
}
37 changes: 23 additions & 14 deletions contracts/StakeVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { StakeManager } from "./StakeManager.sol";
contract StakeVault is Ownable {
error StakeVault__MigrationNotAvailable();

error StakeVault__DepositCoolingDown();
error StakeVault__InDepositCooldown();

error StakeVault__InWithdrawCooldown();

error StakeVault__DepositFailed();

Expand All @@ -24,11 +26,11 @@ contract StakeVault is Ownable {

error StakeVault__InvalidLockTime();

event Deposit(uint256 indexed amount);
event Deposited(uint256 amount);

event Withdraw(uint256 indexed amount);
event Withdrawn(uint256 amount);

event Staked(address from, address to, uint256 _amount, uint256 time);
event Staked(uint256 _amount, uint256 time);

StakeManager private stakeManager;

Expand All @@ -40,9 +42,16 @@ contract StakeVault is Ownable {

uint256 public withdrawCooldownUntil;

modifier whenNotDepositCoolingDown() {
modifier whenNotInDepositCooldown() {
if (block.timestamp <= depositCooldownUntil) {
revert StakeVault__DepositCoolingDown();
revert StakeVault__InDepositCooldown();
}
_;
}

modifier whenNotInWithdrawCooldown() {
if (block.timestamp <= withdrawCooldownUntil) {
revert StakeVault__InWithdrawCooldown();
}
_;
}
Expand All @@ -61,18 +70,18 @@ contract StakeVault is Ownable {
stakeManager = _stakeManager;
}

function deposit(uint256 _amount) external onlyOwner whenNotDepositCoolingDown {
function deposit(uint256 _amount) external onlyOwner whenNotInDepositCooldown {
depositCooldownUntil = block.timestamp + stakeManager.DEPOSIT_COOLDOWN_PERIOD();
_deposit(msg.sender, _amount);
}

function withdraw(uint256 _amount) public onlyOwner onlySufficientBalance(_amount) {
function withdraw(uint256 _amount) public onlyOwner whenNotInWithdrawCooldown onlySufficientBalance(_amount) {
balance -= _amount;
bool success = STAKED_TOKEN.transfer(msg.sender, _amount);
if (!success) {
revert StakeVault__WithdrawFailed();
}
emit Withdraw(_amount);
emit Withdrawn(_amount);
}

function stake(
Expand All @@ -81,13 +90,13 @@ contract StakeVault is Ownable {
)
public
onlyOwner
whenNotDepositCoolingDown
whenNotInDepositCooldown
onlySufficientBalance(_amount)
{
_stake(_amount, _time);
}

function depositAndStake(uint256 _amount, uint256 _time) external onlyOwner whenNotDepositCoolingDown {
function depositAndStake(uint256 _amount, uint256 _time) external onlyOwner whenNotInDepositCooldown {
uint256 stakedBalance = _stakedBalance();
if (stakedBalance == 0 && _time == 0) {
// we expect `depositAndStake` to be called either with a lock time,
Expand All @@ -102,7 +111,7 @@ contract StakeVault is Ownable {
stakeManager.lock(_time);
}

function unstake(uint256 _amount) external onlyOwner {
function unstake(uint256 _amount) external onlyOwner whenNotInWithdrawCooldown {
withdrawCooldownUntil = block.timestamp + stakeManager.WITHDRAW_COOLDOWN_PERIOD();
stakeManager.unstake(_amount);
}
Expand Down Expand Up @@ -136,12 +145,12 @@ contract StakeVault is Ownable {
if (!success) {
revert StakeVault__DepositFailed();
}
emit Deposit(_amount);
emit Deposited(_amount);
}

function _stake(uint256 _amount, uint256 _time) internal {
stakeManager.stake(_amount, _time);
emit Staked(msg.sender, address(this), _amount, _time);
emit Staked(_amount, _time);
}

function _unstakedBalance() internal view returns (uint256) {
Expand Down
Loading

0 comments on commit 8305c5e

Please sign in to comment.