|
|
|
|
|
|
|
pragma solidity ^0.8.14;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC20 {
|
|
|
|
|
|
|
|
function totalSupply() external view returns (uint256);
|
|
|
|
|
|
|
|
|
|
function balanceOf(address account) external view returns (uint256);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function transfer(address to, 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 from,
|
|
address to,
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
library Address {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isContract(address account) internal view returns (bool) {
|
|
|
|
|
|
|
|
|
|
return account.code.length > 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
|
|
) internal 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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
|
|
enum Module {
|
|
RESOLVER,
|
|
TIME,
|
|
PROXY,
|
|
SINGLE_EXEC
|
|
}
|
|
|
|
struct ModuleData {
|
|
Module[] modules;
|
|
bytes[] args;
|
|
}
|
|
|
|
interface IOps {
|
|
function createTask(
|
|
address execAddress,
|
|
bytes calldata execDataOrSelector,
|
|
ModuleData calldata moduleData,
|
|
address feeToken
|
|
) external returns (bytes32 taskId);
|
|
|
|
function cancelTask(bytes32 taskId) external;
|
|
|
|
function getFeeDetails() external view returns (uint256, address);
|
|
|
|
function gelato() external view returns (address payable);
|
|
|
|
function taskTreasury() external view returns (ITaskTreasuryUpgradable);
|
|
}
|
|
|
|
interface ITaskTreasuryUpgradable {
|
|
function depositFunds(
|
|
address receiver,
|
|
address token,
|
|
uint256 amount
|
|
) external payable;
|
|
|
|
function withdrawFunds(
|
|
address payable receiver,
|
|
address token,
|
|
uint256 amount
|
|
) external;
|
|
}
|
|
|
|
interface IOpsProxyFactory {
|
|
function getProxyOf(address account) external view returns (address, bool);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract OpsReady {
|
|
IOps public immutable ops;
|
|
address public immutable dedicatedMsgSender;
|
|
address private immutable _gelato;
|
|
address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
|
address private constant OPS_PROXY_FACTORY =
|
|
0xC815dB16D4be6ddf2685C201937905aBf338F5D7;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modifier onlyDedicatedMsgSender() {
|
|
require(msg.sender == dedicatedMsgSender, "Only dedicated msg.sender");
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
constructor(address _ops, address _taskCreator) {
|
|
ops = IOps(_ops);
|
|
_gelato = IOps(_ops).gelato();
|
|
(dedicatedMsgSender, ) = IOpsProxyFactory(OPS_PROXY_FACTORY).getProxyOf(
|
|
_taskCreator
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _transfer(uint256 _fee, address _feeToken) internal {
|
|
if (_feeToken == ETH) {
|
|
(bool success, ) = _gelato.call{value: _fee}("");
|
|
require(success, "_transfer: ETH transfer failed");
|
|
} else {
|
|
SafeERC20.safeTransfer(IERC20(_feeToken), _gelato, _fee);
|
|
}
|
|
}
|
|
|
|
function _getFeeDetails()
|
|
internal
|
|
view
|
|
returns (uint256 fee, address feeToken)
|
|
{
|
|
(fee, feeToken) = ops.getFeeDetails();
|
|
}
|
|
}
|
|
|
|
interface IUniswapReserve {
|
|
function swap(
|
|
address recipient,
|
|
bool zeroForOne,
|
|
int256 amountSpecified,
|
|
uint160 sqrtPriceLimitX96,
|
|
bytes calldata data
|
|
) external returns (int256 amount0, int256 amount1);
|
|
}
|
|
|
|
interface ERC20Like {
|
|
function approve(address spender, uint value) external returns(bool);
|
|
function transfer(address to, uint value) external returns(bool);
|
|
function balanceOf(address a) external view returns(uint);
|
|
}
|
|
|
|
interface WethLike is ERC20Like {
|
|
function deposit() external payable;
|
|
function withdraw(uint wad) external;
|
|
}
|
|
|
|
interface ReserveLike {
|
|
function trade(
|
|
address srcToken,
|
|
uint256 srcAmount,
|
|
address destToken,
|
|
address payable destAddress,
|
|
uint256 conversionRate,
|
|
bool validate
|
|
) external payable returns (bool);
|
|
}
|
|
|
|
contract GelatoLQTYArb is OpsReady {
|
|
address constant LQTY = 0x6DEA81C8171D0bA574754EF6F8b412F2Ed88c54D;
|
|
address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
|
|
|
|
IUniswapReserve constant LQTYETH = IUniswapReserve(0xD1D5A4c0eA98971894772Dcd6D2f1dc71083C44E);
|
|
uint160 constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;
|
|
uint160 constant MIN_SQRT_RATIO = 4295128739;
|
|
|
|
constructor(address _ops, address _taskCreator) OpsReady(_ops, _taskCreator) {}
|
|
|
|
function swap(uint lqtyQty, address reserve, address lqtyDest, uint minLqtyProfit) external payable returns(uint) {
|
|
bytes memory data = abi.encode(reserve);
|
|
LQTYETH.swap(address(this), true, int256(lqtyQty), MIN_SQRT_RATIO + 1, data);
|
|
|
|
uint retVal = ERC20Like(LQTY).balanceOf(address(this));
|
|
require(retVal >= minLqtyProfit, "insufficient arb profit");
|
|
ERC20Like(LQTY).transfer(lqtyDest, retVal);
|
|
|
|
return retVal;
|
|
}
|
|
|
|
function uniswapV3SwapCallback(
|
|
int256 amount0Delta,
|
|
int256 amount1Delta,
|
|
bytes calldata data
|
|
) external {
|
|
require(msg.sender == address(LQTYETH), "uniswapV3SwapCallback: invalid sender");
|
|
|
|
uint ethAmount = uint(-1 * amount1Delta);
|
|
WethLike(WETH).withdraw(ethAmount);
|
|
uint totalEthBal = address(this).balance;
|
|
|
|
|
|
(uint256 fee, address feeToken) = _getFeeDetails();
|
|
_transfer(fee, feeToken);
|
|
require(totalEthBal > fee, "Fee > ETH received");
|
|
|
|
uint remainingEth = totalEthBal - fee;
|
|
|
|
ReserveLike reserve = abi.decode(data, (ReserveLike));
|
|
reserve.trade{value: remainingEth}(
|
|
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE,
|
|
remainingEth,
|
|
LQTY,
|
|
payable(this),
|
|
1,
|
|
false
|
|
);
|
|
|
|
if(amount0Delta > 0) {
|
|
ERC20Like(LQTY).transfer(msg.sender, uint(amount0Delta));
|
|
}
|
|
}
|
|
|
|
receive() external payable {}
|
|
} |