// Sources flattened with hardhat v2.0.6 https://hardhat.org
// File @openzeppelin/contracts/token/ERC20/IERC20.sol@v3.2.0
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
/**
* @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);
}
// File @openzeppelin/contracts/math/SafeMath.sol@v3.2.0
pragma solidity ^0.6.0;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
// File @openzeppelin/contracts/utils/Address.sol@v3.2.0
pragma solidity ^0.6.2;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies in extcodesize, which returns 0 for contracts in
// construction, since the code is only stored at the end of the
// constructor execution.
uint256 size;
// solhint-disable-next-line no-inline-assembly
assembly { size := extcodesize(account) }
return size > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
// solhint-disable-next-line avoid-low-level-calls, avoid-call-value
(bool success, ) = recipient.call{ value: amount }("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain`call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
return _functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
return _functionCallWithValue(target, data, value, errorMessage);
}
function _functionCallWithValue(address target, bytes memory data, uint256 weiValue, string memory errorMessage) private returns (bytes memory) {
require(isContract(target), "Address: call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returndata) = target.call{ value: weiValue }(data);
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
// solhint-disable-next-line no-inline-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
// File @openzeppelin/contracts/token/ERC20/SafeERC20.sol@v3.2.0
pragma solidity ^0.6.0;
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using SafeMath for uint256;
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(IERC20 token, address spender, uint256 value) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
// solhint-disable-next-line max-line-length
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).add(value);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) { // Return data is optional
// solhint-disable-next-line max-line-length
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
// File @openzeppelin/contracts/utils/ReentrancyGuard.sol@v3.2.0
pragma solidity ^0.6.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor () internal {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and make it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
// File contracts/interface/ISpender.sol
pragma solidity ^0.6.0;
interface ISpender {
function spendFromUser(address _user, address _tokenAddr, uint256 _amount) external;
}
// File contracts/interface/IUniswapExchange.sol
pragma solidity >=0.5.0 <0.8.0;
interface IUniswapExchange {
// Address of ERC20 token sold on this exchange
function tokenAddress() external view returns (address token);
// Address of Uniswap Factory
function factoryAddress() external view returns (address factory);
// Provide Liquidity
function addLiquidity(uint256 min_liquidity, uint256 max_tokens, uint256 deadline) external payable returns (uint256);
function removeLiquidity(uint256 amount, uint256 min_eth, uint256 min_tokens, uint256 deadline) external returns (uint256, uint256);
// Get Prices
function getEthToTokenInputPrice(uint256 eth_sold) external view returns (uint256 tokens_bought);
function getEthToTokenOutputPrice(uint256 tokens_bought) external view returns (uint256 eth_sold);
function getTokenToEthInputPrice(uint256 tokens_sold) external view returns (uint256 eth_bought);
function getTokenToEthOutputPrice(uint256 eth_bought) external view returns (uint256 tokens_sold);
// Trade ETH to ERC20
function ethToTokenSwapInput(uint256 min_tokens, uint256 deadline) external payable returns (uint256 tokens_bought);
function ethToTokenTransferInput(uint256 min_tokens, uint256 deadline, address recipient) external payable returns (uint256 tokens_bought);
function ethToTokenSwapOutput(uint256 tokens_bought, uint256 deadline) external payable returns (uint256 eth_sold);
function ethToTokenTransferOutput(uint256 tokens_bought, uint256 deadline, address recipient) external payable returns (uint256 eth_sold);
// Trade ERC20 to ETH
function tokenToEthSwapInput(uint256 tokens_sold, uint256 min_eth, uint256 deadline) external returns (uint256 eth_bought);
function tokenToEthTransferInput(uint256 tokens_sold, uint256 min_eth, uint256 deadline, address recipient) external returns (uint256 eth_bought);
function tokenToEthSwapOutput(uint256 eth_bought, uint256 max_tokens, uint256 deadline) external returns (uint256 tokens_sold);
function tokenToEthTransferOutput(uint256 eth_bought, uint256 max_tokens, uint256 deadline, address recipient) external returns (uint256 tokens_sold);
// Trade ERC20 to ERC20
function tokenToTokenSwapInput(uint256 tokens_sold, uint256 min_tokens_bought, uint256 min_eth_bought, uint256 deadline, address token_addr) external returns (uint256 tokens_bought);
function tokenToTokenTransferInput(uint256 tokens_sold, uint256 min_tokens_bought, uint256 min_eth_bought, uint256 deadline, address recipient, address token_addr) external returns (uint256 tokens_bought);
function tokenToTokenSwapOutput(uint256 tokens_bought, uint256 max_tokens_sold, uint256 max_eth_sold, uint256 deadline, address token_addr) external returns (uint256 tokens_sold);
function tokenToTokenTransferOutput(uint256 tokens_bought, uint256 max_tokens_sold, uint256 max_eth_sold, uint256 deadline, address recipient, address token_addr) external returns (uint256 tokens_sold);
// Trade ERC20 to Custom Pool
function tokenToExchangeSwapInput(uint256 tokens_sold, uint256 min_tokens_bought, uint256 min_eth_bought, uint256 deadline, address exchange_addr) external returns (uint256 tokens_bought);
function tokenToExchangeTransferInput(uint256 tokens_sold, uint256 min_tokens_bought, uint256 min_eth_bought, uint256 deadline, address recipient, address exchange_addr) external returns (uint256 tokens_bought);
function tokenToExchangeSwapOutput(uint256 tokens_bought, uint256 max_tokens_sold, uint256 max_eth_sold, uint256 deadline, address exchange_addr) external returns (uint256 tokens_sold);
function tokenToExchangeTransferOutput(uint256 tokens_bought, uint256 max_tokens_sold, uint256 max_eth_sold, uint256 deadline, address recipient, address exchange_addr) external returns (uint256 tokens_sold);
// ERC20 comaptibility for liquidity tokens
function name() external view returns (bytes32);
function symbol() external view returns (bytes32);
function decimals() external view returns (uint256);
function transfer(address _to, uint256 _value) external returns (bool);
function transferFrom(address _from, address _to, uint256 value) external returns (bool);
function approve(address _spender, uint256 _value) external returns (bool);
function allowance(address _owner, address _spender) external view returns (uint256);
function balanceOf(address _owner) external view returns (uint256);
function totalSupply() external view returns (uint256);
// Never use
function setup(address token_addr) external;
}
// File contracts/interface/IUniswapFactory.sol
pragma solidity >=0.5.0 <0.8.0;
interface IUniswapFactory {
event PairCreated(
address indexed token0,
address indexed token1,
address pair,
uint256
);
function getPair(address tokenA, address tokenB)
external
view
returns (address pair);
function allPairs(uint256) external view returns (address pair);
function allPairsLength() external view returns (uint256);
function feeTo() external view returns (address);
function feeToSetter() external view returns (address);
function createPair(address tokenA, address tokenB)
external
returns (address pair);
// Create Exchange
function createExchange(address token) external returns (address exchange);
// Get Exchange and Token Info
function getExchange(address token) external view returns (address exchange);
function getToken(address exchange) external view returns (address token);
function getTokenWithId(uint256 tokenId) external view returns (address token);
// Never use
function initializeFactory(address template) external;
}
// File contracts/interface/IUniswapRouterV2.sol
pragma solidity >=0.5.0 <0.8.0;
interface IUniswapRouterV2 {
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function addLiquidity(
address tokenA,
address tokenB,
uint256 amountADesired,
uint256 amountBDesired,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
)
external
returns (
uint256 amountA,
uint256 amountB,
uint256 liquidity
);
function addLiquidityETH(
address token,
uint256 amountTokenDesired,
uint256 amountTokenMin,
uint256 amountETHMin,
address to,
uint256 deadline
)
external
payable
returns (
uint256 amountToken,
uint256 amountETH,
uint256 liquidity
);
function removeLiquidity(
address tokenA,
address tokenB,
uint256 liquidity,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
) external returns (uint256 amountA, uint256 amountB);
function getAmountsOut(uint256 amountIn, address[] calldata path)
external
view
returns (uint256[] memory amounts);
function getAmountsIn(uint256 amountOut, address[] calldata path)
external
view
returns (uint256[] memory amounts);
function swapETHForExactTokens(
uint256 amountOut,
address[] calldata path,
address to,
uint256 deadline
) external payable returns (uint256[] memory amounts);
function swapExactETHForTokens(
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external payable returns (uint256[] memory amounts);
}
// File contracts/interface/ICurveFi.sol
pragma solidity >=0.5.0 <0.8.0;
interface ICurveFi {
function get_virtual_price() external returns (uint256 out);
function add_liquidity(
uint256[2] calldata amounts,
uint256 deadline
) external;
function add_liquidity(
// sBTC pool
uint256[3] calldata amounts,
uint256 min_mint_amount
) external;
function add_liquidity(
// bUSD pool
uint256[4] calldata amounts,
uint256 min_mint_amount
) external;
function get_dx(
int128 i,
int128 j,
uint256 dy
) external view returns (uint256 out);
function get_dx_underlying(
int128 i,
int128 j,
uint256 dy
) external view returns (uint256 out);
function get_dy(
int128 i,
int128 j,
uint256 dx
) external view returns (uint256 out);
function get_dy_underlying(
int128 i,
int128 j,
uint256 dx
) external view returns (uint256 out);
function exchange(
int128 i,
int128 j,
uint256 dx,
uint256 min_dy
) external payable;
function exchange(
int128 i,
int128 j,
uint256 dx,
uint256 min_dy,
uint256 deadline
) external payable;
function exchange_underlying(
int128 i,
int128 j,
uint256 dx,
uint256 min_dy
) external payable;
function exchange_underlying(
int128 i,
int128 j,
uint256 dx,
uint256 min_dy,
uint256 deadline
) external payable;
function remove_liquidity(
uint256 _amount,
uint256 deadline,
uint256[2] calldata min_amounts
) external;
function remove_liquidity_imbalance(
uint256[2] calldata amounts,
uint256 deadline
) external;
function remove_liquidity_imbalance(
uint256[3] calldata amounts,
uint256 max_burn_amount
) external;
function remove_liquidity(uint256 _amount, uint256[3] calldata amounts)
external;
function remove_liquidity_imbalance(
uint256[4] calldata amounts,
uint256 max_burn_amount
) external;
function remove_liquidity(uint256 _amount, uint256[4] calldata amounts)
external;
function commit_new_parameters(
int128 amplification,
int128 new_fee,
int128 new_admin_fee
) external;
function apply_new_parameters() external;
function revert_new_parameters() external;
function commit_transfer_ownership(address _owner) external;
function apply_transfer_ownership() external;
function revert_transfer_ownership() external;
function withdraw_admin_fees() external;
function coins(int128 arg0) external returns (address out);
function underlying_coins(int128 arg0) external returns (address out);
function balances(int128 arg0) external returns (uint256 out);
function A() external returns (int128 out);
function fee() external returns (int128 out);
function admin_fee() external returns (int128 out);
function owner() external returns (address out);
function admin_actions_deadline() external returns (uint256 out);
function transfer_ownership_deadline() external returns (uint256 out);
function future_A() external returns (int128 out);
function future_fee() external returns (int128 out);
function future_admin_fee() external returns (int128 out);
function future_owner() external returns (address out);
}
// File contracts/interface/ISetAllowance.sol
pragma solidity ^0.6.0;
interface ISetAllowance {
function setAllowance(address[] memory tokenList, address spender) external;
function closeAllowance(address[] memory tokenList, address spender) external;
}
// File contracts/interface/IAMM.sol
pragma solidity ^0.6.0;
interface IAMM is ISetAllowance {
function trade(
address _makerAddress,
address _fromAssetAddress,
address _toAssetAddress,
uint256 _takerAssetAmount,
uint256 _makerAssetAmount,
uint256 _feeFactor,
address _spender,
address payable _receiver,
uint256 _nonce,
uint256 _deadline,
bytes memory _sig
) payable external returns (uint256);
}
// File contracts/interface/IWeth.sol
pragma solidity ^0.6.0;
interface IWETH {
function deposit() external payable;
function withdraw(uint256 amount) external;
function transferFrom(address src, address dst, uint wad) external returns (bool);
}
// File contracts/interface/IPermanentStorage.sol
pragma solidity ^0.6.0;
interface IPermanentStorage {
function wethAddr() external view returns (address);
function getCurvePoolInfo(address _makerAddr, address _takerAssetAddr, address _makerAssetAddr) external view returns (int128 takerAssetIndex, int128 makerAssetIndex, uint16 swapMethod, bool supportGetDx);
function setCurvePoolInfo(address _makerAddr, address[] calldata _underlyingCoins, address[] calldata _coins, bool _supportGetDx) external;
function isTransactionSeen(bytes32 _transactionHash) external view returns (bool);
function isRelayerValid(address _relayer) external view returns (bool);
function setTransactionSeen(bytes32 _transactionHash) external;
function setRelayersValid(address[] memory _relayers, bool[] memory _isValids) external;
}
// File contracts/interface/IERC1271Wallet.sol
pragma solidity ^0.6.0;
interface IERC1271Wallet {
/**
* @notice Verifies whether the provided signature is valid with respect to the provided data
* @dev MUST return the correct magic value if the signature provided is valid for the provided data
* > The bytes4 magic value to return when signature is valid is 0x20c13b0b : bytes4(keccak256("isValidSignature(bytes,bytes)")
* > This function MAY modify Ethereum's state
* @param _data Arbitrary length data signed on the behalf of address(this)
* @param _signature Signature byte array associated with _data
* @return magicValue Magic value 0x20c13b0b if the signature is valid and 0x0 otherwise
*
*/
function isValidSignature(
bytes calldata _data,
bytes calldata _signature)
external
view
returns (bytes4 magicValue);
/**
* @notice Verifies whether the provided signature is valid with respect to the provided hash
* @dev MUST return the correct magic value if the signature provided is valid for the provided hash
* > The bytes4 magic value to return when signature is valid is 0x20c13b0b : bytes4(keccak256("isValidSignature(bytes,bytes)")
* > This function MAY modify Ethereum's state
* @param _hash keccak256 hash that was signed
* @param _signature Signature byte array associated with _data
* @return magicValue Magic value 0x20c13b0b if the signature is valid and 0x0 otherwise
*/
function isValidSignature(
bytes32 _hash,
bytes calldata _signature)
external
view
returns (bytes4 magicValue);
}
// File contracts/utils/LibBytes.sol
/*
Copyright 2018 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This is a truncated version of the original LibBytes.sol library from ZeroEx.
*/
pragma solidity ^0.6.0;
library LibBytes {
using LibBytes for bytes;
/***********************************|
| Pop Bytes Functions |
|__________________________________*/
/**
* @dev Pops the last byte off of a byte array by modifying its length.
* @param b Byte array that will be modified.
* @return result The byte that was popped off.
*/
function popLastByte(bytes memory b)
internal
pure
returns (bytes1 result)
{
require(
b.length > 0,
"LibBytes#popLastByte: greater than zero length required"
);
// Store last byte.
result = b[b.length - 1];
assembly {
// Decrement length of byte array.
let newLen := sub(mload(b), 1)
mstore(b, newLen)
}
return result;
}
/// @dev Reads an address from a position in a byte array.
/// @param b Byte array containing an address.
/// @param index Index in byte array of address.
/// @return result address from byte array.
function readAddress(
bytes memory b,
uint256 index
)
internal
pure
returns (address result)
{
require(
b.length >= index + 20, // 20 is length of address
"LibBytes#readAddress greater or equal to 20 length required"
);
// Add offset to index:
// 1. Arrays are prefixed by 32-byte length parameter (add 32 to index)
// 2. Account for size difference between address length and 32-byte storage word (subtract 12 from index)
index += 20;
// Read address from array memory
assembly {
// 1. Add index to address of bytes array
// 2. Load 32-byte word from memory
// 3. Apply 20-byte mask to obtain address
result := and(mload(add(b, index)), 0xffffffffffffffffffffffffffffffffffffffff)
}
return result;
}
/***********************************|
| Read Bytes Functions |
|__________________________________*/
/**
* @dev Reads a bytes32 value from a position in a byte array.
* @param b Byte array containing a bytes32 value.
* @param index Index in byte array of bytes32 value.
* @return result bytes32 value from byte array.
*/
function readBytes32(
bytes memory b,
uint256 index
)
internal
pure
returns (bytes32 result)
{
require(
b.length >= index + 32,
"LibBytes#readBytes32 greater or equal to 32 length required"
);
// Arrays are prefixed by a 256 bit length parameter
index += 32;
// Read the bytes32 from array memory
assembly {
result := mload(add(b, index))
}
return result;
}
/// @dev Reads an unpadded bytes4 value from a position in a byte array.
/// @param b Byte array containing a bytes4 value.
/// @param index Index in byte array of bytes4 value.
/// @return result bytes4 value from byte array.
function readBytes4(
bytes memory b,
uint256 index
)
internal
pure
returns (bytes4 result)
{
require(
b.length >= index + 4,
"LibBytes#readBytes4 greater or equal to 4 length required"
);
// Arrays are prefixed by a 32 byte length field
index += 32;
// Read the bytes4 from array memory
assembly {
result := mload(add(b, index))
// Solidity does not require us to clean the trailing bytes.
// We do it anyway
result := and(result, 0xFFFFFFFF00000000000000000000000000000000000000000000000000000000)
}
return result;
}
function readBytes2(
bytes memory b,
uint256 index
)
internal
pure
returns (bytes2 result)
{
require(
b.length >= index + 2,
"LibBytes#readBytes2 greater or equal to 2 length required"
);
// Arrays are prefixed by a 32 byte length field
index += 32;
// Read the bytes4 from array memory
assembly {
result := mload(add(b, index))
// Solidity does not require us to clean the trailing bytes.
// We do it anyway
result := and(result, 0xFFFF000000000000000000000000000000000000000000000000000000000000)
}
return result;
}
}
// File contracts/utils/AMMLibEIP712.sol
/**
* Copyright 2018 ZeroEx Intl.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
pragma solidity ^0.6.0;
contract AMMLibEIP712 {
/***********************************|
| Constants |
|__________________________________*/
// EIP-191 Header
string public constant EIP191_HEADER = "\x19\x01";
// EIP712Domain
string public constant EIP712_DOMAIN_NAME = "Tokenlon";
string public constant EIP712_DOMAIN_VERSION = "v5";
// EIP712Domain Separator
bytes32 public immutable EIP712_DOMAIN_SEPARATOR = keccak256(
abi.encode(
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
keccak256(bytes(EIP712_DOMAIN_NAME)),
keccak256(bytes(EIP712_DOMAIN_VERSION)),
getChainID(),
address(this)
)
);
// keccak256("tradeWithPermit(address makerAddr,address takerAssetAddr,address makerAssetAddr,uint256 takerAssetAmount,uint256 makerAssetAmount,address userAddr,address receiverAddr,uint256 salt,uint256 deadline)");
bytes32 public constant TRADE_WITH_PERMIT_TYPEHASH = keccak256(
abi.encodePacked(
"tradeWithPermit(",
"address makerAddr,",
"address takerAssetAddr,",
"address makerAssetAddr,",
"uint256 takerAssetAmount,",
"uint256 makerAssetAmount,",
"address userAddr,",
"address receiverAddr,",
"uint256 salt,",
"uint256 deadline",
")"
)
);
/**
* @dev Return `chainId`
*/
function getChainID() internal pure returns (uint) {
uint chainId;
assembly {
chainId := chainid()
}
return chainId;
}
}
// File contracts/utils/SignatureValidator.sol
pragma solidity ^0.6.0;
/**
* @dev Contains logic for signature validation.
* Signatures from wallet contracts assume ERC-1271 support (https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1271.md)
* Notes: Methods are strongly inspired by contracts in https://github.com/0xProject/0x-monorepo/blob/development/
*/
contract SignatureValidator is AMMLibEIP712 {
using LibBytes for bytes;
/***********************************|
| Variables |
|__________________________________*/
// bytes4(keccak256("isValidSignature(bytes,bytes)"))
bytes4 constant internal ERC1271_MAGICVALUE = 0x20c13b0b;
// bytes4(keccak256("isValidSignature(bytes32,bytes)"))
bytes4 constant internal ERC1271_MAGICVALUE_BYTES32 = 0x1626ba7e;
// Allowed signature types.
enum SignatureType {
Illegal, // 0x00, default value
Invalid, // 0x01
EIP712, // 0x02
EthSign, // 0x03
WalletBytes, // 0x04
WalletBytes32, // 0x05
NSignatureTypes // 0x08, number of signature types. Always leave at end.
// 0x SignatureType
// Wallet, // 0x04
// Validator, // 0x05
}
/***********************************|
| Signature Functions |
|__________________________________*/
/**
* @dev Verifies that a hash has been signed by the given signer.
* @param _signerAddress Address that should have signed the given hash.
* @param _hash Hash of the EIP-712 encoded data
* @param _data Full EIP-712 data structure that was hashed and signed
* @param _sig Proof that the hash has been signed by signer.
* For non wallet signatures, _sig is expected to be an array tightly encoded as
* (bytes32 r, bytes32 s, uint8 v, uint256 nonce, SignatureType sigType)
* @return isValid True if the address recovered from the provided signature matches the input signer address.
*/
function isValidSignature(
address _signerAddress,
bytes32 _hash,
bytes memory _data,
bytes memory _sig
)
public
view
returns (bool isValid)
{
require(
_sig.length > 0,
"SignatureValidator#isValidSignature: length greater than 0 required"
);
require(
_signerAddress != address(0x0),
"SignatureValidator#isValidSignature: invalid signer"
);
// Pop last byte off of signature byte array.
uint8 signatureTypeRaw = uint8(_sig.popLastByte());
// Ensure signature is supported
require(
signatureTypeRaw < uint8(SignatureType.NSignatureTypes),
"SignatureValidator#isValidSignature: unsupported signature"
);
// Extract signature type
SignatureType signatureType = SignatureType(signatureTypeRaw);
// Variables are not scoped in Solidity.
uint8 v;
bytes32 r;
bytes32 s;
address recovered;
// Always illegal signature.
// This is always an implicit option since a signer can create a
// signature array with invalid type or length. We may as well make
// it an explicit option. This aids testing and analysis. It is
// also the initialization value for the enum type.
if (signatureType == SignatureType.Illegal) {
revert("SignatureValidator#isValidSignature: illegal signature");
// Signature using EIP712
} else if (signatureType == SignatureType.EIP712) {
require(
_sig.length == 97,
"SignatureValidator#isValidSignature: length 97 required"
);
r = _sig.readBytes32(0);
s = _sig.readBytes32(32);
v = uint8(_sig[64]);
recovered = ecrecover(_hash, v, r, s);
isValid = _signerAddress == recovered;
return isValid;
// Signed using web3.eth_sign() or Ethers wallet.signMessage()
} else if (signatureType == SignatureType.EthSign) {
require(
_sig.length == 97,
"SignatureValidator#isValidSignature: length 97 required"
);
r = _sig.readBytes32(0);
s = _sig.readBytes32(32);
v = uint8(_sig[64]);
recovered = ecrecover(
keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", _hash)),
v,
r,
s
);
isValid = _signerAddress == recovered;
return isValid;
// Signature verified by wallet contract with data validation.
} else if (signatureType == SignatureType.WalletBytes) {
isValid = ERC1271_MAGICVALUE == IERC1271Wallet(_signerAddress).isValidSignature(_data, _sig);
return isValid;
// Signature verified by wallet contract without data validation.
} else if (signatureType == SignatureType.WalletBytes32) {
isValid = ERC1271_MAGICVALUE_BYTES32 == IERC1271Wallet(_signerAddress).isValidSignature(_hash, _sig);
return isValid;
}
// Anything else is illegal (We do not return false because
// the signature may actually be valid, just not in a format
// that we currently support. In this case returning false
// may lead the caller to incorrectly believe that the
// signature was invalid.)
revert("SignatureValidator#isValidSignature: unsupported signature");
}
}
// File contracts/AMMWrapper.sol
pragma solidity ^0.6.0;
contract AMMWrapper is
IAMM,
ReentrancyGuard,
SignatureValidator
{
using SafeMath for uint256;
using SafeERC20 for IERC20;
// Constants do not have storage slot.
string public constant version = "5.1.0";
uint256 private constant MAX_UINT = 2**256 - 1;
uint256 private constant BPS_MAX = 10000;
address private constant ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
address private constant ZERO_ADDRESS = address(0);
address public immutable userProxy;
IWETH public immutable weth;
ISpender public immutable spender;
IPermanentStorage public immutable permStorage;
address public constant UNISWAP_V2_ROUTER_02_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address public constant SUSHISWAP_ROUTER_ADDRESS = 0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F;
// Below are the variables which consume storage slots.
address public operator;
uint256 public subsidyFactor;
/* Struct and event declaration */
// Group the local variables together to prevent
// Compiler error: Stack too deep, try removing local variables.
struct GroupedVars {
bool fromEth;
bool toEth;
string source;
bytes32 transactionHash;
address takerAssetInternalAddr;
address makerAssetInternalAddr;
uint256 receivedAmount;
uint256 settleAmount;
// Variables used as the copy of the function parameters
// to bypass stack too deep error when logging event.
address userAddr;
address takerAssetAddr;
uint256 takerAssetAmount;
address makerAddr;
address makerAssetAddr;
uint256 makerAssetAmount;
address payable receiverAddr;
uint16 feeFactor;
uint16 subsidyFactor;
}
event Swapped(
string source,
bytes32 indexed transactionHash,
address indexed userAddr,
address takerAssetAddr,
uint256 takerAssetAmount,
address makerAddr,
address makerAssetAddr,
uint256 makerAssetAmount,
address receiverAddr,
uint256 settleAmount,
uint256 receivedAmount,
uint16 feeFactor,
uint16 subsidyFactor
);
receive() external payable {}
/************************************************************
* Access control and ownership management *
*************************************************************/
modifier onlyOperator() {
require(operator == msg.sender, "AMMWrapper: not the operator");
_;
}
modifier onlyUserProxy() {
require(address(userProxy) == msg.sender, "AMMWrapper: not the UserProxy contract");
_;
}
function transferOwnership(address _newOperator) external onlyOperator {
require(_newOperator != address(0), "AMMWrapper: operator can not be zero address");
operator = _newOperator;
}
/************************************************************
* Constructor and init functions *
*************************************************************/
constructor (
address _operator,
uint256 _subsidyFactor,
address _userProxy,
ISpender _spender,
IPermanentStorage _permStorage,
IWETH _weth
) public {
operator = _operator;
subsidyFactor = _subsidyFactor;
userProxy = _userProxy;
spender = _spender;
permStorage = _permStorage;
weth = _weth;
}
/************************************************************
* Management functions for Operator *
*************************************************************/
function setSubsidyFactor(uint256 _subsidyFactor) external onlyOperator {
subsidyFactor = _subsidyFactor;
}
/**
* @dev approve spender to transfer tokens from this contract. This is used to collect fee.
*/
function setAllowance(address[] calldata _tokenList, address _spender) override external onlyOperator {
for (uint256 i = 0 ; i < _tokenList.length; i++) {
IERC20(_tokenList[i]).safeApprove(_spender, MAX_UINT);
}
}
function closeAllowance(address[] calldata _tokenList, address _spender) override external onlyOperator {
for (uint256 i = 0 ; i < _tokenList.length; i++) {
IERC20(_tokenList[i]).safeApprove(_spender, 0);
}
}
/**
* @dev convert collected ETH to WETH
*/
function depositETH() external onlyOperator {
uint256 balance = address(this).balance;
if (balance > 0) weth.deposit{value: balance}();
}
/************************************************************
* External functions *
*************************************************************/
function trade(
address _makerAddr,
address _takerAssetAddr,
address _makerAssetAddr,
uint256 _takerAssetAmount,
uint256 _makerAssetAmount,
uint256 _feeFactor,
address _userAddr,
address payable _receiverAddr,
uint256 _salt,
uint256 _deadline,
bytes calldata _sig
)
override
payable
external
nonReentrant
onlyUserProxy
returns (uint256)
{
require(_deadline >= block.timestamp, "AMMWrapper: expired order");
GroupedVars memory vars;
// These variables are copied straight from function parameters and
// used to bypass stack too deep error.
vars.userAddr = _userAddr;
vars.takerAssetAddr = _takerAssetAddr;
vars.takerAssetAmount = _takerAssetAmount;
vars.makerAddr = _makerAddr;
vars.makerAssetAddr = _makerAssetAddr;
vars.makerAssetAmount = _makerAssetAmount;
vars.receiverAddr = _receiverAddr;
vars.subsidyFactor = uint16(subsidyFactor);
vars.feeFactor = uint16(_feeFactor);
if (! permStorage.isRelayerValid(tx.origin)) {
vars.feeFactor = (vars.subsidyFactor > vars.feeFactor) ? vars.subsidyFactor : vars.feeFactor;
vars.subsidyFactor = 0;
}
// Assign trade vairables
vars.fromEth = (_takerAssetAddr == ZERO_ADDRESS || _takerAssetAddr == ETH_ADDRESS);
vars.toEth = (_makerAssetAddr == ZERO_ADDRESS || _makerAssetAddr == ETH_ADDRESS);
vars.takerAssetInternalAddr = vars.fromEth? address(weth) : _takerAssetAddr;
vars.makerAssetInternalAddr = vars.toEth ? address(weth) : _makerAssetAddr;
vars.transactionHash = _prepare(
vars.fromEth,
vars.makerAddr,
vars.takerAssetAddr,
vars.makerAssetAddr,
vars.takerAssetAmount,
vars.makerAssetAmount,
vars.userAddr,
vars.receiverAddr,
_salt,
_deadline,
_sig
);
(vars.source, vars.receivedAmount) = _swap(
vars,
_deadline
);
// Settle
vars.settleAmount = _settle(
vars.toEth,
IERC20(vars.makerAssetInternalAddr),
vars.makerAssetAmount,
vars.receivedAmount,
vars.feeFactor,
vars.subsidyFactor,
vars.receiverAddr
);
emit Swapped(
vars.source,
vars.transactionHash,
vars.userAddr,
vars.takerAssetAddr,
vars.takerAssetAmount,
vars.makerAddr,
vars.makerAssetAddr,
vars.makerAssetAmount,
vars.receiverAddr,
vars.settleAmount,
vars.receivedAmount,
vars.feeFactor,
vars.subsidyFactor
);
return vars.settleAmount;
}
/**
* @dev internal function of `trade`.
* It verifies user signature, transfer tokens from user and store tx hash to prevent replay attack.
*/
function _prepare(
bool fromEth,
address _makerAddr,
address _takerAssetAddr,
address _makerAssetAddr,
uint256 _takerAssetAmount,
uint256 _makerAssetAmount,
address _userAddr,
address _receiverAddr,
uint256 _salt,
uint256 _deadline,
bytes calldata _sig
) internal returns (bytes32 transactionHash) {
// Verify user signature
// TRADE_WITH_PERMIT_TYPEHASH = keccak256("tradeWithPermit(address makerAddr,address takerAssetAddr,address makerAssetAddr,uint256 takerAssetAmount,uint256 makerAssetAmount,address userAddr,address receiverAddr,uint256 salt,uint256 deadline)");
transactionHash = keccak256(
abi.encode(
TRADE_WITH_PERMIT_TYPEHASH,
_makerAddr,
_takerAssetAddr,
_makerAssetAddr,
_takerAssetAmount,
_makerAssetAmount,
_userAddr,
_receiverAddr,
_salt,
_deadline
)
);
bytes32 EIP712SignDigest = keccak256(
abi.encodePacked(
EIP191_HEADER,
EIP712_DOMAIN_SEPARATOR,
transactionHash
)
);
require(isValidSignature(_userAddr, EIP712SignDigest, bytes(""), _sig), "AMMWrapper: invalid user signature");
// Transfer asset from user and deposit to weth if needed
if (fromEth) {
require(msg.value > 0, "AMMWrapper: msg.value is zero");
require(_takerAssetAmount == msg.value, "AMMWrapper: msg.value doesn't match");
// Deposit ETH to weth
weth.deposit{value: msg.value}();
} else {
spender.spendFromUser(_userAddr, _takerAssetAddr, _takerAssetAmount);
}
// Validate that the transaction is not seen before
require(! permStorage.isTransactionSeen(transactionHash), "AMMWrapper: transaction seen before");
// Set transaction as seen
permStorage.setTransactionSeen(transactionHash);
}
/**
* @dev internal function of `trade`.
* It executes the swap on chosen AMM.
*/
function _swap(
GroupedVars memory _vars,
uint256 _deadline
) internal returns (string memory source, uint256 receivedAmount) {
// Approve
IERC20(_vars.takerAssetInternalAddr).safeApprove(_vars.makerAddr, _vars.takerAssetAmount);
// Swap
// minAmount = makerAssetAmount * (10000 - subsidyFactor) / 10000
uint256 minAmount = _vars.makerAssetAmount.mul((BPS_MAX.sub(_vars.subsidyFactor))).div(BPS_MAX);
if (_vars.makerAddr == UNISWAP_V2_ROUTER_02_ADDRESS ||
_vars.makerAddr == SUSHISWAP_ROUTER_ADDRESS) {
source = (_vars.makerAddr == SUSHISWAP_ROUTER_ADDRESS) ? "SushiSwap" : "Uniswap V2";
receivedAmount = _tradeUniswapV2TokenToToken(
_vars.makerAddr,
_vars.takerAssetInternalAddr,
_vars.makerAssetInternalAddr,
_vars.takerAssetAmount,
minAmount,
_deadline
);
} else {
(
int128 fromTokenCurveIndex,
int128 toTokenCurveIndex,
uint16 swapMethod,
) = permStorage.getCurvePoolInfo(
_vars.makerAddr,
_vars.takerAssetInternalAddr,
_vars.makerAssetInternalAddr
);
require(swapMethod != 0,"AMMWrapper: swap method not registered");
if (fromTokenCurveIndex > 0 && toTokenCurveIndex > 0) {
source = "Curve";
// Substract index by 1 because indices stored in `permStorage` starts from 1
fromTokenCurveIndex = fromTokenCurveIndex - 1;
toTokenCurveIndex = toTokenCurveIndex - 1;
uint256 balanceBeforeTrade = IERC20(_vars.makerAssetAddr).balanceOf(address(this));
_tradeCurveTokenToToken(
_vars.makerAddr,
fromTokenCurveIndex,
toTokenCurveIndex,
_vars.takerAssetAmount,
minAmount,
swapMethod
);
uint256 balanceAfterTrade = IERC20(_vars.makerAssetAddr).balanceOf(address(this));
receivedAmount = balanceAfterTrade.sub(balanceBeforeTrade);
} else {
revert("AMMWrapper: Unsupported makerAddr");
}
}
// Close allowance
IERC20(_vars.takerAssetInternalAddr).safeApprove(_vars.makerAddr, 0);
}
/**
* @dev internal function of `trade`.
* It collects fee from the trade or compensates the trade based on the actual amount swapped.
*/
function _settle(
bool _toEth,
IERC20 _makerAsset,
uint256 _makerAssetAmount,
uint256 _receivedAmount,
uint256 _feeFactor,
uint256 _subsidyFactor,
address payable _receiverAddr
)
internal
returns (uint256 settleAmount)
{
if (_receivedAmount == _makerAssetAmount) {
settleAmount = _receivedAmount;
} else if (_receivedAmount > _makerAssetAmount) {
// shouldCollectFee = ((receivedAmount - makerAssetAmount) / receivedAmount) > (feeFactor / 10000)
bool shouldCollectFee = _receivedAmount.sub(_makerAssetAmount).mul(BPS_MAX) > _feeFactor.mul(_receivedAmount);
if (shouldCollectFee) {
// settleAmount = receivedAmount * (1 - feeFactor) / 10000
settleAmount = _receivedAmount.mul(BPS_MAX.sub(_feeFactor)).div(BPS_MAX);
} else {
settleAmount = _makerAssetAmount;
}
} else {
require(_subsidyFactor > 0, "AMMWrapper: this trade will not be subsidized");
// If fee factor is smaller than subsidy factor, choose fee factor as actual subsidy factor
// since we should subsidize less if we charge less.
uint256 actualSubsidyFactor = (_subsidyFactor < _feeFactor) ? _subsidyFactor : _feeFactor;
// inSubsidyRange = ((makerAssetAmount - receivedAmount) / receivedAmount) > (actualSubsidyFactor / 10000)
bool inSubsidyRange = _makerAssetAmount.sub(_receivedAmount).mul(BPS_MAX) <= actualSubsidyFactor.mul(_receivedAmount);
require(inSubsidyRange, "AMMWrapper: amount difference larger than subsidy amount");
bool hasEnoughToSubsidize = (_makerAsset.balanceOf(address(this)) >= _makerAssetAmount);
require(hasEnoughToSubsidize, "AMMWrapper: not enough savings to subsidize");
settleAmount = _makerAssetAmount;
}
// Transfer token/Eth to receiver
if (_toEth) {
weth.withdraw(settleAmount);
_receiverAddr.transfer(settleAmount);
} else {
_makerAsset.safeTransfer(_receiverAddr, settleAmount);
}
}
function _tradeCurveTokenToToken(
address _makerAddr,
int128 i,
int128 j,
uint256 _takerAssetAmount,
uint256 _makerAssetAmount,
uint16 swapMethod
)
internal
{
ICurveFi curve = ICurveFi(_makerAddr);
if (swapMethod == 1) {
curve.exchange(i, j, _takerAssetAmount, _makerAssetAmount);
} else if (swapMethod == 2) {
curve.exchange_underlying(i, j, _takerAssetAmount, _makerAssetAmount);
}
}
function _tradeUniswapV2TokenToToken(
address _makerAddr,
address _takerAssetAddr,
address _makerAssetAddr,
uint256 _takerAssetAmount,
uint256 _makerAssetAmount,
uint256 _deadline
)
internal
returns (uint256)
{
IUniswapRouterV2 router = IUniswapRouterV2(_makerAddr);
address[] memory path = new address[](2);
path[0] = _takerAssetAddr;
path[1] = _makerAssetAddr;
uint256[] memory amounts = router.swapExactTokensForTokens(
_takerAssetAmount,
_makerAssetAmount,
path,
address(this),
_deadline
);
return amounts[1];
}
}
{
"compilationTarget": {
"AMMWrapper.sol": "AMMWrapper"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 1000
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_operator","type":"address"},{"internalType":"uint256","name":"_subsidyFactor","type":"uint256"},{"internalType":"address","name":"_userProxy","type":"address"},{"internalType":"contract ISpender","name":"_spender","type":"address"},{"internalType":"contract IPermanentStorage","name":"_permStorage","type":"address"},{"internalType":"contract IWETH","name":"_weth","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"source","type":"string"},{"indexed":true,"internalType":"bytes32","name":"transactionHash","type":"bytes32"},{"indexed":true,"internalType":"address","name":"userAddr","type":"address"},{"indexed":false,"internalType":"address","name":"takerAssetAddr","type":"address"},{"indexed":false,"internalType":"uint256","name":"takerAssetAmount","type":"uint256"},{"indexed":false,"internalType":"address","name":"makerAddr","type":"address"},{"indexed":false,"internalType":"address","name":"makerAssetAddr","type":"address"},{"indexed":false,"internalType":"uint256","name":"makerAssetAmount","type":"uint256"},{"indexed":false,"internalType":"address","name":"receiverAddr","type":"address"},{"indexed":false,"internalType":"uint256","name":"settleAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"receivedAmount","type":"uint256"},{"indexed":false,"internalType":"uint16","name":"feeFactor","type":"uint16"},{"indexed":false,"internalType":"uint16","name":"subsidyFactor","type":"uint16"}],"name":"Swapped","type":"event"},{"inputs":[],"name":"EIP191_HEADER","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"EIP712_DOMAIN_NAME","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"EIP712_DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"EIP712_DOMAIN_VERSION","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"SUSHISWAP_ROUTER_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"TRADE_WITH_PERMIT_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"UNISWAP_V2_ROUTER_02_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"_tokenList","type":"address[]"},{"internalType":"address","name":"_spender","type":"address"}],"name":"closeAllowance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"depositETH","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_signerAddress","type":"address"},{"internalType":"bytes32","name":"_hash","type":"bytes32"},{"internalType":"bytes","name":"_data","type":"bytes"},{"internalType":"bytes","name":"_sig","type":"bytes"}],"name":"isValidSignature","outputs":[{"internalType":"bool","name":"isValid","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"operator","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"permStorage","outputs":[{"internalType":"contract IPermanentStorage","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"_tokenList","type":"address[]"},{"internalType":"address","name":"_spender","type":"address"}],"name":"setAllowance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_subsidyFactor","type":"uint256"}],"name":"setSubsidyFactor","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"spender","outputs":[{"internalType":"contract ISpender","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"subsidyFactor","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_makerAddr","type":"address"},{"internalType":"address","name":"_takerAssetAddr","type":"address"},{"internalType":"address","name":"_makerAssetAddr","type":"address"},{"internalType":"uint256","name":"_takerAssetAmount","type":"uint256"},{"internalType":"uint256","name":"_makerAssetAmount","type":"uint256"},{"internalType":"uint256","name":"_feeFactor","type":"uint256"},{"internalType":"address","name":"_userAddr","type":"address"},{"internalType":"address payable","name":"_receiverAddr","type":"address"},{"internalType":"uint256","name":"_salt","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"},{"internalType":"bytes","name":"_sig","type":"bytes"}],"name":"trade","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"_newOperator","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"userProxy","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"version","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"weth","outputs":[{"internalType":"contract IWETH","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]