// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.8.0) (utils/Address.sol)pragmasolidity ^0.8.1;/**
* @dev Collection of functions related to the address type
*/libraryAddress{
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/functionisContract(address account) internalviewreturns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0// for contracts in construction, since the code is only stored at the end// of the constructor execution.return account.code.length>0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/functionsendValue(addresspayable recipient, uint256 amount) internal{
require(address(this).balance>= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/functionfunctionCall(address target, bytesmemory data) internalreturns (bytesmemory) {
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/functionfunctionCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalreturns (bytesmemory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/functionfunctionCallWithValue(address target,
bytesmemory data,
uint256 value
) internalreturns (bytesmemory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/functionfunctionCallWithValue(address target,
bytesmemory data,
uint256 value,
stringmemory errorMessage
) internalreturns (bytesmemory) {
require(address(this).balance>= value, "Address: insufficient balance for call");
(bool success, bytesmemory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/functionfunctionStaticCall(address target, bytesmemory data) internalviewreturns (bytesmemory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/functionfunctionStaticCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalviewreturns (bytesmemory) {
(bool success, bytesmemory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/functionfunctionDelegateCall(address target, bytesmemory data) internalreturns (bytesmemory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/functionfunctionDelegateCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalreturns (bytesmemory) {
(bool success, bytesmemory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
* the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
*
* _Available since v4.8._
*/functionverifyCallResultFromTarget(address target,
bool success,
bytesmemory returndata,
stringmemory errorMessage
) internalviewreturns (bytesmemory) {
if (success) {
if (returndata.length==0) {
// only check isContract if the call was successful and the return data is empty// otherwise we already know that it was a contractrequire(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
/**
* @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason or using the provided one.
*
* _Available since v4.3._
*/functionverifyCallResult(bool success,
bytesmemory returndata,
stringmemory errorMessage
) internalpurereturns (bytesmemory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function_revert(bytesmemory returndata, stringmemory errorMessage) privatepure{
// Look for revert reason and bubble it up if presentif (returndata.length>0) {
// The easiest way to bubble the revert reason is using memory via assembly/// @solidity memory-safe-assemblyassembly {
let returndata_size :=mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)pragmasolidity ^0.8.0;/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/interfaceIERC20{
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/eventTransfer(addressindexedfrom, addressindexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/eventApproval(addressindexed owner, addressindexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/functiontotalSupply() externalviewreturns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/functionbalanceOf(address account) externalviewreturns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/functiontransfer(address to, uint256 amount) externalreturns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/functionallowance(address owner, address spender) externalviewreturns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/functionapprove(address spender, uint256 amount) externalreturns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/functiontransferFrom(addressfrom,
address to,
uint256 amount
) externalreturns (bool);
}
// SPDX-License-Identifier: MITpragmasolidity 0.8.19;libraryPrismaMath{
uint256internalconstant DECIMAL_PRECISION =1e18;
/* Precision for Nominal ICR (independent of price). Rationale for the value:
*
* - Making it “too high” could lead to overflows.
* - Making it “too low” could lead to an ICR equal to zero, due to truncation from Solidity floor division.
*
* This value of 1e20 is chosen for safety: the NICR will only overflow for numerator > ~1e39,
* and will only truncate to 0 if the denominator is at least 1e20 times greater than the numerator.
*
*/uint256internalconstant NICR_PRECISION =1e20;
function_min(uint256 _a, uint256 _b) internalpurereturns (uint256) {
return (_a < _b) ? _a : _b;
}
function_max(uint256 _a, uint256 _b) internalpurereturns (uint256) {
return (_a >= _b) ? _a : _b;
}
/*
* Multiply two decimal numbers and use normal rounding rules:
* -round product up if 19'th mantissa digit >= 5
* -round product down if 19'th mantissa digit < 5
*
* Used only inside the exponentiation, _decPow().
*/functiondecMul(uint256 x, uint256 y) internalpurereturns (uint256 decProd) {
uint256 prod_xy = x * y;
decProd = (prod_xy + (DECIMAL_PRECISION /2)) / DECIMAL_PRECISION;
}
/*
* _decPow: Exponentiation function for 18-digit decimal base, and integer exponent n.
*
* Uses the efficient "exponentiation by squaring" algorithm. O(log(n)) complexity.
*
* Called by two functions that represent time in units of minutes:
* 1) TroveManager._calcDecayedBaseRate
* 2) CommunityIssuance._getCumulativeIssuanceFraction
*
* The exponent is capped to avoid reverting due to overflow. The cap 525600000 equals
* "minutes in 1000 years": 60 * 24 * 365 * 1000
*
* If a period of > 1000 years is ever used as an exponent in either of the above functions, the result will be
* negligibly different from just passing the cap, since:
*
* In function 1), the decayed base rate will be 0 for 1000 years or > 1000 years
* In function 2), the difference in tokens issued at 1000 years and any time > 1000 years, will be negligible
*/function_decPow(uint256 _base, uint256 _minutes) internalpurereturns (uint256) {
if (_minutes >525600000) {
_minutes =525600000;
} // cap to avoid overflowif (_minutes ==0) {
return DECIMAL_PRECISION;
}
uint256 y = DECIMAL_PRECISION;
uint256 x = _base;
uint256 n = _minutes;
// Exponentiation-by-squaringwhile (n >1) {
if (n %2==0) {
x = decMul(x, x);
n = n /2;
} else {
// if (n % 2 != 0)
y = decMul(x, y);
x = decMul(x, x);
n = (n -1) /2;
}
}
return decMul(x, y);
}
function_getAbsoluteDifference(uint256 _a, uint256 _b) internalpurereturns (uint256) {
return (_a >= _b) ? _a - _b : _b - _a;
}
function_computeNominalCR(uint256 _coll, uint256 _debt) internalpurereturns (uint256) {
if (_debt >0) {
return (_coll * NICR_PRECISION) / _debt;
}
// Return the maximal value for uint256 if the Trove has a debt of 0. Represents "infinite" CR.else {
// if (_debt == 0)return2**256-1;
}
}
function_computeCR(uint256 _coll, uint256 _debt, uint256 _price) internalpurereturns (uint256) {
if (_debt >0) {
uint256 newCollRatio = (_coll * _price) / _debt;
return newCollRatio;
}
// Return the maximal value for uint256 if the Trove has a debt of 0. Represents "infinite" CR.else {
// if (_debt == 0)return2**256-1;
}
}
function_computeCR(uint256 _coll, uint256 _debt) internalpurereturns (uint256) {
if (_debt >0) {
uint256 newCollRatio = (_coll) / _debt;
return newCollRatio;
}
// Return the maximal value for uint256 if the Trove has a debt of 0. Represents "infinite" CR.else {
// if (_debt == 0)return2**256-1;
}
}
}
Contract Source Code
File 7 of 11: PrismaOwnable.sol
// SPDX-License-Identifier: MITpragmasolidity 0.8.19;import"IPrismaCore.sol";
/**
@title Prisma Ownable
@notice Contracts inheriting `PrismaOwnable` have the same owner as `PrismaCore`.
The ownership cannot be independently modified or renounced.
*/contractPrismaOwnable{
IPrismaCore publicimmutable PRISMA_CORE;
constructor(address _prismaCore) {
PRISMA_CORE = IPrismaCore(_prismaCore);
}
modifieronlyOwner() {
require(msg.sender== PRISMA_CORE.owner(), "Only owner");
_;
}
functionowner() publicviewreturns (address) {
return PRISMA_CORE.owner();
}
functionguardian() publicviewreturns (address) {
return PRISMA_CORE.guardian();
}
}
Contract Source Code
File 8 of 11: SafeERC20.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/utils/SafeERC20.sol)pragmasolidity ^0.8.0;import"IERC20.sol";
import"draft-IERC20Permit.sol";
import"Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/librarySafeERC20{
usingAddressforaddress;
functionsafeTransfer(
IERC20 token,
address to,
uint256 value
) internal{
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
functionsafeTransferFrom(
IERC20 token,
addressfrom,
address to,
uint256 value
) internal{
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/functionsafeApprove(
IERC20 token,
address spender,
uint256 value
) internal{
// safeApprove should only be called when setting an initial allowance,// or when resetting it to zero. To increase and decrease it, use// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'require(
(value ==0) || (token.allowance(address(this), spender) ==0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
functionsafeIncreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal{
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
functionsafeDecreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal{
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
functionsafePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal{
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore +1, "SafeERC20: permit did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/function_callOptionalReturn(IERC20 token, bytesmemory data) private{
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since// we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that// the target address contains contract code and also asserts for success in the low-level call.bytesmemory returndata =address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length>0) {
// Return data is optionalrequire(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
Contract Source Code
File 9 of 11: StabilityPool.sol
// SPDX-License-Identifier: MITpragmasolidity 0.8.19;import"SafeERC20.sol";
import"IERC20.sol";
import"PrismaOwnable.sol";
import"SystemStart.sol";
import"PrismaMath.sol";
import"IDebtToken.sol";
import"IVault.sol";
/**
@title Prisma Stability Pool
@notice Based on Liquity's `StabilityPool`
https://github.com/liquity/dev/blob/main/packages/contracts/contracts/StabilityPool.sol
Prisma's implementation is modified to support multiple collaterals. Deposits into
the stability pool may be used to liquidate any supported collateral type.
*/contractStabilityPoolisPrismaOwnable, SystemStart{
usingSafeERC20forIERC20;
uint256publicconstant DECIMAL_PRECISION =1e18;
uint128publicconstant SUNSET_DURATION =180days;
uint256constant REWARD_DURATION =1weeks;
uint256publicconstant emissionId =0;
IDebtToken publicimmutable debtToken;
IPrismaVault publicimmutable vault;
addresspublicimmutable factory;
addresspublicimmutable liquidationManager;
uint128public rewardRate;
uint32public lastUpdate;
uint32public periodFinish;
mapping(IERC20 collateral =>uint256 index) public indexByCollateral;
IERC20[] public collateralTokens;
// Tracker for Debt held in the pool. Changes when users deposit/withdraw, and when Trove debt is offset.uint256internal totalDebtTokenDeposits;
mapping(address=> AccountDeposit) public accountDeposits; // depositor address -> initial depositmapping(address=> Snapshots) public depositSnapshots; // depositor address -> snapshots struct// index values are mapped against the values within `collateralTokens`mapping(address=>uint256[256]) public depositSums; // depositor address -> sumsmapping(address depositor =>uint80[256] gains) public collateralGainsByDepositor;
mapping(address depositor =>uint256 rewards) private storedPendingReward;
/* Product 'P': Running product by which to multiply an initial deposit, in order to find the current compounded deposit,
* after a series of liquidations have occurred, each of which cancel some debt with the deposit.
*
* During its lifetime, a deposit's value evolves from d_t to d_t * P / P_t , where P_t
* is the snapshot of P taken at the instant the deposit was made. 18-digit decimal.
*/uint256public P = DECIMAL_PRECISION;
uint256publicconstant SCALE_FACTOR =1e9;
// Each time the scale of P shifts by SCALE_FACTOR, the scale is incremented by 1uint128public currentScale;
// With each offset that fully empties the Pool, the epoch is incremented by 1uint128public currentEpoch;
/* collateral Gain sum 'S': During its lifetime, each deposit d_t earns a collateral gain of ( d_t * [S - S_t] )/P_t, where S_t
* is the depositor's snapshot of S taken at the time t when the deposit was made.
*
* The 'S' sums are stored in a nested mapping (epoch => scale => sum):
*
* - The inner mapping records the sum S at different scales
* - The outer mapping records the (scale => sum) mappings, for different epochs.
*/// index values are mapped against the values within `collateralTokens`mapping(uint128=>mapping(uint128=>uint256[256])) public epochToScaleToSums;
/*
* Similarly, the sum 'G' is used to calculate Prisma gains. During it's lifetime, each deposit d_t earns a Prisma gain of
* ( d_t * [G - G_t] )/P_t, where G_t is the depositor's snapshot of G taken at time t when the deposit was made.
*
* Prisma reward events occur are triggered by depositor operations (new deposit, topup, withdrawal), and liquidations.
* In each case, the Prisma reward is issued (i.e. G is updated), before other state changes are made.
*/mapping(uint128=>mapping(uint128=>uint256)) public epochToScaleToG;
// Error tracker for the error correction in the Prisma issuance calculationuint256public lastPrismaError;
// Error trackers for the error correction in the offset calculationuint256public lastCollateralError_Offset;
uint256public lastDebtLossError_Offset;
mapping(uint16=> SunsetIndex) _sunsetIndexes;
Queue queue;
structAccountDeposit {
uint128 amount;
uint128 timestamp; // timestamp of the last deposit
}
structSnapshots {
uint256 P;
uint256 G;
uint128 scale;
uint128 epoch;
}
structSunsetIndex {
uint128 idx;
uint128 expiry;
}
structQueue {
uint16 firstSunsetIndexKey;
uint16 nextSunsetIndexKey;
}
eventStabilityPoolDebtBalanceUpdated(uint256 _newBalance);
eventP_Updated(uint256 _P);
eventS_Updated(uint256 idx, uint256 _S, uint128 _epoch, uint128 _scale);
eventG_Updated(uint256 _G, uint128 _epoch, uint128 _scale);
eventEpochUpdated(uint128 _currentEpoch);
eventScaleUpdated(uint128 _currentScale);
eventDepositSnapshotUpdated(addressindexed _depositor, uint256 _P, uint256 _G);
eventUserDepositChanged(addressindexed _depositor, uint256 _newDeposit);
eventCollateralGainWithdrawn(addressindexed _depositor, uint256[] _collateral);
eventCollateralOverwritten(IERC20 oldCollateral, IERC20 newCollateral);
eventRewardClaimed(addressindexed account, addressindexed recipient, uint256 claimed);
constructor(address _prismaCore,
IDebtToken _debtTokenAddress,
IPrismaVault _vault,
address _factory,
address _liquidationManager
) PrismaOwnable(_prismaCore) SystemStart(_prismaCore) {
debtToken = _debtTokenAddress;
vault = _vault;
factory = _factory;
liquidationManager = _liquidationManager;
periodFinish =uint32(block.timestamp-1);
}
functionenableCollateral(IERC20 _collateral) external{
require(msg.sender== factory, "Not factory");
uint256 length = collateralTokens.length;
bool collateralEnabled;
for (uint256 i =0; i < length; i++) {
if (collateralTokens[i] == _collateral) {
collateralEnabled =true;
break;
}
}
if (!collateralEnabled) {
Queue memory queueCached = queue;
if (queueCached.nextSunsetIndexKey > queueCached.firstSunsetIndexKey) {
SunsetIndex memory sIdx = _sunsetIndexes[queueCached.firstSunsetIndexKey];
if (sIdx.expiry <block.timestamp) {
delete _sunsetIndexes[queue.firstSunsetIndexKey++];
_overwriteCollateral(_collateral, sIdx.idx);
return;
}
}
collateralTokens.push(_collateral);
indexByCollateral[_collateral] = collateralTokens.length;
} else {
// revert if the factory is trying to deploy a new TM with a sunset collateralrequire(indexByCollateral[_collateral] >0, "Collateral is sunsetting");
}
}
function_overwriteCollateral(IERC20 _newCollateral, uint256 idx) internal{
require(indexByCollateral[_newCollateral] ==0, "Collateral must be sunset");
uint256 length = collateralTokens.length;
require(idx < length, "Index too large");
uint256 externalLoopEnd = currentEpoch;
uint256 internalLoopEnd = currentScale;
for (uint128 i; i <= externalLoopEnd; ) {
for (uint128 j; j <= internalLoopEnd; ) {
epochToScaleToSums[i][j][idx] =0;
unchecked {
++j;
}
}
unchecked {
++i;
}
}
indexByCollateral[_newCollateral] = idx +1;
emit CollateralOverwritten(collateralTokens[idx], _newCollateral);
collateralTokens[idx] = _newCollateral;
}
/**
* @notice Starts sunsetting a collateral
* During sunsetting liquidated collateral handoff to the SP will revert
@dev IMPORTANT: When sunsetting a collateral, `TroveManager.startSunset`
should be called on all TM linked to that collateral
@param collateral Collateral to sunset
*/functionstartCollateralSunset(IERC20 collateral) externalonlyOwner{
require(indexByCollateral[collateral] >0, "Collateral already sunsetting");
_sunsetIndexes[queue.nextSunsetIndexKey++] = SunsetIndex(
uint128(indexByCollateral[collateral] -1),
uint128(block.timestamp+ SUNSET_DURATION)
);
delete indexByCollateral[collateral]; //This will prevent calls to the SP in case of liquidations
}
functiongetTotalDebtTokenDeposits() externalviewreturns (uint256) {
return totalDebtTokenDeposits;
}
// --- External Depositor Functions ---/* provideToSP():
*
* - Triggers a Prisma issuance, based on time passed since the last issuance. The Prisma issuance is shared between *all* depositors and front ends
* - Tags the deposit with the provided front end tag param, if it's a new deposit
* - Sends depositor's accumulated gains (Prisma, collateral) to depositor
* - Sends the tagged front end's accumulated Prisma gains to the tagged front end
* - Increases deposit and tagged front end's stake, and takes new snapshots for each.
*/functionprovideToSP(uint256 _amount) external{
require(!PRISMA_CORE.paused(), "Deposits are paused");
require(_amount >0, "StabilityPool: Amount must be non-zero");
_triggerRewardIssuance();
_accrueDepositorCollateralGain(msg.sender);
uint256 compoundedDebtDeposit = getCompoundedDebtDeposit(msg.sender);
_accrueRewards(msg.sender);
debtToken.sendToSP(msg.sender, _amount);
uint256 newTotalDebtTokenDeposits = totalDebtTokenDeposits + _amount;
totalDebtTokenDeposits = newTotalDebtTokenDeposits;
emit StabilityPoolDebtBalanceUpdated(newTotalDebtTokenDeposits);
uint256 newDeposit = compoundedDebtDeposit + _amount;
accountDeposits[msg.sender] = AccountDeposit({
amount: uint128(newDeposit),
timestamp: uint128(block.timestamp)
});
_updateSnapshots(msg.sender, newDeposit);
emit UserDepositChanged(msg.sender, newDeposit);
}
/* withdrawFromSP():
*
* - Triggers a Prisma issuance, based on time passed since the last issuance. The Prisma issuance is shared between *all* depositors and front ends
* - Removes the deposit's front end tag if it is a full withdrawal
* - Sends all depositor's accumulated gains (Prisma, collateral) to depositor
* - Sends the tagged front end's accumulated Prisma gains to the tagged front end
* - Decreases deposit and tagged front end's stake, and takes new snapshots for each.
*
* If _amount > userDeposit, the user withdraws all of their compounded deposit.
*/functionwithdrawFromSP(uint256 _amount) external{
uint256 initialDeposit = accountDeposits[msg.sender].amount;
uint128 depositTimestamp = accountDeposits[msg.sender].timestamp;
require(initialDeposit >0, "StabilityPool: User must have a non-zero deposit");
require(depositTimestamp <block.timestamp, "!Deposit and withdraw same block");
_triggerRewardIssuance();
_accrueDepositorCollateralGain(msg.sender);
uint256 compoundedDebtDeposit = getCompoundedDebtDeposit(msg.sender);
uint256 debtToWithdraw = PrismaMath._min(_amount, compoundedDebtDeposit);
_accrueRewards(msg.sender);
if (debtToWithdraw >0) {
debtToken.returnFromPool(address(this), msg.sender, debtToWithdraw);
_decreaseDebt(debtToWithdraw);
}
// Update deposituint256 newDeposit = compoundedDebtDeposit - debtToWithdraw;
accountDeposits[msg.sender] = AccountDeposit({ amount: uint128(newDeposit), timestamp: depositTimestamp });
_updateSnapshots(msg.sender, newDeposit);
emit UserDepositChanged(msg.sender, newDeposit);
}
// --- Prisma issuance functions ---function_triggerRewardIssuance() internal{
_updateG(_vestedEmissions());
uint256 _periodFinish = periodFinish;
uint256 lastUpdateWeek = (_periodFinish - startTime) /1weeks;
// If the last claim was a week earlier we reclaimif (getWeek() >= lastUpdateWeek) {
uint256 amount = vault.allocateNewEmissions(emissionId);
if (amount >0) {
// If the previous period is not finished we combine new and pending old rewardsif (block.timestamp< _periodFinish) {
uint256 remaining = _periodFinish -block.timestamp;
amount += remaining * rewardRate;
}
rewardRate =uint128(amount / REWARD_DURATION);
periodFinish =uint32(block.timestamp+ REWARD_DURATION);
}
}
lastUpdate =uint32(block.timestamp);
}
function_vestedEmissions() internalviewreturns (uint256) {
uint256 updated = periodFinish;
// Period is not ended we max at current timestampif (updated >block.timestamp) updated =block.timestamp;
// if the last update was after the current update time it means all rewards have been vested alreadyuint256 lastUpdateCached = lastUpdate;
if (lastUpdateCached >= updated) return0; //Nothing to claimuint256 duration = updated - lastUpdateCached;
return duration * rewardRate;
}
function_updateG(uint256 _prismaIssuance) internal{
uint256 totalDebt = totalDebtTokenDeposits; // cached to save an SLOAD/*
* When total deposits is 0, G is not updated. In this case, the Prisma issued can not be obtained by later
* depositors - it is missed out on, and remains in the balanceof the Treasury contract.
*
*/if (totalDebt ==0|| _prismaIssuance ==0) {
return;
}
uint256 prismaPerUnitStaked;
prismaPerUnitStaked = _computePrismaPerUnitStaked(_prismaIssuance, totalDebt);
uint128 currentEpochCached = currentEpoch;
uint128 currentScaleCached = currentScale;
uint256 marginalPrismaGain = prismaPerUnitStaked * P;
uint256 newG = epochToScaleToG[currentEpochCached][currentScaleCached] + marginalPrismaGain;
epochToScaleToG[currentEpochCached][currentScaleCached] = newG;
emit G_Updated(newG, currentEpochCached, currentScaleCached);
}
function_computePrismaPerUnitStaked(uint256 _prismaIssuance,
uint256 _totalDebtTokenDeposits
) internalreturns (uint256) {
/*
* Calculate the Prisma-per-unit staked. Division uses a "feedback" error correction, to keep the
* cumulative error low in the running total G:
*
* 1) Form a numerator which compensates for the floor division error that occurred the last time this
* function was called.
* 2) Calculate "per-unit-staked" ratio.
* 3) Multiply the ratio back by its denominator, to reveal the current floor division error.
* 4) Store this error for use in the next correction when this function is called.
* 5) Note: static analysis tools complain about this "division before multiplication", however, it is intended.
*/uint256 prismaNumerator = (_prismaIssuance * DECIMAL_PRECISION) + lastPrismaError;
uint256 prismaPerUnitStaked = prismaNumerator / _totalDebtTokenDeposits;
lastPrismaError = prismaNumerator - (prismaPerUnitStaked * _totalDebtTokenDeposits);
return prismaPerUnitStaked;
}
// --- Liquidation functions ---/*
* Cancels out the specified debt against the Debt contained in the Stability Pool (as far as possible)
*/functionoffset(IERC20 collateral, uint256 _debtToOffset, uint256 _collToAdd) externalvirtual{
_offset(collateral, _debtToOffset, _collToAdd);
}
function_offset(IERC20 collateral, uint256 _debtToOffset, uint256 _collToAdd) internal{
require(msg.sender== liquidationManager, "StabilityPool: Caller is not Liquidation Manager");
uint256 idx = indexByCollateral[collateral];
idx -=1;
uint256 totalDebt = totalDebtTokenDeposits; // cached to save an SLOADif (totalDebt ==0|| _debtToOffset ==0) {
return;
}
_triggerRewardIssuance();
(uint256 collateralGainPerUnitStaked, uint256 debtLossPerUnitStaked) = _computeRewardsPerUnitStaked(
_collToAdd,
_debtToOffset,
totalDebt
);
_updateRewardSumAndProduct(collateralGainPerUnitStaked, debtLossPerUnitStaked, idx); // updates S and P// Cancel the liquidated Debt debt with the Debt in the stability pool
_decreaseDebt(_debtToOffset);
}
// --- Offset helper functions ---function_computeRewardsPerUnitStaked(uint256 _collToAdd,
uint256 _debtToOffset,
uint256 _totalDebtTokenDeposits
) internalreturns (uint256 collateralGainPerUnitStaked, uint256 debtLossPerUnitStaked) {
/*
* Compute the Debt and collateral rewards. Uses a "feedback" error correction, to keep
* the cumulative error in the P and S state variables low:
*
* 1) Form numerators which compensate for the floor division errors that occurred the last time this
* function was called.
* 2) Calculate "per-unit-staked" ratios.
* 3) Multiply each ratio back by its denominator, to reveal the current floor division error.
* 4) Store these errors for use in the next correction when this function is called.
* 5) Note: static analysis tools complain about this "division before multiplication", however, it is intended.
*/uint256 collateralNumerator = (_collToAdd * DECIMAL_PRECISION) + lastCollateralError_Offset;
if (_debtToOffset == _totalDebtTokenDeposits) {
debtLossPerUnitStaked = DECIMAL_PRECISION; // When the Pool depletes to 0, so does each deposit
lastDebtLossError_Offset =0;
} else {
uint256 debtLossNumerator = (_debtToOffset * DECIMAL_PRECISION) - lastDebtLossError_Offset;
/*
* Add 1 to make error in quotient positive. We want "slightly too much" Debt loss,
* which ensures the error in any given compoundedDebtDeposit favors the Stability Pool.
*/
debtLossPerUnitStaked = (debtLossNumerator / _totalDebtTokenDeposits) +1;
lastDebtLossError_Offset = (debtLossPerUnitStaked * _totalDebtTokenDeposits) - debtLossNumerator;
}
collateralGainPerUnitStaked = collateralNumerator / _totalDebtTokenDeposits;
lastCollateralError_Offset = collateralNumerator - (collateralGainPerUnitStaked * _totalDebtTokenDeposits);
return (collateralGainPerUnitStaked, debtLossPerUnitStaked);
}
// Update the Stability Pool reward sum S and product Pfunction_updateRewardSumAndProduct(uint256 _collateralGainPerUnitStaked,
uint256 _debtLossPerUnitStaked,
uint256 idx
) internal{
uint256 currentP = P;
uint256 newP;
/*
* The newProductFactor is the factor by which to change all deposits, due to the depletion of Stability Pool Debt in the liquidation.
* We make the product factor 0 if there was a pool-emptying. Otherwise, it is (1 - DebtLossPerUnitStaked)
*/uint256 newProductFactor =uint256(DECIMAL_PRECISION) - _debtLossPerUnitStaked;
uint128 currentScaleCached = currentScale;
uint128 currentEpochCached = currentEpoch;
uint256 currentS = epochToScaleToSums[currentEpochCached][currentScaleCached][idx];
/*
* Calculate the new S first, before we update P.
* The collateral gain for any given depositor from a liquidation depends on the value of their deposit
* (and the value of totalDeposits) prior to the Stability being depleted by the debt in the liquidation.
*
* Since S corresponds to collateral gain, and P to deposit loss, we update S first.
*/uint256 marginalCollateralGain = _collateralGainPerUnitStaked * currentP;
uint256 newS = currentS + marginalCollateralGain;
epochToScaleToSums[currentEpochCached][currentScaleCached][idx] = newS;
emit S_Updated(idx, newS, currentEpochCached, currentScaleCached);
// If the Stability Pool was emptied, increment the epoch, and reset the scale and product Pif (newProductFactor ==0) {
currentEpoch = currentEpochCached +1;
emit EpochUpdated(currentEpoch);
currentScale =0;
emit ScaleUpdated(currentScale);
newP = DECIMAL_PRECISION;
// If multiplying P by a non-zero product factor would reduce P below the scale boundary, increment the scale
} elseif ((currentP * newProductFactor) / DECIMAL_PRECISION < SCALE_FACTOR) {
newP = (currentP * newProductFactor * SCALE_FACTOR) / DECIMAL_PRECISION;
currentScale = currentScaleCached +1;
emit ScaleUpdated(currentScale);
} else {
newP = (currentP * newProductFactor) / DECIMAL_PRECISION;
}
require(newP >0, "NewP");
P = newP;
emit P_Updated(newP);
}
function_decreaseDebt(uint256 _amount) internal{
uint256 newTotalDebtTokenDeposits = totalDebtTokenDeposits - _amount;
totalDebtTokenDeposits = newTotalDebtTokenDeposits;
emit StabilityPoolDebtBalanceUpdated(newTotalDebtTokenDeposits);
}
// --- Reward calculator functions for depositor and front end ---/* Calculates the collateral gain earned by the deposit since its last snapshots were taken.
* Given by the formula: E = d0 * (S - S(0))/P(0)
* where S(0) and P(0) are the depositor's snapshots of the sum S and product P, respectively.
* d0 is the last recorded deposit value.
*/functiongetDepositorCollateralGain(address _depositor) externalviewreturns (uint256[] memory collateralGains) {
collateralGains =newuint256[](collateralTokens.length);
uint256 P_Snapshot = depositSnapshots[_depositor].P;
if (P_Snapshot ==0) return collateralGains;
uint80[256] storage depositorGains = collateralGainsByDepositor[_depositor];
uint256 initialDeposit = accountDeposits[_depositor].amount;
uint128 epochSnapshot = depositSnapshots[_depositor].epoch;
uint128 scaleSnapshot = depositSnapshots[_depositor].scale;
uint256[256] storage sums = epochToScaleToSums[epochSnapshot][scaleSnapshot];
uint256[256] storage nextSums = epochToScaleToSums[epochSnapshot][scaleSnapshot +1];
uint256[256] storage depSums = depositSums[_depositor];
for (uint256 i =0; i < collateralGains.length; i++) {
collateralGains[i] = depositorGains[i];
if (sums[i] ==0) continue; // Collateral was overwritten or not gainsuint256 firstPortion = sums[i] - depSums[i];
uint256 secondPortion = nextSums[i] / SCALE_FACTOR;
collateralGains[i] += (initialDeposit * (firstPortion + secondPortion)) / P_Snapshot / DECIMAL_PRECISION;
}
return collateralGains;
}
function_accrueDepositorCollateralGain(address _depositor) privatereturns (bool hasGains) {
uint80[256] storage depositorGains = collateralGainsByDepositor[_depositor];
uint256 collaterals = collateralTokens.length;
uint256 initialDeposit = accountDeposits[_depositor].amount;
hasGains =false;
if (initialDeposit ==0) {
return hasGains;
}
uint128 epochSnapshot = depositSnapshots[_depositor].epoch;
uint128 scaleSnapshot = depositSnapshots[_depositor].scale;
uint256 P_Snapshot = depositSnapshots[_depositor].P;
uint256[256] storage sums = epochToScaleToSums[epochSnapshot][scaleSnapshot];
uint256[256] storage nextSums = epochToScaleToSums[epochSnapshot][scaleSnapshot +1];
uint256[256] storage depSums = depositSums[_depositor];
for (uint256 i =0; i < collaterals; i++) {
if (sums[i] ==0) continue; // Collateral was overwritten or not gains
hasGains =true;
uint256 firstPortion = sums[i] - depSums[i];
uint256 secondPortion = nextSums[i] / SCALE_FACTOR;
depositorGains[i] +=uint80(
(initialDeposit * (firstPortion + secondPortion)) / P_Snapshot / DECIMAL_PRECISION
);
}
return (hasGains);
}
/*
* Calculate the Prisma gain earned by a deposit since its last snapshots were taken.
* Given by the formula: Prisma = d0 * (G - G(0))/P(0)
* where G(0) and P(0) are the depositor's snapshots of the sum G and product P, respectively.
* d0 is the last recorded deposit value.
*/functionclaimableReward(address _depositor) externalviewreturns (uint256) {
uint256 totalDebt = totalDebtTokenDeposits;
uint256 initialDeposit = accountDeposits[_depositor].amount;
if (totalDebt ==0|| initialDeposit ==0) {
return0;
}
uint256 prismaNumerator = (_vestedEmissions() * DECIMAL_PRECISION) + lastPrismaError;
uint256 prismaPerUnitStaked = prismaNumerator / totalDebt;
uint256 marginalPrismaGain = prismaPerUnitStaked * P;
Snapshots memory snapshots = depositSnapshots[_depositor];
uint128 epochSnapshot = snapshots.epoch;
uint128 scaleSnapshot = snapshots.scale;
uint256 firstPortion;
uint256 secondPortion;
if (scaleSnapshot == currentScale) {
firstPortion = epochToScaleToG[epochSnapshot][scaleSnapshot] - snapshots.G + marginalPrismaGain;
secondPortion = epochToScaleToG[epochSnapshot][scaleSnapshot +1] / SCALE_FACTOR;
} else {
firstPortion = epochToScaleToG[epochSnapshot][scaleSnapshot] - snapshots.G;
secondPortion = (epochToScaleToG[epochSnapshot][scaleSnapshot +1] + marginalPrismaGain) / SCALE_FACTOR;
}
return (initialDeposit * (firstPortion + secondPortion)) / snapshots.P / DECIMAL_PRECISION;
}
function_claimableReward(address _depositor) privateviewreturns (uint256) {
uint256 initialDeposit = accountDeposits[_depositor].amount;
if (initialDeposit ==0) {
return0;
}
Snapshots memory snapshots = depositSnapshots[_depositor];
return _getPrismaGainFromSnapshots(initialDeposit, snapshots);
}
function_getPrismaGainFromSnapshots(uint256 initialStake,
Snapshots memory snapshots
) internalviewreturns (uint256) {
/*
* Grab the sum 'G' from the epoch at which the stake was made. The Prisma gain may span up to one scale change.
* If it does, the second portion of the Prisma gain is scaled by 1e9.
* If the gain spans no scale change, the second portion will be 0.
*/uint128 epochSnapshot = snapshots.epoch;
uint128 scaleSnapshot = snapshots.scale;
uint256 G_Snapshot = snapshots.G;
uint256 P_Snapshot = snapshots.P;
uint256 firstPortion = epochToScaleToG[epochSnapshot][scaleSnapshot] - G_Snapshot;
uint256 secondPortion = epochToScaleToG[epochSnapshot][scaleSnapshot +1] / SCALE_FACTOR;
uint256 prismaGain = (initialStake * (firstPortion + secondPortion)) / P_Snapshot / DECIMAL_PRECISION;
return prismaGain;
}
// --- Compounded deposit and compounded front end stake ---/*
* Return the user's compounded deposit. Given by the formula: d = d0 * P/P(0)
* where P(0) is the depositor's snapshot of the product P, taken when they last updated their deposit.
*/functiongetCompoundedDebtDeposit(address _depositor) publicviewreturns (uint256) {
uint256 initialDeposit = accountDeposits[_depositor].amount;
if (initialDeposit ==0) {
return0;
}
Snapshots memory snapshots = depositSnapshots[_depositor];
uint256 compoundedDeposit = _getCompoundedStakeFromSnapshots(initialDeposit, snapshots);
return compoundedDeposit;
}
// Internal function, used to calculcate compounded deposits and compounded front end stakes.function_getCompoundedStakeFromSnapshots(uint256 initialStake,
Snapshots memory snapshots
) internalviewreturns (uint256) {
uint256 snapshot_P = snapshots.P;
uint128 scaleSnapshot = snapshots.scale;
uint128 epochSnapshot = snapshots.epoch;
// If stake was made before a pool-emptying event, then it has been fully cancelled with debt -- so, return 0if (epochSnapshot < currentEpoch) {
return0;
}
uint256 compoundedStake;
uint128 scaleDiff = currentScale - scaleSnapshot;
/* Compute the compounded stake. If a scale change in P was made during the stake's lifetime,
* account for it. If more than one scale change was made, then the stake has decreased by a factor of
* at least 1e-9 -- so return 0.
*/if (scaleDiff ==0) {
compoundedStake = (initialStake * P) / snapshot_P;
} elseif (scaleDiff ==1) {
compoundedStake = (initialStake * P) / snapshot_P / SCALE_FACTOR;
} else {
// if scaleDiff >= 2
compoundedStake =0;
}
/*
* If compounded deposit is less than a billionth of the initial deposit, return 0.
*
* NOTE: originally, this line was in place to stop rounding errors making the deposit too large. However, the error
* corrections should ensure the error in P "favors the Pool", i.e. any given compounded deposit should slightly less
* than it's theoretical value.
*
* Thus it's unclear whether this line is still really needed.
*/if (compoundedStake < initialStake /1e9) {
return0;
}
return compoundedStake;
}
// --- Sender functions for Debt deposit, collateral gains and Prisma gains ---functionclaimCollateralGains(address recipient, uint256[] calldata collateralIndexes) externalvirtual{
_claimCollateralGains(recipient, collateralIndexes);
}
function_claimCollateralGains(address recipient, uint256[] calldata collateralIndexes) internal{
uint256 loopEnd = collateralIndexes.length;
uint256[] memory collateralGains =newuint256[](collateralTokens.length);
uint80[256] storage depositorGains = collateralGainsByDepositor[msg.sender];
for (uint256 i; i < loopEnd; ) {
uint256 collateralIndex = collateralIndexes[i];
uint256 gains = depositorGains[collateralIndex];
if (gains >0) {
collateralGains[collateralIndex] = gains;
depositorGains[collateralIndex] =0;
collateralTokens[collateralIndex].safeTransfer(recipient, gains);
}
unchecked {
++i;
}
}
emit CollateralGainWithdrawn(msg.sender, collateralGains);
}
// --- Stability Pool Deposit Functionality ---function_updateSnapshots(address _depositor, uint256 _newValue) internal{
uint256 length;
if (_newValue ==0) {
delete depositSnapshots[_depositor];
length = collateralTokens.length;
for (uint256 i =0; i < length; i++) {
depositSums[_depositor][i] =0;
}
emit DepositSnapshotUpdated(_depositor, 0, 0);
return;
}
uint128 currentScaleCached = currentScale;
uint128 currentEpochCached = currentEpoch;
uint256 currentP = P;
// Get S and G for the current epoch and current scaleuint256[256] storage currentS = epochToScaleToSums[currentEpochCached][currentScaleCached];
uint256 currentG = epochToScaleToG[currentEpochCached][currentScaleCached];
// Record new snapshots of the latest running product P, sum S, and sum G, for the depositor
depositSnapshots[_depositor].P = currentP;
depositSnapshots[_depositor].G = currentG;
depositSnapshots[_depositor].scale = currentScaleCached;
depositSnapshots[_depositor].epoch = currentEpochCached;
length = collateralTokens.length;
for (uint256 i =0; i < length; i++) {
depositSums[_depositor][i] = currentS[i];
}
emit DepositSnapshotUpdated(_depositor, currentP, currentG);
}
//This assumes the snapshot gets updated in the callerfunction_accrueRewards(address _depositor) internal{
uint256 amount = _claimableReward(_depositor);
storedPendingReward[_depositor] = storedPendingReward[_depositor] + amount;
}
functionclaimReward(address recipient) externalreturns (uint256 amount) {
amount = _claimReward(msg.sender);
if (amount >0) {
vault.transferAllocatedTokens(msg.sender, recipient, amount);
}
emit RewardClaimed(msg.sender, recipient, amount);
return amount;
}
functionvaultClaimReward(address claimant, address) externalreturns (uint256 amount) {
require(msg.sender==address(vault));
return _claimReward(claimant);
}
function_claimReward(address account) internalreturns (uint256 amount) {
uint256 initialDeposit = accountDeposits[account].amount;
if (initialDeposit >0) {
uint128 depositTimestamp = accountDeposits[account].timestamp;
_triggerRewardIssuance();
bool hasGains = _accrueDepositorCollateralGain(account);
uint256 compoundedDebtDeposit = getCompoundedDebtDeposit(account);
uint256 debtLoss = initialDeposit - compoundedDebtDeposit;
amount = _claimableReward(account);
// we update only if the snapshot has changedif (debtLoss >0|| hasGains || amount >0) {
// Update deposituint256 newDeposit = compoundedDebtDeposit;
accountDeposits[account] = AccountDeposit({ amount: uint128(newDeposit), timestamp: depositTimestamp });
_updateSnapshots(account, newDeposit);
}
}
uint256 pending = storedPendingReward[account];
if (pending >0) {
amount += pending;
storedPendingReward[account] =0;
}
return amount;
}
}
Contract Source Code
File 10 of 11: SystemStart.sol
// SPDX-License-Identifier: MITpragmasolidity 0.8.19;import"IPrismaCore.sol";
/**
@title Prisma System Start Time
@dev Provides a unified `startTime` and `getWeek`, used for emissions.
*/contractSystemStart{
uint256immutable startTime;
constructor(address prismaCore) {
startTime = IPrismaCore(prismaCore).startTime();
}
functiongetWeek() publicviewreturns (uint256 week) {
return (block.timestamp- startTime) /1weeks;
}
}
Contract Source Code
File 11 of 11: draft-IERC20Permit.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/draft-IERC20Permit.sol)pragmasolidity ^0.8.0;/**
* @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*/interfaceIERC20Permit{
/**
* @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
* given ``owner``'s signed approval.
*
* IMPORTANT: The same issues {IERC20-approve} has related to transaction
* ordering also apply here.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `deadline` must be a timestamp in the future.
* - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
* over the EIP712-formatted function arguments.
* - the signature must use ``owner``'s current nonce (see {nonces}).
*
* For more information on the signature format, see the
* https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
* section].
*/functionpermit(address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Returns the current nonce for `owner`. This value must be
* included whenever a signature is generated for {permit}.
*
* Every successful call to {permit} increases ``owner``'s nonce by one. This
* prevents a signature from being used multiple times.
*/functionnonces(address owner) externalviewreturns (uint256);
/**
* @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
*/// solhint-disable-next-line func-name-mixedcasefunctionDOMAIN_SEPARATOR() externalviewreturns (bytes32);
}