Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guard support for direct aave v3 interaction #217

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions contracts/guard/src/GuardV0.sol
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ contract GuardV0 is IGuard, Ownable {
validate_approve(callData);
} else if(selector == getSelector("approveDelegation(address,uint256)")) {
validate_approveDelegation(callData);
} else if(selector == getSelector("supply(address,uint256,address,uint16)")) {
validate_aaveSupply(callData);
} else if(selector == getSelector("withdraw(address,uint256,address)")) {
validate_aaveWithdraw(callData);
} else {
revert("Unknown function selector");
}
Expand Down Expand Up @@ -365,9 +369,9 @@ contract GuardV0 is IGuard, Ownable {
} else if (selector == getSelector("transferERC20AllIn(address)")) {
validate_transferERC20AllIn(subCallData);
} else if (selector == getSelector("deposit(address,address)")) {
validate_deposit(subCallData);
validate_1deltaDeposit(subCallData);
} else if (selector == getSelector("withdraw(address,address)")) {
validate_withdraw(subCallData);
validate_1deltaWithdraw(subCallData);
} else if (selector == getSelector("flashSwapExactIn(uint256,uint256,bytes)")) {
validate_flashSwapExactInt(subCallData);
} else if (selector == getSelector("flashSwapExactOut(uint256,uint256,bytes)")) {
Expand Down Expand Up @@ -395,15 +399,15 @@ contract GuardV0 is IGuard, Ownable {
}

// 1delta implementation: https://github.com/1delta-DAO/contracts-delegation/blob/4f27e1593c564c419ff042cdd932ed52d04216bf/contracts/1delta/modules/aave/FlashAggregator.sol#L34-L39
function validate_deposit(bytes memory callData) public view {
function validate_1deltaDeposit(bytes memory callData) public view {
(address token, address receiver) = abi.decode(callData, (address, address));

require(isAllowedAsset(token), "validate_transferERC20AllIn: Token not allowed");
require(isAllowedReceiver(receiver), "validate_deposit: Receiver address not whitelisted by Guard");
}

// 1delta: https://github.com/1delta-DAO/contracts-delegation/blob/4f27e1593c564c419ff042cdd932ed52d04216bf/contracts/1delta/modules/aave/FlashAggregator.sol#L71-L74
function validate_withdraw(bytes memory callData) public view {
function validate_1deltaWithdraw(bytes memory callData) public view {
(address token, address receiver) = abi.decode(callData, (address, address));

require(isAllowedAsset(token), "validate_withdraw: Token not allowed");
Expand Down Expand Up @@ -468,4 +472,27 @@ contract GuardV0 is IGuard, Ownable {
// Reference in 1delta tests: https://github.com/1delta-DAO/contracts-delegation/blob/4f27e1593c564c419ff042cdd932ed52d04216bf/test-ts/1delta/aave/marginSwap.spec.ts#L206
allowDelegationApprovalDestination(brokerProxy, notes);
}

// Aave V3 implementation: https://github.com/aave/aave-v3-core/blob/e0bfed13240adeb7f05cb6cbe5e7ce78657f0621/contracts/protocol/pool/Pool.sol#L145
function validate_aaveSupply(bytes memory callData) public view {
(address token, , , ) = abi.decode(callData, (address, uint, address, uint));

require(isAllowedAsset(token), "Token not allowed");
// require(isAllowedReceiver(wallet), "Receiver address not whitelisted by Guard");
}

// Aave V3 implementation: https://github.com/aave/aave-v3-core/blob/e0bfed13240adeb7f05cb6cbe5e7ce78657f0621/contracts/protocol/pool/Pool.sol#L198
function validate_aaveWithdraw(bytes memory callData) public view {
(address token, , address to) = abi.decode(callData, (address, uint, address));

require(isAllowedAsset(token), "Token not allowed");
require(isAllowedReceiver(to), "Receiver address not whitelisted by Guard");
}

function whitelistAaveV3(address lendingPool, string calldata notes) external {
allowCallSite(lendingPool, getSelector("supply(address,uint256,address,uint16)"), notes);
allowCallSite(lendingPool, getSelector("withdraw(address,uint256,address)"), notes);

allowApprovalDestination(lendingPool, notes);
}
}
10 changes: 10 additions & 0 deletions eth_defi/aave_v3/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,13 @@ class AaveVersion(enum.Enum):
# https://github.com/aave/aave-v3-core/blob/e0bfed13240adeb7f05cb6cbe5e7ce78657f0621/contracts/protocol/libraries/logic/SupplyLogic.sol#L123
# 115792089237316195423570985008687907853269984665640564039457584007913129639935
MAX_AMOUNT = 2**256 - 1

# https://docs.aave.com/developers/deployed-contracts/v3-mainnet
AAVE_V3_DEPLOYMENTS = {
"ethereum": {
"pool": "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2",
},
"arbitrum": {
"pool": "0x794a61358D6845594F94dc1DB02A252b5b4814aD",
},
}
1 change: 1 addition & 0 deletions eth_defi/aave_v3/loan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Aave v3 loan"""

from eth_typing import HexAddress
from web3.contract.contract import Contract, ContractFunction

Expand Down
2 changes: 1 addition & 1 deletion eth_defi/abi/guard/GuardV0.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion eth_defi/abi/guard/SimpleVaultV0.json

Large diffs are not rendered by default.

Loading
Loading