Skip to content

Commit

Permalink
Aave Swapper Updates (#353)
Browse files Browse the repository at this point in the history
* feat: milkman updates

* chore: explicitly make bytes32(0)
  • Loading branch information
efecarranza authored Nov 22, 2024
1 parent f9e12a1 commit 12b604d
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 65 deletions.
34 changes: 14 additions & 20 deletions src/swaps/AaveSwapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,28 @@ import {Initializable} from 'solidity-utils/contracts/transparent-proxy/Initiali
import {AaveV3Ethereum} from 'aave-address-book/AaveV3Ethereum.sol';
import {AaveGovernanceV2} from 'aave-address-book/AaveGovernanceV2.sol';

import {IAaveSwapper} from './interfaces/IAaveSwapper.sol';
import {IPriceChecker} from './interfaces/IExpectedOutCalculator.sol';
import {IMilkman} from './interfaces/IMilkman.sol';

/**
* @title AaveSwapper
* @author Llama
* @author efecarranza.eth
* @notice Helper contract to swap assets using milkman
*/
contract AaveSwapper is Initializable, OwnableWithGuardian, Rescuable {
contract AaveSwapper is IAaveSwapper, Initializable, OwnableWithGuardian, Rescuable {
using SafeERC20 for IERC20;

event SwapCanceled(address indexed fromToken, address indexed toToken, uint256 amount);
event SwapRequested(
address milkman,
address indexed fromToken,
address indexed toToken,
address fromOracle,
address toOracle,
uint256 amount,
address indexed recipient,
uint256 slippage
);

error Invalid0xAddress();
error InvalidAmount();
error InvalidRecipient();
error OracleNotSet();

/// @inheritdoc IAaveSwapper
address public constant BAL80WETH20 = 0x5c6Ee304399DBdB9C8Ef030aB642B10820DB8F56;

/// @inheritdoc IAaveSwapper
function initialize() external initializer {
_transferOwnership(AaveGovernanceV2.SHORT_EXECUTOR);
_updateGuardian(0xA519a7cE7B24333055781133B13532AEabfAC81b);
}

/// @inheritdoc IAaveSwapper
function swap(
address milkman,
address priceChecker,
Expand All @@ -70,6 +57,7 @@ contract AaveSwapper is Initializable, OwnableWithGuardian, Rescuable {
IERC20(fromToken),
IERC20(toToken),
recipient,
bytes32(0),
priceChecker,
data
);
Expand All @@ -86,6 +74,7 @@ contract AaveSwapper is Initializable, OwnableWithGuardian, Rescuable {
);
}

/// @inheritdoc IAaveSwapper
function cancelSwap(
address tradeMilkman,
address priceChecker,
Expand All @@ -104,6 +93,7 @@ contract AaveSwapper is Initializable, OwnableWithGuardian, Rescuable {
IERC20(fromToken),
IERC20(toToken),
recipient,
bytes32(0),
priceChecker,
data
);
Expand All @@ -116,6 +106,7 @@ contract AaveSwapper is Initializable, OwnableWithGuardian, Rescuable {
emit SwapCanceled(fromToken, toToken, amount);
}

/// @inheritdoc IAaveSwapper
function getExpectedOut(
address priceChecker,
uint256 amount,
Expand All @@ -137,17 +128,19 @@ contract AaveSwapper is Initializable, OwnableWithGuardian, Rescuable {
);
}

/// @inheritdoc Rescuable
function whoCanRescue() public view override returns (address) {
return owner();
}

/// @inheritdoc IRescuableBase
function maxRescue(
address erc20Token
) public view override(RescuableBase, IRescuableBase) returns (uint256) {
) public pure override(RescuableBase, IRescuableBase) returns (uint256) {
return type(uint256).max;
}

/// @dev Internal function to encode swap data
function _getPriceCheckerAndData(
address toToken,
address fromOracle,
Expand All @@ -161,6 +154,7 @@ contract AaveSwapper is Initializable, OwnableWithGuardian, Rescuable {
}
}

/// @dev Internal function to encode data for price checker
function _getChainlinkCheckerData(
address fromOracle,
address toOracle
Expand Down
109 changes: 109 additions & 0 deletions src/swaps/interfaces/IAaveSwapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IAaveSwapper {
/// @dev Emitted when a swap is canceled
/// @param fromToken The token to swap from
/// @param toToken The token to swap to
/// @param amount Amount of fromToken to swap
event SwapCanceled(address indexed fromToken, address indexed toToken, uint256 amount);

/// @dev Emitted when a swap is submitted to Cow Swap
/// @param milkman Address of Milkman (Cow Swap) contract
/// @param fromToken Address of the token to swap from
/// @param toToken Address of the token to swap to
/// @param fromOracle Oracle to use for price validation for fromToken
/// @param toOracle Oracle to use for price validation for toToken
/// @param recipient Address receiving the swap
/// @param amount Amount of fromToken to swap
/// @param slippage The allowed slippage for the swap
event SwapRequested(
address milkman,
address indexed fromToken,
address indexed toToken,
address fromOracle,
address toOracle,
uint256 amount,
address indexed recipient,
uint256 slippage
);

/// @dev Provided address cannot be the zero-address
error Invalid0xAddress();

/// @dev Amount has to be greater than zero
error InvalidAmount();

/// @dev Recipient cannot be the zero-address
error InvalidRecipient();

/// @dev Oracle has not be set
error OracleNotSet();

/// @notice Returns the address of the 80-BAL-20-WETH Balancer LP
function BAL80WETH20() external view returns (address);

/// @notice Initializes contract
function initialize() external;

/// @notice Performs a swap via Cow Swap
/// @param milkman Address of Milkman (Cow Swap) contract
/// @param priceChecker Address of price checker to use for swap
/// @param fromToken Address of the token to swap from
/// @param toToken Address of the token to swap to
/// @param fromOracle Oracle to use for price validation for fromToken
/// @param toOracle Oracle to use for price validation for toToken
/// @param recipient Address receiving the swap
/// @param amount Amount of fromToken to swap
/// @param slippage The allowed slippage for the swap
function swap(
address milkman,
address priceChecker,
address fromToken,
address toToken,
address fromOracle,
address toOracle,
address recipient,
uint256 amount,
uint256 slippage
) external;

/// @notice Canceels a pending swap via Cow Swap
/// @param tradeMilkman Address of Milkman instance that holds funds in escrow
/// @param priceChecker Address of price checker to use for swap
/// @param fromToken Address of the token to swap from
/// @param toToken Address of the token to swap to
/// @param fromOracle Oracle to use for price validation for fromToken
/// @param toOracle Oracle to use for price validation for toToken
/// @param recipient Address receiving the swap
/// @param amount Amount of fromToken to swap
/// @param slippage The allowed slippage for the swap
function cancelSwap(
address tradeMilkman,
address priceChecker,
address fromToken,
address toToken,
address fromOracle,
address toOracle,
address recipient,
uint256 amount,
uint256 slippage
) external;

/// @notice Returns the expected amount out in token to swap to
/// @param priceChecker Address of price checker to use for swap
/// @param amount Amount of fromToken to swap
/// @param fromToken Address of the token to swap from
/// @param toToken Address of the token to swap to
/// @param fromOracle Oracle to use for price validation for fromToken
/// @param toOracle Oracle to use for price validation for toToken
function getExpectedOut(
address priceChecker,
uint256 amount,
address fromToken,
address toToken,
address fromOracle,
address toOracle
) external view returns (uint256);
}
3 changes: 3 additions & 0 deletions src/swaps/interfaces/IMilkman.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ interface IMilkman {
/// @param fromToken The token that the user wishes to sell.
/// @param toToken The token that the user wishes to receive.
/// @param to Who should receive the tokens.
/// @param appData The app data to be used in the CoW Protocol order.
/// @param priceChecker A contract that verifies an order (mainly its minOut and fee) before Milkman signs it.
/// @param priceCheckerData Data that gets passed to the price checker.
function requestSwapExactTokensForTokens(
uint256 amountIn,
IERC20 fromToken,
IERC20 toToken,
address to,
bytes32 appData,
address priceChecker,
bytes calldata priceCheckerData
) external;
Expand All @@ -28,6 +30,7 @@ interface IMilkman {
IERC20 fromToken,
IERC20 toToken,
address to,
bytes32 appData,
address priceChecker,
bytes calldata priceCheckerData
) external;
Expand Down
Loading

0 comments on commit 12b604d

Please sign in to comment.