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

adds constraints to setTargetPrice and writeOff functions #162

Merged
merged 2 commits into from
Feb 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions contracts/Sweep/Sweep.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ contract SweepCoin is BaseSweep {
error LessTargetPrice();
error OutOfTargetPriceChange();
error InvalidPeriodStart();
error BadLimits();

/* ======= MODIFIERS ====== */

Expand All @@ -67,6 +68,11 @@ contract SweepCoin is BaseSweep {
_;
}

modifier validTargetPrice(uint256 _target) {
_validTargetPrice(_target);
_;
}

// Constructor
function initialize(
address lzEndpoint,
Expand Down Expand Up @@ -273,7 +279,9 @@ contract SweepCoin is BaseSweep {
function setTargetPrice(
uint256 newCurrentTargetPrice,
uint256 newNextTargetPrice
) external onlyBalancer {
) external onlyBalancer validTargetPrice(newCurrentTargetPrice) validTargetPrice(newNextTargetPrice) {
_checkLimit(10000, newCurrentTargetPrice);

currentTargetPrice = newCurrentTargetPrice;
nextTargetPrice = newNextTargetPrice;

Expand Down Expand Up @@ -329,8 +337,10 @@ contract SweepCoin is BaseSweep {
* @notice Write Off
* @param newPrice.
*/
function writeOff(uint256 newPrice, address insolventDebtor) external onlyGov whenPaused {
function writeOff(uint256 newPrice, address insolventDebtor) external onlyGov whenPaused validTargetPrice(newPrice) {
if (targetPrice() < ammPrice()) revert WriteOffNotAllowed();
_checkLimit(250000, newPrice);

uint256 multiplier = SPREAD_PRECISION.mulDiv(targetPrice(), newPrice);
uint256 len = minterAddresses.length;

Expand Down Expand Up @@ -377,4 +387,15 @@ contract SweepCoin is BaseSweep {
) external view returns (uint256 sweepAmount) {
sweepAmount = usdAmount.mulDiv(10 ** decimals(), targetPrice());
}

function _validTargetPrice(uint256 _target) internal pure {
if(_target == 0) revert ZeroAmountDetected();
}

function _checkLimit(uint256 limit, uint256 _targetPrice) internal view {
uint256 lower = currentTargetPrice * (SPREAD_PRECISION - limit) / SPREAD_PRECISION;
uint256 upper = currentTargetPrice * (SPREAD_PRECISION + limit) / SPREAD_PRECISION;

if(_targetPrice < lower || _targetPrice > upper) revert BadLimits();
}
}
14 changes: 14 additions & 0 deletions test/token/sweep_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ contract("Sweep - settings", async function () {
it('sets a new current target price correctly', async () => {
newCurrentTargetPrice = 1010000;
newNextTargetPrice = 1020000;
lowerTarget = 980000;

await expect(sweep.connect(newAddress).setTargetPrice(newCurrentTargetPrice, 0))
.to.be.revertedWithCustomError(sweep, "ZeroAmountDetected");

await expect(sweep.connect(newAddress).setTargetPrice(0, newCurrentTargetPrice))
.to.be.revertedWithCustomError(sweep, "ZeroAmountDetected");

await expect(sweep.connect(newAddress).setTargetPrice(newNextTargetPrice, newNextTargetPrice))
.to.be.revertedWithCustomError(sweep, "BadLimits");

await expect(sweep.connect(newAddress).setTargetPrice(lowerTarget, newNextTargetPrice))
.to.be.revertedWithCustomError(sweep, "BadLimits");

expect(await sweep.currentTargetPrice()).to.equal(await sweep.nextTargetPrice());
await sweep.connect(newAddress).setTargetPrice(newCurrentTargetPrice, newNextTargetPrice);
expect(await sweep.currentTargetPrice()).to.equal(newCurrentTargetPrice);
Expand Down
11 changes: 11 additions & 0 deletions test/token/sweep_writeoff.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ contract("Sweep - WriteOff", async function () {
.to.be.revertedWithCustomError(sweep, "WriteOffNotAllowed")

await amm.setPrice(price);
await expect(sweep.writeOff(0, offChainAsset.address))
.to.be.revertedWithCustomError(sweep, "ZeroAmountDetected")

lowerTargetPrice = 0.74e6;
await expect(sweep.writeOff(lowerTargetPrice, offChainAsset.address))
.to.be.revertedWithCustomError(sweep, "BadLimits")

higherTargetPrice = 1.26e6;
await expect(sweep.writeOff(higherTargetPrice, offChainAsset.address))
.to.be.revertedWithCustomError(sweep, "BadLimits")

await sweep.writeOff(newTargetPrice, offChainAsset.address);

expect(await offChainAsset.sweepBorrowed()).to.equal(borrowAmount);
Expand Down
Loading