Skip to content

Commit

Permalink
feat: use 1e8 bps
Browse files Browse the repository at this point in the history
  • Loading branch information
baroooo committed Nov 25, 2024
1 parent 637a861 commit 1d86bd5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion contracts/goodDollar/GoodDollarExchangeProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ contract GoodDollarExchangeProvider is IGoodDollarExchangeProvider, BancorExchan

require(newRatioUint > 0, "New ratio must be greater than 0");

uint256 allowedSlippage = (exchange.reserveRatio * maxSlippagePercentage) / 100;
uint256 allowedSlippage = (exchange.reserveRatio * maxSlippagePercentage) / MAX_WEIGHT;
require(exchange.reserveRatio - newRatioUint <= allowedSlippage, "Slippage exceeded");

exchanges[exchangeId].reserveRatio = newRatioUint;
Expand Down
6 changes: 4 additions & 2 deletions contracts/goodDollar/GoodDollarExpansionController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ contract GoodDollarExpansionController is IGoodDollarExpansionController, Pausab
// MAX_WEIGHT is the max rate that can be assigned to an exchange
uint256 public constant MAX_WEIGHT = 1e18;

uint32 public constant BANCOR_MAX_WEIGHT = 1e8;

// Address of the distribution helper contract
IDistributionHelper public distributionHelper;

Expand Down Expand Up @@ -191,7 +193,7 @@ contract GoodDollarExpansionController is IGoodDollarExpansionController, Pausab
/// @inheritdoc IGoodDollarExpansionController
function mintRewardFromReserveRatio(bytes32 exchangeId, address to, uint256 amount) external onlyAvatar {
// Defaults to no slippage protection
mintRewardFromReserveRatio(exchangeId, to, amount, 100);
mintRewardFromReserveRatio(exchangeId, to, amount, BANCOR_MAX_WEIGHT);
}

