|
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.7.0;
|
|
|
|
struct FarmingPositionRequest {
|
|
uint256 setupIndex;
|
|
uint256 amount0;
|
|
uint256 amount1;
|
|
address positionOwner;
|
|
uint256 amount0Min;
|
|
uint256 amount1Min;
|
|
}
|
|
|
|
struct FarmingSetupConfiguration {
|
|
bool add;
|
|
bool disable;
|
|
uint256 index;
|
|
FarmingSetupInfo info;
|
|
}
|
|
|
|
struct FarmingSetupInfo {
|
|
uint256 blockDuration;
|
|
uint256 startBlock;
|
|
uint256 originalRewardPerBlock;
|
|
uint256 minStakeable;
|
|
uint256 renewTimes;
|
|
address liquidityPoolTokenAddress;
|
|
address mainTokenAddress;
|
|
bool involvingETH;
|
|
uint256 setupsCount;
|
|
uint256 lastSetupIndex;
|
|
int24 tickLower;
|
|
int24 tickUpper;
|
|
}
|
|
|
|
struct FarmingSetup {
|
|
uint256 infoIndex;
|
|
bool active;
|
|
uint256 startBlock;
|
|
uint256 endBlock;
|
|
uint256 lastUpdateBlock;
|
|
uint256 objectId;
|
|
uint256 rewardPerBlock;
|
|
uint128 totalSupply;
|
|
}
|
|
|
|
struct FarmingPosition {
|
|
address uniqueOwner;
|
|
uint256 setupIndex;
|
|
uint256 creationBlock;
|
|
uint128 liquidityPoolTokenAmount;
|
|
uint256 reward;
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.7.0;
|
|
pragma abicoder v2;
|
|
|
|
|
|
interface IFarmExtension {
|
|
|
|
function init(bool byMint, address host, address treasury) external;
|
|
|
|
function setHost(address host) external;
|
|
function setTreasury(address treasury) external;
|
|
|
|
function data() external view returns(address farmMainContract, bool byMint, address host, address treasury, address rewardTokenAddress);
|
|
|
|
function transferTo(uint256 amount) external;
|
|
function backToYou(uint256 amount) external payable;
|
|
|
|
function setFarmingSetups(FarmingSetupConfiguration[] memory farmingSetups) external;
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.7.0;
|
|
|
|
|
|
|
|
interface IFarmMain {
|
|
|
|
function ONE_HUNDRED() external view returns(uint256);
|
|
function _rewardTokenAddress() external view returns(address);
|
|
function position(uint256 positionId) external view returns (FarmingPosition memory);
|
|
function setups() external view returns (FarmingSetup[] memory);
|
|
function setup(uint256 setupIndex) external view returns (FarmingSetup memory, FarmingSetupInfo memory);
|
|
function setFarmingSetups(FarmingSetupConfiguration[] memory farmingSetups) external;
|
|
function openPosition(FarmingPositionRequest calldata request) external payable returns(uint256 positionId);
|
|
function addLiquidity(uint256 positionId, FarmingPositionRequest calldata request) external payable;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.7.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 safeApprove(address spender, uint256 amount) external;
|
|
|
|
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
|
|
|
|
function decimals() external view returns (uint8);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.7.0;
|
|
|
|
interface IERC20Mintable {
|
|
function mint(address wallet, uint256 amount) external returns (bool);
|
|
function burn(address wallet, uint256 amount) external returns (bool);
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.7.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contract FarmExtension is IFarmExtension {
|
|
|
|
|
|
address internal _host;
|
|
address internal _treasury;
|
|
|
|
address internal _farmMainContract;
|
|
|
|
address internal _rewardTokenAddress;
|
|
|
|
bool internal _byMint;
|
|
|
|
|
|
|
|
|
|
modifier farmMainOnly() {
|
|
require(msg.sender == _farmMainContract, "Unauthorized");
|
|
_;
|
|
}
|
|
|
|
|
|
modifier hostOnly() {
|
|
require(msg.sender == _host, "Unauthorized");
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
receive() external payable {
|
|
require(_farmMainContract != address(0) && _rewardTokenAddress == address(0), "ETH not allowed");
|
|
}
|
|
|
|
function init(bool byMint, address host, address treasury) public virtual override {
|
|
require(_farmMainContract == address(0), "Already init");
|
|
require((_host = host) != address(0), "blank host");
|
|
_rewardTokenAddress = IFarmMain(_farmMainContract = msg.sender)._rewardTokenAddress();
|
|
_byMint = byMint;
|
|
_treasury = treasury != address(0) ? treasury : host;
|
|
}
|
|
|
|
function data() view public virtual override returns(address farmMainContract, bool byMint, address host, address treasury, address rewardTokenAddress) {
|
|
return (_farmMainContract, _byMint, _host, _treasury, _rewardTokenAddress);
|
|
}
|
|
|
|
|
|
|
|
|
|
function setHost(address host) public virtual override hostOnly {
|
|
_host = host;
|
|
}
|
|
|
|
|
|
|
|
|
|
function setTreasury(address treasury) public virtual override hostOnly {
|
|
_treasury = treasury;
|
|
}
|
|
|
|
|
|
|
|
|
|
function setFarmingSetups(FarmingSetupConfiguration[] memory farmingSetups) public virtual override hostOnly {
|
|
IFarmMain(_farmMainContract).setFarmingSetups(farmingSetups);
|
|
}
|
|
|
|
|
|
|
|
|
|
function transferTo(uint256 amount) public virtual override farmMainOnly {
|
|
if(_rewardTokenAddress != address(0)) {
|
|
return _byMint ? _mintAndTransfer(_rewardTokenAddress, _farmMainContract, amount) : _safeTransfer(_rewardTokenAddress, _farmMainContract, amount);
|
|
}
|
|
(bool result, ) = _farmMainContract.call{value:amount}("");
|
|
require(result, "ETH transfer failed.");
|
|
}
|
|
|
|
|
|
|
|
|
|
function backToYou(uint256 amount) payable public virtual override farmMainOnly {
|
|
if(_rewardTokenAddress != address(0)) {
|
|
_safeTransferFrom(_rewardTokenAddress, msg.sender, _byMint ? address(this) : _treasury, amount);
|
|
if(_byMint) {
|
|
_burn(_rewardTokenAddress, amount);
|
|
}
|
|
} else {
|
|
require(msg.value == amount, "invalid sent amount");
|
|
if(_treasury != address(this)) {
|
|
(bool result, ) = _treasury.call{value:amount}("");
|
|
require(result, "ETH transfer failed.");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function _mintAndTransfer(address erc20TokenAddress, address recipient, uint256 value) internal virtual {
|
|
IERC20Mintable(erc20TokenAddress).mint(recipient, value);
|
|
}
|
|
|
|
function _burn(address erc20TokenAddress, uint256 value) internal virtual {
|
|
IERC20Mintable(erc20TokenAddress).burn(msg.sender, value);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _safeApprove(address erc20TokenAddress, address to, uint256 value) internal virtual {
|
|
bytes memory returnData = _call(erc20TokenAddress, abi.encodeWithSelector(IERC20(erc20TokenAddress).approve.selector, to, value));
|
|
require(returnData.length == 0 || abi.decode(returnData, (bool)), 'APPROVE_FAILED');
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _safeTransfer(address erc20TokenAddress, address to, uint256 value) internal virtual {
|
|
bytes memory returnData = _call(erc20TokenAddress, abi.encodeWithSelector(IERC20(erc20TokenAddress).transfer.selector, to, value));
|
|
require(returnData.length == 0 || abi.decode(returnData, (bool)), 'TRANSFER_FAILED');
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _safeTransferFrom(address erc20TokenAddress, address from, address to, uint256 value) internal virtual {
|
|
bytes memory returnData = _call(erc20TokenAddress, abi.encodeWithSelector(IERC20(erc20TokenAddress).transferFrom.selector, from, to, value));
|
|
require(returnData.length == 0 || abi.decode(returnData, (bool)), 'TRANSFERFROM_FAILED');
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _call(address location, bytes memory payload) private returns(bytes memory returnData) {
|
|
assembly {
|
|
let result := call(gas(), location, 0, add(payload, 0x20), mload(payload), 0, 0)
|
|
let size := returndatasize()
|
|
returnData := mload(0x40)
|
|
mstore(returnData, size)
|
|
let returnDataPayloadStart := add(returnData, 0x20)
|
|
returndatacopy(returnDataPayloadStart, 0, size)
|
|
mstore(0x40, add(returnDataPayloadStart, size))
|
|
switch result case 0 {revert(returnDataPayloadStart, size)}
|
|
}
|
|
}
|
|
} |