-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from Bananapus/adjust/redemption-hook-values
adjust redemption hook values
- Loading branch information
Showing
4 changed files
with
136 additions
and
81 deletions.
There are no files selected for viewing
Submodule sphinx
updated
73 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.17; | ||
|
||
import {mulDiv} from "@prb/math/src/Common.sol"; | ||
|
||
import {JBConstants} from "./JBConstants.sol"; | ||
|
||
library JBRedemptionFormula { | ||
/// @notice The amount of surplus which is available for reclaiming via redemption given the number of tokens being | ||
/// redeemed, the total supply, the current surplus, and the current ruleset. | ||
/// @param surplus The surplus amount to make the calculation with. | ||
/// @param tokenCount The number of tokens to make the calculation with, as a fixed point number with 18 decimals. | ||
/// @param totalSupply The total supply of tokens to make the calculation with, as a fixed point number with 18 | ||
/// decimals. | ||
/// @param redemptionRate The redemption rate with which the reclaimable surplus is being calculated. | ||
/// @return The amount of surplus tokens that can be reclaimed. | ||
function reclaimableSurplusFrom( | ||
uint256 surplus, | ||
uint256 tokenCount, | ||
uint256 totalSupply, | ||
uint256 redemptionRate | ||
) | ||
internal | ||
pure | ||
returns (uint256) | ||
{ | ||
// If the redemption rate is 0, nothing is claimable. | ||
if (redemptionRate == 0) return 0; | ||
|
||
// If the amount being redeemed is the total supply, return the rest of the surplus. | ||
if (tokenCount == totalSupply) return surplus; | ||
|
||
// Get a reference to the linear proportion. | ||
uint256 base = mulDiv(surplus, tokenCount, totalSupply); | ||
|
||
// These conditions are all part of the same curve. Edge conditions are separated because fewer operation are | ||
// necessary. | ||
if (redemptionRate == JBConstants.MAX_REDEMPTION_RATE) { | ||
return base; | ||
} | ||
|
||
return mulDiv( | ||
base, | ||
redemptionRate + mulDiv(tokenCount, JBConstants.MAX_REDEMPTION_RATE - redemptionRate, totalSupply), | ||
JBConstants.MAX_REDEMPTION_RATE | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.17; | ||
|
||
import {mulDiv} from "@prb/math/src/Common.sol"; | ||
|
||
import {JBConstants} from "./JBConstants.sol"; | ||
|
||
/// @notice Redemption calculations. | ||
library JBRedemptions { | ||
/// @notice Returns the amount of surplus terminal tokens which can be reclaimed based on the total surplus, the | ||
/// number of tokens being redeemed, the total token supply, and the ruleset's redemption rate. | ||
/// @param surplus The total amount of surplus terminal tokens. | ||
/// @param tokensRedeemed The number of tokens being redeemed, as a fixed point number with 18 decimals. | ||
/// @param totalSupply The total token supply, as a fixed point number with 18 decimals. | ||
/// @param redemptionRate The current ruleset's redemption rate. | ||
/// @return The amount of surplus tokens that can be reclaimed. | ||
function reclaimFrom( | ||
uint256 surplus, | ||
uint256 tokensRedeemed, | ||
uint256 totalSupply, | ||
uint256 redemptionRate | ||
) | ||
internal | ||
pure | ||
returns (uint256) | ||
{ | ||
// If the redemption rate is 0, no surplus can be reclaimed. | ||
if (redemptionRate == 0) return 0; | ||
|
||
// If the total supply is being redeemed, return the entire surplus. | ||
if (tokensRedeemed == totalSupply) return surplus; | ||
|
||
// Get a reference to the linear proportion. | ||
uint256 base = mulDiv(surplus, tokensRedeemed, totalSupply); | ||
|
||
// These conditions are all part of the same curve. | ||
// Edge conditions are separated to minimize the operations performed in those cases. | ||
if (redemptionRate == JBConstants.MAX_REDEMPTION_RATE) { | ||
return base; | ||
} | ||
|
||
return mulDiv( | ||
base, | ||
redemptionRate + mulDiv(tokensRedeemed, JBConstants.MAX_REDEMPTION_RATE - redemptionRate, totalSupply), | ||
JBConstants.MAX_REDEMPTION_RATE | ||
); | ||
} | ||
} |