2023-05-16 21:59:25 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2023-07-24 15:16:03 +00:00
|
|
|
pragma solidity ^0.8.18;
|
2023-05-16 21:59:25 +00:00
|
|
|
|
2023-09-12 16:37:30 +00:00
|
|
|
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
|
|
|
|
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
|
|
|
import { StakeManager } from "./StakeManager.sol";
|
|
|
|
|
2023-06-21 14:20:23 +00:00
|
|
|
/**
|
|
|
|
* @title StakeVault
|
|
|
|
* @author Ricardo Guilherme Schmidt <ricardo3@status.im>
|
|
|
|
* @notice Secures user stake
|
|
|
|
*/
|
2023-09-12 16:37:30 +00:00
|
|
|
contract StakeVault is Ownable {
|
2023-11-07 08:38:00 +00:00
|
|
|
error StakeVault__MigrationNotAvailable();
|
|
|
|
|
2024-01-11 13:54:39 +00:00
|
|
|
error StakeVault__StakingFailed();
|
|
|
|
|
|
|
|
error StakeVault__UnstakingFailed();
|
|
|
|
|
2023-08-21 06:21:59 +00:00
|
|
|
StakeManager private stakeManager;
|
2023-11-07 08:40:38 +00:00
|
|
|
|
|
|
|
ERC20 private immutable STAKED_TOKEN;
|
2023-08-21 06:21:59 +00:00
|
|
|
|
|
|
|
event Staked(address from, address to, uint256 _amount, uint256 time);
|
|
|
|
|
|
|
|
constructor(address _owner, ERC20 _stakedToken, StakeManager _stakeManager) {
|
2023-06-25 14:44:12 +00:00
|
|
|
_transferOwnership(_owner);
|
2023-11-07 08:40:38 +00:00
|
|
|
STAKED_TOKEN = _stakedToken;
|
2023-08-21 06:21:59 +00:00
|
|
|
stakeManager = _stakeManager;
|
2023-05-16 21:59:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-25 14:44:12 +00:00
|
|
|
function stake(uint256 _amount, uint256 _time) external onlyOwner {
|
2024-01-11 13:54:39 +00:00
|
|
|
bool success = STAKED_TOKEN.transferFrom(msg.sender, address(this), _amount);
|
|
|
|
if (!success) {
|
|
|
|
revert StakeVault__StakingFailed();
|
|
|
|
}
|
2023-06-25 14:44:12 +00:00
|
|
|
stakeManager.stake(_amount, _time);
|
2023-08-21 06:21:59 +00:00
|
|
|
|
|
|
|
emit Staked(msg.sender, address(this), _amount, _time);
|
2023-05-24 13:25:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 21:38:55 +00:00
|
|
|
function lock(uint256 _time) external onlyOwner {
|
|
|
|
stakeManager.lock(_time);
|
|
|
|
}
|
|
|
|
|
2023-06-25 14:44:12 +00:00
|
|
|
function unstake(uint256 _amount) external onlyOwner {
|
|
|
|
stakeManager.unstake(_amount);
|
2024-01-11 13:54:39 +00:00
|
|
|
bool success = STAKED_TOKEN.transfer(msg.sender, _amount);
|
|
|
|
if (!success) {
|
|
|
|
revert StakeVault__UnstakingFailed();
|
|
|
|
}
|
2023-05-24 13:25:52 +00:00
|
|
|
}
|
2023-05-16 21:59:25 +00:00
|
|
|
|
2023-06-26 15:26:02 +00:00
|
|
|
function leave() external onlyOwner {
|
|
|
|
stakeManager.leave();
|
2023-11-07 08:40:38 +00:00
|
|
|
STAKED_TOKEN.transferFrom(address(this), msg.sender, STAKED_TOKEN.balanceOf(address(this)));
|
2023-06-26 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
2023-06-25 14:44:12 +00:00
|
|
|
/**
|
|
|
|
* @notice Opt-in migration to a new StakeManager contract.
|
|
|
|
*/
|
2023-06-23 21:38:55 +00:00
|
|
|
function updateManager() external onlyOwner {
|
2023-06-23 23:01:59 +00:00
|
|
|
StakeManager migrated = stakeManager.migrate();
|
2023-11-07 08:38:00 +00:00
|
|
|
if (address(migrated) == address(0)) revert StakeVault__MigrationNotAvailable();
|
2023-06-23 21:38:55 +00:00
|
|
|
stakeManager = migrated;
|
|
|
|
}
|
2023-11-07 08:40:38 +00:00
|
|
|
|
|
|
|
function stakedToken() external view returns (ERC20) {
|
|
|
|
return STAKED_TOKEN;
|
|
|
|
}
|
2023-09-12 16:37:30 +00:00
|
|
|
}
|