Skip to content

Commit

Permalink
feat: push
Browse files Browse the repository at this point in the history
  • Loading branch information
ypatil12 committed Sep 30, 2024
1 parent bcacb32 commit e1844a1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
17 changes: 8 additions & 9 deletions src/contracts/core/DelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ contract DelegationManager is
operator: operator,
staker: staker,
strategy: strategies[i],
existingShares: Shares(0),
addedOwnedShares: ownedShares[i],
existingShares: Shares.wrap(0),
addedShares: ownedShares[i].unwrap().wrapShares(),
totalMagnitude: totalMagnitudes[i]
});
}
Expand Down Expand Up @@ -745,14 +745,13 @@ contract DelegationManager is
Shares existingShares,
Shares addedShares
) internal returns (uint256 newDepositScalingFactor) {
uint256 newDepositScalingFactor;
if (existingShares.isZero()) {
newDepositScalingFactor = WAD / totalMagnitude;
} else {
newDepositScalingFactor = existingShares.calculateNewScalingFactor(
addedShares: addedShares,
oldStakerScalingFactor: depositScalingFactors[staker][strategy],
totalMagnitude: totalMagnitude
newDepositScalingFactor = existingShares.calculateNewDepositScalingFactor(
addedShares,
depositScalingFactors[staker][strategy],
totalMagnitude
);
}

Expand Down Expand Up @@ -866,8 +865,8 @@ contract DelegationManager is

// forgefmt: disable-next-item
ownedShares[i] = shares.toOwnedShares(
stakerScalingFactor: depositScalingFactors[staker][strategies[i]],
operatorMagnitude: totalMagnitude
depositScalingFactors[staker][strategies[i]],
totalMagnitude
);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/contracts/core/StrategyManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ contract StrategyManager is
IERC20 token,
OwnedShares ownedShares
) external onlyDelegationManager {
_addOwnedShares(staker, token, strategy, ownedShares);
//TODO: fix
// _addShares(staker, token, strategy, ownedShares);
}

/// @notice Used by the DelegationManager to convert withdrawn shares to tokens and send them to a recipient
Expand Down Expand Up @@ -238,7 +239,7 @@ contract StrategyManager is
* @param staker The address to add shares to
* @param token The token that is being deposited (used for indexing)
* @param strategy The Strategy in which the `staker` is receiving shares
* @param shares The amount of shares to grant to the `staker`
* @param sharesToAdd The amount of shares to grant to the `staker`
* @dev In particular, this function calls `delegation.increaseDelegatedShares(staker, strategy, shares)` to ensure that all
* delegated shares are tracked, increases the stored share amount in `stakerStrategyShares[staker][strategy]`, and adds `strategy`
* to the `staker`'s list of strategies, if it is not in the list already.
Expand Down Expand Up @@ -293,7 +294,7 @@ contract StrategyManager is
uint256 depositedShares = strategy.deposit(token, amount);

// add the returned shares to the staker's existing shares for this strategy
_addShares(staker, token, strategy, depositedShares);
_addShares(staker, token, strategy, depositedShares.wrapShares());

return depositedShares.wrapShares();
}
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/interfaces/IDelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ interface IDelegationManager is ISignatureUtils {
* @param strategy The strategy in which to increase the delegated shares.
* @param existingShares The number of deposit shares the staker already has in the strategy. This is the shares amount stored in the
* StrategyManager/EigenPodManager for the staker's shares.
* @param addedOwnedShares The number of shares to added to the staker's shares in the strategy. This amount will be scaled prior to adding
* @param addedShares The number of shares to added to the staker's shares in the strategy. This amount will be scaled prior to adding
* to the operator's scaled shares.
*
* @dev *If the staker is actively delegated*, then increases the `staker`'s delegated scaled shares in `strategy`.
Expand All @@ -359,7 +359,7 @@ interface IDelegationManager is ISignatureUtils {
address staker,
IStrategy strategy,
Shares existingShares,
OwnedShares addedOwnedShares
Shares addedShares
) external;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/contracts/interfaces/IStrategyManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface IStrategyManager is IShareManager {
* @param token Is the token that `staker` deposited.
* @param shares Is the number of new shares `staker` has been granted in `strategy`.
*/
event Deposit(address staker, IERC20 token, IStrategy strategy, OwnedShares shares);
event Deposit(address staker, IERC20 token, IStrategy strategy, Shares shares);

/// @notice Emitted when the `strategyWhitelister` is changed
event StrategyWhitelisterChanged(address previousAddress, address newAddress);
Expand All @@ -65,7 +65,7 @@ interface IStrategyManager is IShareManager {
* WARNING: Depositing tokens that allow reentrancy (eg. ERC-777) into a strategy is not recommended. This can lead to attack vectors
* where the token balance and corresponding strategy shares are not in sync upon reentrancy.
*/
function depositIntoStrategy(IStrategy strategy, IERC20 token, uint256 amount) external returns (OwnedShares shares);
function depositIntoStrategy(IStrategy strategy, IERC20 token, uint256 amount) external returns (Shares shares);

/**
* @notice Used for depositing an asset into the specified strategy with the resultant shares credited to `staker`,
Expand Down Expand Up @@ -94,7 +94,7 @@ interface IStrategyManager is IShareManager {
address staker,
uint256 expiry,
bytes memory signature
) external returns (OwnedShares shares);
) external returns (Shares shares);

/**
* @notice Get all details on the staker's deposits and corresponding shares
Expand Down
28 changes: 20 additions & 8 deletions src/contracts/libraries/SlashingLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ library SlashingLib {

function toOwnedShares(
Shares shares,
uint64 stakerScalingFactor,
uint64 operatorMagnitude
uint256 stakerScalingFactor,
uint256 operatorMagnitude
) internal pure returns (OwnedShares) {
/**
* ownedShares = shares * stakerScalingFactor * operatorMagnitude
Expand All @@ -118,9 +118,9 @@ library SlashingLib {
function calculateNewDepositScalingFactor(
Shares currentShares,
Shares addedShares,
uint64 stakerScalingFactor,
uint64 operatorMagnitude
) internal pure returns (uint64) {
uint256 stakerScalingFactor,
uint256 operatorMagnitude
) internal pure returns (uint256) {
/**
* Base Equations:
* (1) newShares = currentShares + addedShares
Expand All @@ -145,18 +145,18 @@ library SlashingLib {
*/

// Step 1: Calculate Numerator
OwnedShares currentOwnedShares = currentShares.toOwnedShares(stakerScalingFactor, operatorMagnitude);
uint256 currentOwnedShares = currentShares.toOwnedShares(stakerScalingFactor, operatorMagnitude).unwrap();

// Step 2: Compute currentShares + addedShares
uint256 ownedPlusAddedShares = currentOwnedShares.add(addedShares).unwrap();
uint256 ownedPlusAddedShares = currentOwnedShares + addedShares.unwrap();

// Step 3: Calculate newStakerScalingFactor
// Note: We divide by operatorMagnitude to preserve

//TODO: figure out if we only need to do one divWad here
uint256 newStakerScalingFactor =
ownedPlusAddedShares
.divWad(currentShares.unwrap() + addedOwnedShares.unwrap())
.divWad(currentShares.unwrap() + addedShares.unwrap())
.divWad(operatorMagnitude);

return newStakerScalingFactor;
Expand All @@ -168,6 +168,10 @@ library SlashingLib {
return x.unwrap() + y;
}

function add(Shares x, Shares y) internal pure returns (Shares) {
return (x.unwrap() + y.unwrap()).wrapShares();
}

function add(DelegatedShares x, uint256 y) internal pure returns (uint256) {
return x.unwrap() + y;
}
Expand All @@ -188,6 +192,10 @@ library SlashingLib {
return x.unwrap() - y;
}

function sub(Shares x, Shares y) internal pure returns (Shares) {
return (x.unwrap() - y.unwrap()).wrapShares();
}

function sub(DelegatedShares x, uint256 y) internal pure returns (uint256) {
return x.unwrap() - y;
}
Expand Down Expand Up @@ -216,6 +224,10 @@ library SlashingLib {
return x.unwrap() == 0;
}

function isZero(OwnedShares x) internal pure returns (bool) {
return x.unwrap() == 0;
}

function lte(Shares x, Shares y) internal pure returns (bool) {
return x.unwrap() <= y.unwrap();
}
Expand Down

0 comments on commit e1844a1

Please sign in to comment.