Verified contractExactMIT
AaveCoWDebtSwapHook
Address0x4fc8ce69bd753bc94ef770cc549260036757009e
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.24
- Same code
Only this contract
Details
- Compiler
- solc 0.8.24+commit.e11b9ed9
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,401,781
- Creation transaction
- 0xcb631b92f7…cddf2170fa
src/AaveCoWDebtSwapHook.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
/// @title Aave CoW debt-swap hook (Phase 3 draft)
/// @notice Draft, unaudited contract for the CoW "Swap debt" flow.
/// The pre-hook borrows the new debt asset on behalf of the owner (the
/// owner must delegate borrowing to this hook), CoW sells it for the
/// old debt asset, and the post-hook repays the old variable debt.
/// @dev Do not deploy without an independent review. Immutable: no owner, no
/// upgrade path and no arbitrary calls.
contract AaveCoWDebtSwapHook {
error NotHookExecutor();
error ZeroAddress();
error Expired();
error NonceUsed();
error InvalidSignature();
error InvalidAsset();
error SameAsset();
error NothingToBorrow();
error NothingToRepay();
error NoPendingRepay();
error AlreadyRepaid();
error BorrowFailed();
error TransferFailed();
error Reentrancy();
event Borrowed(address indexed owner, address indexed sellAsset, uint256 amount, uint256 nonce);
event Repaid(address indexed owner, address indexed repayAsset, uint256 amount, uint256 nonce);
bytes32 private constant DOMAIN_TYPEHASH = keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
);
bytes32 private constant NAME_HASH = keccak256("NexaSwap Aave CoW Debt Swap");
bytes32 private constant VERSION_HASH = keccak256("1");
bytes32 private constant AUTH_TYPEHASH = keccak256(
"DebtSwapHook(address owner,address sellAsset,uint256 borrowAmount,address repayAsset,uint256 repayMaxAmount,uint256 nonce,uint256 deadline)"
);
uint256 private constant SECP256K1N_DIV_2 =
0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0;
address public immutable pool;
address public immutable hookExecutor;
mapping(address owner => mapping(uint256 nonce => bool used)) public nonceUsed;
struct Pending {
address repayAsset;
uint256 repayMax;
uint256 deadline;
bool repaid;
}
mapping(address owner => mapping(uint256 nonce => Pending)) public pending;
struct DebtSwapAuth {
address owner;
address sellAsset;
uint256 borrowAmount;
address repayAsset;
uint256 repayMaxAmount;
uint256 nonce;
uint256 deadline;
}
struct BorrowRequest {
DebtSwapAuth auth;
bytes signature;
}
uint256 private _entered = 1;
modifier nonReentrant() {
if (_entered != 1) revert Reentrancy();
_entered = 2;
_;
_entered = 1;
}
constructor(address pool_, address hookExecutor_) {
if (pool_ == address(0) || hookExecutor_ == address(0)) revert ZeroAddress();
pool = pool_;
hookExecutor = hookExecutor_;
}
/// @notice Pre-hook: borrows `sellAsset` on behalf of the owner and sends
/// it to the owner so the CoW order can sell it.
function borrowForOrder(BorrowRequest calldata request)
external
nonReentrant
returns (uint256 received)
{
DebtSwapAuth calldata auth = request.auth;
_validateAuth(auth);
if (_recover(hashAuth(auth), request.signature) != auth.owner) {
revert InvalidSignature();
}
nonceUsed[auth.owner][auth.nonce] = true;
pending[auth.owner][auth.nonce] = Pending({
repayAsset: auth.repayAsset,
repayMax: auth.repayMaxAmount,
deadline: auth.deadline,
repaid: false
});
uint256 before = IERC20Minimal(auth.sellAsset).balanceOf(address(this));
IAavePool(pool).borrow(auth.sellAsset, auth.borrowAmount, 2, 0, auth.owner);
received = IERC20Minimal(auth.sellAsset).balanceOf(address(this)) - before;
if (received == 0) revert BorrowFailed();
_safeTransfer(auth.sellAsset, auth.owner, received);
emit Borrowed(auth.owner, auth.sellAsset, received, auth.nonce);
}
/// @notice Post-hook: repays the old debt with the asset bought by CoW.
function repayForOrder(address owner, uint256 nonce)
external
nonReentrant
returns (uint256 repaid)
{
if (msg.sender != hookExecutor) revert NotHookExecutor();
return _repayPending(owner, nonce);
}
/// @notice Owner recovery path if the post-hook reverted.
function repayPending(uint256 nonce) external nonReentrant returns (uint256 repaid) {
return _repayPending(msg.sender, nonce);
}
/// @notice EIP-712 digest for the single pre/post authorization.
function hashAuth(DebtSwapAuth calldata auth) public view returns (bytes32) {
bytes32 structHash = keccak256(
abi.encode(
AUTH_TYPEHASH,
auth.owner,
auth.sellAsset,
auth.borrowAmount,
auth.repayAsset,
auth.repayMaxAmount,
auth.nonce,
auth.deadline
)
);
return keccak256(abi.encodePacked("\x19\x01", _domainSeparator(), structHash));
}
function _repayPending(address owner, uint256 nonce) private returns (uint256 repaid) {
Pending memory record = pending[owner][nonce];
if (record.repayAsset == address(0)) revert NoPendingRepay();
if (record.repaid) revert AlreadyRepaid();
if (block.timestamp > record.deadline) revert Expired();
uint256 balance = IERC20Minimal(record.repayAsset).balanceOf(owner);
uint256 amount = balance < record.repayMax ? balance : record.repayMax;
if (amount == 0) revert NothingToRepay();
pending[owner][nonce].repaid = true;
_safeTransferFrom(record.repayAsset, owner, address(this), amount);
_safeApprove(record.repayAsset, pool, amount);
repaid = IAavePool(pool).repay(record.repayAsset, amount, 2, owner);
if (repaid == 0) revert NothingToRepay();
if (repaid > amount) revert NothingToRepay();
_safeApprove(record.repayAsset, pool, 0);
uint256 leftover = amount - repaid;
if (leftover > 0) _safeTransfer(record.repayAsset, owner, leftover);
emit Repaid(owner, record.repayAsset, repaid, nonce);
}
function _validateAuth(DebtSwapAuth calldata auth) private view {
if (msg.sender != hookExecutor) revert NotHookExecutor();
if (block.timestamp > auth.deadline) revert Expired();
if (
auth.owner == address(0) || auth.sellAsset == address(0)
|| auth.repayAsset == address(0)
) {
revert ZeroAddress();
}
if (auth.sellAsset == auth.repayAsset) revert SameAsset();
if (auth.sellAsset.code.length == 0 || auth.repayAsset.code.length == 0) {
revert InvalidAsset();
}
if (auth.borrowAmount == 0 || auth.repayMaxAmount == 0) {
revert NothingToBorrow();
}
if (nonceUsed[auth.owner][auth.nonce]) revert NonceUsed();
}
function _domainSeparator() internal view returns (bytes32) {
return keccak256(
abi.encode(DOMAIN_TYPEHASH, NAME_HASH, VERSION_HASH, block.chainid, address(this))
);
}
function _recover(bytes32 digest, bytes calldata signature) internal pure returns (address) {
if (signature.length != 65) revert InvalidSignature();
bytes32 r;
bytes32 s;
uint8 v;
assembly {
r := calldataload(signature.offset)
s := calldataload(add(signature.offset, 32))
v := byte(0, calldataload(add(signature.offset, 64)))
}
if (v < 27) v += 27;
if (v != 27 && v != 28) revert InvalidSignature();
if (uint256(s) > SECP256K1N_DIV_2) revert InvalidSignature();
address signer = ecrecover(digest, v, r, s);
if (signer == address(0)) revert InvalidSignature();
return signer;
}
function _safeTransferFrom(address token, address from, address to, uint256 amount) private {
(bool ok, bytes memory data) =
token.call(abi.encodeWithSelector(0x23b872dd, from, to, amount));
if (!ok || (data.length != 0 && !abi.decode(data, (bool)))) revert TransferFailed();
}
function _safeTransfer(address token, address to, uint256 amount) private {
(bool ok, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, amount));
if (!ok || (data.length != 0 && !abi.decode(data, (bool)))) revert TransferFailed();
}
function _safeApprove(address token, address spender, uint256 amount) private {
(bool ok, bytes memory data) =
token.call(abi.encodeWithSelector(0x095ea7b3, spender, amount));
if (!ok || (data.length != 0 && !abi.decode(data, (bool)))) revert TransferFailed();
}
}
interface IAavePool {
function borrow(
address asset,
uint256 amount,
uint256 interestRateMode,
uint16 referralCode,
address onBehalfOf
) external;
function repay(address asset, uint256 amount, uint256 interestRateMode, address onBehalfOf)
external
returns (uint256);
}
interface IERC20Minimal {
function balanceOf(address account) external view returns (uint256);
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.