Skip to content

Commit

Permalink
Prevent burning the shares if there is no reward (#20)
Browse files Browse the repository at this point in the history
* Prevent burning the shares if there is no reward

* Run prettier

* Simplify the redeem burn check
  • Loading branch information
alxkzmn committed Jul 15, 2021
1 parent 6f48d13 commit e905795
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ typechain
.vscode/
coverage
coverage.json
.secret
.secret

.DS_Store
4 changes: 2 additions & 2 deletions contracts/core/HodlERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ contract HodlERC20 is ERC20PermitUpgradeable {
* @param _share shares to burn
*/
function redeem(uint256 _share) external {
require(totalReward > 0 || totalBonusReward > 0, "NOTHING_TO_REDEEM");
_redeem(_share);
}

Expand Down Expand Up @@ -303,16 +304,15 @@ contract HodlERC20 is ERC20PermitUpgradeable {
function _redeem(uint256 _share) private {
uint256 baseTokenPayout = _rewardFromShares(_share, totalReward);
uint256 bonusTokenPayout = _rewardFromShares(_share, totalBonusReward);

// reduce total price recorded
totalReward = totalReward.sub(baseTokenPayout);
totalBonusReward = totalBonusReward.sub(bonusTokenPayout);
totalShares = totalShares.sub(_share);

// subtract shares from user shares. this will revert if user doesn't have sufficient shares
_shares[msg.sender] = _shares[msg.sender].sub(_share);

emit Redeem(msg.sender, _share, baseTokenPayout, bonusTokenPayout);

if (baseTokenPayout > 0) token.safeTransfer(msg.sender, baseTokenPayout);
if (bonusTokenPayout > 0) bonusToken.safeTransfer(msg.sender, bonusTokenPayout);
}
Expand Down
27 changes: 24 additions & 3 deletions test/HodlERC20NoBonus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("HodlERC20 Tests Without Bonus Token", function () {
let accounts: SignerWithAddress[] = [];

let feeRecipient: SignerWithAddress;
let donor: SignerWithAddress;
let depositor: SignerWithAddress;

const penalty = 50; // 5%
Expand All @@ -29,9 +30,10 @@ describe("HodlERC20 Tests Without Bonus Token", function () {
expiry = BigNumber.from(parseInt(currentBlock.timestamp.toString()) + totalDuration);

accounts = await ethers.getSigners();
const [_depositor, _feeRecipient] = accounts;
const [depositor1, depositor2, _feeRecipient] = accounts;

depositor = _depositor;
donor = depositor1;
depositor = depositor2;
feeRecipient = _feeRecipient;
});

Expand All @@ -51,6 +53,10 @@ describe("HodlERC20 Tests Without Bonus Token", function () {
await token.init("WETH", "WETH", 18);
await bonusToken.init("BONUS", "BONUS", 18);

// mint 10 WETH to depositor
const mintAmount = utils.parseUnits("10", "ether");
await token.mint(depositor.address, mintAmount);

await hodl.init(
token.address,
penalty,
Expand All @@ -69,10 +75,25 @@ describe("HodlERC20 Tests Without Bonus Token", function () {
describe("#donations", () => {
it("Should fail if bonus token was not set up", async function () {
const amountToDonate = utils.parseUnits("0.5");
await expect(hodl.connect(depositor).donate(amountToDonate, bonusToken.address)).to.be.revertedWith(
await expect(hodl.connect(donor).donate(amountToDonate, bonusToken.address)).to.be.revertedWith(
"TOKEN_NOT_ALLOWED"
);
});
});
});
describe("redemption", () => {
it("Should not be able to redeem shares when there is no reward", async function () {
const depositAmount = utils.parseUnits("1");
await token.connect(depositor).approve(hodl.address, ethers.constants.MaxUint256);
await hodl.connect(depositor).deposit(depositAmount, depositor.address);

const hWethBalance = await hodl.balanceOf(depositor.address);
expect(hWethBalance).to.eq(depositAmount);

const shares = await hodl.shares(depositor.address);
expect(shares.lt(depositAmount)).to.be.true;

await expect(hodl.connect(donor).redeem(shares)).to.be.revertedWith("NOTHING_TO_REDEEM");
});
});
});

0 comments on commit e905795

Please sign in to comment.