Skip to content

Commit

Permalink
Add min limit when add/remove booster (#416)
Browse files Browse the repository at this point in the history
* Add min limit when add/remove booster

* Update

* remove mini require in deployment

* code style

---------

Co-authored-by: Ian He <[email protected]>
  • Loading branch information
nz-neooo and ianhe8x authored Jul 3, 2024
1 parent b707f64 commit 9d7b544
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
22 changes: 15 additions & 7 deletions contracts/RewardsBooster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ contract RewardsBooster is Initializable, OwnableUpgradeable, IRewardsBooster, S
uint256 _amount
) external onlyRegisteredDeployment(_deploymentId) {
_addBoosterDeployment(_deploymentId, msg.sender, _amount);

IERC20(settings.getContractAddress(SQContracts.SQToken)).safeTransferFrom(
msg.sender,
address(this),
Expand Down Expand Up @@ -298,6 +299,8 @@ contract RewardsBooster is Initializable, OwnableUpgradeable, IRewardsBooster, S
deploymentPool.accRewardsPerBooster = accRewardsPerBooster;
totalBoosterPoint += _amount;

require(deploymentPool.accountBooster[_account] >= minimumDeploymentBooster, 'RB015');

emit DeploymentBoosterAdded(_deploymentId, _account, _amount);
}

Expand All @@ -319,6 +322,12 @@ contract RewardsBooster is Initializable, OwnableUpgradeable, IRewardsBooster, S
deploymentPool.accRewardsPerBooster = accRewardsPerBooster;
totalBoosterPoint -= _amount;

require(
deploymentPool.accountBooster[_account] >= minimumDeploymentBooster ||
deploymentPool.accountBooster[_account] == 0,
'RB016'
);

emit DeploymentBoosterRemoved(_deploymentId, _account, _amount);
}

Expand Down Expand Up @@ -428,13 +437,12 @@ contract RewardsBooster is Initializable, OwnableUpgradeable, IRewardsBooster, S
DeploymentPool storage deployment = deploymentPools[_deploymentId];

// Only accrue rewards if over a threshold
uint256 newRewards = (deployment.boosterPoint >= minimumDeploymentBooster) // Accrue new rewards since last snapshot
? MathUtil.mulDiv(
getAccRewardsPerBooster() - deployment.accRewardsPerBoosterSnapshot,
deployment.boosterPoint,
FIXED_POINT_SCALING_FACTOR
)
: 0;
// Accrue new rewards since last snapshot
uint256 newRewards = MathUtil.mulDiv(
getAccRewardsPerBooster() - deployment.accRewardsPerBoosterSnapshot,
deployment.boosterPoint,
FIXED_POINT_SCALING_FACTOR
);
return deployment.accRewardsForDeployment + newRewards;
}

