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

Fix Review #1

Closed
wants to merge 44 commits into from
Closed

Fix Review #1

wants to merge 44 commits into from

Conversation

sherlock-admin
Copy link
Contributor

Fix Review of

Repo: mento-protocol/mento-core
Commit Hash: f4e3b950b4beea4f94fd0dcd0b7170c4e86f9cf7

Comment on lines +125 to +131

if (tokenIn == exchange.tokenAddress) {
require(scaledAmountIn < exchange.tokenSupply, "amountIn is greater than tokenSupply");
// apply exit contribution
scaledAmountIn = (scaledAmountIn * (MAX_WEIGHT - exchange.exitContribution)) / MAX_WEIGHT;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#557.

Fixes issues: #36, #64

Comment on lines +147 to +153

if (tokenIn == exchange.tokenAddress) {
// apply exit contribution
scaledAmountIn = (scaledAmountIn * MAX_WEIGHT) / (MAX_WEIGHT - exchange.exitContribution);
require(scaledAmountIn < exchange.tokenSupply, "amountIn is greater than tokenSupply");
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#557.

Fixes issues: #36, #64

PoolExchange memory exchange = getPoolExchange(exchangeId);
uint256 scaledReserveRatio = uint256(exchange.reserveRatio) * 1e10;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#560.

Fixes issues: #23

Comment on lines +165 to +167
uint256 priceScaled = unwrap(wrap(exchange.reserveBalance).div(denominator));

price = priceScaled / tokenPrecisionMultipliers[exchange.reserveAsset];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#560.

Fixes issues: #23

@@ -165,9 +180,9 @@ contract BancorExchangeProvider is IExchangeProvider, IBancorExchangeProvider, B

/// @inheritdoc IBancorExchangeProvider
function setReserve(address _reserve) public onlyOwner {
require(address(_reserve) != address(0), "Reserve address must be set");
require(_reserve != address(0), "Reserve address must be set");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#545.

Fixes issues: #79

reserve = IReserve(_reserve);
emit ReserveUpdated(address(_reserve));
emit ReserveUpdated(_reserve);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#545.

Fixes issues: #79

Comment on lines +215 to +223
uint256 exitContribution = 0;

if (tokenIn == exchange.tokenAddress) {
require(scaledAmountIn < exchange.tokenSupply, "amountIn is greater than tokenSupply");
// apply exit contribution
exitContribution = (scaledAmountIn * exchange.exitContribution) / MAX_WEIGHT;
scaledAmountIn -= exitContribution;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#557.

Fixes issues: #36, #64

uint256 scaledAmountOut = _getScaledAmountOut(exchange, tokenIn, tokenOut, scaledAmountIn);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#557.

Fixes issues: #36, #64

Comment on lines +227 to +229
if (exitContribution > 0) {
_accountExitContribution(exchangeId, exitContribution);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#557.

Fixes issues: #36, #64

Comment on lines +245 to +258

uint256 exitContribution = 0;
uint256 scaledAmountInWithExitContribution = scaledAmountIn;

if (tokenIn == exchange.tokenAddress) {
// apply exit contribution
scaledAmountInWithExitContribution = (scaledAmountIn * MAX_WEIGHT) / (MAX_WEIGHT - exchange.exitContribution);
require(
scaledAmountInWithExitContribution < exchange.tokenSupply,
"amountIn required is greater than tokenSupply"
);
exitContribution = scaledAmountInWithExitContribution - scaledAmountIn;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#557.

Fixes issues: #36, #64

Comment on lines +260 to +262
if (exitContribution > 0) {
_accountExitContribution(exchangeId, exitContribution);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#557.

Fixes issues: #36, #64


amountIn = scaledAmountIn / tokenPrecisionMultipliers[tokenIn];
amountIn = scaledAmountInWithExitContribution / tokenPrecisionMultipliers[tokenIn];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#557.

Fixes issues: #36, #64

@@ -262,7 +307,7 @@ contract BancorExchangeProvider is IExchangeProvider, IBancorExchangeProvider, B

function _setExitContribution(bytes32 exchangeId, uint32 exitContribution) internal {
require(exchanges[exchangeId].reserveAsset != address(0), "Exchange does not exist");
require(exitContribution <= MAX_WEIGHT, "Exit contribution is too high");
require(exitContribution < MAX_WEIGHT, "Exit contribution is too high");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#549.

Fixes issues: #55

Comment on lines +338 to +358
/**
* @notice Accounting of exit contribution on a swap.
* @dev Accounting of exit contribution without changing the current price of an exchange.
* this is done by updating the reserve ratio and subtracting the exit contribution from the token supply.
* Formula: newRatio = (Supply * oldRatio) / (Supply - exitContribution)
* @param exchangeId The ID of the pool
* @param exitContribution The amount of the token to be removed from the pool, scaled to 18 decimals
*/
function _accountExitContribution(bytes32 exchangeId, uint256 exitContribution) internal {
PoolExchange memory exchange = getPoolExchange(exchangeId);
uint256 scaledReserveRatio = uint256(exchange.reserveRatio) * 1e10;
UD60x18 nominator = wrap(exchange.tokenSupply).mul(wrap(scaledReserveRatio));
UD60x18 denominator = wrap(exchange.tokenSupply - exitContribution);
UD60x18 newRatioScaled = nominator.div(denominator);

uint256 newRatio = unwrap(newRatioScaled) / 1e10;

exchanges[exchangeId].reserveRatio = uint32(newRatio);
exchanges[exchangeId].tokenSupply -= exitContribution;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#557.

Fixes issues: #36, #64

@@ -362,6 +424,7 @@ contract BancorExchangeProvider is IExchangeProvider, IBancorExchangeProvider, B
require(exchange.reserveRatio > 1, "Reserve ratio is too low");
require(exchange.reserveRatio <= MAX_WEIGHT, "Reserve ratio is too high");
require(exchange.exitContribution <= MAX_WEIGHT, "Exit contribution is too high");
require(exchange.reserveBalance > 0, "Reserve balance must be greater than 0");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#558.

Fixes issues: #57

Comment on lines +147 to +152

// The division and multiplication by 1e10 here ensures that the new ratio used for calculating the amount to mint
// is the same as the one set in the exchange but only scaled to 18 decimals.
// Ignored, because the division and multiplication by 1e10 is needed see comment above.
// slither-disable-next-line divide-before-multiply
UD60x18 newRatio = wrap((unwrap(scaledRatio.mul(wrap(reserveRatioScalar))) / 1e10) * 1e10);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#548.

Fixes issues: #21

@@ -190,21 +195,31 @@ contract GoodDollarExchangeProvider is IGoodDollarExchangeProvider, BancorExchan
/**
* @inheritdoc IGoodDollarExchangeProvider
* @dev Calculates the new reserve ratio needed to mint the G$ reward while keeping the current price the same.
* calculation: newRatio = reserveBalance / (tokenSupply + reward) * currentPrice
* calculation: newRatio = (tokenSupply * reserveRatio) / (tokenSupply + reward)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#554.

Fixes issues: #59

Comment on lines +200 to +204
function updateRatioForReward(
bytes32 exchangeId,
uint256 reward,
uint256 maxSlippagePercentage
) external onlyExpansionController whenNotPaused {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#554.

Fixes issues: #59

Comment on lines +207 to +212
uint256 scaledRatio = uint256(exchange.reserveRatio) * 1e10;
uint256 scaledReward = reward * tokenPrecisionMultipliers[exchange.tokenAddress];

UD60x18 numerator = wrap(exchange.tokenSupply).mul(wrap(scaledRatio));
UD60x18 denominator = wrap(exchange.tokenSupply).add(wrap(scaledReward));
uint256 newScaledRatio = unwrap(numerator.div(denominator));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#554.

Fixes issues: #59

UD60x18 denominator = wrap(exchange.tokenSupply).add(wrap(scaledReward));
uint256 newScaledRatio = unwrap(numerator.div(denominator));

uint32 newRatioUint = uint32(newScaledRatio / 1e10);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#554.

Fixes issues: #59

Comment on lines +216 to 217
require(newRatioUint > 0, "New ratio must be greater than 0");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#544.

Fixes issues: #29

Comment on lines +218 to 220
uint256 allowedSlippage = (exchange.reserveRatio * maxSlippagePercentage) / MAX_WEIGHT;
require(exchange.reserveRatio - newRatioUint <= allowedSlippage, "Slippage exceeded");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#554.

Fixes issues: #59

exchanges[exchangeId].reserveRatio = newRatioUint;
exchanges[exchangeId].tokenSupply += rewardScaled;
exchanges[exchangeId].tokenSupply += scaledReward;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#554.

Fixes issues: #59

@@ -5,24 +5,27 @@ import { IGoodDollarExpansionController } from "contracts/interfaces/IGoodDollar
import { IGoodDollarExchangeProvider } from "contracts/interfaces/IGoodDollarExchangeProvider.sol";
import { IBancorExchangeProvider } from "contracts/interfaces/IBancorExchangeProvider.sol";
import { IERC20 } from "openzeppelin-contracts-next/contracts/token/ERC20/IERC20.sol";
import { IERC20Metadata } from "openzeppelin-contracts-next/contracts/token/ERC20/extensions/IERC20Metadata.sol";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#551.

Fixes issues: #7

import { OwnableUpgradeable } from "openzeppelin-contracts-upgradeable/contracts/access/OwnableUpgradeable.sol";
import { unwrap, wrap, powu } from "prb/math/UD60x18.sol";

/**
* @title GoodDollarExpansionController
* @notice Provides functionality to expand the supply of GoodDollars.
*/
contract GoodDollarExpansionController is IGoodDollarExpansionController, PausableUpgradeable, OwnableUpgradeable {
contract GoodDollarExpansionController is IGoodDollarExpansionController, OwnableUpgradeable {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#552.

Fixes issues: #49

Comment on lines +24 to +28
// EXPANSION_MAX_WEIGHT is the max rate that can be assigned to an exchange
uint256 public constant EXPANSION_MAX_WEIGHT = 1e18;

// BANCOR_MAX_WEIGHT is used for BPS calculations in GoodDollarExchangeProvider
uint32 public constant BANCOR_MAX_WEIGHT = 1e8;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#554.

Fixes issues: #59

@@ -123,7 +125,7 @@ contract GoodDollarExpansionController is IGoodDollarExpansionController, Pausab

/// @inheritdoc IGoodDollarExpansionController
function setExpansionConfig(bytes32 exchangeId, uint64 expansionRate, uint32 expansionFrequency) external onlyAvatar {
require(expansionRate < MAX_WEIGHT, "Expansion rate must be less than 100%");
require(expansionRate < EXPANSION_MAX_WEIGHT, "Expansion rate must be less than 100%");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#554.

Fixes issues: #59

Comment on lines +159 to +161
uint256 contractReserveBalance = IERC20(exchange.reserveAsset).balanceOf(reserve) *
(10 ** (18 - IERC20Metadata(exchange.reserveAsset).decimals()));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#551.

Fixes issues: #7

@@ -172,11 +176,10 @@ contract GoodDollarExpansionController is IGoodDollarExpansionController, Pausab
.getPoolExchange(exchangeId);
ExchangeExpansionConfig memory config = getExpansionConfig(exchangeId);

bool shouldExpand = block.timestamp > config.lastExpansion + config.expansionFrequency;
bool shouldExpand = block.timestamp >= config.lastExpansion + config.expansionFrequency;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change related to mento-protocol/mento-core#553.

Fixes issues: #50

@rcstanciu rcstanciu closed this Dec 4, 2024
@rcstanciu rcstanciu deleted the fix-review branch December 4, 2024 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants