false
true
0
PulseChain Testnet v4
Blockchain
Blocks
Blocks
Uncles
Forked Blocks (Reorgs)
Transactions
Confirmed
Pending
Verified contracts
Tokens
All
tPLS
APIs
GraphQL
RPC
Eth RPC
Apps
Testnet V4 Beacon Explorer
PulseX
PulseChain Bridge
Testnet V4 Faucet
Become a Validator
PulseChain
Testnets
Testnet V4
/
Search
/
Search
Connection Lost
New Solidity Smart Contract Verification
Contract Address
The 0x address supplied on contract creation.
Is Yul contract
No
Yes
Select Yes if you want to verify Yul contract.
Contract Name
Must match the name specified in the code. For example, in
contract MyContract {..}
MyContract
is the contract name.
Include nightly builds
No
Yes
Select yes if you want to show nightly builds.
Compiler
The compiler version is specified in
pragma solidity X.X.X
. Use the compiler version rather than the nightly build. If using the Solidity compiler, run
solc —version
to check.
EVM Version
homestead
tangerineWhistle
spuriousDragon
byzantium
constantinople
petersburg
istanbul
berlin
london
paris
shanghai
default
The EVM version the contract is written for. If the bytecode does not match the version, we try to verify using the latest EVM version.
EVM version details
.
Optimization
No
Yes
If you enabled optimization during compilation, select yes.
Optimization runs
Enter the Solidity Contract Code
// SPDX-License-Identifier: MIT pragma solidity 0.6.11; import './Interfaces/IBorrowerOperations.sol'; import './Interfaces/IStabilityPool.sol'; import './Interfaces/IBorrowerOperations.sol'; import './Interfaces/IVaultManager.sol'; import './Interfaces/IUSDLToken.sol'; import './Interfaces/ISortedVaults.sol'; import "./Interfaces/ICommunityIssuance.sol"; import "./Dependencies/LiquidLoansBase.sol"; import "./Dependencies/SafeMath.sol"; import "./Dependencies/LiquidLoansSafeMath128.sol"; import "./Dependencies/Ownable.sol"; import "./Dependencies/CheckContract.sol"; import "./Dependencies/console.sol"; /* * The Stability Pool holds USDL tokens deposited by Stability Pool depositors. * * When a vault is liquidated, then depending on system conditions, some of its USDL debt gets offset with * USDL in the Stability Pool: that is, the offset debt evaporates, and an equal amount of USDL tokens in the Stability Pool is burned. * * Thus, a liquidation causes each depositor to receive a USDL loss, in proportion to their deposit as a share of total deposits. * They also receive an PLS gain, as the PLS collateral of the liquidated vault is distributed among Stability depositors, * in the same proportion. * * When a liquidation occurs, it depletes every deposit by the same fraction: for example, a liquidation that depletes 40% * of the total USDL in the Stability Pool, depletes 40% of each deposit. * * A deposit that has experienced a series of liquidations is termed a "compounded deposit": each liquidation depletes the deposit, * multiplying it by some factor in range ]0,1[ * * * --- IMPLEMENTATION --- * * We use a highly scalable method of tracking deposits and PLS gains that has O(1) complexity. * * When a liquidation occurs, rather than updating each depositor's deposit and PLS gain, we simply update two state variables: * a product P, and a sum S. * * A mathematical manipulation allows us to factor out the initial deposit, and accurately track all depositors' compounded deposits * and accumulated PLS gains over time, as liquidations occur, using just these two variables P and S. When depositors join the * Stability Pool, they get a snapshot of the latest P and S: P_t and S_t, respectively. * * The formula for a depositor's accumulated PLS gain is derived in the math proof. * * For a given deposit d_t, the ratio P/P_t tells us the factor by which a deposit has decreased since it joined the Stability Pool, * and the term d_t * (S - S_t)/P_t gives us the deposit's total accumulated PLS gain. * * Each liquidation updates the product P and sum S. After a series of liquidations, a compounded deposit and corresponding PLS gain * can be calculated using the initial deposit, the depositor’s snapshots of P and S, and the latest values of P and S. * * Any time a depositor updates their deposit (withdrawal, top-up) their accumulated PLS gain is paid out, their new deposit is recorded * (based on their latest compounded deposit and modified by the withdrawal/top-up), and they receive new snapshots of the latest P and S. * Essentially, they make a fresh deposit that overwrites the old one. * * * --- SCALE FACTOR --- * * Since P is a running product in range ]0,1] that is always-decreasing, it should never reach 0 when multiplied by a number in range ]0,1[. * Unfortunately, Solidity floor division always reaches 0, sooner or later. * * A series of liquidations that nearly empty the Pool (and thus each multiply P by a very small number in range ]0,1[ ) may push P * to its 18 digit decimal limit, and round it to 0, when in fact the Pool hasn't been emptied: this would break deposit tracking. * * So, to track P accurately, we use a scale factor: if a liquidation would cause P to decrease to <1e-9 (and be rounded to 0 by Solidity), * we first multiply P by 1e9, and increment a currentScale factor by 1. * * The added benefit of using 1e9 for the scale factor (rather than 1e18) is that it ensures negligible precision loss close to the * scale boundary: when P is at its minimum value of 1e9, the relative precision loss in P due to floor division is only on the * order of 1e-9. * * --- EPOCHS --- * * Whenever a liquidation fully empties the Stability Pool, all deposits should become 0. However, setting P to 0 would make P be 0 * forever, and break all future reward calculations. * * So, every time the Stability Pool is emptied by a liquidation, we reset P = 1 and currentScale = 0, and increment the currentEpoch by 1. * * --- TRACKING DEPOSIT OVER SCALE CHANGES AND EPOCHS --- * * When a deposit is made, it gets snapshots of the currentEpoch and the currentScale. * * When calculating a compounded deposit, we compare the current epoch to the deposit's epoch snapshot. If the current epoch is newer, * then the deposit was present during a pool-emptying liquidation, and necessarily has been depleted to 0. * * Otherwise, we then compare the current scale to the deposit's scale snapshot. If they're equal, the compounded deposit is given by d_t * P/P_t. * If it spans one scale change, it is given by d_t * P/(P_t * 1e9). If it spans more than one scale change, we define the compounded deposit * as 0, since it is now less than 1e-9'th of its initial value (e.g. a deposit of 1 billion USDL has depleted to < 1 USDL). * * * --- TRACKING DEPOSITOR'S PLS GAIN OVER SCALE CHANGES AND EPOCHS --- * * In the current epoch, the latest value of S is stored upon each scale change, and the mapping (scale -> S) is stored for each epoch. * * This allows us to calculate a deposit's accumulated PLS gain, during the epoch in which the deposit was non-zero and earned PLS. * * We calculate the depositor's accumulated PLS gain for the scale at which they made the deposit, using the PLS gain formula: * e_1 = d_t * (S - S_t) / P_t * * and also for scale after, taking care to divide the latter by a factor of 1e9: * e_2 = d_t * S / (P_t * 1e9) * * The gain in the second scale will be full, as the starting point was in the previous scale, thus no need to subtract anything. * The deposit therefore was present for reward events from the beginning of that second scale. * * S_i-S_t + S_{i+1} * .<--------.------------> * . . * . S_i . S_{i+1} * <--.-------->.<-----------> * S_t. . * <->. . * t . * |---+---------|-------------|-----... * i i+1 * * The sum of (e_1 + e_2) captures the depositor's total accumulated PLS gain, handling the case where their * deposit spanned one scale change. We only care about gains across one scale change, since the compounded * deposit is defined as being 0 once it has spanned more than one scale change. * * * --- UPDATING P WHEN A LIQUIDATION OCCURS --- * * Please see the implementation spec in the math proof document, which closely follows on from the compounded deposit / PLS gain derivations. * * * --- LOAN ISSUANCE TO STABILITY POOL DEPOSITORS --- * * An LOAN issuance event occurs at every deposit operation, and every liquidation. * * Each deposit is tagged with the address of the front end through which it was made. * * All deposits earn a share of the issued LOAN in proportion to the deposit as a share of total deposits. The LOAN earned * by a given deposit, is split between the depositor and the front end through which the deposit was made, based on the front end's kickbackRate. * * Please see the system Readme for an overview: * https://github.com/Liquid-Loans-Official/monorepo/blob/main/README.md#loan-issuance-to-stability-providers * * We use the same mathematical product-sum approach to track LOAN gains for depositors, where 'G' is the sum corresponding to LOAN gains. * The product P (and snapshot P_t) is re-used, as the ratio P/P_t tracks a deposit's depletion due to liquidations. * */ contract StabilityPool is LiquidLoansBase, Ownable, CheckContract, IStabilityPool { using LiquidLoansSafeMath128 for uint128; string constant public NAME = "StabilityPool"; IBorrowerOperations public borrowerOperations; IVaultManager public vaultManager; IUSDLToken public usdlToken; // Needed to check if there are pending liquidations ISortedVaults public sortedVaults; ICommunityIssuance public communityIssuance; uint256 internal PLS; // deposited pulse tracker // Tracker for USDL held in the pool. Changes when users deposit/withdraw, and when Vault debt is offset. uint256 internal totalUSDLDeposits; // --- Data structures --- struct FrontEnd { uint kickbackRate; bool registered; } struct Deposit { uint initialValue; address frontEndTag; } struct Snapshots { uint S; uint P; uint G; uint128 scale; uint128 epoch; } mapping (address => Deposit) public deposits; // depositor address -> Deposit struct mapping (address => Snapshots) public depositSnapshots; // depositor address -> snapshots struct mapping (address => FrontEnd) public frontEnds; // front end address -> FrontEnd struct mapping (address => uint) public frontEndStakes; // front end address -> last recorded total deposits, tagged with that front end mapping (address => Snapshots) public frontEndSnapshots; // front end address -> snapshots struct /* 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 USDL 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. */ uint public P = DECIMAL_PRECISION; uint public constant SCALE_FACTOR = 1e9; // Each time the scale of P shifts by SCALE_FACTOR, the scale is incremented by 1 uint128 public currentScale; // With each offset that fully empties the Pool, the epoch is incremented by 1 uint128 public currentEpoch; /* PLS Gain sum 'S': During its lifetime, each deposit d_t earns an PLS 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. */ mapping (uint128 => mapping(uint128 => uint)) public epochToScaleToSum; /* * Similarly, the sum 'G' is used to calculate LOAN gains. During it's lifetime, each deposit d_t earns a LOAN 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. * * LOAN reward events occur are triggered by depositor operations (new deposit, topup, withdrawal), and liquidations. * In each case, the LOAN reward is issued (i.e. G is updated), before other state changes are made. */ mapping (uint128 => mapping(uint128 => uint)) public epochToScaleToG; // Error tracker for the error correction in the LOAN issuance calculation uint public lastLOANError; // Error trackers for the error correction in the offset calculation uint public lastPLSError_Offset; uint public lastUSDLLossError_Offset; // --- Events --- event StabilityPoolPLSBalanceUpdated(uint _newBalance); event StabilityPoolUSDLBalanceUpdated(uint _newBalance); event BorrowerOperationsAddressChanged(address _newBorrowerOperationsAddress); event VaultManagerAddressChanged(address _newVaultManagerAddress); event ActivePoolAddressChanged(address _newActivePoolAddress); event DefaultPoolAddressChanged(address _newDefaultPoolAddress); event USDLTokenAddressChanged(address _newUSDLTokenAddress); event SortedVaultsAddressChanged(address _newSortedVaultsAddress); event PriceFeedAddressChanged(address _newPriceFeedAddress); event CommunityIssuanceAddressChanged(address _newCommunityIssuanceAddress); event P_Updated(uint _P); event S_Updated(uint _S, uint128 _epoch, uint128 _scale); event G_Updated(uint _G, uint128 _epoch, uint128 _scale); event EpochUpdated(uint128 _currentEpoch); event ScaleUpdated(uint128 _currentScale); event FrontEndRegistered(address indexed _frontEnd, uint _kickbackRate); event FrontEndTagSet(address indexed _depositor, address indexed _frontEnd); event DepositSnapshotUpdated(address indexed _depositor, uint _P, uint _S, uint _G); event FrontEndSnapshotUpdated(address indexed _frontEnd, uint _P, uint _G); event UserDepositChanged(address indexed _depositor, uint _newDeposit); event FrontEndStakeChanged(address indexed _frontEnd, uint _newFrontEndStake, address _depositor); event PLSGainWithdrawn(address indexed _depositor, uint _PLS, uint _USDLLoss); event LOANPaidToDepositor(address indexed _depositor, uint _LOAN); event LOANPaidToFrontEnd(address indexed _frontEnd, uint _LOAN); event PulseSent(address _to, uint _amount); // --- Contract setters --- function setAddresses( address _borrowerOperationsAddress, address _vaultManagerAddress, address _activePoolAddress, address _usdlTokenAddress, address _sortedVaultsAddress, address _priceFeedAddress, address _communityIssuanceAddress ) external override onlyOwner { checkContract(_borrowerOperationsAddress); checkContract(_vaultManagerAddress); checkContract(_activePoolAddress); checkContract(_usdlTokenAddress); checkContract(_sortedVaultsAddress); checkContract(_priceFeedAddress); checkContract(_communityIssuanceAddress); borrowerOperations = IBorrowerOperations(_borrowerOperationsAddress); vaultManager = IVaultManager(_vaultManagerAddress); activePool = IActivePool(_activePoolAddress); usdlToken = IUSDLToken(_usdlTokenAddress); sortedVaults = ISortedVaults(_sortedVaultsAddress); priceFeed = IPriceFeed(_priceFeedAddress); communityIssuance = ICommunityIssuance(_communityIssuanceAddress); emit BorrowerOperationsAddressChanged(_borrowerOperationsAddress); emit VaultManagerAddressChanged(_vaultManagerAddress); emit ActivePoolAddressChanged(_activePoolAddress); emit USDLTokenAddressChanged(_usdlTokenAddress); emit SortedVaultsAddressChanged(_sortedVaultsAddress); emit PriceFeedAddressChanged(_priceFeedAddress); emit CommunityIssuanceAddressChanged(_communityIssuanceAddress); _renounceOwnership(); } // --- Getters for public variables. Required by IPool interface --- function getPLS() external view override returns (uint) { return PLS; } function getTotalUSDLDeposits() external view override returns (uint) { return totalUSDLDeposits; } // --- External Depositor Functions --- /* provideToSP(): * * - Triggers a LOAN issuance, based on time passed since the last issuance. The LOAN 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 (LOAN, PLS) to depositor * - Sends the tagged front end's accumulated LOAN gains to the tagged front end * - Increases deposit and tagged front end's stake, and takes new snapshots for each. */ function provideToSP(uint _amount, address _frontEndTag) external override { _requireFrontEndIsRegisteredOrZero(_frontEndTag); _requireFrontEndNotRegistered(msg.sender); _requireNonZeroAmount(_amount); uint initialDeposit = deposits[msg.sender].initialValue; ICommunityIssuance communityIssuanceCached = communityIssuance; _triggerLOANIssuance(communityIssuanceCached); if (initialDeposit == 0) {_setFrontEndTag(msg.sender, _frontEndTag);} uint depositorPLSGain = getDepositorPLSGain(msg.sender); uint compoundedUSDLDeposit = getCompoundedUSDLDeposit(msg.sender); uint USDLLoss = initialDeposit.sub(compoundedUSDLDeposit); // Needed only for event log // First pay out any LOAN gains address frontEnd = deposits[msg.sender].frontEndTag; _payOutLOANGains(communityIssuanceCached, msg.sender, frontEnd); // Update front end stake uint compoundedFrontEndStake = getCompoundedFrontEndStake(frontEnd); uint newFrontEndStake = compoundedFrontEndStake.add(_amount); _updateFrontEndStakeAndSnapshots(frontEnd, newFrontEndStake); emit FrontEndStakeChanged(frontEnd, newFrontEndStake, msg.sender); _sendUSDLtoStabilityPool(msg.sender, _amount); uint newDeposit = compoundedUSDLDeposit.add(_amount); _updateDepositAndSnapshots(msg.sender, newDeposit); emit UserDepositChanged(msg.sender, newDeposit); emit PLSGainWithdrawn(msg.sender, depositorPLSGain, USDLLoss); // USDL Loss required for event log _sendPLSGainToDepositor(depositorPLSGain); } /* withdrawFromSP(): * * - Triggers a LOAN issuance, based on time passed since the last issuance. The LOAN 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 (LOAN, PLS) to depositor * - Sends the tagged front end's accumulated LOAN 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. */ function withdrawFromSP(uint _amount) external override { if (_amount !=0) {_requireNoUnderCollateralizedVaults();} uint initialDeposit = deposits[msg.sender].initialValue; _requireUserHasDeposit(initialDeposit); ICommunityIssuance communityIssuanceCached = communityIssuance; _triggerLOANIssuance(communityIssuanceCached); uint depositorPLSGain = getDepositorPLSGain(msg.sender); uint compoundedUSDLDeposit = getCompoundedUSDLDeposit(msg.sender); uint USDLtoWithdraw = LiquidLoansMath._min(_amount, compoundedUSDLDeposit); uint USDLLoss = initialDeposit.sub(compoundedUSDLDeposit); // Needed only for event log // First pay out any LOAN gains address frontEnd = deposits[msg.sender].frontEndTag; _payOutLOANGains(communityIssuanceCached, msg.sender, frontEnd); // Update front end stake uint compoundedFrontEndStake = getCompoundedFrontEndStake(frontEnd); uint newFrontEndStake = compoundedFrontEndStake.sub(USDLtoWithdraw); _updateFrontEndStakeAndSnapshots(frontEnd, newFrontEndStake); emit FrontEndStakeChanged(frontEnd, newFrontEndStake, msg.sender); _sendUSDLToDepositor(msg.sender, USDLtoWithdraw); // Update deposit uint newDeposit = compoundedUSDLDeposit.sub(USDLtoWithdraw); _updateDepositAndSnapshots(msg.sender, newDeposit); emit UserDepositChanged(msg.sender, newDeposit); emit PLSGainWithdrawn(msg.sender, depositorPLSGain, USDLLoss); // USDL Loss required for event log _sendPLSGainToDepositor(depositorPLSGain); } /* withdrawPLSGainToVault: * - Triggers a LOAN issuance, based on time passed since the last issuance. The LOAN issuance is shared between *all* depositors and front ends * - Sends all depositor's LOAN gain to depositor * - Sends all tagged front end's LOAN gain to the tagged front end * - Transfers the depositor's entire PLS gain from the Stability Pool to the caller's vault * - Leaves their compounded deposit in the Stability Pool * - Updates snapshots for deposit and tagged front end stake */ function withdrawPLSGainToVault(address _upperHint, address _lowerHint) external override { uint initialDeposit = deposits[msg.sender].initialValue; _requireUserHasDeposit(initialDeposit); _requireUserHasVault(msg.sender); _requireUserHasPLSGain(msg.sender); ICommunityIssuance communityIssuanceCached = communityIssuance; _triggerLOANIssuance(communityIssuanceCached); uint depositorPLSGain = getDepositorPLSGain(msg.sender); uint compoundedUSDLDeposit = getCompoundedUSDLDeposit(msg.sender); uint USDLLoss = initialDeposit.sub(compoundedUSDLDeposit); // Needed only for event log // First pay out any LOAN gains address frontEnd = deposits[msg.sender].frontEndTag; _payOutLOANGains(communityIssuanceCached, msg.sender, frontEnd); // Update front end stake uint compoundedFrontEndStake = getCompoundedFrontEndStake(frontEnd); uint newFrontEndStake = compoundedFrontEndStake; _updateFrontEndStakeAndSnapshots(frontEnd, newFrontEndStake); emit FrontEndStakeChanged(frontEnd, newFrontEndStake, msg.sender); _updateDepositAndSnapshots(msg.sender, compoundedUSDLDeposit); /* Emit events before transferring PLS gain to Vault. This lets the event log make more sense (i.e. so it appears that first the PLS gain is withdrawn and then it is deposited into the Vault, not the other way around). */ emit PLSGainWithdrawn(msg.sender, depositorPLSGain, USDLLoss); emit UserDepositChanged(msg.sender, compoundedUSDLDeposit); PLS = PLS.sub(depositorPLSGain); emit StabilityPoolPLSBalanceUpdated(PLS); emit PulseSent(msg.sender, depositorPLSGain); borrowerOperations.movePLSGainToVault{ value: depositorPLSGain }(msg.sender, _upperHint, _lowerHint); } // --- LOAN issuance functions --- function _triggerLOANIssuance(ICommunityIssuance _communityIssuance) internal { uint LOANIssuance = _communityIssuance.issueLOAN(); _updateG(LOANIssuance); } function _updateG(uint _LOANIssuance) internal { uint totalUSDL = totalUSDLDeposits; // cached to save an SLOAD /* * When total deposits is 0, G is not updated. In this case, the LOAN issued can not be obtained by later * depositors - it is missed out on, and remains in the balanceof the CommunityIssuance contract. * */ if (totalUSDL == 0 || _LOANIssuance == 0) {return;} uint LOANPerUnitStaked; LOANPerUnitStaked =_computeLOANPerUnitStaked(_LOANIssuance, totalUSDL); uint marginalLOANGain = LOANPerUnitStaked.mul(P); epochToScaleToG[currentEpoch][currentScale] = epochToScaleToG[currentEpoch][currentScale].add(marginalLOANGain); emit G_Updated(epochToScaleToG[currentEpoch][currentScale], currentEpoch, currentScale); } function _computeLOANPerUnitStaked(uint _LOANIssuance, uint _totalUSDLDeposits) internal returns (uint) { /* * Calculate the LOAN-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. */ uint LOANNumerator = _LOANIssuance.mul(DECIMAL_PRECISION).add(lastLOANError); uint LOANPerUnitStaked = LOANNumerator.div(_totalUSDLDeposits); lastLOANError = LOANNumerator.sub(LOANPerUnitStaked.mul(_totalUSDLDeposits)); return LOANPerUnitStaked; } // --- Liquidation functions --- /* * Cancels out the specified debt against the USDL contained in the Stability Pool (as far as possible) * and transfers the Vault's PLS collateral from ActivePool to StabilityPool. * Only called by liquidation functions in the VaultManager. */ function offset(uint _debtToOffset, uint _collToAdd) external override { _requireCallerIsVaultManager(); uint totalUSDL = totalUSDLDeposits; // cached to save an SLOAD if (totalUSDL == 0 || _debtToOffset == 0) { return; } _triggerLOANIssuance(communityIssuance); (uint PLSGainPerUnitStaked, uint USDLLossPerUnitStaked) = _computeRewardsPerUnitStaked(_collToAdd, _debtToOffset, totalUSDL); _updateRewardSumAndProduct(PLSGainPerUnitStaked, USDLLossPerUnitStaked); // updates S and P _moveOffsetCollAndDebt(_collToAdd, _debtToOffset); } // --- Offset helper functions --- function _computeRewardsPerUnitStaked( uint _collToAdd, uint _debtToOffset, uint _totalUSDLDeposits ) internal returns (uint PLSGainPerUnitStaked, uint USDLLossPerUnitStaked) { /* * Compute the USDL and PLS 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. */ uint PLSNumerator = _collToAdd.mul(DECIMAL_PRECISION).add(lastPLSError_Offset); assert(_debtToOffset <= _totalUSDLDeposits); if (_debtToOffset == _totalUSDLDeposits) { USDLLossPerUnitStaked = DECIMAL_PRECISION; // When the Pool depletes to 0, so does each deposit lastUSDLLossError_Offset = 0; } else { uint USDLLossNumerator = _debtToOffset.mul(DECIMAL_PRECISION).sub(lastUSDLLossError_Offset); /* * Add 1 to make error in quotient positive. We want "slightly too much" USDL loss, * which ensures the error in any given compoundedUSDLDeposit favors the Stability Pool. */ USDLLossPerUnitStaked = (USDLLossNumerator.div(_totalUSDLDeposits)).add(1); lastUSDLLossError_Offset = (USDLLossPerUnitStaked.mul(_totalUSDLDeposits)).sub(USDLLossNumerator); } PLSGainPerUnitStaked = PLSNumerator.div(_totalUSDLDeposits); lastPLSError_Offset = PLSNumerator.sub(PLSGainPerUnitStaked.mul(_totalUSDLDeposits)); return (PLSGainPerUnitStaked, USDLLossPerUnitStaked); } // Update the Stability Pool reward sum S and product P function _updateRewardSumAndProduct(uint _PLSGainPerUnitStaked, uint _USDLLossPerUnitStaked) internal { uint currentP = P; uint newP; assert(_USDLLossPerUnitStaked <= DECIMAL_PRECISION); /* * The newProductFactor is the factor by which to change all deposits, due to the depletion of Stability Pool USDL in the liquidation. * We make the product factor 0 if there was a pool-emptying. Otherwise, it is (1 - USDLLossPerUnitStaked) */ uint newProductFactor = uint(DECIMAL_PRECISION).sub(_USDLLossPerUnitStaked); uint128 currentScaleCached = currentScale; uint128 currentEpochCached = currentEpoch; uint currentS = epochToScaleToSum[currentEpochCached][currentScaleCached]; /* * Calculate the new S first, before we update P. * The PLS 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 PLS gain, and P to deposit loss, we update S first. */ uint marginalPLSGain = _PLSGainPerUnitStaked.mul(currentP); uint newS = currentS.add(marginalPLSGain); epochToScaleToSum[currentEpochCached][currentScaleCached] = newS; emit S_Updated(newS, currentEpochCached, currentScaleCached); // If the Stability Pool was emptied, increment the epoch, and reset the scale and product P if (newProductFactor == 0) { currentEpoch = currentEpochCached.add(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 } else if (currentP.mul(newProductFactor).div(DECIMAL_PRECISION) < SCALE_FACTOR) { newP = currentP.mul(newProductFactor).mul(SCALE_FACTOR).div(DECIMAL_PRECISION); currentScale = currentScaleCached.add(1); emit ScaleUpdated(currentScale); } else { newP = currentP.mul(newProductFactor).div(DECIMAL_PRECISION); } assert(newP > 0); P = newP; emit P_Updated(newP); } function _moveOffsetCollAndDebt(uint _collToAdd, uint _debtToOffset) internal { IActivePool activePoolCached = activePool; // Cancel the liquidated USDL debt with the USDL in the stability pool activePoolCached.decreaseUSDLDebt(_debtToOffset); _decreaseUSDL(_debtToOffset); // Burn the debt that was successfully offset usdlToken.burn(address(this), _debtToOffset); activePoolCached.sendPLS(address(this), _collToAdd); } function _decreaseUSDL(uint _amount) internal { uint newTotalUSDLDeposits = totalUSDLDeposits.sub(_amount); totalUSDLDeposits = newTotalUSDLDeposits; emit StabilityPoolUSDLBalanceUpdated(newTotalUSDLDeposits); } // --- Reward calculator functions for depositor and front end --- /* Calculates the PLS 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. */ function getDepositorPLSGain(address _depositor) public view override returns (uint) { uint initialDeposit = deposits[_depositor].initialValue; if (initialDeposit == 0) { return 0; } Snapshots memory snapshots = depositSnapshots[_depositor]; uint PLSGain = _getPLSGainFromSnapshots(initialDeposit, snapshots); return PLSGain; } function _getPLSGainFromSnapshots(uint initialDeposit, Snapshots memory snapshots) internal view returns (uint) { /* * Grab the sum 'S' from the epoch at which the stake was made. The PLS gain may span up to one scale change. * If it does, the second portion of the PLS 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; uint S_Snapshot = snapshots.S; uint P_Snapshot = snapshots.P; uint firstPortion = epochToScaleToSum[epochSnapshot][scaleSnapshot].sub(S_Snapshot); uint secondPortion = epochToScaleToSum[epochSnapshot][scaleSnapshot.add(1)].div(SCALE_FACTOR); uint PLSGain = initialDeposit.mul(firstPortion.add(secondPortion)).div(P_Snapshot).div(DECIMAL_PRECISION); return PLSGain; } /* * Calculate the LOAN gain earned by a deposit since its last snapshots were taken. * Given by the formula: LOAN = 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. */ function getDepositorLOANGain(address _depositor) public view override returns (uint) { uint initialDeposit = deposits[_depositor].initialValue; if (initialDeposit == 0) {return 0;} address frontEndTag = deposits[_depositor].frontEndTag; /* * If not tagged with a front end, the depositor gets a 100% cut of what their deposit earned. * Otherwise, their cut of the deposit's earnings is equal to the kickbackRate, set by the front end through * which they made their deposit. */ uint kickbackRate = frontEndTag == address(0) ? DECIMAL_PRECISION : frontEnds[frontEndTag].kickbackRate; Snapshots memory snapshots = depositSnapshots[_depositor]; uint LOANGain = kickbackRate.mul(_getLOANGainFromSnapshots(initialDeposit, snapshots)).div(DECIMAL_PRECISION); return LOANGain; } /* * Return the LOAN gain earned by the front end. Given by the formula: E = 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 value of the front end's total tagged deposits. */ function getFrontEndLOANGain(address _frontEnd) public view override returns (uint) { uint frontEndStake = frontEndStakes[_frontEnd]; if (frontEndStake == 0) { return 0; } uint kickbackRate = frontEnds[_frontEnd].kickbackRate; uint frontEndShare = uint(DECIMAL_PRECISION).sub(kickbackRate); Snapshots memory snapshots = frontEndSnapshots[_frontEnd]; uint LOANGain = frontEndShare.mul(_getLOANGainFromSnapshots(frontEndStake, snapshots)).div(DECIMAL_PRECISION); return LOANGain; } function _getLOANGainFromSnapshots(uint initialStake, Snapshots memory snapshots) internal view returns (uint) { /* * Grab the sum 'G' from the epoch at which the stake was made. The LOAN gain may span up to one scale change. * If it does, the second portion of the LOAN 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; uint G_Snapshot = snapshots.G; uint P_Snapshot = snapshots.P; uint firstPortion = epochToScaleToG[epochSnapshot][scaleSnapshot].sub(G_Snapshot); uint secondPortion = epochToScaleToG[epochSnapshot][scaleSnapshot.add(1)].div(SCALE_FACTOR); uint LOANGain = initialStake.mul(firstPortion.add(secondPortion)).div(P_Snapshot).div(DECIMAL_PRECISION); return LOANGain; } // --- 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. */ function getCompoundedUSDLDeposit(address _depositor) public view override returns (uint) { uint initialDeposit = deposits[_depositor].initialValue; if (initialDeposit == 0) { return 0; } Snapshots memory snapshots = depositSnapshots[_depositor]; uint compoundedDeposit = _getCompoundedStakeFromSnapshots(initialDeposit, snapshots); return compoundedDeposit; } /* * Return the front end's compounded stake. Given by the formula: D = D0 * P/P(0) * where P(0) is the depositor's snapshot of the product P, taken at the last time * when one of the front end's tagged deposits updated their deposit. * * The front end's compounded stake is equal to the sum of its depositors' compounded deposits. */ function getCompoundedFrontEndStake(address _frontEnd) public view override returns (uint) { uint frontEndStake = frontEndStakes[_frontEnd]; if (frontEndStake == 0) { return 0; } Snapshots memory snapshots = frontEndSnapshots[_frontEnd]; uint compoundedFrontEndStake = _getCompoundedStakeFromSnapshots(frontEndStake, snapshots); return compoundedFrontEndStake; } // Internal function, used to calculcate compounded deposits and compounded front end stakes. function _getCompoundedStakeFromSnapshots( uint initialStake, Snapshots memory snapshots ) internal view returns (uint) { uint 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 0 if (epochSnapshot < currentEpoch) { return 0; } uint compoundedStake; uint128 scaleDiff = currentScale.sub(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.mul(P).div(snapshot_P); } else if (scaleDiff == 1) { compoundedStake = initialStake.mul(P).div(snapshot_P).div(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.div(1e9)) {return 0;} return compoundedStake; } // --- Sender functions for USDL deposit, PLS gains and LOAN gains --- // Transfer the USDL tokens from the user to the Stability Pool's address, and update its recorded USDL function _sendUSDLtoStabilityPool(address _address, uint _amount) internal { usdlToken.sendToPool(_address, address(this), _amount); uint newTotalUSDLDeposits = totalUSDLDeposits.add(_amount); totalUSDLDeposits = newTotalUSDLDeposits; emit StabilityPoolUSDLBalanceUpdated(newTotalUSDLDeposits); } function _sendPLSGainToDepositor(uint _amount) internal { if (_amount == 0) {return;} uint newPLS = PLS.sub(_amount); PLS = newPLS; emit StabilityPoolPLSBalanceUpdated(newPLS); emit PulseSent(msg.sender, _amount); (bool success, ) = msg.sender.call{ value: _amount }(""); require(success, "StabilityPool: sending PLS failed"); } // Send USDL to user and decrease USDL in Pool function _sendUSDLToDepositor(address _depositor, uint USDLWithdrawal) internal { if (USDLWithdrawal == 0) {return;} usdlToken.returnFromPool(address(this), _depositor, USDLWithdrawal); _decreaseUSDL(USDLWithdrawal); } // --- External Front End functions --- // Front end makes a one-time selection of kickback rate upon registering function registerFrontEnd(uint _kickbackRate) external override { _requireFrontEndNotRegistered(msg.sender); _requireUserHasNoDeposit(msg.sender); _requireValidKickbackRate(_kickbackRate); frontEnds[msg.sender].kickbackRate = _kickbackRate; frontEnds[msg.sender].registered = true; emit FrontEndRegistered(msg.sender, _kickbackRate); } // --- Stability Pool Deposit Functionality --- function _setFrontEndTag(address _depositor, address _frontEndTag) internal { deposits[_depositor].frontEndTag = _frontEndTag; emit FrontEndTagSet(_depositor, _frontEndTag); } function _updateDepositAndSnapshots(address _depositor, uint _newValue) internal { deposits[_depositor].initialValue = _newValue; if (_newValue == 0) { delete deposits[_depositor].frontEndTag; delete depositSnapshots[_depositor]; emit DepositSnapshotUpdated(_depositor, 0, 0, 0); return; } uint128 currentScaleCached = currentScale; uint128 currentEpochCached = currentEpoch; uint currentP = P; // Get S and G for the current epoch and current scale uint currentS = epochToScaleToSum[currentEpochCached][currentScaleCached]; uint 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].S = currentS; depositSnapshots[_depositor].G = currentG; depositSnapshots[_depositor].scale = currentScaleCached; depositSnapshots[_depositor].epoch = currentEpochCached; emit DepositSnapshotUpdated(_depositor, currentP, currentS, currentG); } function _updateFrontEndStakeAndSnapshots(address _frontEnd, uint _newValue) internal { frontEndStakes[_frontEnd] = _newValue; if (_newValue == 0) { delete frontEndSnapshots[_frontEnd]; emit FrontEndSnapshotUpdated(_frontEnd, 0, 0); return; } uint128 currentScaleCached = currentScale; uint128 currentEpochCached = currentEpoch; uint currentP = P; // Get G for the current epoch and current scale uint currentG = epochToScaleToG[currentEpochCached][currentScaleCached]; // Record new snapshots of the latest running product P and sum G for the front end frontEndSnapshots[_frontEnd].P = currentP; frontEndSnapshots[_frontEnd].G = currentG; frontEndSnapshots[_frontEnd].scale = currentScaleCached; frontEndSnapshots[_frontEnd].epoch = currentEpochCached; emit FrontEndSnapshotUpdated(_frontEnd, currentP, currentG); } function _payOutLOANGains(ICommunityIssuance _communityIssuance, address _depositor, address _frontEnd) internal { // Pay out front end's LOAN gain if (_frontEnd != address(0)) { uint frontEndLOANGain = getFrontEndLOANGain(_frontEnd); _communityIssuance.sendLOAN(_frontEnd, frontEndLOANGain); emit LOANPaidToFrontEnd(_frontEnd, frontEndLOANGain); } // Pay out depositor's LOAN gain uint depositorLOANGain = getDepositorLOANGain(_depositor); _communityIssuance.sendLOAN(_depositor, depositorLOANGain); emit LOANPaidToDepositor(_depositor, depositorLOANGain); } // --- 'require' functions --- function _requireCallerIsActivePool() internal view { require( msg.sender == address(activePool), "StabilityPool: Caller is not ActivePool"); } function _requireCallerIsVaultManager() internal view { require(msg.sender == address(vaultManager), "StabilityPool: Caller is not VaultManager"); } function _requireNoUnderCollateralizedVaults() internal { uint price = priceFeed.fetchPrice(); address lowestVault = sortedVaults.getLast(); uint ICR = vaultManager.getCurrentICR(lowestVault, price); require(ICR >= MCR, "StabilityPool: Cannot withdraw while there are vaults with ICR < MCR"); } function _requireUserHasDeposit(uint _initialDeposit) internal pure { require(_initialDeposit > 0, 'StabilityPool: User must have a non-zero deposit'); } function _requireUserHasNoDeposit(address _address) internal view { uint initialDeposit = deposits[_address].initialValue; require(initialDeposit == 0, 'StabilityPool: User must have no deposit'); } function _requireNonZeroAmount(uint _amount) internal pure { require(_amount > 0, 'StabilityPool: Amount must be non-zero'); } function _requireUserHasVault(address _depositor) internal view { require(vaultManager.getVaultStatus(_depositor) == 1, "StabilityPool: caller must have an active vault to withdraw PLSGain to"); } function _requireUserHasPLSGain(address _depositor) internal view { uint PLSGain = getDepositorPLSGain(_depositor); require(PLSGain > 0, "StabilityPool: caller must have non-zero PLS Gain"); } function _requireFrontEndNotRegistered(address _address) internal view { require(!frontEnds[_address].registered, "StabilityPool: must not already be a registered front end"); } function _requireFrontEndIsRegisteredOrZero(address _address) internal view { require(frontEnds[_address].registered || _address == address(0), "StabilityPool: Tag must be a registered front end, or the zero address"); } function _requireValidKickbackRate(uint _kickbackRate) internal pure { require (_kickbackRate <= DECIMAL_PRECISION, "StabilityPool: Kickback rate must be in range [0,1]"); } // --- Fallback function --- receive() external payable { _requireCallerIsActivePool(); PLS = PLS.add(msg.value); StabilityPoolPLSBalanceUpdated(PLS); } } // 2022 Liquid Loans
We recommend using flattened code. This is necessary if your code utilizes a library or inherits dependencies. Use the
POA solidity flattener or the
truffle flattener
.
Try to fetch constructor arguments automatically
No
Yes
ABI-encoded Constructor Arguments (if required by the contract)
Add arguments in
ABI hex encoded form
. Constructor arguments are written right to left, and will be found at the end of the input created bytecode. They may also be
parsed here.
Add Contract Libraries
Contract Libraries
Library 1 Name
A library name called in the .sol file. Multiple libraries (up to 10) may be added for each contract. Click the Add Library button to add an additional one.
Library 1 Address
The 0x library address. This can be found in the generated json file or Truffle output (if using truffle).
Library 2 Name
Library 2 Address
Library 3 Name
Library 3 Address
Library 4 Name
Library 4 Address
Library 5 Name
Library 5 Address
Library 6 Name
Library 6 Address
Library 7 Name
Library 7 Address
Library 8 Name
Library 8 Address
Library 9 Name
Library 9 Address
Library 10 Name
Library 10 Address
Add Library
Loading...
Verify & publish
Cancel
Ok
Ok
Ok
No
Yes