Skip to content

Commit

Permalink
accmumulate throttles during dissolve()/mint() (#1197)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrent authored Sep 10, 2024
1 parent 57456f9 commit 8697de8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions contracts/p0/RToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ contract RTokenP0 is ComponentP0, ERC20PermitUpgradeable, IRToken {
/// @custom:protected
function mint(uint192 baskets) external exchangeRateIsValidAfter {
require(_msgSender() == address(main.backingManager()), "not backing manager");
issuanceThrottle.useAvailable(totalSupply(), 0);
redemptionThrottle.useAvailable(totalSupply(), 0);
_scaleUp(address(main.backingManager()), baskets);
}

Expand All @@ -285,6 +287,8 @@ contract RTokenP0 is ComponentP0, ERC20PermitUpgradeable, IRToken {
/// @custom:protected
function dissolve(uint256 amount) external exchangeRateIsValidAfter {
require(_msgSender() == address(main.backingManager()), "not backing manager");
issuanceThrottle.useAvailable(totalSupply(), 0);
redemptionThrottle.useAvailable(totalSupply(), 0);
_scaleDown(_msgSender(), amount);
}

Expand Down
13 changes: 12 additions & 1 deletion contracts/p1/RToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,12 @@ contract RTokenP1 is ComponentP1, ERC20PermitUpgradeable, IRToken {
// BU exchange rate cannot decrease, and it can only increase when < FIX_ONE.
function mint(uint192 baskets) external {
require(_msgSender() == address(backingManager), "not backing manager");
_scaleUp(address(backingManager), baskets, totalSupply());
uint256 supply = totalSupply();

// Accumulate the throttle before the supply change
issuanceThrottle.useAvailable(supply, 0);
redemptionThrottle.useAvailable(supply, 0);
_scaleUp(address(backingManager), baskets, supply);
}

/// Melt a quantity of RToken from the caller's account, increasing the basket rate
Expand All @@ -372,6 +377,7 @@ contract RTokenP1 is ComponentP1, ERC20PermitUpgradeable, IRToken {
require(caller == address(furnace), "furnace only");
_burn(caller, amtRToken);
emit Melted(amtRToken);
// do not update throttles: melting is frequent and always small
}

/// Burn an amount of RToken from caller's account and scale basketsNeeded down
Expand All @@ -387,6 +393,11 @@ contract RTokenP1 is ComponentP1, ERC20PermitUpgradeable, IRToken {
function dissolve(uint256 amount) external {
address caller = _msgSender();
require(caller == address(backingManager), "not backing manager");
uint256 supply = totalSupply();

// Accumulate the throttle before the supply change
issuanceThrottle.useAvailable(supply, 0);
redemptionThrottle.useAvailable(supply, 0);
_scaleDown(caller, amount);
}

Expand Down

0 comments on commit 8697de8

Please sign in to comment.