-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* build: pre and post hooks * fix: hooks * fix: safe approve * chore: organize * fix: order
- Loading branch information
1 parent
82c2751
commit ae73e78
Showing
7 changed files
with
508 additions
and
7 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// SPDX-License-Identifier: AGPL-3.0 | ||
pragma solidity 0.8.18; | ||
|
||
import {Hooks} from "./Hooks.sol"; | ||
import {BaseHealthCheck, ERC20} from "../HealthCheck/BaseHealthCheck.sol"; | ||
|
||
/** | ||
* @title Base Hooks | ||
* @author Yearn.finance | ||
* @notice This contract can be inherited by any Yearn | ||
* strategy wishing to implement pre or post deposit, withdraw | ||
* or transfer hooks in their strategy. | ||
*/ | ||
abstract contract BaseHooks is BaseHealthCheck, Hooks { | ||
constructor( | ||
address _asset, | ||
string memory _name | ||
) BaseHealthCheck(_asset, _name) {} | ||
|
||
// Deposit | ||
function deposit( | ||
uint256 assets, | ||
address receiver | ||
) external virtual returns (uint256 shares) { | ||
_preDepositHook(assets, shares, receiver); | ||
shares = abi.decode( | ||
_delegateCall( | ||
abi.encodeCall(TokenizedStrategy.deposit, (assets, receiver)) | ||
), | ||
(uint256) | ||
); | ||
_postDepositHook(assets, shares, receiver); | ||
} | ||
|
||
// Mint | ||
function mint( | ||
uint256 shares, | ||
address receiver | ||
) external virtual returns (uint256 assets) { | ||
_preDepositHook(assets, shares, receiver); | ||
assets = abi.decode( | ||
_delegateCall( | ||
abi.encodeCall(TokenizedStrategy.mint, (shares, receiver)) | ||
), | ||
(uint256) | ||
); | ||
_postDepositHook(assets, shares, receiver); | ||
} | ||
|
||
// Withdraw | ||
function withdraw( | ||
uint256 assets, | ||
address receiver, | ||
address owner | ||
) external virtual returns (uint256 shares) { | ||
return withdraw(assets, receiver, owner, 0); | ||
} | ||
|
||
function withdraw( | ||
uint256 assets, | ||
address receiver, | ||
address owner, | ||
uint256 maxLoss | ||
) public virtual returns (uint256 shares) { | ||
_preWithdrawHook(assets, shares, receiver, owner, maxLoss); | ||
shares = abi.decode( | ||
_delegateCall( | ||
// Have to use encodeWithSignature due to overloading parameters. | ||
abi.encodeWithSignature( | ||
"withdraw(uint256,address,address,uint256)", | ||
assets, | ||
receiver, | ||
owner, | ||
maxLoss | ||
) | ||
), | ||
(uint256) | ||
); | ||
_postWithdrawHook(assets, shares, receiver, owner, maxLoss); | ||
} | ||
|
||
// Redeem | ||
function redeem( | ||
uint256 shares, | ||
address receiver, | ||
address owner | ||
) external virtual returns (uint256) { | ||
// We default to not limiting a potential loss. | ||
return redeem(shares, receiver, owner, MAX_BPS); | ||
} | ||
|
||
function redeem( | ||
uint256 shares, | ||
address receiver, | ||
address owner, | ||
uint256 maxLoss | ||
) public returns (uint256 assets) { | ||
_preWithdrawHook(assets, shares, receiver, owner, maxLoss); | ||
assets = abi.decode( | ||
_delegateCall( | ||
// Have to use encodeWithSignature due to overloading parameters. | ||
abi.encodeWithSignature( | ||
"redeem(uint256,address,address,uint256)", | ||
shares, | ||
receiver, | ||
owner, | ||
maxLoss | ||
) | ||
), | ||
(uint256) | ||
); | ||
_postWithdrawHook(assets, shares, receiver, owner, maxLoss); | ||
} | ||
|
||
// Transfer | ||
function transferFrom( | ||
address from, | ||
address to, | ||
uint256 amount | ||
) public virtual returns (bool success) { | ||
_preTransferHook(from, to, amount); | ||
success = abi.decode( | ||
_delegateCall( | ||
abi.encodeCall( | ||
TokenizedStrategy.transferFrom, | ||
(from, to, amount) | ||
) | ||
), | ||
(bool) | ||
); | ||
_postTransferHook(from, to, amount, success); | ||
} | ||
|
||
// Transfer from | ||
function transfer( | ||
address to, | ||
uint256 amount | ||
) external virtual returns (bool success) { | ||
_preTransferHook(msg.sender, to, amount); | ||
success = abi.decode( | ||
_delegateCall( | ||
abi.encodeCall(TokenizedStrategy.transfer, (to, amount)) | ||
), | ||
(bool) | ||
); | ||
_postTransferHook(msg.sender, to, amount, success); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: AGPL-3.0 | ||
pragma solidity 0.8.18; | ||
|
||
contract DepositHooks { | ||
function _preDepositHook( | ||
uint256 assets, | ||
uint256 shares, | ||
address receiver | ||
) internal virtual {} | ||
|
||
function _postDepositHook( | ||
uint256 assets, | ||
uint256 shares, | ||
address receiver | ||
) internal virtual {} | ||
} | ||
|
||
contract WithdrawHooks { | ||
function _preWithdrawHook( | ||
uint256 assets, | ||
uint256 shares, | ||
address receiver, | ||
address owner, | ||
uint256 maxLoss | ||
) internal virtual {} | ||
|
||
function _postWithdrawHook( | ||
uint256 assets, | ||
uint256 shares, | ||
address receiver, | ||
address owner, | ||
uint256 maxLoss | ||
) internal virtual {} | ||
} | ||
|
||
contract TransferHooks { | ||
function _preTransferHook( | ||
address from, | ||
address to, | ||
uint256 amount | ||
) internal virtual {} | ||
|
||
function _postTransferHook( | ||
address from, | ||
address to, | ||
uint256 amount, | ||
bool success | ||
) internal virtual {} | ||
} | ||
|
||
contract Hooks is DepositHooks, WithdrawHooks, TransferHooks {} | ||
Oops, something went wrong.