Skip to content

Commit

Permalink
Made drip rate configurable in the ComptrollerV2 (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Jan 28, 2021
1 parent 348f629 commit 8c6795a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
24 changes: 20 additions & 4 deletions contracts/comptroller/ComptrollerV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "@openzeppelin/contracts-upgradeable/utils/SafeCastUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/Initializable.sol";
import "@pooltogether/fixed-point/contracts/FixedPoint.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

import "../utils/ExtendedSafeCast.sol";
import "../token/TokenListener.sol";
Expand All @@ -15,7 +16,7 @@ import "../token/TokenListener.sol";
/// @notice The tokens are dripped at a "drip rate per second". This is the number of tokens that
/// are dripped each second. A user's share of the dripped tokens is based on how many 'measure' tokens they hold.
/* solium-disable security/no-block-members */
contract ComptrollerV2 is Initializable, TokenListener {
contract ComptrollerV2 is OwnableUpgradeable, TokenListener {
using SafeMathUpgradeable for uint256;
using SafeCastUpgradeable for uint256;
using ExtendedSafeCast for uint256;
Expand All @@ -35,6 +36,10 @@ contract ComptrollerV2 is Initializable, TokenListener {
uint256 newTokens
);

event DripRateChanged(
uint256 dripRatePerSecond
);

struct UserState {
uint128 lastExchangeRateMantissa;
uint128 balance;
Expand Down Expand Up @@ -70,11 +75,11 @@ contract ComptrollerV2 is Initializable, TokenListener {
IERC20Upgradeable _measure,
uint256 _dripRatePerSecond
) public initializer {
require(_dripRatePerSecond > 0, "ComptrollerV2/dripRate-gt-zero");
__Ownable_init();
lastDripTimestamp = _currentTime();
asset = _asset;
measure = _measure;
dripRatePerSecond = _dripRatePerSecond;
lastDripTimestamp = _currentTime();
setDripRatePerSecond(_dripRatePerSecond);

emit Initialized(
asset,
Expand Down Expand Up @@ -136,6 +141,17 @@ contract ComptrollerV2 is Initializable, TokenListener {
return newTokens;
}

function setDripRatePerSecond(uint256 _dripRatePerSecond) public onlyOwner {
require(_dripRatePerSecond > 0, "ComptrollerV2/dripRate-gt-zero");

// ensure we're all caught up
drip();

dripRatePerSecond = _dripRatePerSecond;

emit DripRateChanged(dripRatePerSecond);
}

/// @notice Captures new tokens for a user
/// @dev This must be called before changes to the user's balance (i.e. before mint, transfer or burns)
/// @param user The user to capture tokens for
Expand Down
1 change: 1 addition & 0 deletions contracts/comptroller/ComptrollerV2ProxyFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ contract ComptrollerV2ProxyFactory is ProxyFactory {
comptroller.initialize(
_asset, _measure, _dripRatePerSecond
);
comptroller.transferOwnership(msg.sender);
return comptroller;
}

Expand Down
18 changes: 18 additions & 0 deletions test/ComptrollerV2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,22 @@ describe('ComptrollerV2', () => {
expect(await dripToken.balanceOf(wallet._address)).to.equal(toWei('5'))
})
})

describe('setDripRatePerSecond()', () => {
it('should allow the owner to change the drip rate', async () => {
await expect(comptroller.setDripRatePerSecond('1'))
.to.emit(comptroller, 'DripRateChanged')
.withArgs('1')
})

it('should not allow anyone else to set drip rate', async () => {
await expect(comptroller.connect(wallet2).setDripRatePerSecond('1'))
.to.be.revertedWith('Ownable: caller is not the owner')
})

it('requires that the rip rate be greater than zero', async () => {
await expect(comptroller.setDripRatePerSecond('0'))
.to.be.revertedWith('ComptrollerV2/dripRate-gt-zero')
})
})
})
1 change: 1 addition & 0 deletions test/ComptrollerV2ProxyFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('ComptrollerV2ProxyFactory', () => {
expect(await comptrollerV2.asset()).to.equal(asset.address)
expect(await comptrollerV2.measure()).to.equal(measure.address)
expect(await comptrollerV2.dripRatePerSecond()).to.equal(ethers.utils.parseEther('0.01'))
expect(await comptrollerV2.owner()).to.equal(wallet._address)

})
})
Expand Down

0 comments on commit 8c6795a

Please sign in to comment.