Contract Name:
SlippageAccounter
Contract Source Code:
<i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { IVersion } from "./IVersion.sol";
interface IAddressProviderEvents {
/// @dev Emits when an address is set for a contract role
event AddressSet(bytes32 indexed service, address indexed newAddress);
}
/// @title Optimised for front-end Address Provider interface
interface IAddressProvider is IAddressProviderEvents, IVersion {
/// @return Address of ACL contract
function getACL() external view returns (address);
/// @return Address of ContractsRegister
function getContractsRegister() external view returns (address);
/// @return Address of AccountFactory
function getAccountFactory() external view returns (address);
/// @return Address of DataCompressor
function getDataCompressor() external view returns (address);
/// @return Address of GEAR token
function getGearToken() external view returns (address);
/// @return Address of WETH token
function getWethToken() external view returns (address);
/// @return Address of WETH Gateway
function getWETHGateway() external view returns (address);
/// @return Address of PriceOracle
function getPriceOracle() external view returns (address);
/// @return Address of DAO Treasury Multisig
function getTreasuryContract() external view returns (address);
/// @return Address of PathFinder
function getLeveragedActions() external view returns (address);
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { IVersion } from "./IVersion.sol";
interface IPriceOracleV2Events {
/// @dev Emits when a new price feed is added
event NewPriceFeed(address indexed token, address indexed priceFeed);
}
interface IPriceOracleV2Exceptions {
/// @dev Thrown if a price feed returns 0
error ZeroPriceException();
/// @dev Thrown if the last recorded result was not updated in the last round
error ChainPriceStaleException();
/// @dev Thrown on attempting to get a result for a token that does not have a price feed
error PriceOracleNotExistsException();
}
/// @title Price oracle interface
interface IPriceOracleV2 is
IPriceOracleV2Events,
IPriceOracleV2Exceptions,
IVersion
{
/// @dev Converts a quantity of an asset to USD (decimals = 8).
/// @param amount Amount to convert
/// @param token Address of the token to be converted
function convertToUSD(uint256 amount, address token)
external
view
returns (uint256);
/// @dev Converts a quantity of USD (decimals = 8) to an equivalent amount of an asset
/// @param amount Amount to convert
/// @param token Address of the token converted to
function convertFromUSD(uint256 amount, address token)
external
view
returns (uint256);
/// @dev Converts one asset into another
///
/// @param amount Amount to convert
/// @param tokenFrom Address of the token to convert from
/// @param tokenTo Address of the token to convert to
function convert(
uint256 amount,
address tokenFrom,
address tokenTo
) external view returns (uint256);
/// @dev Returns collateral values for two tokens, required for a fast check
/// @param amountFrom Amount of the outbound token
/// @param tokenFrom Address of the outbound token
/// @param amountTo Amount of the inbound token
/// @param tokenTo Address of the inbound token
/// @return collateralFrom Value of the outbound token amount in USD
/// @return collateralTo Value of the inbound token amount in USD
function fastCheck(
uint256 amountFrom,
address tokenFrom,
uint256 amountTo,
address tokenTo
) external view returns (uint256 collateralFrom, uint256 collateralTo);
/// @dev Returns token's price in USD (8 decimals)
/// @param token The token to compute the price for
function getPrice(address token) external view returns (uint256);
/// @dev Returns the price feed address for the passed token
/// @param token Token to get the price feed for
function priceFeeds(address token)
external
view
returns (address priceFeed);
/// @dev Returns the price feed for the passed token,
/// with additional parameters
/// @param token Token to get the price feed for
function priceFeedsWithFlags(address token)
external
view
returns (
address priceFeed,
bool skipCheck,
uint256 decimals
);
}
interface IPriceOracleV2Ext is IPriceOracleV2 {
/// @dev Sets a price feed if it doesn't exist, or updates an existing one
/// @param token Address of the token to set the price feed for
/// @param priceFeed Address of a USD price feed adhering to Chainlink's interface
function addPriceFeed(address token, address priceFeed) external;
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
/// @title IVersion
/// @dev Declares a version function which returns the contract's version
interface IVersion {
/// @dev Returns contract version
function version() external view returns (uint256);
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
interface ICurvePool {
function coins(uint256 i) external view returns (address);
function underlying_coins(uint256 i) external view returns (address);
function balances(uint256 i) external view returns (uint256);
function coins(int128) external view returns (address);
function underlying_coins(int128) external view returns (address);
function balances(int128) external view returns (uint256);
function exchange(
int128 i,
int128 j,
uint256 dx,
uint256 min_dy
) external;
function exchange_underlying(
int128 i,
int128 j,
uint256 dx,
uint256 min_dy
) external;
function get_dy_underlying(
int128 i,
int128 j,
uint256 dx
) external view returns (uint256);
function get_dy(
int128 i,
int128 j,
uint256 dx
) external view returns (uint256);
function get_virtual_price() external view returns (uint256);
function token() external view returns (address);
function remove_liquidity_one_coin(
uint256 _token_amount,
int128 i,
uint256 min_amount
) external;
function A() external view returns (uint256);
function A_precise() external view returns (uint256);
function calc_withdraw_one_coin(uint256 _burn_amount, int128 i)
external
view
returns (uint256);
function admin_balances(uint256 i) external view returns (uint256);
function admin() external view returns (address);
function fee() external view returns (uint256);
function admin_fee() external view returns (uint256);
function block_timestamp_last() external view returns (uint256);
function initial_A() external view returns (uint256);
function future_A() external view returns (uint256);
function initial_A_time() external view returns (uint256);
function future_A_time() external view returns (uint256);
// Some pools implement ERC20
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint256);
function balanceOf(address) external view returns (uint256);
function allowance(address, address) external view returns (uint256);
function totalSupply() external view returns (uint256);
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @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.
*/
abstract 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.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @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");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
pragma solidity ^0.8.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 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) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
/// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.13;
import {Ownable} from "openzeppelin-contracts/access/Ownable.sol";
import {ISlippageAccounter} from "./interfaces/ISlippageAccounter.sol";
import {ICurvePool} from "gearbox_integrations/integrations/curve/ICurvePool.sol";
import {IPriceOracleV2} from "gearbox_core/interfaces/IPriceOracle.sol";
import {IAddressProvider} from "gearbox_core/interfaces/IAddressProvider.sol";
contract SlippageAccounter is ISlippageAccounter, Ownable {
ICurvePool public curvePool =
ICurvePool(0xDC24316b9AE028F1497c275EB9192a3Ea0f67022);
address public FRAX = 0x853d955aCEf822Db058eb8505911ED77F175b99e;
address public WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
uint256 public MAX_BPS = 1e4;
uint256 public leverage = 2 * 1e4;
address public addressProvider = 0xcF64698AFF7E5f27A11dff868AF228653ba53be0;
constructor(uint256 _leverage) {
leverage = _leverage;
}
function getSlippageAccountedAmount(uint256 amountIn)
external
view
returns (uint256 amountOut)
{
if (leverage == 0) {
return amountIn;
}
IPriceOracleV2 priceOracle = IPriceOracleV2(
IAddressProvider(addressProvider).getPriceOracle()
);
uint256 wethBorrowed = priceOracle.convert(
(amountIn * leverage) / MAX_BPS,
FRAX,
WETH
);
uint256 stETHReceivedOnCurve = curvePool.get_dy(0, 1, wethBorrowed);
uint256 stETHReceivedOnLido = wethBorrowed - 1;
uint256 stETHConverted = stETHReceivedOnLido > stETHReceivedOnCurve
? stETHReceivedOnLido
: stETHReceivedOnCurve;
uint256 wethReturned = curvePool.get_dy(1, 0, stETHConverted);
uint256 slippageAmount = (amountIn * (wethBorrowed - wethReturned)) /
(2 * wethBorrowed); // half to account for only deposit
amountOut = amountIn - slippageAmount;
}
function setLeverage(uint256 _leverage) external onlyOwner {
leverage = _leverage;
}
function setAddressProvider(address _addressProvider) external onlyOwner {
addressProvider = _addressProvider;
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
/// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;
interface ISlippageAccounter {
function getSlippageAccountedAmount(uint256 amountIn)
external
returns (uint256);
}