Skip to content

Commit

Permalink
Feat(454): Exchange provider tests (#529)
Browse files Browse the repository at this point in the history
### Description

This PR adds additional tests to to the exchange provider and includes
fuzz testing

### Related issues

- Fixes mento-protocol/mento-general#454
-

---------

Co-authored-by: bowd <dumbogdan@gmail.com>
Co-authored-by: philbow61 <philip.raetsch@gmail.com>
Co-authored-by: baroooo <baranseltekin@gmail.com>
Co-authored-by: philbow61 <80156619+philbow61@users.noreply.github.com>
  • Loading branch information
5 people authored Oct 23, 2024
1 parent 578747c commit 2ee14b4
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 66 deletions.
14 changes: 6 additions & 8 deletions contracts/goodDollar/GoodDollarExchangeProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,21 @@ contract GoodDollarExchangeProvider is IGoodDollarExchangeProvider, BancorExchan
*/
function mintFromExpansion(
bytes32 exchangeId,
uint256 expansionScaler
uint256 reserveRatioScalar
) external onlyExpansionController whenNotPaused returns (uint256 amountToMint) {
require(expansionScaler > 0, "Expansion rate must be greater than 0");
require(reserveRatioScalar > 0, "Reserve ratio scalar must be greater than 0");
PoolExchange memory exchange = getPoolExchange(exchangeId);

UD60x18 scaledRatio = wrap(uint256(exchange.reserveRatio) * 1e10);
UD60x18 newRatio = scaledRatio.mul(wrap(expansionScaler));
UD60x18 newRatio = scaledRatio.mul(wrap(reserveRatioScalar));

uint32 newRatioUint = uint32(unwrap(newRatio) / 1e10);
require(newRatioUint > 0, "New ratio must be greater than 0");

UD60x18 numerator = wrap(exchange.tokenSupply).mul(scaledRatio);
numerator = numerator.sub(wrap(exchange.tokenSupply).mul(newRatio));

uint256 scaledAmountToMint = unwrap(numerator.div(newRatio));
uint32 newRatioUint = uint32(unwrap(newRatio) / 1e10);

exchanges[exchangeId].reserveRatio = newRatioUint;
exchanges[exchangeId].tokenSupply += scaledAmountToMint;
Expand Down Expand Up @@ -193,10 +195,6 @@ contract GoodDollarExchangeProvider is IGoodDollarExchangeProvider, BancorExchan
function updateRatioForReward(bytes32 exchangeId, uint256 reward) external onlyExpansionController whenNotPaused {
PoolExchange memory exchange = getPoolExchange(exchangeId);

if (reward == 0) {
return;
}

uint256 currentPriceScaled = currentPrice(exchangeId) * tokenPrecisionMultipliers[exchange.reserveAsset];
uint256 rewardScaled = reward * tokenPrecisionMultipliers[exchange.tokenAddress];

Expand Down
14 changes: 7 additions & 7 deletions contracts/goodDollar/GoodDollarExpansionController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ contract GoodDollarExpansionController is IGoodDollarExpansionController, Pausab

bool shouldExpand = block.timestamp > config.lastExpansion + config.expansionFrequency;
if (shouldExpand || config.lastExpansion == 0) {
uint256 expansionScaler = _getExpansionScaler(config);
uint256 reserveRatioScalar = _getReserveRatioScalar(config);

exchangeExpansionConfigs[exchangeId].lastExpansion = uint32(block.timestamp);
amountMinted = goodDollarExchangeProvider.mintFromExpansion(exchangeId, expansionScaler);
amountMinted = goodDollarExchangeProvider.mintFromExpansion(exchangeId, reserveRatioScalar);

IGoodDollar(exchange.tokenAddress).mint(address(distributionHelper), amountMinted);
distributionHelper.onDistribution(amountMinted);
Expand Down Expand Up @@ -218,11 +218,11 @@ contract GoodDollarExpansionController is IGoodDollarExpansionController, Pausab
}

/**
* @notice Calculates the expansion scaler for the given expansion config.
* @notice Calculates the reserve ratio scalar for the given expansion config.
* @param config The expansion config.
* @return expansionScaler The expansion scaler.
* @return reserveRatioScalar The reserve ratio scalar.
*/
function _getExpansionScaler(ExchangeExpansionConfig memory config) internal view returns (uint256) {
function _getReserveRatioScalar(ExchangeExpansionConfig memory config) internal view returns (uint256) {
uint256 numberOfExpansions;

// If there was no previous expansion, we expand once.
Expand All @@ -232,7 +232,7 @@ contract GoodDollarExpansionController is IGoodDollarExpansionController, Pausab
numberOfExpansions = (block.timestamp - config.lastExpansion) / config.expansionFrequency;
}

uint256 stepExpansionScaler = MAX_WEIGHT - config.expansionRate;
return unwrap(powu(wrap(stepExpansionScaler), numberOfExpansions));
uint256 stepReserveRatioScalar = MAX_WEIGHT - config.expansionRate;
return unwrap(powu(wrap(stepReserveRatioScalar), numberOfExpansions));
}
}
4 changes: 2 additions & 2 deletions contracts/interfaces/IGoodDollarExchangeProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ interface IGoodDollarExchangeProvider {
/**
* @notice Calculates the amount of G$ tokens to be minted as a result of the expansion.
* @param exchangeId The ID of the pool to calculate the expansion for.
* @param expansionScaler Scaler for calculating the new reserve ratio.
* @param reserveRatioScalar Scaler for calculating the new reserve ratio.
* @return amountToMint Amount of G$ tokens to be minted as a result of the expansion.
*/
function mintFromExpansion(bytes32 exchangeId, uint256 expansionScaler) external returns (uint256 amountToMint);
function mintFromExpansion(bytes32 exchangeId, uint256 reserveRatioScalar) external returns (uint256 amountToMint);

/**
* @notice Calculates the amount of G$ tokens to be minted as a result of the collected reserve interest.
Expand Down
Loading

0 comments on commit 2ee14b4

Please sign in to comment.