文件 1 的 8:Address.sol
pragma solidity ^0.8.0;
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly {
size := extcodesize(account)
}
return size > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(
address(this).balance >= amount,
"Address: insufficient balance"
);
(bool success, ) = recipient.call{ value: amount }("");
require(
success,
"Address: unable to send value, recipient may have reverted"
);
}
function functionCall(address target, bytes memory data)
internal
returns (bytes memory)
{
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
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"
);
}
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"
);
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) =
target.call{ value: value }(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data)
internal
view
returns (bytes memory)
{
return
functionStaticCall(
target,
data,
"Address: low-level static call failed"
);
}
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionDelegateCall(address target, bytes memory data)
internal
returns (bytes memory)
{
return
functionDelegateCall(
target,
data,
"Address: low-level delegate call failed"
);
}
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function _verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) private pure returns (bytes memory) {
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
文件 2 的 8:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
this;
return msg.data;
}
}
文件 3 的 8:IERC20.sol
pragma solidity ^0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount)
external
returns (bool);
function allowance(address owner, address spender)
external
view
returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
文件 4 的 8:Ownable.sol
pragma solidity ^0.8.0;
import "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
constructor() {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
function owner() public view virtual returns (address) {
return _owner;
}
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(
newOwner != address(0),
"Ownable: new owner is the zero address"
);
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
文件 5 的 8:SafeERC20.sol
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../../../utils/Address.sol";
library SafeERC20 {
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)
);
}
function safeApprove(
IERC20 token,
address spender,
uint256 value
) internal {
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) + value;
_callOptionalReturn(
token,
abi.encodeWithSelector(
token.approve.selector,
spender,
newAllowance
)
);
}
function safeDecreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(
oldAllowance >= value,
"SafeERC20: decreased allowance below zero"
);
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(
token,
abi.encodeWithSelector(
token.approve.selector,
spender,
newAllowance
)
);
}
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata =
address(token).functionCall(
data,
"SafeERC20: low-level call failed"
);
if (returndata.length > 0) {
require(
abi.decode(returndata, (bool)),
"SafeERC20: ERC20 operation did not succeed"
);
}
}
}
文件 6 的 8:ZapBaseV2.sol
pragma solidity ^0.8.0;
import "../oz/0.8.0/access/Ownable.sol";
import "../oz/0.8.0/token/ERC20/utils/SafeERC20.sol";
abstract contract ZapBaseV2 is Ownable {
using SafeERC20 for IERC20;
bool public stopped = false;
mapping(address => bool) public feeWhitelist;
uint256 public goodwill;
uint256 affiliateSplit;
mapping(address => bool) public affiliates;
mapping(address => mapping(address => uint256)) public affiliateBalance;
mapping(address => uint256) public totalAffiliateBalance;
mapping(address => bool) public approvedTargets;
address internal constant ETHAddress =
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
constructor(uint256 _goodwill, uint256 _affiliateSplit) {
goodwill = _goodwill;
affiliateSplit = _affiliateSplit;
}
modifier stopInEmergency {
if (stopped) {
revert("Temporarily Paused");
} else {
_;
}
}
function _getBalance(address token)
internal
view
returns (uint256 balance)
{
if (token == address(0)) {
balance = address(this).balance;
} else {
balance = IERC20(token).balanceOf(address(this));
}
}
function _approveToken(address token, address spender) internal {
IERC20 _token = IERC20(token);
if (_token.allowance(address(this), spender) > 0) return;
else {
_token.safeApprove(spender, type(uint256).max);
}
}
function _approveToken(
address token,
address spender,
uint256 amount
) internal {
IERC20(token).safeApprove(spender, 0);
IERC20(token).safeApprove(spender, amount);
}
function toggleContractActive() public onlyOwner {
stopped = !stopped;
}
function set_feeWhitelist(address zapAddress, bool status)
external
onlyOwner
{
feeWhitelist[zapAddress] = status;
}
function set_new_goodwill(uint256 _new_goodwill) public onlyOwner {
require(
_new_goodwill >= 0 && _new_goodwill <= 100,
"GoodWill Value not allowed"
);
goodwill = _new_goodwill;
}
function set_new_affiliateSplit(uint256 _new_affiliateSplit)
external
onlyOwner
{
require(
_new_affiliateSplit <= 100,
"Affiliate Split Value not allowed"
);
affiliateSplit = _new_affiliateSplit;
}
function set_affiliate(address _affiliate, bool _status)
external
onlyOwner
{
affiliates[_affiliate] = _status;
}
function withdrawTokens(address[] calldata tokens) external onlyOwner {
for (uint256 i = 0; i < tokens.length; i++) {
uint256 qty;
if (tokens[i] == ETHAddress) {
qty = address(this).balance - totalAffiliateBalance[tokens[i]];
Address.sendValue(payable(owner()), qty);
} else {
qty =
IERC20(tokens[i]).balanceOf(address(this)) -
totalAffiliateBalance[tokens[i]];
IERC20(tokens[i]).safeTransfer(owner(), qty);
}
}
}
function affilliateWithdraw(address[] calldata tokens) external {
uint256 tokenBal;
for (uint256 i = 0; i < tokens.length; i++) {
tokenBal = affiliateBalance[msg.sender][tokens[i]];
affiliateBalance[msg.sender][tokens[i]] = 0;
totalAffiliateBalance[tokens[i]] =
totalAffiliateBalance[tokens[i]] -
tokenBal;
if (tokens[i] == ETHAddress) {
Address.sendValue(payable(msg.sender), tokenBal);
} else {
IERC20(tokens[i]).safeTransfer(msg.sender, tokenBal);
}
}
}
function setApprovedTargets(
address[] calldata targets,
bool[] calldata isApproved
) external onlyOwner {
require(targets.length == isApproved.length, "Invalid Input length");
for (uint256 i = 0; i < targets.length; i++) {
approvedTargets[targets[i]] = isApproved[i];
}
}
receive() external payable {
require(msg.sender != tx.origin, "Do not send ETH directly");
}
}
文件 7 的 8:ZapInBaseV3.sol
pragma solidity ^0.8.0;
import "./ZapBaseV2.sol";
abstract contract ZapInBaseV3 is ZapBaseV2 {
using SafeERC20 for IERC20;
function _pullTokens(
address token,
uint256 amount,
address affiliate,
bool enableGoodwill,
bool shouldSellEntireBalance
) internal returns (uint256 value) {
uint256 totalGoodwillPortion;
if (token == address(0)) {
require(msg.value > 0, "No eth sent");
totalGoodwillPortion = _subtractGoodwill(
ETHAddress,
msg.value,
affiliate,
enableGoodwill
);
return msg.value - totalGoodwillPortion;
}
require(amount > 0, "Invalid token amount");
require(msg.value == 0, "Eth sent with token");
if (shouldSellEntireBalance) {
require(
Address.isContract(msg.sender),
"ERR: shouldSellEntireBalance is true for EOA"
);
amount = IERC20(token).allowance(msg.sender, address(this));
}
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
totalGoodwillPortion = _subtractGoodwill(
token,
amount,
affiliate,
enableGoodwill
);
return amount - totalGoodwillPortion;
}
function _subtractGoodwill(
address token,
uint256 amount,
address affiliate,
bool enableGoodwill
) internal returns (uint256 totalGoodwillPortion) {
bool whitelisted = feeWhitelist[msg.sender];
if (enableGoodwill && !whitelisted && goodwill > 0) {
totalGoodwillPortion = (amount * goodwill) / 10000;
if (affiliates[affiliate]) {
if (token == address(0)) {
token = ETHAddress;
}
uint256 affiliatePortion =
(totalGoodwillPortion * affiliateSplit) / 100;
affiliateBalance[affiliate][token] += affiliatePortion;
totalAffiliateBalance[token] += affiliatePortion;
}
}
}
}
文件 8 的 8:yVault_ZapIn_V4.sol
pragma solidity ^0.8.0;
import "../_base/ZapInBaseV3.sol";
interface IWETH {
function deposit() external payable;
function transfer(address to, uint256 value) external returns (bool);
function withdraw(uint256) external;
}
interface IYVault {
function deposit(uint256) external;
function withdraw(uint256) external;
function getPricePerFullShare() external view returns (uint256);
function token() external view returns (address);
function pricePerShare() external view returns (uint256);
}
interface IAaveLendingPoolAddressesProvider {
function getLendingPool() external view returns (address);
function getLendingPoolCore() external view returns (address payable);
}
interface IAaveLendingPoolCore {
function getReserveATokenAddress(address _reserve)
external
view
returns (address);
}
interface IAaveLendingPool {
function deposit(
address _reserve,
uint256 _amount,
uint16 _referralCode
) external payable;
}
contract yVault_ZapIn_V4 is ZapInBaseV3 {
using SafeERC20 for IERC20;
IAaveLendingPoolAddressesProvider
private constant lendingPoolAddressProvider =
IAaveLendingPoolAddressesProvider(
0x24a42fD28C976A61Df5D00D0599C34c4f90748c8
);
address private constant wethTokenAddress =
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
event zapIn(address sender, address pool, uint256 tokensRec);
constructor(
address _curveZapIn,
uint256 _goodwill,
uint256 _affiliateSplit
) ZapBaseV2(_goodwill, _affiliateSplit) {
approvedTargets[_curveZapIn] = true;
approvedTargets[0xDef1C0ded9bec7F1a1670819833240f027b25EfF] = true;
}
function ZapIn(
address fromToken,
uint256 amountIn,
address toVault,
address superVault,
bool isAaveUnderlying,
uint256 minYVTokens,
address intermediateToken,
address swapTarget,
bytes calldata swapData,
address affiliate,
bool shouldSellEntireBalance
) external payable stopInEmergency returns (uint256 tokensReceived) {
uint256 toInvest =
_pullTokens(
fromToken,
amountIn,
affiliate,
true,
shouldSellEntireBalance
);
uint256 intermediateAmt =
_fillQuote(
fromToken,
intermediateToken,
toInvest,
swapTarget,
swapData
);
if (isAaveUnderlying) {
address aaveLendingPoolCore =
lendingPoolAddressProvider.getLendingPoolCore();
_approveToken(intermediateToken, aaveLendingPoolCore);
IAaveLendingPool(lendingPoolAddressProvider.getLendingPool())
.deposit(intermediateToken, intermediateAmt, 0);
intermediateToken = IAaveLendingPoolCore(aaveLendingPoolCore)
.getReserveATokenAddress(intermediateToken);
}
return
_zapIn(
toVault,
superVault,
minYVTokens,
intermediateToken,
intermediateAmt
);
}
function _zapIn(
address toVault,
address superVault,
uint256 minYVTokens,
address intermediateToken,
uint256 intermediateAmt
) internal returns (uint256 tokensReceived) {
if (superVault == address(0)) {
tokensReceived = _vaultDeposit(
intermediateToken,
intermediateAmt,
toVault,
minYVTokens,
true
);
} else {
uint256 intermediateYVTokens =
_vaultDeposit(
intermediateToken,
intermediateAmt,
toVault,
0,
false
);
tokensReceived = _vaultDeposit(
IYVault(superVault).token(),
intermediateYVTokens,
superVault,
minYVTokens,
true
);
}
}
function _vaultDeposit(
address underlyingVaultToken,
uint256 amount,
address toVault,
uint256 minTokensRec,
bool shouldTransfer
) internal returns (uint256 tokensReceived) {
_approveToken(underlyingVaultToken, toVault);
uint256 iniYVaultBal = IERC20(toVault).balanceOf(address(this));
IYVault(toVault).deposit(amount);
tokensReceived =
IERC20(toVault).balanceOf(address(this)) -
iniYVaultBal;
require(tokensReceived >= minTokensRec, "Err: High Slippage");
if (shouldTransfer) {
IERC20(toVault).safeTransfer(msg.sender, tokensReceived);
emit zapIn(msg.sender, toVault, tokensReceived);
}
}
function _fillQuote(
address _fromTokenAddress,
address toToken,
uint256 _amount,
address _swapTarget,
bytes memory swapData
) internal returns (uint256 amtBought) {
if (_fromTokenAddress == toToken) {
return _amount;
}
if (_fromTokenAddress == address(0) && toToken == wethTokenAddress) {
IWETH(wethTokenAddress).deposit{ value: _amount }();
return _amount;
}
uint256 valueToSend;
if (_fromTokenAddress == address(0)) {
valueToSend = _amount;
} else {
_approveToken(_fromTokenAddress, _swapTarget);
}
uint256 iniBal = _getBalance(toToken);
require(approvedTargets[_swapTarget], "Target not Authorized");
(bool success, ) = _swapTarget.call{ value: valueToSend }(swapData);
require(success, "Error Swapping Tokens 1");
uint256 finalBal = _getBalance(toToken);
amtBought = finalBal - iniBal;
}
}
{
"compilationTarget": {
"contracts/yEarn/yVault_ZapIn_V4.sol": "yVault_ZapIn_V4"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_curveZapIn","type":"address"},{"internalType":"uint256","name":"_goodwill","type":"uint256"},{"internalType":"uint256","name":"_affiliateSplit","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"address","name":"pool","type":"address"},{"indexed":false,"internalType":"uint256","name":"tokensRec","type":"uint256"}],"name":"zapIn","type":"event"},{"inputs":[{"internalType":"address","name":"fromToken","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address","name":"toVault","type":"address"},{"internalType":"address","name":"superVault","type":"address"},{"internalType":"bool","name":"isAaveUnderlying","type":"bool"},{"internalType":"uint256","name":"minYVTokens","type":"uint256"},{"internalType":"address","name":"intermediateToken","type":"address"},{"internalType":"address","name":"swapTarget","type":"address"},{"internalType":"bytes","name":"swapData","type":"bytes"},{"internalType":"address","name":"affiliate","type":"address"},{"internalType":"bool","name":"shouldSellEntireBalance","type":"bool"}],"name":"ZapIn","outputs":[{"internalType":"uint256","name":"tokensReceived","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"affiliateBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"affiliates","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"tokens","type":"address[]"}],"name":"affilliateWithdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"approvedTargets","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"feeWhitelist","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"goodwill","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"targets","type":"address[]"},{"internalType":"bool[]","name":"isApproved","type":"bool[]"}],"name":"setApprovedTargets","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_affiliate","type":"address"},{"internalType":"bool","name":"_status","type":"bool"}],"name":"set_affiliate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"zapAddress","type":"address"},{"internalType":"bool","name":"status","type":"bool"}],"name":"set_feeWhitelist","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_new_affiliateSplit","type":"uint256"}],"name":"set_new_affiliateSplit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_new_goodwill","type":"uint256"}],"name":"set_new_goodwill","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"stopped","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"toggleContractActive","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"totalAffiliateBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"tokens","type":"address[]"}],"name":"withdrawTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]