Skip to content

Commit

Permalink
origin pull
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanjay Yadav authored and Sanjay Yadav committed Nov 17, 2023
2 parents efcfa04 + cb26fe2 commit d771bfb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
52 changes: 44 additions & 8 deletions contracts/SDUtilityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.16;

import './library/UtilLib.sol';
import './interfaces/IStaderConfig.sol';
import './interfaces/IStaderOracle.sol';
import './interfaces/ISDIncentiveController.sol';
import './interfaces/ISDUtilityPool.sol';
import './interfaces/SDCollateral/ISDCollateral.sol';
Expand All @@ -14,7 +15,10 @@ import '@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol'

contract SDUtilityPool is ISDUtilityPool, AccessControlUpgradeable, PausableUpgradeable {
using Math for uint256;
uint256 constant DECIMAL = 1e18;

uint256 public constant DECIMAL = 1e18;

// State variables

/**
* @notice Fraction of fee currently set aside for protocol
Expand All @@ -41,12 +45,10 @@ contract SDUtilityPool is ISDUtilityPool, AccessControlUpgradeable, PausableUpgr
*/
uint256 public totalProtocolFee;

// Additional state variables
uint256 public utilizationRatePerBlock;

uint256 public cTokenTotalSupply;

uint256 public maxETHWorthOfSDPerValidator;

uint256 public nextRequestIdToFinalize;
uint256 public nextRequestId;
uint256 public sdRequestedForWithdraw;
Expand All @@ -59,15 +61,13 @@ contract SDUtilityPool is ISDUtilityPool, AccessControlUpgradeable, PausableUpgr
uint256 public maxNonRedeemedDelegatorRequestCount;

bytes32 public constant NODE_REGISTRY_CONTRACT = keccak256('NODE_REGISTRY_CONTRACT');

IStaderConfig public staderConfig;
RiskConfig public riskConfig;

// Mappings
mapping(address => UtilizerStruct) public override utilizerData;

mapping(address => uint256) public override delegatorCTokenBalance;

mapping(uint256 => DelegatorWithdrawInfo) public override delegatorWithdrawRequests;

mapping(address => uint256[]) public override requestIdsByDelegatorAddress;

/// @custom:oz-upgrades-unsafe-allow constructor
Expand Down Expand Up @@ -316,6 +316,16 @@ contract SDUtilityPool is ISDUtilityPool, AccessControlUpgradeable, PausableUpgr
emit AccruedFees(feeAccumulated, totalProtocolFee, totalUtilizedSD);
}

function liquidationCall(address account) external override {
UserData memory userData = getUserData(account);

IERC20(staderConfig.getStaderToken()).transferFrom(msg.sender, address(this), userData.totalFeeSD);

uint256 sdPriceInEth = IStaderOracle(staderConfig.getStaderOracle()).getSDPriceInETH();

utilizerData[account].utilizeIndex = 0;
}

/**
* @notice Accrue fee to updated utilizeIndex and then calculate account's utilize balance using the updated utilizeIndex
* @param account The address whose balance should be calculated after updating utilizeIndex
Expand Down Expand Up @@ -682,4 +692,30 @@ contract SDUtilityPool is ISDUtilityPool, AccessControlUpgradeable, PausableUpgr
}
revert CannotFindRequestId();
}

function getUserData(address account) public view returns (UserData memory) {
address staderOracle = staderConfig.getStaderOracle();
uint256 sdPriceInEth = IStaderOracle(staderOracle).getSDPriceInETH();
uint256 accountUtilizePrev = _utilizerBalanceStoredInternal(account);
uint256 totalFeeSD = accountUtilizePrev - utilizerData[account].principal;

// Multiplying other values by sdPriceInEth to avoid division
uint256 totalCollateralInEth = getOperatorTotalEth(account);
uint256 collateralTimesPrice = totalCollateralInEth * sdPriceInEth;

// Ensuring that we do not divide by zero
require(totalFeeSD > 0, 'Total Fee cannot be zero');
uint256 healthFactor = (collateralTimesPrice * riskConfig.liquidationThreshold) / (totalFeeSD * sdPriceInEth);

return UserData(totalFeeSD, totalCollateralInEth, 0, healthFactor, 0);
}

function getOperatorTotalEth(address operator) public view returns (uint256) {
address nodeRegistry = staderConfig.getPermissionlessNodeRegistry();
uint256 operatorId = INodeRegistry(nodeRegistry).operatorIDByAddress(operator);
uint256 totalValidators = INodeRegistry(nodeRegistry).getOperatorTotalKeys(operatorId);

uint256 totalEth = totalValidators * 2 ether;
return totalEth;
}
}
23 changes: 23 additions & 0 deletions contracts/interfaces/ISDUtilityPool.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.16;

struct UserData {
uint256 totalFeeSD;
uint256 totalCollateralInSD;
uint256 ltv;
uint256 healthFactor;
uint256 lockedEth;
}

struct Config {
uint256 liquidationThreshold;
uint256 liquidationBonus;
uint256 ltv;
}

interface ISDUtilityPool {
error SDTransferFailed();
error CannotFindRequestId();
Expand Down Expand Up @@ -47,6 +61,13 @@ interface ISDUtilityPool {
uint256 requestBlock; // block number of withdraw request
}

struct RiskConfig {
uint256 liquidationThreshold;
uint256 liquidationBonus;
uint256 liquidationFee;
uint256 ltv;
}

function delegate(uint256 sdAmount) external;

function requestWithdraw(uint256 cTokenAmount) external returns (uint256);
Expand All @@ -71,6 +92,8 @@ interface ISDUtilityPool {

function accrueFee() external;

function liquidationCall(address account) external;

function utilizerBalanceCurrent(address account) external returns (uint256);

function exchangeRateCurrent() external returns (uint256);
Expand Down

0 comments on commit d771bfb

Please sign in to comment.