Skip to content

Refactor rebase function parameters to separate positive and negative growth #297

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

Merged
merged 8 commits into from
Apr 1, 2025
Merged
69 changes: 46 additions & 23 deletions contracts/UFragmentsPolicy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ contract UFragmentsPolicy is Ownable {

// DECIMALS decimal fixed point numbers.
// Used in computation of (Upper-Lower)/(1-(Upper/Lower)/2^(Growth*delta))) + Lower
int256 public rebaseFunctionLowerPercentage;
int256 public rebaseFunctionUpperPercentage;
int256 public rebaseFunctionGrowth;
/// @custom:oz-renamed-from rebaseFunctionLowerPercentage
int256 public rebaseFunctionNegativePercentageLimit;
Copy link
Member

Choose a reason for hiding this comment

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

Here we are basically renaming 3 storage variables and adding one more. Changes look upgrade safe. 👍

We can use the /// @custom:oz-renamed-from operator natspec comment to keep track of pervious names. It helps the oz upgrade tooling keep track of the storage state ..

https://github.com/ampleforth/ampleforth-contracts/blob/master/.openzeppelin/mainnet.json#L155

https://forum.openzeppelin.com/t/rename-struct-property/38519/4

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

/// @custom:oz-renamed-from rebaseFunctionUpperPercentage
int256 public rebaseFunctionPositivePercentageLimit;
/// @custom:oz-renamed-from rebaseFunctionGrowth
int256 public rebaseFunctionPositiveGrowth;
int256 public rebaseFunctionNegativeGrowth;

int256 private constant ONE = int256(10**DECIMALS);

Expand Down Expand Up @@ -166,25 +170,30 @@ contract UFragmentsPolicy is Ownable {
orchestrator = orchestrator_;
}

function setRebaseFunctionGrowth(int256 rebaseFunctionGrowth_) external onlyOwner {
require(rebaseFunctionGrowth_ >= 0);
rebaseFunctionGrowth = rebaseFunctionGrowth_;
}

function setRebaseFunctionLowerPercentage(int256 rebaseFunctionLowerPercentage_)
function setRebaseFunctionNegativePercentageLimit(int256 rebaseFunctionNegativePercentageLimit_)
external
onlyOwner
{
require(rebaseFunctionLowerPercentage_ <= 0);
rebaseFunctionLowerPercentage = rebaseFunctionLowerPercentage_;
require(rebaseFunctionNegativePercentageLimit_ <= 0);
rebaseFunctionNegativePercentageLimit = rebaseFunctionNegativePercentageLimit_;
}

function setRebaseFunctionUpperPercentage(int256 rebaseFunctionUpperPercentage_)
function setRebaseFunctionPositivePercentageLimit(int256 rebaseFunctionPositivePercentageLimit_)
external
onlyOwner
{
require(rebaseFunctionUpperPercentage_ >= 0);
rebaseFunctionUpperPercentage = rebaseFunctionUpperPercentage_;
require(rebaseFunctionPositivePercentageLimit_ >= 0);
rebaseFunctionPositivePercentageLimit = rebaseFunctionPositivePercentageLimit_;
}

function setRebaseFunctionPositiveGrowth(int256 rebaseFunctionPositiveGrowth_) external onlyOwner {
require(rebaseFunctionPositiveGrowth_ >= 0);
rebaseFunctionPositiveGrowth = rebaseFunctionPositiveGrowth_;
}

function setRebaseFunctionNegativeGrowth(int256 rebaseFunctionNegativeGrowth_) external onlyOwner {
require(rebaseFunctionNegativeGrowth_ >= 0);
rebaseFunctionNegativeGrowth = rebaseFunctionNegativeGrowth_;
}

/**
Expand Down Expand Up @@ -246,9 +255,10 @@ contract UFragmentsPolicy is Ownable {
// deviationThreshold = 0.05e18 = 5e16
deviationThreshold = 25 * 10**(DECIMALS - 3);

rebaseFunctionGrowth = int256(45 * (10**DECIMALS));
rebaseFunctionUpperPercentage = int256(5 * (10**(DECIMALS - 2))); // 0.05
rebaseFunctionLowerPercentage = int256((-77) * int256(10**(DECIMALS - 3))); // -0.077
rebaseFunctionPositiveGrowth = int256(45 * (10**DECIMALS)); // Positive growth
rebaseFunctionNegativeGrowth = int256(45 * (10**DECIMALS)); // Negative growth
rebaseFunctionPositivePercentageLimit = int256(5 * (10**(DECIMALS - 2))); // 0.05
rebaseFunctionNegativePercentageLimit = int256((-77) * int256(10**(DECIMALS - 3))); // -0.077

minRebaseTimeIntervalSec = 1 days;
rebaseWindowOffsetSec = 7200; // 2AM UTC
Expand Down Expand Up @@ -334,12 +344,25 @@ contract UFragmentsPolicy is Ownable {
}
int256 targetRateSigned = targetRate.toInt256Safe();
int256 normalizedRate = rate.toInt256Safe().mul(ONE).div(targetRateSigned);
int256 rebasePercentage = computeRebasePercentage(
normalizedRate,
rebaseFunctionLowerPercentage,
rebaseFunctionUpperPercentage,
rebaseFunctionGrowth
);

// Determine growth and bounds based on positive or negative rebase
int256 rebasePercentage;
if (normalizedRate >= ONE) {
rebasePercentage = computeRebasePercentage(
normalizedRate,
-rebaseFunctionPositivePercentageLimit,
rebaseFunctionPositivePercentageLimit,
rebaseFunctionPositiveGrowth
);
} else {
rebasePercentage = computeRebasePercentage(
normalizedRate,
rebaseFunctionNegativePercentageLimit,
-rebaseFunctionNegativePercentageLimit,
rebaseFunctionNegativeGrowth
);
}

return uFrags.totalSupply().toInt256Safe().mul(rebasePercentage).div(ONE);
}

Expand Down
14 changes: 8 additions & 6 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ task('deploy:amplforce:testnet', 'Deploy ampleforth contract suite for testnet')

// Policy
const DEVIATION_TRESHOLD = utils.parseUnits('0.002', 18) // 0.002% (ie) 0.05/24)
const LOWER = utils.parseUnits('-0.005', 18)
const UPPER = utils.parseUnits('0.005', 18)
const GROWTH = utils.parseUnits('3', 18)
const LOWER = utils.parseUnits('-0.005', 18) // rebaseFunctionNegativePercentageLimit
const UPPER = utils.parseUnits('0.005', 18) // rebaseFunctionPositivePercentageLimit
const POSITIVE_GROWTH = utils.parseUnits('31', 18) // rebaseFunctionPositiveGrowth;
const NEGATIVE_GROWTH = utils.parseUnits('41', 18) // rebaseFunctionNegativeGrowth;
const MIN_REBASE_INTERVAL = 1200 // 20 mins
const REBASE_WINDOW_OFFSET = 0
const REBASE_WINDOW_LEN = 2400 // 40 mins
Expand Down Expand Up @@ -119,9 +120,10 @@ task('deploy:amplforce:testnet', 'Deploy ampleforth contract suite for testnet')

// configure parameters
await waitFor(policy.setDeviationThreshold(DEVIATION_TRESHOLD))
await waitFor(policy.setRebaseFunctionGrowth(GROWTH))
await waitFor(policy.setRebaseFunctionLowerPercentage(LOWER))
await waitFor(policy.setRebaseFunctionUpperPercentage(UPPER))
await waitFor(policy.setRebaseFunctionPositiveGrowth(POSITIVE_GROWTH))
await waitFor(policy.setRebaseFunctionNegativeGrowth(NEGATIVE_GROWTH))
await waitFor(policy.setRebaseFunctionNegativePercentageLimit(LOWER))
await waitFor(policy.setRebaseFunctionPositivePercentageLimit(UPPER))
await waitFor(
policy.setRebaseTimingParameters(
MIN_REBASE_INTERVAL,
Expand Down
Loading