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

Chore: Automate end start swapping #235

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
58 changes: 56 additions & 2 deletions contracts/lbp/LBPManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ 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,
Ended
}
TaskState public state;
// Constants
uint256 private constant HUNDRED_PERCENT = 1e18; // Used in calculating the fee.

Expand Down Expand Up @@ -180,7 +191,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;

Expand All @@ -192,7 +202,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.
)
);

Expand Down Expand Up @@ -234,6 +244,45 @@ contract LBPManager {
vault.joinPool(lbp.getPoolId(), address(this), address(this), request);
}

/**
* @dev start LBP before 5 minutes of start time
* @notice it can be invoked by anyone only once
*/
function startLbp() external {
uint256 startTime;
uint256 buffer = 5 minutes;
bool isSwapEnabled = lbp.getSwapEnabled();
(startTime, , ) = lbp.getGradualWeightUpdateParams();
require(state == TaskState.Waiting, "LBPManager: started");
require(
Copy link

Choose a reason for hiding this comment

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

image

block.timestamp > startTimeEndTime[0] - buffer,
"LBPManager: not the right time"
);
state = TaskState.Started;
if (!isSwapEnabled) {
lbp.setSwapEnabled(true);
}
}

/**
* @dev ends LBP once end time is reached
* @notice it can be invoked by anyone only once
*/
function endLbp() external {
uint256 endTime;
bool isSwapEnabled = lbp.getSwapEnabled();
(, endTime, ) = lbp.getGradualWeightUpdateParams();
require(state == TaskState.Started, "LBPManager: !started or ended");
require(
block.timestamp >= startTimeEndTime[1],
"LBPManager: after >= end time"
);
state = TaskState.Ended;
if (isSwapEnabled) {
lbp.setSwapEnabled(false);
}
}

/**
* @dev Exit pool or remove liquidity from pool.
Copy link

Choose a reason for hiding this comment

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

This documentation is wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

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

documentation for removeLiquidity?

Copy link

Choose a reason for hiding this comment

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

The documentation on the line linked to this comment. "Exit pool or remove liquidity from pool."

Copy link

@dkent600 dkent600 Jan 6, 2022

Choose a reason for hiding this comment

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

That documentation is completely wrong for setSwapEnabled

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm, This documentation is for removeLiquidity. The lines between 286-349 are hidden due to no changes. The correct docs for setSwapEnabled starts at line 347.

* @param _receiver Address of the liquidity receiver, after exiting the LBP.
Expand Down Expand Up @@ -302,6 +351,11 @@ contract LBPManager {
* @param _swapEnabled Enables/disables swapping.
*/
function setSwapEnabled(bool _swapEnabled) external onlyAdmin {
require(
block.timestamp >= startTimeEndTime[0] &&
block.timestamp < startTimeEndTime[1],
"LBPManager: only between start time and end time"
);
lbp.setSwapEnabled(_swapEnabled);
}

Expand Down
192 changes: 192 additions & 0 deletions test/unit/lbp-manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,13 +1003,105 @@ 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: not the right 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: 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 = {
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(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,
Expand All @@ -1023,16 +1115,116 @@ 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();
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 startLBP wasn't invoked", async () => {
await expect(
lbpManagerInstance.connect(owner).endLbp()
).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: after >= end time");
});
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 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).endLbp()
).to.be.revertedWith("LBPManager: !started or ended");
});
it("$ can be invoked by anyone", async () => {
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 () => {
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", () => {
describe("$ fails on call exit pool", () => {
Expand Down