Skip to content

Commit

Permalink
fix: allow reward distributor with same reward token
Browse files Browse the repository at this point in the history
  • Loading branch information
narayanprusty committed Aug 16, 2023
1 parent c95047d commit 7603b4e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
10 changes: 0 additions & 10 deletions contracts/Comptroller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -965,16 +965,6 @@ contract Comptroller is
function addRewardsDistributor(RewardsDistributor _rewardsDistributor) external onlyOwner {
require(!rewardsDistributorExists[address(_rewardsDistributor)], "already exists");

uint256 rewardsDistributorsLength = rewardsDistributors.length;

for (uint256 i; i < rewardsDistributorsLength; ++i) {
address rewardToken = address(rewardsDistributors[i].rewardToken());
require(
rewardToken != address(_rewardsDistributor.rewardToken()),
"distributor already exists with this reward"
);
}

uint256 rewardsDistributorsLen = rewardsDistributors.length;
_ensureMaxLoops(rewardsDistributorsLen + 1);

Expand Down
51 changes: 47 additions & 4 deletions tests/hardhat/Rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe("Rewards: Tests", async function () {
expect(res[1][2].toString()).equal(convertToUnit(0.1, 18));
});

it("Cannot add reward distributors with duplicate reward tokens", async function () {
it("Can add reward distributors with duplicate reward tokens", async function () {
const RewardsDistributor = await ethers.getContractFactory("RewardsDistributor");
rewardsDistributor = (await upgrades.deployProxy(RewardsDistributor, [
comptrollerProxy.address,
Expand All @@ -254,9 +254,9 @@ describe("Rewards: Tests", async function () {
fakeAccessControlManager.address,
])) as RewardsDistributor;

await expect(comptrollerProxy.addRewardsDistributor(rewardsDistributor.address)).to.be.revertedWith(
"distributor already exists with this reward",
);
await expect(comptrollerProxy.addRewardsDistributor(rewardsDistributor.address))
.to.emit(comptrollerProxy, "NewRewardsDistributor")
.withArgs(rewardsDistributor.address);
});

it("Emits event correctly", async () => {
Expand Down Expand Up @@ -321,6 +321,49 @@ describe("Rewards: Tests", async function () {
expect((await xvs.balanceOf(user1.address)).toString()).be.equal(convertToUnit(500.5, 18));
});

it("Multiple reward distributors with same reward token", async () => {
const RewardsDistributor = await ethers.getContractFactory("RewardsDistributor");
const newRewardsDistributor = (await upgrades.deployProxy(RewardsDistributor, [
comptrollerProxy.address,
xvs.address,
maxLoopsLimit,
fakeAccessControlManager.address,
])) as RewardsDistributor;

await comptrollerProxy.addRewardsDistributor(newRewardsDistributor.address);

const initialXvs = convertToUnit(1000000, 18);
await xvs.faucet(initialXvs);
await xvs.transfer(newRewardsDistributor.address, initialXvs);

const [, user1] = await ethers.getSigners();

expect((await xvs.balanceOf(user1.address)).toString()).to.be.equal("0");

await rewardsDistributor.setContributorRewardTokenSpeed(user1.address, convertToUnit(0.5, 18));
await newRewardsDistributor.setContributorRewardTokenSpeed(user1.address, convertToUnit(0.5, 18));

await mine(1000);
await rewardsDistributor.updateContributorRewards(user1.address);
await newRewardsDistributor.updateContributorRewards(user1.address);

await rewardsDistributor["claimRewardToken(address,address[])"](user1.address, [vWBTC.address, vDAI.address]);
await newRewardsDistributor["claimRewardToken(address,address[])"](user1.address, [vWBTC.address, vDAI.address]);

/*
Reward Distributor 1
Formula: speed * blocks
0.5 * 1001 = 500.5
Reward Distributor 2
Formula: speed * blocks
0.5 * 1003 = 501.5
Total xvs reward = 500.5 + 501.5 = 1002
*/
expect((await xvs.balanceOf(user1.address)).toString()).be.equal(convertToUnit(1002, 18));
});

it("pause rewards", async () => {
const [, user1, user2] = await ethers.getSigners();

Expand Down

0 comments on commit 7603b4e

Please sign in to comment.