Verified contractPartial
ACOFlashExercise
Address0x9e8a4b05ba9e5720a4ea8b04b3cb2dfcdf500103
- Balance
0 ETH
- Transactions
53
- Compiler
solc 0.6.6
- Same code
Only this contract
Details
- Compiler
- solc 0.6.6+commit.6c089d02
- Language
- solidity
- Optimization
- Yes, 200 runs
- License
- Not stated
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 11,199,949
- Creation transaction
- 0x86b3e8c9d8…7cf2937019
10 files
- ACOFlashExercise.solmain22.87 KB
- ACOAssetHelper.sol6.91 KB
- IACOToken.sol3.3 KB
- IERC20.sol2.76 KB
- IUniswapV2Callee.sol157 B
- IUniswapV2Factory.sol659 B
- IUniswapV2Pair.sol2.42 KB
- IUniswapV2Router02.sol4.75 KB
- IWETH.sol194 B
- Ownable.sol2.69 KB
ACOFlashExercise.sol
pragma solidity ^0.6.6;
import "./Ownable.sol";
import './ACOAssetHelper.sol';
import "./IWETH.sol";
import "./IUniswapV2Pair.sol";
import "./IUniswapV2Callee.sol";
import "./IUniswapV2Factory.sol";
import "./IUniswapV2Router02.sol";
import "./IACOToken.sol";
/**
* @title ACOFlashExercise
* @dev Contract to exercise ACO tokens using Uniswap Flash Swap.
*/
contract ACOFlashExercise is Ownable, IUniswapV2Callee {
/**
* @dev The Uniswap factory address.
*/
address immutable public uniswapFactory;
/**
* @dev The Uniswap Router address.
*/
address immutable public uniswapRouter;
/**
* @dev The WETH address used on Uniswap.
*/
address immutable public weth;
/**
* @dev Addresses of Uniswap middle route for a swap. (baseAsset => quoteAsset => middleRoute)
*/
mapping(address => mapping(address => address[])) internal middleRoute;
constructor(address _uniswapRouter) public {
super.init();
uniswapRouter = _uniswapRouter;
uniswapFactory = IUniswapV2Router02(_uniswapRouter).factory();
weth = IUniswapV2Router02(_uniswapRouter).WETH();
}
/**
* @dev To accept ether from the WETH.
*/
receive() external payable {}
/**
* @dev Function to withdraw a stuck asset on the contract.
* Only can be called by the admin.
* @param asset Address of the asset.
* @param destination Address of the destination.
*/
function withdrawStuckAsset(address asset, address destination) onlyOwner public {
uint256 amount = ACOAssetHelper._getAssetBalanceOf(asset, address(this));
if (amount > 0) {
ACOAssetHelper._transferAsset(asset, destination, amount);
}
}
/**
* @dev Function to set the addresses of Uniswap middle route for a swap.
* Only can be called by the admin.
* @param baseAsset Address of the base asset.
* @param quoteAsset Address of the quote asset.
* @param uniswapMiddleRoute Addresses of Uniswap middle route for a swap.
*/
function setUniswapMiddleRoute(address baseAsset, address quoteAsset, address[] memory uniswapMiddleRoute) onlyOwner public {
_validateUniswapMiddleRoute(baseAsset, quoteAsset, uniswapMiddleRoute);
(bool reversed, address[] storage route) = _getMiddleRoute(baseAsset, quoteAsset);
if (route.length > 0) {
if (reversed) {
delete middleRoute[quoteAsset][baseAsset];
} else {
delete middleRoute[baseAsset][quoteAsset];
}
}
address _uniswapRouter = uniswapRouter;
address _weth = weth;
ACOAssetHelper._callApproveERC20(_getUniswapToken(baseAsset, _weth), _uniswapRouter, ACOAssetHelper.MAX_UINT);
ACOAssetHelper._callApproveERC20(_getUniswapToken(quoteAsset, _weth), _uniswapRouter, ACOAssetHelper.MAX_UINT);
middleRoute[baseAsset][quoteAsset] = uniswapMiddleRoute;
}
/**
* @dev Function to get the Uniswap middle route set for a pair.
* @param baseAsset Address of the base asset.
* @param quoteAsset Address of the quote asset.
* @return If the assets are reversed and the middle route addresses.
*/
function getMiddleRoute(address baseAsset, address quoteAsset) public view returns(bool, address[] memory) {
return _getMiddleRoute(baseAsset, quoteAsset);
}
/**
* @dev Function to get if it is possible execute a flash exercise.
* @param acoToken Address of the ACO token.
* @return TRUE if it is possible use the flash exercise, otherwise FALSE.
*/
function hasFlashExercise(address acoToken) public view returns(bool) {
(address pair,) = _getUniswapData(acoToken);
return pair != address(0);
}
/**
* @dev Function to get the required amount of collateral to be paid to Uniswap and the expected amount to exercise the ACO token.
* @param acoToken Address of the ACO token.
* @param tokenAmount Amount of tokens to be exercised.
* @param accounts The array of addresses to be exercised. Whether the array is empty the exercise will be executed using the standard method.
* @return The required amount of collateral to be paid to Uniswap and the expected amount to exercise the ACO token.
*/
function getExerciseData(address acoToken, uint256 tokenAmount, address[] memory accounts) public view returns(uint256, uint256) {
if (tokenAmount > 0) {
(address pair, address[] memory path) = _getUniswapData(acoToken);
if (pair != address(0)) {
(address exerciseAddress, uint256 expectedAmount) = _getAcoExerciseData(acoToken, tokenAmount, accounts);
exerciseAddress = _getUniswapToken(exerciseAddress, weth);
(uint256 reserve0, uint256 reserve1,) = IUniswapV2Pair(pair).getReserves();
uint256 reserveIn = 0;
uint256 reserveOut = 0;
if (exerciseAddress == IUniswapV2Pair(pair).token0() && expectedAmount < reserve0) {
reserveIn = reserve1;
reserveOut = reserve0;
} else if (exerciseAddress == IUniswapV2Pair(pair).token1() && expectedAmount < reserve1) {
reserveIn = reserve0;
reserveOut = reserve1;
}
if (reserveIn > 0 && reserveOut > 0) {
uint256 requiredAmount = IUniswapV2Router02(uniswapRouter).getAmountIn(expectedAmount, reserveIn, reserveOut);
uint256 requiredCollateral;
if (path.length > 0) {
requiredCollateral = IUniswapV2Router02(uniswapRouter).getAmountsIn(requiredAmount, path)[0];
} else {
requiredCollateral = requiredAmount;
}
return (requiredCollateral, expectedAmount);
}
}
}
return (0, 0);
}
/**
* @dev Function to get the estimated collateral to be received through a flash exercise.
* @param acoToken Address of the ACO token.
* @param tokenAmount Amount of tokens to be exercised.
* @return The estimated collateral to be received through a flash exercise using the standard exercise function.
*/
function getEstimatedReturn(address acoToken, uint256 tokenAmount) public view returns(uint256) {
(uint256 requiredAmount,) = getExerciseData(acoToken, tokenAmount, new address[](0));
if (requiredAmount > 0) {
(uint256 collateralAmount,) = IACOToken(acoToken).getCollateralOnExercise(tokenAmount);
if (requiredAmount < collateralAmount) {
return collateralAmount - requiredAmount;
}
}
return 0;
}
/**
* @dev Function to flash exercise ACO tokens.
* The flash exercise uses the flash swap functionality on Uniswap.
* No asset is required to exercise the ACO token because the own collateral redeemed is used to fulfill the terms of the contract.
* The account will receive the remaining difference.
* @param acoToken Address of the ACO token.
* @param tokenAmount Amount of tokens to be exercised.
* @param minimumCollateral The minimum amount of collateral accepted to be received on the flash exercise.
* @param salt Random number to calculate the start index of the array of accounts to be exercised.
*/
function flashExercise(address acoToken, uint256 tokenAmount, uint256 minimumCollateral, uint256 salt) public {
_flashExercise(acoToken, tokenAmount, minimumCollateral, salt, new address[](0));
}
/**
* @dev Function to flash exercise ACO tokens.
* The flash exercise uses the flash swap functionality on Uniswap.
* No asset is required to exercise the ACO token because the own collateral redeemed is used to fulfill the terms of the contract.
* The account will receive the remaining difference.
* @param acoToken Address of the ACO token.
* @param tokenAmount Amount of tokens to be exercised.
* @param minimumCollateral The minimum amount of collateral accepted to be received on the flash exercise.
* @param accounts The array of addresses to get the deposited collateral.
*/
function flashExerciseAccounts(
address acoToken,
uint256 tokenAmount,
uint256 minimumCollateral,
address[] memory accounts
) public {
require(accounts.length > 0, "ACOFlashExercise::flashExerciseAccounts: Accounts are required");
_flashExercise(acoToken, tokenAmount, minimumCollateral, 0, accounts);
}
/**
* @dev External function to be called by the Uniswap pair on flash swap transaction.
* @param sender Address of the sender of the Uniswap swap. It must be the ACOFlashExercise contract.
* @param amount0Out Amount of token0 on Uniswap pair to be received on the flash swap.
* @param amount1Out Amount of token1 on Uniswap pair to be received on the flash swap.
* @param data The ABI encoded with ACO token flash exercise data.
*/
function uniswapV2Call(
address sender,
uint256 amount0Out,
uint256 amount1Out,
bytes calldata data
) external override {
require(sender == address(this), "ACOFlashExercise::uniswapV2Call: Invalid sender");
uint256 requiredAmount = _getFlasSwapPaymentRequired(amount0Out, amount1Out);
(address account, address collateral, uint256 collateralAmount, uint256 remainingAmount, address[] memory path) = _exercise(requiredAmount, data);
_sendAmounts(
account,
collateral,
collateralAmount,
remainingAmount,
requiredAmount,
path
);
}
/**
* @dev Internal function to send the remaining collateral amount to the account and pay the Uniswap.
* @param account Address of the account.
* @param collateral Address of the collateral.
* @param collateralAmount The collateral amount received on the exercise.
* @param remainingAmount The remaining collateral amount to be paid to the account.
* @param uniswapRequiredAmount The required amount to pay the Uniswap.
* @param path The second swap required path due to the middle route.
*/
function _sendAmounts(
address account,
address collateral,
uint256 collateralAmount,
uint256 remainingAmount,
uint256 uniswapRequiredAmount,
address[] memory path
) internal {
if (path.length > 0) {
uint256 toPayAmount = collateralAmount - remainingAmount;
if (ACOAssetHelper._isEther(collateral)) {
IWETH(weth).deposit{value: toPayAmount}();
}
IUniswapV2Router02(uniswapRouter).swapTokensForExactTokens(uniswapRequiredAmount, toPayAmount, path, msg.sender, block.timestamp);
} else {
address uniswapAsset;
if (ACOAssetHelper._isEther(collateral)) {
uniswapAsset = weth;
IWETH(uniswapAsset).deposit{value: uniswapRequiredAmount}();
} else {
uniswapAsset = collateral;
}
ACOAssetHelper._callTransferERC20(uniswapAsset, msg.sender, uniswapRequiredAmount);
}
ACOAssetHelper._transferAsset(collateral, account, remainingAmount);
}
/**
* @dev Internal function to validate the minimum collateral amount requested.
* @param requiredAmount The required collateral amount to pay Uniswap.
* @param collateralAmount The collateral amount received on the exercise.
* @param minimumCollateral The minimum collateral amount requested.
* @return The remaining amount to be paid to the account.
*/
function _validateMinimumCollateral(uint256 requiredAmount, uint256 collateralAmount, uint256 minimumCollateral) internal pure returns(uint256) {
require(requiredAmount <= collateralAmount, "ACOFlashExercise::_validateMinimumCollateral: Insufficient collateral amount");
uint256 remainingAmount = collateralAmount - requiredAmount;
require(remainingAmount >= minimumCollateral, "ACOFlashExercise::_validateMinimumCollateral: Minimum amount not satisfied");
return remainingAmount;
}
/**
* @dev Internal function to exercise ACO token and get the data for the payments.
* @param requiredAmount The required amount to pay Uniswap.
* @param data The ABI encoded with ACO token flash exercise data.
* @return Account address, collateral address, collateral amount received on exercise, remaining collateral amount to be paid to account and the second swap required path due to the middle route.
*/
function _exercise(uint256 requiredAmount, bytes memory data) internal returns(
address,
address,
uint256,
uint256,
address[] memory
) {
(address account,
address acoToken,
uint256 tokenAmount,
uint256 salt,
uint256 collateralAmount,
uint256 remainingAmount,
address[] memory accounts,
address[] memory path) = _getExerciseBaseData(requiredAmount, data);
_exerciseAco(account, acoToken, tokenAmount, salt, accounts);
address collateral = IACOToken(acoToken).collateral();
return (account, collateral, collateralAmount, remainingAmount, path);
}
/**
* @dev Internal function to get the base exercise data and validate the amounts.
* @param requiredAmount The required amount to pay Uniswap.
* @param data The ABI encoded with ACO token flash exercise data.
*/
function _getExerciseBaseData(uint256 requiredAmount, bytes memory data) internal view returns(
address account,
address acoToken,
uint256 tokenAmount,
uint256 salt,
uint256 collateralAmount,
uint256 remainingAmount,
address[] memory accounts,
address[] memory path
) {
uint256 minimumCollateral;
(account,
acoToken,
tokenAmount,
minimumCollateral,
salt,
accounts,
path) = abi.decode(data, (address, address, uint256, uint256, uint256, address[], address[]));
(collateralAmount,) = IACOToken(acoToken).getCollateralOnExercise(tokenAmount);
uint256 requiredCollateral;
if (path.length > 0) {
requiredCollateral = IUniswapV2Router02(uniswapRouter).getAmountsIn(requiredAmount, path)[0];
} else {
requiredCollateral = requiredAmount;
}
remainingAmount = _validateMinimumCollateral(requiredCollateral, collateralAmount, minimumCollateral);
}
/**
* @dev Internal function to exercise an ACO token.
* @param account Address of the owner of the ACO.
* @param acoToken Address of the ACO token.
* @param tokenAmount Amount of tokens to be exercised.
* @param salt Random number to calculate the start index of the array of accounts to be exercised when using standard method.
* @param accounts The array of addresses to get the deposited collateral. Whether the array is empty the exercise will be executed using the standard method.
*/
function _exerciseAco(
address account,
address acoToken,
uint256 tokenAmount,
uint256 salt,
address[] memory accounts
) internal {
(address exerciseAddress, uint256 expectedAmount) = _getAcoExerciseData(acoToken, tokenAmount, accounts);
uint256 ethValue = 0;
if (ACOAssetHelper._isEther(exerciseAddress)) {
ethValue = expectedAmount;
IWETH(weth).withdraw(expectedAmount);
} else {
ACOAssetHelper._callApproveERC20(exerciseAddress, acoToken, expectedAmount);
}
if (accounts.length == 0) {
IACOToken(acoToken).exerciseFrom{value: ethValue}(account, tokenAmount, salt);
} else {
IACOToken(acoToken).exerciseAccountsFrom{value: ethValue}(account, tokenAmount, accounts);
}
}
/**
* @dev Internal function to get the required amount to pay the Uniswap on flash swap.
* @param amount0Out Amount of token0 on Uniswap pair to be received on the flash swap.
* @param amount1Out Amount of token1 on Uniswap pair to be received on the flash swap.
* @return The required amount to be paid.
*/
function _getFlasSwapPaymentRequired(uint256 amount0Out, uint256 amount1Out) internal view returns(uint256) {
address token0 = IUniswapV2Pair(msg.sender).token0();
address token1 = IUniswapV2Pair(msg.sender).token1();
require(msg.sender == IUniswapV2Factory(uniswapFactory).getPair(token0, token1), "ACOFlashExercise::uniswapV2Call: Invalid transaction sender");
require(amount0Out == 0 || amount1Out == 0, "ACOFlashExercise::uniswapV2Call: Invalid out amounts");
(uint256 reserve0, uint256 reserve1,) = IUniswapV2Pair(msg.sender).getReserves();
uint256 reserveIn;
uint256 reserveOut;
if (amount0Out == 0) {
reserveIn = reserve0;
reserveOut = reserve1;
} else {
reserveIn = reserve1;
reserveOut = reserve0;
}
return IUniswapV2Router02(uniswapRouter).getAmountIn((amount0Out + amount1Out), reserveIn, reserveOut);
}
/**
* @dev Internal function to get the ACO tokens exercise data.
* @param acoToken Address of the ACO token.
* @param tokenAmount Amount of tokens to be exercised.
* @param accounts The array of addresses to be exercised. Whether the array is empty the exercise will be executed using the standard method.
* @return The asset and the respective amount that should be sent to get the collateral.
*/
function _getAcoExerciseData(address acoToken, uint256 tokenAmount, address[] memory accounts) internal view returns(address, uint256) {
(address exerciseAddress, uint256 expectedAmount) = IACOToken(acoToken).getBaseExerciseData(tokenAmount);
if (accounts.length == 0) {
expectedAmount = expectedAmount + IACOToken(acoToken).maxExercisedAccounts();
} else {
expectedAmount = expectedAmount + accounts.length;
}
return (exerciseAddress, expectedAmount);
}
/**
* @dev Internal function to flash exercise ACO tokens.
* @param acoToken Address of the ACO token.
* @param tokenAmount Amount of tokens to be exercised.
* @param minimumCollateral The minimum amount of collateral accepted to be received on the flash exercise.
* @param salt Random number to calculate the start index of the array of accounts to be exercised when using standard method.
* @param accounts The array of addresses to get the deposited collateral. Whether the array is empty the exercise will be executed using the standard method.
*/
function _flashExercise(
address acoToken,
uint256 tokenAmount,
uint256 minimumCollateral,
uint256 salt,
address[] memory accounts
) internal {
(address pair, address[] memory path) = _getUniswapData(acoToken);
require(pair != address(0), "ACOFlashExercise::_flashExercise: Invalid Uniswap pair");
(address exerciseAddress, uint256 expectedAmount) = _getAcoExerciseData(acoToken, tokenAmount, accounts);
uint256 amount0Out = 0;
uint256 amount1Out = 0;
if (_getUniswapToken(exerciseAddress, weth) == IUniswapV2Pair(pair).token0()) {
amount0Out = expectedAmount;
} else {
amount1Out = expectedAmount;
}
bytes memory data = abi.encode(msg.sender, acoToken, tokenAmount, minimumCollateral, salt, accounts, path);
IUniswapV2Pair(pair).swap(amount0Out, amount1Out, address(this), data);
}
/**
* @dev Internal function to get Uniswap token address.
* The Ethereum address on ACO must be swapped to WETH to be used on Uniswap.
* @param token Address of the token on ACO.
* @return Uniswap token address.
*/
function _getUniswapToken(address token, address _weth) internal pure returns(address) {
if (ACOAssetHelper._isEther(token)) {
return _weth;
} else {
return token;
}
}
/**
* @dev Internal function to get the Uniswap base data for the flash exercise.
* @param acoToken Address of the ACO token.
* @return Uniswap pair for execute the flash swap, the second swap required path due to the middle route.
*/
function _getUniswapData(address acoToken) internal view returns(address, address[] memory) {
IACOToken _aco = IACOToken(acoToken);
address underlying = _aco.underlying();
address strikeAsset = _aco.strikeAsset();
address _weth = weth;
(bool reversed, address[] storage route) = _getMiddleRoute(underlying, strikeAsset);
if (route.length > 0) {
bool isCall = _aco.isCall();
address swapAsset;
address[] memory path = new address[](route.length + 1);
path[0] = _getUniswapToken((isCall ? underlying : strikeAsset), _weth);
if ((isCall && reversed) || (!isCall && !reversed)) {
swapAsset = _getUniswapToken(route[0], _weth);
uint256 index = 1;
for (uint256 i = route.length; i > 1; --i) {
path[index] = _getUniswapToken(route[i - 1], _weth);
++index;
}
} else {
uint256 lastIndex = route.length - 1;
swapAsset = _getUniswapToken(route[lastIndex], _weth);
uint256 index = 1;
for (uint256 i = 0; i < lastIndex; ++i) {
path[index] = _getUniswapToken(route[i], _weth);
++index;
}
}
path[route.length] = swapAsset;
address pair = IUniswapV2Factory(uniswapFactory).getPair(_getUniswapToken((isCall ? strikeAsset : underlying), _weth), swapAsset);
return (pair, path);
} else {
address pair = IUniswapV2Factory(uniswapFactory).getPair(_getUniswapToken(underlying, _weth), _getUniswapToken(strikeAsset, _weth));
return (pair, new address[](0));
}
}
/**
* @dev Internal function to get the Uniswap middle route.
* @param baseAsset Address of the base asset.
* @param quoteAsset Address of the quote asset.
* @return If the assets are reversed and the middle route addresses.
*/
function _getMiddleRoute(address baseAsset, address quoteAsset) internal view returns(bool, address[] storage) {
address[] storage route = middleRoute[baseAsset][quoteAsset];
if (route.length > 0) {
return (false, route);
} else {
address[] storage route2 = middleRoute[quoteAsset][baseAsset];
return ((route2.length > 0), route2);
}
}
/**
* @dev Internal function to validate the addresses on the Uniswap middle route.
* @param asset0 Address of a pair asset.
* @param asset1 Address of another pair asset.
* @param uniswapMiddleRoute Addresses of Uniswap middle route for a swap.
*/
function _validateUniswapMiddleRoute(address asset0, address asset1, address[] memory uniswapMiddleRoute) internal pure {
for (uint256 i = 0; i < uniswapMiddleRoute.length; ++i) {
address asset = uniswapMiddleRoute[i];
require(asset0 != asset && asset1 != asset, "ACOFlashExercise::_validateUniswapMiddleRoute: Invalid middle route");
for (uint256 j = i+1; j < uniswapMiddleRoute.length; ++j) {
require(asset != uniswapMiddleRoute[j], "ACOFlashExercise::_validateUniswapMiddleRoute: Invalid middle route");
}
}
}
}ACOAssetHelper.sol
pragma solidity ^0.6.6;
library ACOAssetHelper {
uint256 internal constant MAX_UINT = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
/**
* @dev Internal function to get if the address is for Ethereum (0x0).
* @param _address Address to be checked.
* @return Whether the address is for Ethereum.
*/
function _isEther(address _address) internal pure returns(bool) {
return _address == address(0);
}
/**
* @dev Internal function to approve ERC20 tokens.
* @param token Address of the token.
* @param spender Authorized address.
* @param amount Amount to authorize.
*/
function _callApproveERC20(address token, address spender, uint256 amount) internal {
(bool success, bytes memory returndata) = token.call(abi.encodeWithSelector(0x095ea7b3, spender, amount));
require(success && (returndata.length == 0 || abi.decode(returndata, (bool))), "ACOAssetHelper::_callApproveERC20");
}
/**
* @dev Internal function to transfer ERC20 tokens.
* @param token Address of the token.
* @param recipient Address of the transfer destination.
* @param amount Amount to transfer.
*/
function _callTransferERC20(address token, address recipient, uint256 amount) internal {
(bool success, bytes memory returndata) = token.call(abi.encodeWithSelector(0xa9059cbb, recipient, amount));
require(success && (returndata.length == 0 || abi.decode(returndata, (bool))), "ACOAssetHelper::_callTransferERC20");
}
/**
* @dev Internal function to call transferFrom on ERC20 tokens.
* @param token Address of the token.
* @param sender Address of the sender.
* @param recipient Address of the transfer destination.
* @param amount Amount to transfer.
*/
function _callTransferFromERC20(address token, address sender, address recipient, uint256 amount) internal {
(bool success, bytes memory returndata) = token.call(abi.encodeWithSelector(0x23b872dd, sender, recipient, amount));
require(success && (returndata.length == 0 || abi.decode(returndata, (bool))), "ACOAssetHelper::_callTransferFromERC20");
}
/**
* @dev Internal function to the asset symbol.
* @param asset Address of the asset.
* @return The asset symbol.
*/
function _getAssetSymbol(address asset) internal view returns(string memory) {
if (_isEther(asset)) {
return "ETH";
} else {
(bool success, bytes memory returndata) = asset.staticcall(abi.encodeWithSelector(0x95d89b41));
require(success, "ACOAssetHelper::_getAssetSymbol");
return abi.decode(returndata, (string));
}
}
/**
* @dev Internal function to the asset decimals.
* @param asset Address of the asset.
* @return The asset decimals.
*/
function _getAssetDecimals(address asset) internal view returns(uint8) {
if (_isEther(asset)) {
return uint8(18);
} else {
(bool success, bytes memory returndata) = asset.staticcall(abi.encodeWithSelector(0x313ce567));
require(success, "ACOAssetHelper::_getAssetDecimals");
return abi.decode(returndata, (uint8));
}
}
/**
* @dev Internal function to the asset name.
* @param asset Address of the asset.
* @return The asset name.
*/
function _getAssetName(address asset) internal view returns(string memory) {
if (_isEther(asset)) {
return "Ethereum";
} else {
(bool success, bytes memory returndata) = asset.staticcall(abi.encodeWithSelector(0x06fdde03));
require(success, "ACOAssetHelper::_getAssetName");
return abi.decode(returndata, (string));
}
}
/**
* @dev Internal function to the asset balance of an account.
* @param asset Address of the asset.
* @param account Address of the account.
* @return The account balance.
*/
function _getAssetBalanceOf(address asset, address account) internal view returns(uint256) {
if (_isEther(asset)) {
return account.balance;
} else {
(bool success, bytes memory returndata) = asset.staticcall(abi.encodeWithSelector(0x70a08231, account));
require(success, "ACOAssetHelper::_getAssetBalanceOf");
return abi.decode(returndata, (uint256));
}
}
/**
* @dev Internal function to the asset allowance between two addresses.
* @param asset Address of the asset.
* @param owner Address of the owner of the tokens.
* @param spender Address of the spender authorized.
* @return The owner allowance for the spender.
*/
function _getAssetAllowance(address asset, address owner, address spender) internal view returns(uint256) {
if (_isEther(asset)) {
return 0;
} else {
(bool success, bytes memory returndata) = asset.staticcall(abi.encodeWithSelector(0xdd62ed3e, owner, spender));
require(success, "ACOAssetHelper::_getAssetAllowance");
return abi.decode(returndata, (uint256));
}
}
/**
* @dev Internal function to transfer an asset.
* @param asset Address of the asset to be transferred.
* @param to Address of the destination.
* @param amount The amount to be transferred.
*/
function _transferAsset(address asset, address to, uint256 amount) internal {
if (_isEther(asset)) {
(bool success,) = to.call{value:amount}(new bytes(0));
require(success, 'ACOAssetHelper::_transferAsset');
} else {
_callTransferERC20(asset, to, amount);
}
}
/**
* @dev Internal function to receive an asset.
* @param asset Address of the asset to be received.
* @param amount The amount to be received.
*/
function _receiveAsset(address asset, uint256 amount) internal {
if (_isEther(asset)) {
require(msg.value == amount, "ACOAssetHelper:: Invalid ETH amount");
} else {
require(msg.value == 0, "ACOAssetHelper:: Ether is not expected");
_callTransferFromERC20(asset, msg.sender, address(this), amount);
}
}
/**
* @dev Internal function to check asset allowance and set to Infinity if necessary.
* @param asset Address of the asset.
* @param owner Address of the owner of the tokens.
* @param spender Address of the spender authorized.
* @param amount Amount to check allowance.
*/
function _setAssetInfinityApprove(address asset, address owner, address spender, uint256 amount) internal {
if (_getAssetAllowance(asset, owner, spender) < amount) {
_callApproveERC20(asset, spender, MAX_UINT);
}
}
}IACOToken.sol
pragma solidity ^0.6.6;
import "./IERC20.sol";
interface IACOToken is IERC20 {
function init(address _underlying, address _strikeAsset, bool _isCall, uint256 _strikePrice, uint256 _expiryTime, uint256 _acoFee, address payable _feeDestination, uint256 _maxExercisedAccounts) external;
function name() external view returns(string memory);
function symbol() external view returns(string memory);
function decimals() external view returns(uint8);
function underlying() external view returns (address);
function strikeAsset() external view returns (address);
function feeDestination() external view returns (address);
function isCall() external view returns (bool);
function strikePrice() external view returns (uint256);
function expiryTime() external view returns (uint256);
function totalCollateral() external view returns (uint256);
function acoFee() external view returns (uint256);
function maxExercisedAccounts() external view returns (uint256);
function underlyingSymbol() external view returns (string memory);
function strikeAssetSymbol() external view returns (string memory);
function underlyingDecimals() external view returns (uint8);
function strikeAssetDecimals() external view returns (uint8);
function currentCollateral(address account) external view returns(uint256);
function unassignableCollateral(address account) external view returns(uint256);
function assignableCollateral(address account) external view returns(uint256);
function currentCollateralizedTokens(address account) external view returns(uint256);
function unassignableTokens(address account) external view returns(uint256);
function assignableTokens(address account) external view returns(uint256);
function getCollateralAmount(uint256 tokenAmount) external view returns(uint256);
function getTokenAmount(uint256 collateralAmount) external view returns(uint256);
function getBaseExerciseData(uint256 tokenAmount) external view returns(address, uint256);
function numberOfAccountsWithCollateral() external view returns(uint256);
function getCollateralOnExercise(uint256 tokenAmount) external view returns(uint256, uint256);
function collateral() external view returns(address);
function mintPayable() external payable returns(uint256);
function mintToPayable(address account) external payable returns(uint256);
function mint(uint256 collateralAmount) external returns(uint256);
function mintTo(address account, uint256 collateralAmount) external returns(uint256);
function burn(uint256 tokenAmount) external returns(uint256);
function burnFrom(address account, uint256 tokenAmount) external returns(uint256);
function redeem() external returns(uint256);
function redeemFrom(address account) external returns(uint256);
function exercise(uint256 tokenAmount, uint256 salt) external payable returns(uint256);
function exerciseFrom(address account, uint256 tokenAmount, uint256 salt) external payable returns(uint256);
function exerciseAccounts(uint256 tokenAmount, address[] calldata accounts) external payable returns(uint256);
function exerciseAccountsFrom(address account, uint256 tokenAmount, address[] calldata accounts) external payable returns(uint256);
}IERC20.sol
pragma solidity ^0.6.6;
// Contract on https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
IUniswapV2Callee.sol
pragma solidity ^0.6.6;
interface IUniswapV2Callee {
function uniswapV2Call(address sender, uint amount0, uint amount1, bytes calldata data) external;
}IUniswapV2Factory.sol
pragma solidity ^0.6.6;
interface IUniswapV2Factory {
event PairCreated(address indexed token0, address indexed token1, address pair, uint);
function feeTo() external view returns (address);
function feeToSetter() external view returns (address);
function getPair(address tokenA, address tokenB) external view returns (address pair);
function allPairs(uint) external view returns (address pair);
function allPairsLength() external view returns (uint);
function createPair(address tokenA, address tokenB) external returns (address pair);
function setFeeTo(address) external;
function setFeeToSetter(address) external;
}IUniswapV2Pair.sol
pragma solidity ^0.6.6;
interface IUniswapV2Pair {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);
function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function nonces(address owner) external view returns (uint);
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
event Mint(address indexed sender, uint amount0, uint amount1);
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);
function MINIMUM_LIQUIDITY() external pure returns (uint);
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function price0CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function kLast() external view returns (uint);
function mint(address to) external returns (uint liquidity);
function burn(address to) external returns (uint amount0, uint amount1);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function skim(address to) external;
function sync() external;
function initialize(address, address) external;
}IUniswapV2Router02.sol
pragma solidity 0.6.6;
interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function removeLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB);
function removeLiquidityETH(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountToken, uint amountETH);
function removeLiquidityWithPermit(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountA, uint amountB);
function removeLiquidityETHWithPermit(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountToken, uint amountETH);
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}
interface IUniswapV2Router02 is IUniswapV2Router01 {
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountETH);
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountETH);
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
}IWETH.sol
pragma solidity ^0.6.6;
interface IWETH {
function deposit() external payable;
function transfer(address to, uint value) external returns (bool);
function withdraw(uint) external;
}Ownable.sol
// SPDX-License-Identifier: MIT
// Adapted from OpenZeppelin
pragma solidity ^0.6.0;
/*
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with GSN meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes memory) {
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
return msg.data;
}
}
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
function init() internal {
require(_owner == address(0), "Ownable: Contract initialized");
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view returns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(_owner == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
}
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.