Skip to content

Commit

Permalink
feat: add sweep function to harvest additional ERC20 (#19)
Browse files Browse the repository at this point in the history
* feat: add sweep function

* misc: cleanup test file
  • Loading branch information
antoncoding committed Jun 27, 2021
1 parent ff293fb commit 726f251
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
11 changes: 10 additions & 1 deletion contracts/core/HodlERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ contract HodlERC20 is ERC20PermitUpgradeable {
}
}

/**
* @dev sweep additional erc20 tokens into feeRecipient's address
* @param _token token address, cannot be bonus token or main token
* @param _amount amount of token to send out.
*/
function sweep(address _token, uint256 _amount) external {
require(_token != address(token) && _token != address(bonusToken), "INVALID_TOKEN_TO_SWEEP");
IERC20WithDetail(_token).transfer(feeRecipient, _amount);
}

/**********************
* private Functions *
**********************/
Expand Down Expand Up @@ -262,7 +272,6 @@ contract HodlERC20 is ERC20PermitUpgradeable {
_burn(msg.sender, _amount);

(uint256 payout, uint256 reward, uint256 fee) = _calculateExitPayout(_amount);
(_amount);

// increase total in reward pool
totalReward = totalReward.add(reward);
Expand Down
35 changes: 33 additions & 2 deletions test/HodlERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ethers, waffle } from "hardhat";
import { expect } from "chai";
import { HodlERC20, MockERC20 } from "../typechain";
import { BigNumber, utils } from "ethers";
// import { calculateShares } from './utils';
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";

describe("HodlERC20 Tests", function () {
Expand All @@ -12,6 +11,8 @@ describe("HodlERC20 Tests", function () {
let hodl: HodlERC20;
let token: MockERC20;
let bonusToken: MockERC20;
let randomToken: MockERC20;

let totalTime: BigNumber;
let accounts: SignerWithAddress[] = [];

Expand Down Expand Up @@ -58,12 +59,15 @@ describe("HodlERC20 Tests", function () {
const ERC20 = await ethers.getContractFactory("MockERC20");
const erc20 = await ERC20.deploy();
const bonusErc20 = await ERC20.deploy();
const randomErc20 = await ERC20.deploy();
token = erc20 as MockERC20;
bonusToken = bonusErc20 as MockERC20;
randomToken = randomErc20 as MockERC20;

await token.init("WETH", "WETH", 18);
await bonusToken.init("BONUS", "BONUS", 18);
await randomToken.init("COMP", "COMP", 18);

// mint 100 WETH to account 0, 1 , 2, 3
// every depositor got 10 weth
const mintAmount = utils.parseUnits("10", "ether");
await token.mint(depositor1.address, mintAmount);
Expand Down Expand Up @@ -395,6 +399,33 @@ describe("HodlERC20 Tests", function () {
expect(bonusBalanceAfter.gt(0.0)).to.be.true;
});
});
describe("#donations", () => {
it("should revert if trying to sweep bonus token", async () => {
await expect(hodl.sweep(bonusToken.address, 0)).to.be.revertedWith("INVALID_TOKEN_TO_SWEEP");
});
it("should revert if trying to sweep main token", async () => {
await expect(hodl.sweep(token.address, 0)).to.be.revertedWith("INVALID_TOKEN_TO_SWEEP");
});
it("should be able to sweep token out of the contract", async function () {
const tokenAmount = 2000000;
await randomToken.mint(hodl.address, tokenAmount);

const feeRecipientBalanceBefore = await randomToken.balanceOf(feeRecipient.address);
const hodlContractBalanceBefore = await randomToken.balanceOf(hodl.address);

await hodl.sweep(randomToken.address, tokenAmount);

const feeRecipientBalanceAfter = await randomToken.balanceOf(feeRecipient.address);
const hodlContractBalanceAfter = await randomToken.balanceOf(hodl.address);

expect(
hodlContractBalanceBefore
.sub(hodlContractBalanceAfter)
.eq(feeRecipientBalanceAfter.sub(feeRecipientBalanceBefore))
).to.be.true;
expect(hodlContractBalanceBefore.sub(hodlContractBalanceAfter).eq(tokenAmount)).to.be.true;
});
});
});

describe("lock period", () => {
Expand Down

0 comments on commit 726f251

Please sign in to comment.