Expand Down
2 changes: 2 additions & 0 deletions publish/revertcode.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,7 @@
"RB012": "Invalid param length to set MissedLabor",
"RB013": "Deployment ids can not be same",
"RB014": "Caller is not a controller of the account",
"RB015": "Deployment booster is too small, need add more"
"RB016": "Deployment booster already too samll, need remove all"
"OPD01": "l2token address is empty"
}
34 changes: 17 additions & 17 deletions test/RewardsBooster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,25 @@ describe('RewardsBooster Contract', () => {

describe('boost deployments', () => {
it('can add and remove booster to a deployment', async () => {
const boosterAmount = etherParse('1000');
const boosterAmount = etherParse('10000');
const balanceBefore = await token.balanceOf(root.address);
await token.increaseAllowance(rewardsBooster.address, boosterAmount);

const minimumDeploymentBooster = await rewardsBooster.minimumDeploymentBooster();
await expect(rewardsBooster.boostDeployment(deploymentId0, etherParse('9999'))).to.revertedWith('RB015');

await rewardsBooster.boostDeployment(deploymentId0, boosterAmount);

await expect(rewardsBooster.boostDeployment(deploymentIds[4], boosterAmount)).to.revertedWith('RB008');

expect(await rewardsBooster.getRunnerDeploymentBooster(deploymentId0, root.address)).to.eq(boosterAmount);
const balanceAfter = await token.balanceOf(root.address);
expect(balanceBefore.sub(balanceAfter)).to.eq(boosterAmount);

await expect(rewardsBooster.removeBoosterDeployment(deploymentId0, etherParse('9999'))).to.revertedWith(
'RB016'
);

await rewardsBooster.removeBoosterDeployment(deploymentId0, boosterAmount);
expect(await token.balanceOf(root.address)).to.eq(balanceBefore);
});
Expand All @@ -279,15 +288,6 @@ describe('RewardsBooster Contract', () => {
reward = await rewardsBooster.getAccRewardsForDeployment(deploymentId0);
expect(reward).to.eq(perBlockReward.mul(1000));
});
it('can not get booster reward if boosted token less than minimum', async () => {
const boosterAmount = etherParse('9999');
await boosterDeployment(token, rewardsBooster, root, deploymentId0, boosterAmount);
let reward = await rewardsBooster.getAccRewardsForDeployment(deploymentId0);
expect(reward).to.eq(0);
await blockTravel(1000);
reward = await rewardsBooster.getAccRewardsForDeployment(deploymentId0);
expect(reward).to.eq(0);
});

it('can query deployment rewards, complex scene', async () => {
const perBlockReward = await rewardsBooster.issuancePerBlock();
Expand Down Expand Up @@ -439,39 +439,39 @@ describe('RewardsBooster Contract', () => {
});

it('can swap booster from one deployment to another', async () => {
await boosterDeployment(token, rewardsBooster, consumer0, deploymentId1, etherParse('10000'));
await boosterDeployment(token, rewardsBooster, consumer0, deploymentId1, etherParse('40000'));

await blockTravel(999);
const consumer = consumer0.address;
// swap with consumer account
await rewardsBooster
.connect(consumer0)
.swapBoosterDeployment(consumer, deploymentId1, deploymentId2, etherParse('3000'));
.swapBoosterDeployment(consumer, deploymentId1, deploymentId2, etherParse('13000'));
// check states
const accRewardsPerBooster = await rewardsBooster.getAccRewardsPerBooster();
const pool1 = await rewardsBooster.deploymentPools(deploymentId1);
expect(pool1.boosterPoint).to.eq(etherParse('7000'));
expect(pool1.boosterPoint).to.eq(etherParse('27000'));
expect(pool1.accRewardsPerBooster).to.eq(accRewardsPerBooster);
const runnerDeployment2Booster = await rewardsBooster.getRunnerDeploymentBooster(
deploymentId1,
consumer
);
expect(runnerDeployment2Booster).to.eq(etherParse('7000'));
expect(runnerDeployment2Booster).to.eq(etherParse('27000'));

const pool2 = await rewardsBooster.deploymentPools(deploymentId2);
expect(pool2.boosterPoint).to.eq(etherParse('3000'));
expect(pool2.boosterPoint).to.eq(etherParse('13000'));
expect(pool2.accRewardsPerBooster).to.eq(accRewardsPerBooster);
const runnerDeployment3Booster = await rewardsBooster.getRunnerDeploymentBooster(
deploymentId2,
consumer
);
expect(runnerDeployment3Booster).to.eq(etherParse('3000'));
expect(runnerDeployment3Booster).to.eq(etherParse('13000'));

// swap with controller account
await consumerRegistry.connect(consumer0).addController(consumer, controller.address);
await rewardsBooster
.connect(consumer0)
.swapBoosterDeployment(consumer, deploymentId1, deploymentId3, etherParse('3000'));
.swapBoosterDeployment(consumer, deploymentId1, deploymentId3, etherParse('10000'));
});

it('fail to swap booster with invalid params', async () => {
Expand Down

0 comments on commit 9d7b544

Please sign in to comment.