Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [L-02] Lack of input validation #47

Merged
merged 3 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions contracts/AcceleratingDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ contract AcceleratingDistributor is ReentrancyGuard, Ownable, Multicall {
uint256 amount,
address beneficiary
) external nonReentrant onlyEnabled(stakedToken) {
require(beneficiary != address(0), "Invalid beneficiary");
_stake(stakedToken, amount, beneficiary);
}

Expand All @@ -208,6 +209,8 @@ contract AcceleratingDistributor is ReentrancyGuard, Ownable, Multicall {
* @param amount The amount of the token to withdraw.
*/
function unstake(address stakedToken, uint256 amount) public nonReentrant onlyInitialized(stakedToken) {
require(amount > 0, "Invalid amount");

_updateReward(stakedToken, msg.sender);
UserDeposit storage userDeposit = stakingTokens[stakedToken].stakingBalances[msg.sender];

Expand Down Expand Up @@ -400,6 +403,7 @@ contract AcceleratingDistributor is ReentrancyGuard, Ownable, Multicall {
uint256 amount,
address staker
) internal {
require(amount > 0, "Invalid amount");
_updateReward(stakedToken, staker);

UserDeposit storage userDeposit = stakingTokens[stakedToken].stakingBalances[staker];
Expand Down
67 changes: 67 additions & 0 deletions test/AcceleratingDistributor.Admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { baseEmissionRate, maxMultiplier, secondsToMaxMultiplier } from "./const
let timer: Contract, acrossToken: Contract, distributor: Contract, lpToken1: Contract;
let owner: SignerWithAddress, rando: SignerWithAddress;

const zeroAddress = ethers.constants.AddressZero;

describe("AcceleratingDistributor: Admin Functions", async function () {
beforeEach(async function () {
[owner, rando] = await ethers.getSigners();
Expand Down Expand Up @@ -187,4 +189,69 @@ describe("AcceleratingDistributor: Admin Functions", async function () {
await expect(distributor.connect(owner).withdrawReward(lpToken1.address)).to.not.be.reverted;
await expect(distributor.connect(owner).exit(lpToken1.address)).to.not.be.reverted;
});

it("Input validation on staking-related methods", async function () {
await lpToken1.mint(owner.address, toWei(69));
await lpToken1.connect(owner).approve(distributor.address, toWei(69));

// Modifiers take precedence when staking is disabled.
for (const stakingEnabled of [true, false]) {
await distributor.configureStakingToken(
lpToken1.address,
stakingEnabled,
baseEmissionRate,
maxMultiplier,
secondsToMaxMultiplier
);

await expect(distributor.connect(owner).stake(lpToken1.address, toWei(0))).to.be.revertedWith(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool way to do this test and save code

stakingEnabled ? "Invalid amount" : "stakedToken not enabled"
);
await expect(distributor.connect(owner).stakeFor(lpToken1.address, toWei(0), zeroAddress)).to.be.revertedWith(
stakingEnabled ? "Invalid beneficiary" : "stakedToken not enabled"
);
await expect(distributor.connect(owner).stakeFor(lpToken1.address, toWei(1), zeroAddress)).to.be.revertedWith(
stakingEnabled ? "Invalid beneficiary" : "stakedToken not enabled"
);

if (stakingEnabled) {
await expect(distributor.connect(owner).stake(lpToken1.address, toWei(1))).to.not.be.reverted;
await expect(distributor.connect(owner).unstake(lpToken1.address, toWei(0))).to.be.revertedWith(
"Invalid amount"
);
await expect(distributor.connect(owner).unstake(lpToken1.address, toWei(1))).to.not.be.reverted;
} else {
await expect(distributor.connect(owner).stake(lpToken1.address, toWei(1))).to.be.revertedWith(
"stakedToken not enabled"
);
await expect(distributor.connect(owner).unstake(lpToken1.address, toWei(0))).to.be.revertedWith(
"Invalid amount"
);

// Staked balance is 0.
await expect(distributor.connect(owner).unstake(lpToken1.address, toWei(1))).to.be.reverted;
}
}

// Validate withdrawal guards when staking is disabled.
await distributor.configureStakingToken(
lpToken1.address,
true,
baseEmissionRate,
maxMultiplier,
secondsToMaxMultiplier
);
await expect(distributor.connect(owner).stake(lpToken1.address, toWei(2))).to.not.be.reverted;

await distributor.configureStakingToken(
lpToken1.address,
false,
baseEmissionRate,
maxMultiplier,
secondsToMaxMultiplier
);
await expect(distributor.connect(owner).unstake(lpToken1.address, toWei(0))).to.be.revertedWith("Invalid amount");
await expect(distributor.connect(owner).unstake(lpToken1.address, toWei(1))).to.not.be.reverted;
await expect(distributor.connect(owner).exit(lpToken1.address)).to.not.be.reverted;
});
});