Skip to content

Commit

Permalink
Merge pull request #268 from pooltogether/pool-2266-migrate-gaugerewa…
Browse files Browse the repository at this point in the history
…rd-add-tests

feat(contracts): add GaugeReward contract  @PierrickGT
  • Loading branch information
PierrickGT authored May 18, 2022
2 parents 14290f1 + 72e3475 commit e8c03b7
Show file tree
Hide file tree
Showing 6 changed files with 775 additions and 56 deletions.
181 changes: 127 additions & 54 deletions contracts/GaugeController.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.6;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import "./interfaces/IGaugeController.sol";
import "./interfaces/IGaugeReward.sol";
import "./libraries/TwabLib.sol";
import "./libraries/ExtendedSafeCastLib.sol";

Expand All @@ -14,23 +17,35 @@ contract GaugeController is IGaugeController {
}

IERC20 public token;
address public rewardVault;
mapping(address => uint256) public rewards;
IGaugeReward public gaugeReward;

/**
* @notice Tracks user deposit balance
* @dev user => balance
*/
mapping(address => uint256) public balances;
mapping(address => mapping(address => uint256)) public gaugeBalances;

// records total voting power in a gauge
/**
* @notice Tracks user balance per gauge
* @dev user => gauge => balance
*/
mapping(address => mapping(address => uint256)) public userGaugeBalance;

/**
* @notice Tracks gauge total voting power
* @dev gauge => voting power
*/
mapping(address => TwabLib.Account) internal gaugeTwabs;

// records scales for gauges.
/**
* @notice Tracks gauge scale total voting power
* @dev gauge => scale voting power
*/
mapping(address => TwabLib.Account) internal gaugeScaleTwabs;

