-
Notifications
You must be signed in to change notification settings - Fork 4
/
Minter.sol
188 lines (157 loc) · 6.77 KB
/
Minter.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// SPDX-License-Identifier: GPL-3
pragma solidity ^0.8.15;
import "src/interfaces/IMinter.sol";
import "src/interfaces/IRewardsDistributor.sol";
import "src/interfaces/IVoter.sol";
import "src/interfaces/IVotingEscrow.sol";
import "src/interfaces/IAlchemixToken.sol";
import "src/interfaces/IRevenueHandler.sol";
import "src/interfaces/synthetix/IStakingRewards.sol";
import "src/libraries/Math.sol";
import "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import "openzeppelin-contracts/contracts/utils/math/SafeMath.sol";
/**
* @title Minter
* @notice Contract to handle ALCX emissions and their distriubtion
*/
contract Minter is IMinter {
using SafeERC20 for IERC20;
using SafeMath for uint256;
// Allows minting once per epoch (epoch = 2 week, reset every Thursday 00:00 UTC)
uint256 public immutable DURATION = 2 weeks;
uint256 public immutable BPS = 10_000;
/// @notice The ALCX tail emissions rate
uint256 public constant TAIL_EMISSIONS_RATE = 2194e18;
/// @notice Amount of emissions minted per epoch
uint256 public epochEmissions;
/// @notice The active period
uint256 public activePeriod;
/// @notice The stepdown rate of emissions
uint256 public stepdown;
/// @notice The current amount of ALCX rewards
uint256 public rewards;
/// @notice The current circulating supply of ALCX emissions
uint256 public supply;
/// @notice The bps of emissions going to veALCX holders
uint256 public veAlcxEmissionsRate;
/// @notice The bps of emissions going to TIME stakers
uint256 public timeEmissionsRate;
/// @notice The bps of emissions going to treasury
uint256 public treasuryEmissionsRate;
address public admin;
address public pendingAdmin;
address public initializer;
address public treasury;
IAlchemixToken public immutable alcx;
IVoter public immutable voter;
IVotingEscrow public immutable ve;
IRewardsDistributor public immutable rewardsDistributor;
IRevenueHandler public immutable revenueHandler;
IStakingRewards public immutable timeGauge;
constructor(InitializationParams memory params) {
stepdown = params.stepdown;
rewards = params.rewards;
supply = params.supply;
initializer = msg.sender;
admin = msg.sender;
alcx = IAlchemixToken(params.alcx);
voter = IVoter(params.voter);
ve = IVotingEscrow(params.ve);
rewardsDistributor = IRewardsDistributor(params.rewardsDistributor);
revenueHandler = IRevenueHandler(params.revenueHandler);
timeGauge = IStakingRewards(params.timeGauge);
treasury = params.treasury;
activePeriod = ((block.timestamp + DURATION) / DURATION) * DURATION;
veAlcxEmissionsRate = 2500; // 25%
timeEmissionsRate = 2000; // 20%
treasuryEmissionsRate = 1500; // 15%
}
/*
View functions
*/
/// @inheritdoc IMinter
function epochEmission() public view returns (uint256) {
return rewards - stepdown;
}
/// @inheritdoc IMinter
function circulatingEmissionsSupply() public view returns (uint256) {
return supply;
}
/// @inheritdoc IMinter
function calculateEmissions(uint256 _emissions, uint256 _rate) public pure returns (uint256) {
return _emissions.mul(_rate).div(BPS);
}
function initialize() external {
require(initializer == msg.sender, "not initializer");
require(msg.sender != address(0), "already initialized");
initializer = address(0);
}
/*
External functions
*/
function setAdmin(address _admin) external {
require(msg.sender == admin, "not admin");
pendingAdmin = _admin;
}
function acceptAdmin() external {
require(msg.sender == pendingAdmin, "not pending admin");
admin = pendingAdmin;
emit AdminUpdated(pendingAdmin);
}
/// @inheritdoc IMinter
function setTreasury(address _treasury) external {
require(msg.sender == admin, "not admin");
require(_treasury != address(0), "treasury cannot be 0x0");
treasury = _treasury;
emit TreasuryUpdated(_treasury);
}
/// @inheritdoc IMinter
function setVeAlcxEmissionsRate(uint256 _veAlcxEmissionsRate) external {
require(msg.sender == admin, "not admin");
require(_veAlcxEmissionsRate <= BPS, "cannot be greater than 100%");
veAlcxEmissionsRate = _veAlcxEmissionsRate;
emit SetVeAlcxEmissionsRate(_veAlcxEmissionsRate);
}
/// @inheritdoc IMinter
function updatePeriod() external returns (uint256) {
require(msg.sender == address(voter), "not voter");
uint256 period = activePeriod;
if (block.timestamp >= period + DURATION && initializer == address(0)) {
// Only trigger if new epoch
period = (block.timestamp / DURATION) * DURATION;
activePeriod = period;
epochEmissions = epochEmission();
uint256 veAlcxEmissions = calculateEmissions(epochEmissions, veAlcxEmissionsRate);
uint256 timeEmissions = calculateEmissions(epochEmissions, timeEmissionsRate);
uint256 treasuryEmissions = calculateEmissions(epochEmissions, treasuryEmissionsRate);
uint256 gaugeEmissions = epochEmissions.sub(veAlcxEmissions).sub(timeEmissions).sub(treasuryEmissions);
uint256 balanceOf = alcx.balanceOf(address(this));
if (balanceOf < epochEmissions) alcx.mint(address(this), epochEmissions - balanceOf);
// Set rewards for next epoch
rewards -= stepdown;
// Adjust updated emissions total
supply += rewards;
// Once we reach the emissions tail stepdown is 0
if (rewards <= TAIL_EMISSIONS_RATE) {
stepdown = 0;
}
// If there are no votes, send emissions to veALCX holders
if (voter.totalWeight() > 0) {
alcx.approve(address(voter), gaugeEmissions);
voter.notifyRewardAmount(gaugeEmissions);
} else {
veAlcxEmissions += gaugeEmissions;
}
// Logic to distrubte minted tokens
IERC20(address(alcx)).safeTransfer(address(rewardsDistributor), veAlcxEmissions);
rewardsDistributor.checkpointToken(); // Checkpoint token balance that was just minted in rewards distributor
rewardsDistributor.checkpointTotalSupply(); // Checkpoint supply
IERC20(address(alcx)).safeTransfer(address(timeGauge), timeEmissions);
timeGauge.notifyRewardAmount(timeEmissions);
IERC20(address(alcx)).safeTransfer(treasury, treasuryEmissions);
revenueHandler.checkpoint();
emit Mint(msg.sender, epochEmissions, supply);
}
return period;
}
}