/// @inheritdoc IGoodDollarExpansionController
Expand All @@ -203,7 +205,7 @@ contract GoodDollarExpansionController is IGoodDollarExpansionController, Pausab
) public onlyAvatar {
require(to != address(0), "Recipient address must be set");
require(amount > 0, "Amount must be greater than 0");
require(maxSlippagePercentage <= 100, "Max slippage percentage cannot be greater than 100%");
require(maxSlippagePercentage <= BANCOR_MAX_WEIGHT, "Max slippage percentage cannot be greater than 100%");
IBancorExchangeProvider.PoolExchange memory exchange = IBancorExchangeProvider(address(goodDollarExchangeProvider))
.getPoolExchange(exchangeId);

Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IGoodDollarExchangeProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ interface IGoodDollarExchangeProvider {
* @notice Calculates the reserve ratio needed to mint the given G$ reward.
* @param exchangeId The ID of the pool the G$ reward is minted from.
* @param reward The amount of G$ tokens to be minted as a reward.
* @param maxSlippagePercentage Maximum allowed percentage difference between new and old reserve ratio (0-100).
* @param maxSlippagePercentage Maximum allowed percentage difference between new and old reserve ratio (0-1e8).
*/
function updateRatioForReward(bytes32 exchangeId, uint256 reward, uint256 maxSlippagePercentage) external;

Expand Down
20 changes: 10 additions & 10 deletions test/unit/goodDollar/GoodDollarExchangeProvider.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -686,13 +686,13 @@ contract GoodDollarExchangeProviderTest_updateRatioForReward is GoodDollarExchan
function test_updateRatioForReward_whenCallerIsNotExpansionController_shouldRevert() public {
vm.prank(makeAddr("NotExpansionController"));
vm.expectRevert("Only ExpansionController can call this function");
exchangeProvider.updateRatioForReward(exchangeId, reward, 100);
exchangeProvider.updateRatioForReward(exchangeId, reward, 1e8);
}

function test_updateRatioForReward_whenExchangeIdIsInvalid_shouldRevert() public {
vm.prank(expansionControllerAddress);
vm.expectRevert("Exchange does not exist");
exchangeProvider.updateRatioForReward(bytes32(0), reward, 100);
exchangeProvider.updateRatioForReward(bytes32(0), reward, 1e8);
}

function test_updateRatioForReward_whenRewardLarger0_shouldReturnCorrectRatioAndEmit() public {
Expand All @@ -704,7 +704,7 @@ contract GoodDollarExchangeProviderTest_updateRatioForReward is GoodDollarExchan
vm.expectEmit(true, true, true, true);
emit ReserveRatioUpdated(exchangeId, expectedReserveRatio);
vm.prank(expansionControllerAddress);
exchangeProvider.updateRatioForReward(exchangeId, reward, 100);
exchangeProvider.updateRatioForReward(exchangeId, reward, 1e8);

IBancorExchangeProvider.PoolExchange memory poolExchangeAfter = exchangeProvider.getPoolExchange(exchangeId);
uint256 priceAfter = exchangeProvider.currentPrice(exchangeId);
Expand All @@ -730,7 +730,7 @@ contract GoodDollarExchangeProviderTest_updateRatioForReward is GoodDollarExchan
vm.expectEmit(true, true, true, true);
emit ReserveRatioUpdated(exchangeId, expectedReserveRatio);
vm.prank(expansionControllerAddress);
exchangeProvider.updateRatioForReward(exchangeId, _reward, 100);
exchangeProvider.updateRatioForReward(exchangeId, _reward, 1e8);

IBancorExchangeProvider.PoolExchange memory poolExchangeAfter = exchangeProvider.getPoolExchange(exchangeId);
uint256 priceAfter = exchangeProvider.currentPrice(exchangeId);
Expand All @@ -755,7 +755,7 @@ contract GoodDollarExchangeProviderTest_updateRatioForReward is GoodDollarExchan
vm.expectEmit(true, true, true, true);
emit ReserveRatioUpdated(exchangeId, expectedReserveRatio);
vm.prank(expansionControllerAddress);
exchangeProvider.updateRatioForReward(exchangeId, _reward, 100);
exchangeProvider.updateRatioForReward(exchangeId, _reward, 1e8);

IBancorExchangeProvider.PoolExchange memory poolExchangeAfter = exchangeProvider.getPoolExchange(exchangeId);
uint256 priceAfter = exchangeProvider.currentPrice(exchangeId);
Expand All @@ -779,12 +779,12 @@ contract GoodDollarExchangeProviderTest_updateRatioForReward is GoodDollarExchan

vm.prank(expansionControllerAddress);
vm.expectRevert("Slippage exceeded");
exchangeProvider.updateRatioForReward(exchangeId, _reward, 12);
exchangeProvider.updateRatioForReward(exchangeId, _reward, 12 * 1e6);

vm.expectEmit(true, true, true, true);
emit ReserveRatioUpdated(exchangeId, expectedReserveRatio);
vm.prank(expansionControllerAddress);
exchangeProvider.updateRatioForReward(exchangeId, _reward, 13);
exchangeProvider.updateRatioForReward(exchangeId, _reward, 13 * 1e6);
}

function test_updateRatioForReward_withMultipleConsecutiveRewards() public {
Expand All @@ -796,7 +796,7 @@ contract GoodDollarExchangeProviderTest_updateRatioForReward is GoodDollarExchan

vm.startPrank(expansionControllerAddress);
for (uint256 i = 0; i < 5; i++) {
exchangeProvider.updateRatioForReward(exchangeId, reward, 100);
exchangeProvider.updateRatioForReward(exchangeId, reward, 1e8);
totalReward += reward;
}
vm.stopPrank();
Expand Down Expand Up @@ -824,7 +824,7 @@ contract GoodDollarExchangeProviderTest_updateRatioForReward is GoodDollarExchan
uint256 priceBefore = exchangeProvider.currentPrice(exchangeId);

vm.prank(expansionControllerAddress);
exchangeProvider.updateRatioForReward(exchangeId, fuzzedReward, 100);
exchangeProvider.updateRatioForReward(exchangeId, fuzzedReward, 1e8);

IBancorExchangeProvider.PoolExchange memory poolExchangeAfter = exchangeProvider.getPoolExchange(exchangeId);
uint256 priceAfter = exchangeProvider.currentPrice(exchangeId);
Expand Down Expand Up @@ -910,6 +910,6 @@ contract GoodDollarExchangeProviderTest_pausable is GoodDollarExchangeProviderTe

exchangeProvider.mintFromExpansion(exchangeId, 1e18);
exchangeProvider.mintFromInterest(exchangeId, 1e18);
exchangeProvider.updateRatioForReward(exchangeId, 1e18, 100);
exchangeProvider.updateRatioForReward(exchangeId, 1e18, 1e8);
}
}
2 changes: 1 addition & 1 deletion test/unit/goodDollar/GoodDollarExpansionController.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ contract GoodDollarExpansionControllerTest_mintRewardFromReserveRatio is GoodDol
function test_mintRewardFromReserveRatio_whenSlippageIsGreaterThan100_shouldRevert() public {
vm.prank(avatarAddress);
vm.expectRevert("Max slippage percentage cannot be greater than 100%");
expansionController.mintRewardFromReserveRatio(exchangeId, makeAddr("To"), 1000e18, 101);
expansionController.mintRewardFromReserveRatio(exchangeId, makeAddr("To"), 1000e18, 1e8 + 1);
}

function test_mintRewardFromReserveRatio_whenCallerIsAvatar_shouldMintAndEmit() public {
Expand Down

0 comments on commit 1d86bd5

Please sign in to comment.