constructor (
IERC20 _token,
address _rewardVault
) {
constructor(IERC20 _token, IGaugeReward _gaugeReward) {
token = _token;
rewardVault = _rewardVault;
gaugeReward = _gaugeReward;
}

function deposit(address _to, uint256 _amount) public {
Expand All @@ -45,22 +60,35 @@ contract GaugeController is IGaugeController {

function increaseGauge(address _gauge, uint256 _amount) public requireGauge(_gauge) {
balances[msg.sender] -= _amount;
gaugeBalances[msg.sender][_gauge] += _amount;
userGaugeBalance[msg.sender][_gauge] += _amount;

TwabLib.Account storage gaugeTwab = gaugeTwabs[_gauge];
(
TwabLib.AccountDetails memory twabDetails,,
) = TwabLib.increaseBalance(gaugeTwab, _amount.toUint208(), uint32(block.timestamp));
(TwabLib.AccountDetails memory twabDetails, , ) = TwabLib.increaseBalance(
gaugeTwab,
_amount.toUint208(),
uint32(block.timestamp)
);

gaugeTwab.details = twabDetails;

gaugeReward.afterIncreaseGauge(_gauge, msg.sender, uint256(twabDetails.balance) - _amount);
}

function decreaseGauge(address _gauge, uint256 _amount) public requireGauge(_gauge) {
balances[msg.sender] += _amount;
gaugeBalances[msg.sender][_gauge] -= _amount;
userGaugeBalance[msg.sender][_gauge] -= _amount;

TwabLib.Account storage gaugeTwab = gaugeTwabs[_gauge];
(
TwabLib.AccountDetails memory twabDetails,,
) = TwabLib.decreaseBalance(gaugeTwab, _amount.toUint208(), "insuff", uint32(block.timestamp));
(TwabLib.AccountDetails memory twabDetails, , ) = TwabLib.decreaseBalance(
gaugeTwab,
_amount.toUint208(),
"insuff",
uint32(block.timestamp)
);

gaugeTwab.details = twabDetails;

gaugeReward.afterDecreaseGauge(_gauge, msg.sender, uint256(twabDetails.balance) + _amount);
}

function addGauge(address _gauge) public {
Expand All @@ -69,78 +97,123 @@ contract GaugeController is IGaugeController {

function addGaugeWithScale(address _gauge, uint256 _scale) public {
TwabLib.Account storage gaugeScaleTwab = gaugeScaleTwabs[_gauge];
(
TwabLib.AccountDetails memory twabDetails,,
) = TwabLib.increaseBalance(gaugeScaleTwab, _scale.toUint208(), uint32(block.timestamp));
(TwabLib.AccountDetails memory twabDetails, , ) = TwabLib.increaseBalance(
gaugeScaleTwab,
_scale.toUint208(),
uint32(block.timestamp)
);
gaugeScaleTwab.details = twabDetails;
}

function removeGauge(address _gauge) public {
TwabLib.Account storage gaugeScaleTwab = gaugeScaleTwabs[_gauge];
TwabLib.AccountDetails memory twabDetails = gaugeScaleTwab.details;
(
twabDetails,,
) = TwabLib.decreaseBalance(gaugeScaleTwab, twabDetails.balance, "insuff", uint32(block.timestamp));
(twabDetails, , ) = TwabLib.decreaseBalance(
gaugeScaleTwab,
twabDetails.balance,
"insuff",
uint32(block.timestamp)
);
gaugeScaleTwab.details = twabDetails;
}

function setGaugeScale(address _gauge, uint256 _scale) public {
TwabLib.Account storage gaugeScaleTwab = gaugeScaleTwabs[_gauge];
TwabLib.AccountDetails memory twabDetails = gaugeScaleTwab.details;
if (twabDetails.balance > _scale) {
(
twabDetails,,
) = TwabLib.decreaseBalance(gaugeScaleTwab, twabDetails.balance - _scale.toUint208(), "insuff", uint32(block.timestamp));
(twabDetails, , ) = TwabLib.decreaseBalance(
gaugeScaleTwab,
twabDetails.balance - _scale.toUint208(),
"insuff",
uint32(block.timestamp)
);
} else {
(
twabDetails,,
) = TwabLib.increaseBalance(gaugeScaleTwab, _scale.toUint208() - twabDetails.balance, uint32(block.timestamp));
(twabDetails, , ) = TwabLib.increaseBalance(
gaugeScaleTwab,
_scale.toUint208() - twabDetails.balance,
uint32(block.timestamp)
);
}
gaugeScaleTwab.details = twabDetails;
}

function getGauge(address _gauge) public view returns (uint256) {
/// @inheritdoc IGaugeController
function getGaugeBalance(address _gauge) external view override returns (uint256) {
return gaugeTwabs[_gauge].details.balance;
}

function getGaugeScale(address _gauge) public view returns (uint256) {
/// @inheritdoc IGaugeController
function getGaugeScaleBalance(address _gauge) external view override returns (uint256) {
return gaugeScaleTwabs[_gauge].details.balance;
}

function getScaledAverageGaugeBetween(address _gauge, uint256 _startTime, uint256 _endTime) external override view returns (uint256) {
/// @inheritdoc IGaugeController
function getScaledAverageGaugeBetween(
address _gauge,
uint256 _startTime,
uint256 _endTime
) external view override returns (uint256) {
uint256 gauge = _getAverageGaugeBetween(_gauge, _startTime, _endTime);
uint256 gaugeScale = _getAverageGaugeScaleBetween(_gauge, _startTime, _endTime);
return (gauge*gaugeScale) / 1 ether;
return (gauge * gaugeScale) / 1 ether;
}

function getAverageGaugeBetween(address _gauge, uint256 _startTime, uint256 _endTime) external view returns (uint256) {
function getAverageGaugeBetween(
address _gauge,
uint256 _startTime,
uint256 _endTime
) external view returns (uint256) {
return _getAverageGaugeBetween(_gauge, _startTime, _endTime);
}

function getAverageGaugeScaleBetween(address _gauge, uint256 _startTime, uint256 _endTime) external view returns (uint256) {
function getAverageGaugeScaleBetween(
address _gauge,
uint256 _startTime,
uint256 _endTime
) external view returns (uint256) {
return _getAverageGaugeScaleBetween(_gauge, _startTime, _endTime);
}

function _getAverageGaugeBetween(address _gauge, uint256 _startTime, uint256 _endTime) internal view returns (uint256) {
TwabLib.AccountDetails memory gaugeDetails = gaugeTwabs[_gauge].details;
return TwabLib.getAverageBalanceBetween(
gaugeTwabs[_gauge].twabs,
gaugeDetails,
uint32(_startTime),
uint32(_endTime),
uint32(block.timestamp)
);
/// @inheritdoc IGaugeController
function getUserGaugeBalance(address _gauge, address _user)
external
view
override
returns (uint256)
{
return userGaugeBalance[_user][_gauge];
}

function _getAverageGaugeScaleBetween(address _gauge, uint256 _startTime, uint256 _endTime) internal view returns (uint256) {
function _getAverageGaugeBetween(
address _gauge,
uint256 _startTime,
uint256 _endTime
) internal view returns (uint256) {
TwabLib.AccountDetails memory gaugeDetails = gaugeTwabs[_gauge].details;
return
TwabLib.getAverageBalanceBetween(
gaugeTwabs[_gauge].twabs,
gaugeDetails,
uint32(_startTime),
uint32(_endTime),
uint32(block.timestamp)
);
}

function _getAverageGaugeScaleBetween(
address _gauge,
uint256 _startTime,
uint256 _endTime
) internal view returns (uint256) {
TwabLib.AccountDetails memory gaugeScaleDetails = gaugeScaleTwabs[_gauge].details;
return TwabLib.getAverageBalanceBetween(
gaugeScaleTwabs[_gauge].twabs,
gaugeScaleDetails,
uint32(_startTime),
uint32(_endTime),
uint32(block.timestamp)
);
return
TwabLib.getAverageBalanceBetween(
gaugeScaleTwabs[_gauge].twabs,
gaugeScaleDetails,
uint32(_startTime),
uint32(_endTime),
uint32(block.timestamp)
);
}

function isGauge(address _gauge) public view returns (bool) {
Expand Down
Loading

0 comments on commit e8c03b7

Please sign in to comment.