Skip to content

Commit

Permalink
remove the depositedWSAccountedFor
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrowDom committed Jan 10, 2025
1 parent 5a07769 commit 0b5aea3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 50 deletions.
43 changes: 4 additions & 39 deletions contracts/contracts/strategies/sonic/SonicStakingStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,8 @@ import { SonicValidatorDelegator } from "./SonicValidatorDelegator.sol";
* @author Origin Protocol Inc
*/
contract SonicStakingStrategy is SonicValidatorDelegator {
/// @dev This contract receives Wrapped S (wS) as the deposit asset, but unlike other strategies doesn't immediately
/// deposit it to an underlying platform. Rather a special privilege account delegates it to the validators.
/// For that reason calling wrappedSonic.balanceOf(this) in a deposit function can contain wS that has just been
/// deposited and also wS that has previously been deposited. To keep a correct count we need to keep track
/// of wS that has already been accounted for.
/// This value represents the amount of wS balance of this contract that has already been accounted for by the
/// deposit events.
uint256 public depositedWSAccountedFor;

// For future use
uint256[49] private __gap;
uint256[50] private __gap;

constructor(
BaseStrategyConfig memory _baseConfig,
Expand All @@ -40,7 +31,6 @@ contract SonicStakingStrategy is SonicValidatorDelegator {
nonReentrant
{
require(_asset == wrappedSonic, "Unsupported asset");
depositedWSAccountedFor += _amount;
_deposit(_asset, _amount);
}

Expand All @@ -58,19 +48,12 @@ contract SonicStakingStrategy is SonicValidatorDelegator {
/**
* @notice Deposit the entire balance of wrapped S in this strategy contract
*/

/// @notice Unlike other strategies, this does not deposit assets into the underlying platform.
/// It just emits the Deposit event.
/// To deposit native S into Sonic validators, `delegate` must be used.
/// Will NOT revert if the strategy is paused from an accounting failure.
/// @notice Deposits the WS asset into the underlying platform
function depositAll() external virtual override onlyVault nonReentrant {
uint256 wSBalance = IERC20(wrappedSonic).balanceOf(address(this));
uint256 newWS = wSBalance - depositedWSAccountedFor;

if (newWS > 0) {
depositedWSAccountedFor = wSBalance;

_deposit(wrappedSonic, newWS);
if (wSBalance > 0) {
_deposit(wrappedSonic, wSBalance);
}
}

Expand Down Expand Up @@ -100,8 +83,6 @@ contract SonicStakingStrategy is SonicValidatorDelegator {
require(_amount > 0, "Must withdraw something");
require(_recipient != address(0), "Must specify recipient");

_wSWithdrawn(_amount);

// slither-disable-next-line unchecked-transfer unused-return
IERC20(_asset).transfer(_recipient, _amount);

Expand All @@ -118,22 +99,6 @@ contract SonicStakingStrategy is SonicValidatorDelegator {
}
}

/// @dev Called when Wrapped S (wS) is withdrawn from the strategy or delegated to a validator so
/// the strategy knows how much wS it has on deposit.
/// This is so it can emit the correct amount in the Deposit event in depositAll().
function _wSWithdrawn(uint256 _amount) internal override {
/* In an ideal world we wouldn't need to reduce the deduction amount when the
* depositedWSAccountedFor is smaller than the _amount.
*
* The reason this is required is that a malicious actor could sent Wrapped S directly
* to this contract and that would circumvent the increase of depositedWSAccountedFor
* property. When the S would be staked the depositedWSAccountedFor amount could
* be deducted so much that it would be negative.
*/
uint256 deductAmount = Math.min(_amount, depositedWSAccountedFor);
depositedWSAccountedFor -= deductAmount;
}

/**
* @dev Returns bool indicating whether asset is supported by strategy
* @param _asset Address of the asset
Expand Down
11 changes: 0 additions & 11 deletions contracts/contracts/strategies/sonic/SonicValidatorDelegator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ abstract contract SonicValidatorDelegator is InitializableAbstractStrategy {
// unwrap Wrapped Sonic (wS) to native Sonic (S)
IWrappedSonic(wrappedSonic).withdraw(amount);

_wSWithdrawn(amount);

ISFC(sfc).delegate{ value: amount }(validatorId);

emit Delegated(validatorId, amount);
Expand Down Expand Up @@ -319,13 +317,4 @@ abstract contract SonicValidatorDelegator is InitializableAbstractStrategy {
}
return false;
}

/***************************************
Abstract
****************************************/

/// @dev Called when Wrapped S (wS) is withdrawn from the strategy or delegated to a validator so
/// the strategy knows how much wS it has on deposit.
/// This is so it can emit the correct amount in the Deposit event in depositAll().
function _wSWithdrawn(uint256 _amount) internal virtual;
}

0 comments on commit 0b5aea3

Please sign in to comment.