From 5fea4ecad625c6d67df5a1e5ee367de1b6cf8a30 Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Mon, 3 Jan 2022 20:58:02 +0530 Subject: [PATCH 01/11] changes for automating tasks of starting and closing lbp using gelato --- contracts/lbp/LBPManager.sol | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/contracts/lbp/LBPManager.sol b/contracts/lbp/LBPManager.sol index 8e0da1b5..25249fb8 100644 --- a/contracts/lbp/LBPManager.sol +++ b/contracts/lbp/LBPManager.sol @@ -25,6 +25,9 @@ import "../utils/interface/ILBP.sol"; */ // solhint-disable-next-line max-states-count contract LBPManager { + + enum TaskState{Waiting, Started, Ended} + TaskState public state; // Constants uint256 private constant HUNDRED_PERCENT = 1e18; // Used in calculating the fee. @@ -192,7 +195,7 @@ contract LBPManager { startWeights, swapFeePercentage, address(this), - true // SwapEnabled is set to true at pool creation. + false // SwapEnabled is set to true at pool creation. ) ); @@ -234,6 +237,35 @@ contract LBPManager { vault.joinPool(lbp.getPoolId(), address(this), address(this), request); } + /** + * @dev start LBP just before one block + * @notice it can be invoked by anyone only once + */ + function startLbp() external { + uint256 startTime; + uint256 blockTime = 5 minutes; + bool isSwapEnabled = lbp.getSwapEnabled(); + (startTime, ,) = lbp.getGradualWeightUpdateParams(); + require(!isSwapEnabled && state == TaskState.Waiting, "LBPManager: swapping already active"); + require(block.timestamp > startTime - blockTime, "LBPManager: Only once block before start time"); + state = TaskState.Started; + lbp.setSwapEnabled(true); + } + + /** + * @dev ends LBP just after one block + * @notice it can be invoked by anyone only once + */ + function endLbp() external { + uint256 endTime; + bool isSwapEnabled = lbp.getSwapEnabled(); + (, endTime,) = lbp.getGradualWeightUpdateParams(); + require(isSwapEnabled && state == TaskState.Started, "LBPManager: swapping already active"); + require(block.timestamp > endTime, "LBPManager: Only once block before start time"); + state = TaskState.Ended; + lbp.setSwapEnabled(false); + } + /** * @dev Exit pool or remove liquidity from pool. * @param _receiver Address of the liquidity receiver, after exiting the LBP. @@ -302,6 +334,7 @@ contract LBPManager { * @param _swapEnabled Enables/disables swapping. */ function setSwapEnabled(bool _swapEnabled) external onlyAdmin { + require(TaskState.Ended != state, "LBPManager: LBP ended"); lbp.setSwapEnabled(_swapEnabled); } From 95f2ac5480a3f4f388472e6f499988147e8300d6 Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Mon, 3 Jan 2022 20:58:34 +0530 Subject: [PATCH 02/11] formatted code --- contracts/lbp/LBPManager.sol | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/contracts/lbp/LBPManager.sol b/contracts/lbp/LBPManager.sol index 25249fb8..20178051 100644 --- a/contracts/lbp/LBPManager.sol +++ b/contracts/lbp/LBPManager.sol @@ -25,8 +25,11 @@ import "../utils/interface/ILBP.sol"; */ // solhint-disable-next-line max-states-count contract LBPManager { - - enum TaskState{Waiting, Started, Ended} + enum TaskState { + Waiting, + Started, + Ended + } TaskState public state; // Constants uint256 private constant HUNDRED_PERCENT = 1e18; // Used in calculating the fee. @@ -245,9 +248,15 @@ contract LBPManager { uint256 startTime; uint256 blockTime = 5 minutes; bool isSwapEnabled = lbp.getSwapEnabled(); - (startTime, ,) = lbp.getGradualWeightUpdateParams(); - require(!isSwapEnabled && state == TaskState.Waiting, "LBPManager: swapping already active"); - require(block.timestamp > startTime - blockTime, "LBPManager: Only once block before start time"); + (startTime, , ) = lbp.getGradualWeightUpdateParams(); + require( + !isSwapEnabled && state == TaskState.Waiting, + "LBPManager: swapping already active" + ); + require( + block.timestamp > startTime - blockTime, + "LBPManager: Only once block before start time" + ); state = TaskState.Started; lbp.setSwapEnabled(true); } @@ -259,9 +268,15 @@ contract LBPManager { function endLbp() external { uint256 endTime; bool isSwapEnabled = lbp.getSwapEnabled(); - (, endTime,) = lbp.getGradualWeightUpdateParams(); - require(isSwapEnabled && state == TaskState.Started, "LBPManager: swapping already active"); - require(block.timestamp > endTime, "LBPManager: Only once block before start time"); + (, endTime, ) = lbp.getGradualWeightUpdateParams(); + require( + isSwapEnabled && state == TaskState.Started, + "LBPManager: swapping already active" + ); + require( + block.timestamp > endTime, + "LBPManager: Only once block before start time" + ); state = TaskState.Ended; lbp.setSwapEnabled(false); } From 79c52d24ff4655af3c8e05aaa420c676b841699c Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Thu, 6 Jan 2022 19:29:04 +0530 Subject: [PATCH 03/11] made changes as requested --- contracts/lbp/LBPManager.sol | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/contracts/lbp/LBPManager.sol b/contracts/lbp/LBPManager.sol index 20178051..d136e247 100644 --- a/contracts/lbp/LBPManager.sol +++ b/contracts/lbp/LBPManager.sol @@ -246,23 +246,25 @@ contract LBPManager { */ function startLbp() external { uint256 startTime; - uint256 blockTime = 5 minutes; + uint256 buffer = 5 minutes; bool isSwapEnabled = lbp.getSwapEnabled(); (startTime, , ) = lbp.getGradualWeightUpdateParams(); require( - !isSwapEnabled && state == TaskState.Waiting, - "LBPManager: swapping already active" + state == TaskState.Waiting, + "LBPManager: already started" ); require( - block.timestamp > startTime - blockTime, - "LBPManager: Only once block before start time" + block.timestamp > startTimeEndTime[0] - buffer, + "LBPManager: one block before start time" ); state = TaskState.Started; - lbp.setSwapEnabled(true); + if(!isSwapEnabled){ + lbp.setSwapEnabled(true); + } } /** - * @dev ends LBP just after one block + * @dev ends LBP once end time is reached * @notice it can be invoked by anyone only once */ function endLbp() external { @@ -271,11 +273,11 @@ contract LBPManager { (, endTime, ) = lbp.getGradualWeightUpdateParams(); require( isSwapEnabled && state == TaskState.Started, - "LBPManager: swapping already active" + "LBPManager: swapping disabled" ); require( - block.timestamp > endTime, - "LBPManager: Only once block before start time" + block.timestamp >= startTimeEndTime[1], + "LBPManager: haven't reached end time" ); state = TaskState.Ended; lbp.setSwapEnabled(false); @@ -349,7 +351,10 @@ contract LBPManager { * @param _swapEnabled Enables/disables swapping. */ function setSwapEnabled(bool _swapEnabled) external onlyAdmin { - require(TaskState.Ended != state, "LBPManager: LBP ended"); + require( + block.timestamp >= startTimeEndTime[0] && block.timestamp < startTimeEndTime[1], + "LBPManager: only between start time and end time" + ); lbp.setSwapEnabled(_swapEnabled); } From 0fa28eab9382830e139ed661568a77f6e7c7d232 Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Thu, 6 Jan 2022 19:29:30 +0530 Subject: [PATCH 04/11] added test for start lbp --- contracts/lbp/LBPManager.sol | 10 ++--- test/unit/lbp-manager.spec.js | 77 +++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/contracts/lbp/LBPManager.sol b/contracts/lbp/LBPManager.sol index d136e247..152452d5 100644 --- a/contracts/lbp/LBPManager.sol +++ b/contracts/lbp/LBPManager.sol @@ -249,16 +249,13 @@ contract LBPManager { uint256 buffer = 5 minutes; bool isSwapEnabled = lbp.getSwapEnabled(); (startTime, , ) = lbp.getGradualWeightUpdateParams(); - require( - state == TaskState.Waiting, - "LBPManager: already started" - ); + require(state == TaskState.Waiting, "LBPManager: already started"); require( block.timestamp > startTimeEndTime[0] - buffer, "LBPManager: one block before start time" ); state = TaskState.Started; - if(!isSwapEnabled){ + if (!isSwapEnabled) { lbp.setSwapEnabled(true); } } @@ -352,7 +349,8 @@ contract LBPManager { */ function setSwapEnabled(bool _swapEnabled) external onlyAdmin { require( - block.timestamp >= startTimeEndTime[0] && block.timestamp < startTimeEndTime[1], + block.timestamp >= startTimeEndTime[0] && + block.timestamp < startTimeEndTime[1], "LBPManager: only between start time and end time" ); lbp.setSwapEnabled(_swapEnabled); diff --git a/test/unit/lbp-manager.spec.js b/test/unit/lbp-manager.spec.js index 918cc302..fa557180 100644 --- a/test/unit/lbp-manager.spec.js +++ b/test/unit/lbp-manager.spec.js @@ -1003,6 +1003,83 @@ describe(">> Contract: LBPManager", () => { }); }); }); + describe("# startLbp", () => { + beforeEach(async () => { + const fundingAmount = { + initialBalances: INITIAL_BALANCES, + feePercentage: FEE_PERCENTAGE_ZERO, + }; + + const startTime = (await time.latest()).add( + await time.duration.minutes(10) + ); + const endTime = startTime.add(await time.duration.minutes(20)); + + const initializeLBPManagerParams = paramGenerator.initializeParams( + lbpFactoryInstance.address, + NAME, + SYMBOL, + tokenAddresses, + INITIAL_BALANCES, + START_WEIGHTS, + startTime.toString(), + endTime.toString(), + END_WEIGHTS, + fees, + beneficiary.address, + METADATA + ); + + const initialState = { + initializeLBPManagerParams, + fundingAmount, + poolFunded: true, + }; + ({ lbpManagerInstance, tokenInstances, amountToAddForFee } = + await setupInitialState(contractInstances, initialState)); + }); + it("$ reverts when invoked before condition currentTime > startTime - 5 minutes is met", async () => { + await expect( + lbpManagerInstance.connect(owner).startLbp() + ).to.be.revertedWith("LBPManager: one block before start time"); + }); + it("$ cannot use setSwapEnabled outside the weight change period", async () => { + await expect( + lbpManagerInstance.connect(admin).setSwapEnabled(true) + ).to.be.revertedWith("LBPManager: only between start time and end time"); + }); + it("$ starts lbp swapping", async () => { + await time.increase(await time.duration.minutes(5)); + expect(await lbpManagerInstance.getSwapEnabled()).to.be.false; + await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be + .reverted; + expect(await lbpManagerInstance.state()).to.be.equal(1); + expect(await lbpManagerInstance.getSwapEnabled()).to.be.true; + }); + it("$ can only be invoked once", async () => { + await time.increase(await time.duration.minutes(5)); + await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be + .reverted; + await expect( + lbpManagerInstance.connect(owner).startLbp() + ).to.be.revertedWith("LBPManager: already started"); + }); + it("$ can be invoked by anyone", async () => { + await time.increase(await time.duration.minutes(5)); + await expect(lbpManagerInstance.connect(beneficiary).startLbp()).to.not.be + .reverted; + }); + it("$ updates state even if swap is already enabled", async () => { + await time.increase(await time.duration.minutes(10)); + await expect(lbpManagerInstance.connect(admin).setSwapEnabled(true)).to + .not.be.reverted; + expect(await lbpManagerInstance.getSwapEnabled()).to.be.true; + expect(await lbpManagerInstance.state()).to.be.equal(0); + await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be + .reverted; + expect(await lbpManagerInstance.state()).to.be.equal(1); + }); + }); describe("# setSwapEnabled", () => { beforeEach(async () => { const fundingAmount = { From 049ac52707848fa62af89f92647a0f6f727cf93a Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Thu, 6 Jan 2022 19:38:25 +0530 Subject: [PATCH 05/11] added tests for new condition at setSwapEnabled --- test/unit/lbp-manager.spec.js | 110 ++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/test/unit/lbp-manager.spec.js b/test/unit/lbp-manager.spec.js index fa557180..c05a58dc 100644 --- a/test/unit/lbp-manager.spec.js +++ b/test/unit/lbp-manager.spec.js @@ -1087,6 +1087,26 @@ describe(">> Contract: LBPManager", () => { feePercentage: FEE_PERCENTAGE_ZERO, }; + const startTime = (await time.latest()).add( + await time.duration.minutes(10) + ); + const endTime = startTime.add(await time.duration.minutes(10)); + + const initializeLBPManagerParams = paramGenerator.initializeParams( + lbpFactoryInstance.address, + NAME, + SYMBOL, + tokenAddresses, + INITIAL_BALANCES, + START_WEIGHTS, + startTime.toString(), + endTime.toString(), + END_WEIGHTS, + fees, + beneficiary.address, + METADATA + ); + const initialState = { initializeLBPManagerParams, fundingAmount, @@ -1100,16 +1120,106 @@ describe(">> Contract: LBPManager", () => { lbpManagerInstance.connect(owner).setSwapEnabled(false) ).to.be.revertedWith("LBPManager: caller is not admin"); }); + it("» reverts when invoked before start time", async () => { + await expect( + lbpManagerInstance.connect(admin).setSwapEnabled(false) + ).to.be.revertedWith("LBPManager: only between start time and end time"); + }); it("» setSwapEnabled to false", async () => { + await time.increase(time.duration.minutes(10)); expect(await lbpManagerInstance.admin()).to.equal(admin.address); await lbpManagerInstance.connect(admin).setSwapEnabled(false); expect(await lbpManagerInstance.getSwapEnabled()).to.equal(false); }); it("» setSwapEnabled to true", async () => { + await time.increase(time.duration.minutes(10)); expect(await lbpManagerInstance.admin()).to.equal(admin.address); await lbpManagerInstance.connect(admin).setSwapEnabled(true); expect(await lbpManagerInstance.getSwapEnabled()).to.equal(true); }); + it("» reverts when invoked after end time", async () => { + await time.increase(time.duration.minutes(20)); + await expect( + lbpManagerInstance.connect(admin).setSwapEnabled(false) + ).to.be.revertedWith("LBPManager: only between start time and end time"); + }); + }); + describe("# endLbp", () => { + beforeEach(async () => { + const fundingAmount = { + initialBalances: INITIAL_BALANCES, + feePercentage: FEE_PERCENTAGE_ZERO, + }; + + const startTime = (await time.latest()).add( + await time.duration.minutes(10) + ); + const endTime = startTime.add(await time.duration.minutes(20)); + + const initializeLBPManagerParams = paramGenerator.initializeParams( + lbpFactoryInstance.address, + NAME, + SYMBOL, + tokenAddresses, + INITIAL_BALANCES, + START_WEIGHTS, + startTime.toString(), + endTime.toString(), + END_WEIGHTS, + fees, + beneficiary.address, + METADATA + ); + + const initialState = { + initializeLBPManagerParams, + fundingAmount, + poolFunded: true, + }; + ({ lbpManagerInstance, tokenInstances, amountToAddForFee } = + await setupInitialState(contractInstances, initialState)); + }); + it("$ reverts when invoked before condition currentTime > startTime - 5 minutes is met", async () => { + await expect( + lbpManagerInstance.connect(owner).startLbp() + ).to.be.revertedWith("LBPManager: one block before start time"); + }); + it("$ cannot use setSwapEnabled outside the weight change period", async () => { + await expect( + lbpManagerInstance.connect(admin).setSwapEnabled(true) + ).to.be.revertedWith("LBPManager: only between start time and end time"); + }); + it("$ starts lbp swapping", async () => { + await time.increase(await time.duration.minutes(5)); + expect(await lbpManagerInstance.getSwapEnabled()).to.be.false; + await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be + .reverted; + expect(await lbpManagerInstance.state()).to.be.equal(1); + expect(await lbpManagerInstance.getSwapEnabled()).to.be.true; + }); + it("$ can only be invoked once", async () => { + await time.increase(await time.duration.minutes(5)); + await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be + .reverted; + await expect( + lbpManagerInstance.connect(owner).startLbp() + ).to.be.revertedWith("LBPManager: already started"); + }); + it("$ can be invoked by anyone", async () => { + await time.increase(await time.duration.minutes(5)); + await expect(lbpManagerInstance.connect(beneficiary).startLbp()).to.not.be + .reverted; + }); + it("$ updates state even if swap is already enabled", async () => { + await time.increase(await time.duration.minutes(10)); + await expect(lbpManagerInstance.connect(admin).setSwapEnabled(true)).to + .not.be.reverted; + expect(await lbpManagerInstance.getSwapEnabled()).to.be.true; + expect(await lbpManagerInstance.state()).to.be.equal(0); + await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be + .reverted; + expect(await lbpManagerInstance.state()).to.be.equal(1); + }); }); describe("# withdraw liquidity from the pool", () => { describe("$ fails on call exit pool", () => { From 19455a421ecec9a59136d38344556a80ac75dc23 Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Thu, 6 Jan 2022 19:59:22 +0530 Subject: [PATCH 06/11] updated contract with new error string and endLbp to only updated swapEnabled if required --- contracts/lbp/LBPManager.sol | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/contracts/lbp/LBPManager.sol b/contracts/lbp/LBPManager.sol index 152452d5..801b3cd0 100644 --- a/contracts/lbp/LBPManager.sol +++ b/contracts/lbp/LBPManager.sol @@ -269,15 +269,17 @@ contract LBPManager { bool isSwapEnabled = lbp.getSwapEnabled(); (, endTime, ) = lbp.getGradualWeightUpdateParams(); require( - isSwapEnabled && state == TaskState.Started, - "LBPManager: swapping disabled" + state == TaskState.Started, + "LBPManager: not started or already ended" ); require( block.timestamp >= startTimeEndTime[1], "LBPManager: haven't reached end time" ); state = TaskState.Ended; - lbp.setSwapEnabled(false); + if (isSwapEnabled) { + lbp.setSwapEnabled(false); + } } /** From cfff0f1e749da0e19b4a952fddef2528f588ffd7 Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Thu, 6 Jan 2022 19:59:40 +0530 Subject: [PATCH 07/11] added test for endLbp --- test/unit/lbp-manager.spec.js | 50 ++++++++++++++++------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/test/unit/lbp-manager.spec.js b/test/unit/lbp-manager.spec.js index c05a58dc..5da52ca9 100644 --- a/test/unit/lbp-manager.spec.js +++ b/test/unit/lbp-manager.spec.js @@ -1043,11 +1043,6 @@ describe(">> Contract: LBPManager", () => { lbpManagerInstance.connect(owner).startLbp() ).to.be.revertedWith("LBPManager: one block before start time"); }); - it("$ cannot use setSwapEnabled outside the weight change period", async () => { - await expect( - lbpManagerInstance.connect(admin).setSwapEnabled(true) - ).to.be.revertedWith("LBPManager: only between start time and end time"); - }); it("$ starts lbp swapping", async () => { await time.increase(await time.duration.minutes(5)); expect(await lbpManagerInstance.getSwapEnabled()).to.be.false; @@ -1151,9 +1146,7 @@ describe(">> Contract: LBPManager", () => { feePercentage: FEE_PERCENTAGE_ZERO, }; - const startTime = (await time.latest()).add( - await time.duration.minutes(10) - ); + const startTime = await time.latest(); const endTime = startTime.add(await time.duration.minutes(20)); const initializeLBPManagerParams = paramGenerator.initializeParams( @@ -1179,46 +1172,49 @@ describe(">> Contract: LBPManager", () => { ({ lbpManagerInstance, tokenInstances, amountToAddForFee } = await setupInitialState(contractInstances, initialState)); }); - it("$ reverts when invoked before condition currentTime > startTime - 5 minutes is met", async () => { + it("$ reverts when startLBP wasn't invoked", async () => { await expect( - lbpManagerInstance.connect(owner).startLbp() - ).to.be.revertedWith("LBPManager: one block before start time"); - }); - it("$ cannot use setSwapEnabled outside the weight change period", async () => { - await expect( - lbpManagerInstance.connect(admin).setSwapEnabled(true) - ).to.be.revertedWith("LBPManager: only between start time and end time"); + lbpManagerInstance.connect(owner).endLbp() + ).to.be.revertedWith("LBPManager: not started or already ended"); }); - it("$ starts lbp swapping", async () => { - await time.increase(await time.duration.minutes(5)); + it("$ ends lbp swapping", async () => { expect(await lbpManagerInstance.getSwapEnabled()).to.be.false; await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be .reverted; expect(await lbpManagerInstance.state()).to.be.equal(1); + await time.increase(await time.duration.minutes(20)); expect(await lbpManagerInstance.getSwapEnabled()).to.be.true; + await expect(lbpManagerInstance.connect(owner).endLbp()).to.not.be + .reverted; + expect(await lbpManagerInstance.getSwapEnabled()).to.be.false; + expect(await lbpManagerInstance.state()).to.be.equal(2); }); it("$ can only be invoked once", async () => { - await time.increase(await time.duration.minutes(5)); await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be .reverted; + await time.increase(await time.duration.minutes(20)); + await expect(lbpManagerInstance.connect(owner).endLbp()).to.not.be + .reverted; await expect( - lbpManagerInstance.connect(owner).startLbp() - ).to.be.revertedWith("LBPManager: already started"); + lbpManagerInstance.connect(owner).endLbp() + ).to.be.revertedWith("LBPManager: not started or already ended"); }); it("$ can be invoked by anyone", async () => { await time.increase(await time.duration.minutes(5)); await expect(lbpManagerInstance.connect(beneficiary).startLbp()).to.not.be .reverted; }); - it("$ updates state even if swap is already enabled", async () => { - await time.increase(await time.duration.minutes(10)); - await expect(lbpManagerInstance.connect(admin).setSwapEnabled(true)).to - .not.be.reverted; - expect(await lbpManagerInstance.getSwapEnabled()).to.be.true; - expect(await lbpManagerInstance.state()).to.be.equal(0); + it("$ updates state even if swap is already disabled", async () => { await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be .reverted; + await expect(lbpManagerInstance.connect(admin).setSwapEnabled(false)).to + .not.be.reverted; + await time.increase(await time.duration.minutes(20)); expect(await lbpManagerInstance.state()).to.be.equal(1); + expect(await lbpManagerInstance.getSwapEnabled()).to.be.false; + await expect(lbpManagerInstance.connect(owner).endLbp()).to.not.be + .reverted; + expect(await lbpManagerInstance.state()).to.be.equal(2); }); }); describe("# withdraw liquidity from the pool", () => { From 27dbbb34f5c5cf2a727196ee798afd69890183b4 Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Thu, 6 Jan 2022 20:08:44 +0530 Subject: [PATCH 08/11] added test for end lbp to check correct implementation before end time --- test/unit/lbp-manager.spec.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/test/unit/lbp-manager.spec.js b/test/unit/lbp-manager.spec.js index 5da52ca9..4fe7ddf8 100644 --- a/test/unit/lbp-manager.spec.js +++ b/test/unit/lbp-manager.spec.js @@ -1177,6 +1177,13 @@ describe(">> Contract: LBPManager", () => { lbpManagerInstance.connect(owner).endLbp() ).to.be.revertedWith("LBPManager: not started or already ended"); }); + it("$ reverts when current time < end time", async () => { + await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be + .reverted; + await expect( + lbpManagerInstance.connect(owner).endLbp() + ).to.be.revertedWith("LBPManager: haven't reached end time"); + }); it("$ ends lbp swapping", async () => { expect(await lbpManagerInstance.getSwapEnabled()).to.be.false; await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be @@ -1200,8 +1207,10 @@ describe(">> Contract: LBPManager", () => { ).to.be.revertedWith("LBPManager: not started or already ended"); }); it("$ can be invoked by anyone", async () => { - await time.increase(await time.duration.minutes(5)); - await expect(lbpManagerInstance.connect(beneficiary).startLbp()).to.not.be + await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be + .reverted; + await time.increase(await time.duration.minutes(20)); + await expect(lbpManagerInstance.connect(beneficiary).endLbp()).to.not.be .reverted; }); it("$ updates state even if swap is already disabled", async () => { From 302a3df5f9a3810e4ccae26fc6f0f28d2ab2edc4 Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Thu, 6 Jan 2022 20:09:07 +0530 Subject: [PATCH 09/11] removed unnecessary check --- contracts/lbp/LBPManager.sol | 1 - 1 file changed, 1 deletion(-) diff --git a/contracts/lbp/LBPManager.sol b/contracts/lbp/LBPManager.sol index 801b3cd0..f2d5d153 100644 --- a/contracts/lbp/LBPManager.sol +++ b/contracts/lbp/LBPManager.sol @@ -186,7 +186,6 @@ contract LBPManager { */ function initializeLBP(address _sender) external onlyAdmin { // solhint-disable-next-line reason-string - require(initialized == true, "LBPManager: LBPManager not initialized"); require(!poolFunded, "LBPManager: pool already funded"); poolFunded = true; From 4b4386e20b020bb7121dcabb8687578aaeee7500 Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Mon, 10 Jan 2022 19:45:24 +0530 Subject: [PATCH 10/11] refacotored error strings --- contracts/lbp/LBPManager.sol | 15 ++++++++++----- test/unit/lbp-manager.spec.js | 10 +++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/contracts/lbp/LBPManager.sol b/contracts/lbp/LBPManager.sol index f2d5d153..7d6da89a 100644 --- a/contracts/lbp/LBPManager.sol +++ b/contracts/lbp/LBPManager.sol @@ -25,6 +25,11 @@ import "../utils/interface/ILBP.sol"; */ // solhint-disable-next-line max-states-count contract LBPManager { + /* + Waiting:- current time < start time - 5 minutes, swapping is disabled + Started:- start time - 5 minutes <= current time < endTime, swapping enabled, can also be updated by admin + Ended:- current time > end time, swapping disableds + */ enum TaskState { Waiting, Started, @@ -240,7 +245,7 @@ contract LBPManager { } /** - * @dev start LBP just before one block + * @dev start LBP before 5 minutes of start time * @notice it can be invoked by anyone only once */ function startLbp() external { @@ -248,10 +253,10 @@ contract LBPManager { uint256 buffer = 5 minutes; bool isSwapEnabled = lbp.getSwapEnabled(); (startTime, , ) = lbp.getGradualWeightUpdateParams(); - require(state == TaskState.Waiting, "LBPManager: already started"); + require(state == TaskState.Waiting, "LBPManager: started"); require( block.timestamp > startTimeEndTime[0] - buffer, - "LBPManager: one block before start time" + "LBPManager: not the right time" ); state = TaskState.Started; if (!isSwapEnabled) { @@ -269,11 +274,11 @@ contract LBPManager { (, endTime, ) = lbp.getGradualWeightUpdateParams(); require( state == TaskState.Started, - "LBPManager: not started or already ended" + "LBPManager: !started or ended" ); require( block.timestamp >= startTimeEndTime[1], - "LBPManager: haven't reached end time" + "LBPManager: after >= end time" ); state = TaskState.Ended; if (isSwapEnabled) { diff --git a/test/unit/lbp-manager.spec.js b/test/unit/lbp-manager.spec.js index 4fe7ddf8..0a8799ea 100644 --- a/test/unit/lbp-manager.spec.js +++ b/test/unit/lbp-manager.spec.js @@ -1041,7 +1041,7 @@ describe(">> Contract: LBPManager", () => { it("$ reverts when invoked before condition currentTime > startTime - 5 minutes is met", async () => { await expect( lbpManagerInstance.connect(owner).startLbp() - ).to.be.revertedWith("LBPManager: one block before start time"); + ).to.be.revertedWith("LBPManager: not the right time"); }); it("$ starts lbp swapping", async () => { await time.increase(await time.duration.minutes(5)); @@ -1057,7 +1057,7 @@ describe(">> Contract: LBPManager", () => { .reverted; await expect( lbpManagerInstance.connect(owner).startLbp() - ).to.be.revertedWith("LBPManager: already started"); + ).to.be.revertedWith("LBPManager: started"); }); it("$ can be invoked by anyone", async () => { await time.increase(await time.duration.minutes(5)); @@ -1175,14 +1175,14 @@ describe(">> Contract: LBPManager", () => { it("$ reverts when startLBP wasn't invoked", async () => { await expect( lbpManagerInstance.connect(owner).endLbp() - ).to.be.revertedWith("LBPManager: not started or already ended"); + ).to.be.revertedWith("LBPManager: !started or ended"); }); it("$ reverts when current time < end time", async () => { await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be .reverted; await expect( lbpManagerInstance.connect(owner).endLbp() - ).to.be.revertedWith("LBPManager: haven't reached end time"); + ).to.be.revertedWith("LBPManager: after >= end time"); }); it("$ ends lbp swapping", async () => { expect(await lbpManagerInstance.getSwapEnabled()).to.be.false; @@ -1204,7 +1204,7 @@ describe(">> Contract: LBPManager", () => { .reverted; await expect( lbpManagerInstance.connect(owner).endLbp() - ).to.be.revertedWith("LBPManager: not started or already ended"); + ).to.be.revertedWith("LBPManager: !started or ended"); }); it("$ can be invoked by anyone", async () => { await expect(lbpManagerInstance.connect(owner).startLbp()).to.not.be From 1b50cfeef61efbfaad67abd749fbf92c0eebe712 Mon Sep 17 00:00:00 2001 From: Mihirsinh Parmar Date: Mon, 10 Jan 2022 19:45:43 +0530 Subject: [PATCH 11/11] added formatted files --- contracts/lbp/LBPManager.sol | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/contracts/lbp/LBPManager.sol b/contracts/lbp/LBPManager.sol index 7d6da89a..e226bb4c 100644 --- a/contracts/lbp/LBPManager.sol +++ b/contracts/lbp/LBPManager.sol @@ -272,10 +272,7 @@ contract LBPManager { uint256 endTime; bool isSwapEnabled = lbp.getSwapEnabled(); (, endTime, ) = lbp.getGradualWeightUpdateParams(); - require( - state == TaskState.Started, - "LBPManager: !started or ended" - ); + require(state == TaskState.Started, "LBPManager: !started or ended"); require( block.timestamp >= startTimeEndTime[1], "LBPManager: after >= end time"