Skip to content

Commit

Permalink
Adding protection against slippage / inflation attack
Browse files Browse the repository at this point in the history
It is required to provide expected min shares or assets. If the req is
not satisfied, the tx will be reverted.
  • Loading branch information
dimpar committed Nov 30, 2023
1 parent bf767b6 commit 69eba90
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions core/contracts/AcreRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,26 @@ contract AcreRouter is OwnableUpgradeable {
/// @notice Routes funds from stBTC (Acre) to a given vault
/// @param vault Address of the vault to route the funds to.
/// @param amount Amount of TBTC to deposit.
function deposit(address vault, uint256 amount) public {
/// @param minSharesOut Minimum amount of shares to receive.
function deposit(
address vault,
uint256 amount,
uint256 minSharesOut
) public returns (uint256 sharesOut) {
require(msg.sender == address(stBTC), "stBTC only");
if (!isVault[vault]) {
revert("Vault is not approved");
}

tBTC.safeTransferFrom(msg.sender, address(this), amount);
tBTC.safeIncreaseAllowance(vault, amount);
// TODO: implement protection from the inflation attack / slippage
IERC4626(vault).deposit(amount, address(this));
// stBTC is the Acre contract where the shares will be minted to
if (
(sharesOut = IERC4626(vault).deposit(amount, address(stBTC))) <
minSharesOut
) {
revert("Not enough shares received");
}
}

/// @notice Redeem TBTC from a vault and approves them to be collected
Expand All @@ -101,19 +111,27 @@ contract AcreRouter is OwnableUpgradeable {
/// of the underlying asset in the vault. Concrete amount of the
/// underlying asset is calculated by calling `convertToAssets` on
/// the vault and the shares are burned.
function redeem(address vault, uint256 shares) public {
/// @param minAssetsOut Minimum amount of TBTC to receive.
function redeem(
address vault,
uint256 shares,
uint256 minAssetsOut
) public returns (uint256 assetsOut) {
require(msg.sender == address(stBTC), "stBTC only");

if (!isVault[vault]) {
revert("Vault is not approved");
}

// TODO: implement protection from the inflation attack / slippage
uint256 assets = IERC4626(vault).redeem(
shares,
address(this),
address(this)
);
tBTC.safeIncreaseAllowance(address(stBTC), assets);
if (
(assetsOut = IERC4626(vault).redeem(
shares,
address(this),
address(stBTC)
)) < minAssetsOut
) {
revert("Not enough assets received");
}
tBTC.safeIncreaseAllowance(address(stBTC), assetsOut);
}
}

0 comments on commit 69eba90

Please sign in to comment.