PIDController

Git Source

Inherits: Authorizable, Modifiable, IPIDController

Redemption Rate Feedback Mechanism (RRFM) controller that implements a PI controller

State Variables

_NEGATIVE_RATE_LIMIT

The lower (when negative) bound for the redemption rate

Represents a rate of -99.999% per second (as -100% is not allowed) in RAY precision

uint256 internal constant _NEGATIVE_RATE_LIMIT = RAY - 1;

_POSITIVE_RATE_LIMIT

The upper bound for the redemption rate

Maximum possible value that can be safely casted to int256

uint256 internal constant _POSITIVE_RATE_LIMIT = uint256(type(int256).max);

seedProposer

Returns the address allowed to call computeRate method

address public seedProposer;

_params

Getter for the unpacked contract parameters struct

PIDControllerParams public _params;

_deviationObservation

Raw data about the last deviation observation

The last deviation observation, containing latest timestamp, proportional and integral terms

DeviationObservation public _deviationObservation;

_controllerGains

Raw data about the Kp and Ki values used in this calculator

ControllerGains public _controllerGains;

Functions

params

Getter for the contract parameters struct

function params() external view returns (PIDControllerParams memory _pidParams);

Returns

NameTypeDescription
_pidParamsPIDControllerParamsThe PID controller parameters struct

deviationObservation

Returns the last deviation observation, containing latest timestamp, proportional and integral terms

function deviationObservation() external view returns (DeviationObservation memory __deviationObservation);

Returns

NameTypeDescription
__deviationObservationDeviationObservationThe last deviation observation struct

controllerGains

Returns the Kp and Ki values used in this calculator

The values are expressed in WAD, Kp stands for proportional and Ki for integral terms

function controllerGains() external view returns (ControllerGains memory _cGains);

constructor

constructor(
  ControllerGains memory _cGains,
  PIDControllerParams memory _pidParams,
  DeviationObservation memory _importedState
) Authorizable(msg.sender) validParams;

Parameters

NameTypeDescription
_cGainsControllerGainsInitial valid controller gains settings struct
_pidParamsPIDControllerParamsInitial valid PID controller parameters struct
_importedStateDeviationObservationImported initial state of the controller (optional)

getBoundedRedemptionRate

Return a redemption rate bounded by feedbackOutputLowerBound and feedbackOutputUpperBound as well as the timeline over which that rate will take effect

function getBoundedRedemptionRate(int256 _piOutput) external view returns (uint256 _newRedemptionRate);

Parameters

NameTypeDescription
_piOutputint256The raw redemption rate computed from the proportional and integral terms

Returns

NameTypeDescription
_newRedemptionRateuint256_redemptionRate The bounded redemption rate

_getBoundedRedemptionRate

Computes the new redemption rate by taking into account the feedbackOutputUpperBound and feedbackOutputLowerBound

function _getBoundedRedemptionRate(int256 _piOutput) internal view virtual returns (uint256 _newRedemptionRate);

_getBoundedPIOutput

Computes the pi output by taking into account the feedbackOutputUpperBound and feedbackOutputLowerBound

function _getBoundedPIOutput(int256 _piOutput) internal view virtual returns (int256 _boundedPIOutput);

computeRate

Compute a new redemption rate

function computeRate(uint256 _marketPrice, uint256 _redemptionPrice) external returns (uint256 _newRedemptionRate);

Parameters

NameTypeDescription
_marketPriceuint256The system coin market price
_redemptionPriceuint256The system coin redemption price

Returns

NameTypeDescription
_newRedemptionRateuint256_redemptionRate The computed redemption rate

_getProportionalTerm

Computes the proportional term using the scaled difference between market price and redemption price

function _getProportionalTerm(
  uint256 _marketPrice,
  uint256 _redemptionPrice
) internal view virtual returns (int256 _proportionalTerm);

breaksNoiseBarrier

Returns whether the P + I sum exceeds the noise barrier

function breaksNoiseBarrier(uint256 _piSum, uint256 _redemptionPrice) external view virtual returns (bool _breaksNb);

Parameters

NameTypeDescription
_piSumuint256Represents a sum between P + I
_redemptionPriceuint256The system coin redemption price

Returns

NameTypeDescription
_breaksNbboolWhether the P + I sum exceeds the noise barrier

_breaksNoiseBarrier

Returns whether the P + I sum exceeds the noise barrier

function _breaksNoiseBarrier(uint256 _piSum, uint256 _redemptionPrice) internal view virtual returns (bool _breaksNb);

getGainAdjustedPIOutput

Apply Kp to the proportional term and Ki to the integral term (by multiplication) and then sum P and I

function getGainAdjustedPIOutput(
  int256 _proportionalTerm,
  int256 _integralTerm
) external view returns (int256 _gainAdjustedPIOutput);

Parameters

NameTypeDescription
_proportionalTermint256The proportional term
_integralTermint256The integral term

Returns

NameTypeDescription
_gainAdjustedPIOutputint256_piOutput The sum of P and I

_getGainAdjustedPIOutput

Computes the gain adjusted PI output by multiplying P by Kp and I by Ki and then sum P & I

function _getGainAdjustedPIOutput(
  int256 _proportionalTerm,
  int256 _integralTerm
) internal view virtual returns (int256 _adjustedPIOutput);

_getGainAdjustedTerms

Computes the gain adjusted terms by multiplying P by Kp and I by Ki

function _getGainAdjustedTerms(
  int256 _proportionalTerm,
  int256 _integralTerm
) internal view virtual returns (int256 _ajustedProportionalTerm, int256 _adjustedIntegralTerm);

getGainAdjustedTerms

Independently return and calculate P * Kp and I * Ki

function getGainAdjustedTerms(
  int256 _proportionalTerm,
  int256 _integralTerm
) external view returns (int256 _ajustedProportionalTerm, int256 _adjustedIntegralTerm);

Parameters

NameTypeDescription
_proportionalTermint256The proportional term
_integralTermint256The integral term

Returns

NameTypeDescription
_ajustedProportionalTermint256_proportionalGain The proportional gain
_adjustedIntegralTermint256_integralGain The integral gain

_updateDeviation

Push new observations in deviationObservations while also updating priceDeviationCumulative

function _updateDeviation(
  int256 _proportionalTerm,
  uint256 _accumulatedLeak
) internal virtual returns (int256 _integralTerm);

Parameters

NameTypeDescription
_proportionalTermint256The proportionalTerm
_accumulatedLeakuint256The total leak (similar to a negative interest rate) applied to priceDeviationCumulative before proportionalTerm is added to it

getNextDeviationCumulative

Compute a new priceDeviationCumulative (integral term)

function getNextDeviationCumulative(
  int256 _proportionalTerm,
  uint256 _accumulatedLeak
) external view returns (int256 _nextDeviationCumulative, int256 _appliedDeviation);

Parameters

NameTypeDescription
_proportionalTermint256The proportional term (redemptionPrice - marketPrice)
_accumulatedLeakuint256The total leak applied to priceDeviationCumulative before it is summed with the new time adjusted deviation

Returns

NameTypeDescription
_nextDeviationCumulativeint256_priceDeviationCumulative The new priceDeviationCumulative
_appliedDeviationint256_timeAdjustedDeviation The new time adjusted deviation

_getNextDeviationCumulative

Computes the new priceDeviationCumulative (integral term)

function _getNextDeviationCumulative(
  int256 _proportionalTerm,
  uint256 _accumulatedLeak
) internal view virtual returns (int256 _nextDeviationCumulative, int256 _appliedDeviation);

getNextRedemptionRate

Compute and return the upcoming redemption rate

This method is used to provide a view of the next redemption rate without updating the state of the controller

function getNextRedemptionRate(
  uint256 _marketPrice,
  uint256 _redemptionPrice,
  uint256 _accumulatedLeak
) external view returns (uint256 _redemptionRate, int256 _proportionalTerm, int256 _integralTerm);

Parameters

NameTypeDescription
_marketPriceuint256The system coin market price
_redemptionPriceuint256The system coin redemption price
_accumulatedLeakuint256The total leak applied to priceDeviationCumulative before it is summed with the proportionalTerm

Returns

NameTypeDescription
_redemptionRateuint256The upcoming redemption rate
_proportionalTermint256The upcoming proportional term
_integralTermint256The upcoming integral term

timeSinceLastUpdate

Returns the time elapsed since the last computeRate call

function timeSinceLastUpdate() external view returns (uint256 _elapsed);

_timeSinceLastUpdate

function _timeSinceLastUpdate() internal view returns (uint256 _elapsed);

_modifyParameters

Internal function to be overriden with custom logic to modify parameters

This function is set to revert if not overriden

function _modifyParameters(bytes32 _param, bytes memory _data) internal override;

_validateParameters

Internal function to be overriden with custom logic to validate parameters

function _validateParameters() internal view override;