Skip to content

Commit

Permalink
Merge pull request #2 from harvestfi/chore/forked-contracts
Browse files Browse the repository at this point in the history
chore: forked contracts
  • Loading branch information
CryptJS13 authored Jul 10, 2023
2 parents 9279cfc + f73f5db commit 2f2cd86
Show file tree
Hide file tree
Showing 48 changed files with 6,630 additions and 14 deletions.
14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

108 changes: 108 additions & 0 deletions src/UniStatusViewer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//SPDX-License-Identifier: Unlicense
pragma solidity 0.7.6;
pragma experimental ABIEncoderV2;

import "hardhat/console.sol";

// ERC20
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/math/SafeMathUpgradeable.sol";

// Harvest
import "./interface/IUniVaultV1.sol";
import "./interface/IUniVaultStorageV1.sol";

// UniswapV3 core
import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol";
import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import "@uniswap/v3-core/contracts/libraries/TickMath.sol";

// UniswapV3 periphery contracts
import "@uniswap/v3-periphery/contracts/interfaces/INonfungiblePositionManager.sol";
import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "@uniswap/v3-periphery/contracts/libraries/LiquidityAmounts.sol";

// Uniswap v2
import "./interface/uniswapV2/IUniswapV2Pair.sol";
import "./interface/uniswapV2/IUniswapV2Factory.sol";
import "./interface/uniswapV2/IUniswapV2Router02.sol";

contract UniStatusViewer {
using SafeMathUpgradeable for uint256;

address constant _UNI_POS_MANAGER = 0xC36442b4a4522E871399CD717aBDD847Ab11FE88;
address constant _UNI_POOL_FACTORY = 0x1F98431c8aD98523631AE4a59f267346ea31F984;
address constant _V2_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address constant _V2_FACTORY = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;

function getSqrtPriceX96(address _token0, address _token1, uint24 _fee) public view returns (uint160) {
address poolAddr = IUniswapV3Factory(_UNI_POOL_FACTORY).getPool(_token0, _token1, _fee);
(uint160 sqrtPriceX96,,,,,,) = IUniswapV3Pool(poolAddr).slot0();
return sqrtPriceX96;
}

function getSqrtPriceX96ForPosition(uint256 posId) public view returns (uint160) {
(,, address _token0, address _token1, uint24 _fee,,,,,,,) = INonfungiblePositionManager(_UNI_POS_MANAGER).positions(posId);
return getSqrtPriceX96(_token0, _token1, _fee);
}

function getAmountsForPosition(uint256 posId) public view returns (uint256 amount0, uint256 amount1) {
(,, address _token0, address _token1, uint24 _fee, int24 _tickLower, int24 _tickUpper, uint128 _liquidity,,,,) =
INonfungiblePositionManager(_UNI_POS_MANAGER).positions(posId);
uint160 sqrtRatioX96 = getSqrtPriceX96(_token0, _token1, _fee);
uint160 sqrtRatioXA96 = TickMath.getSqrtRatioAtTick(_tickLower);
uint160 sqrtRatioXB96 = TickMath.getSqrtRatioAtTick(_tickUpper);
(amount0, amount1) = LiquidityAmounts.getAmountsForLiquidity(sqrtRatioX96, sqrtRatioXA96, sqrtRatioXB96, _liquidity);
}

function getAmountsForUserShare(address vaultAddr, uint256 userShare)
public
view
returns (uint256 userAmount0, uint256 userAmount1)
{
uint256 posId = IUniVaultStorageV1(IUniVaultV1(vaultAddr).getStorage()).posId();
(uint256 amount0, uint256 amount1) = getAmountsForPosition(posId);
userAmount0 = amount0.mul(userShare).div(IERC20Upgradeable(vaultAddr).totalSupply());
userAmount1 = amount1.mul(userShare).div(IERC20Upgradeable(vaultAddr).totalSupply());
}

function enumerateNftRelevantTo(uint256 posId, address nftOwner) public view returns (uint256[] memory) {
(,, address _token0, address _token1,,,,,,,,) = INonfungiblePositionManager(_UNI_POS_MANAGER).positions(posId);
uint256 userNftCount = INonfungiblePositionManager(_UNI_POS_MANAGER).balanceOf(nftOwner);
uint256[] memory ids = new uint256[](userNftCount);

uint256 validCount = 0;
for (uint256 i = 0; i < userNftCount; i++) {
uint256 id = INonfungiblePositionManager(_UNI_POS_MANAGER).tokenOfOwnerByIndex(nftOwner, i);

(,, address _utoken0, address _utoken1,,,,,,,,) = INonfungiblePositionManager(_UNI_POS_MANAGER).positions(posId);
if (_utoken0 == _token0 && _utoken1 == _token1) {
ids[validCount] = id;
validCount++;
}
}

uint256[] memory relevantIds = new uint256[](validCount);
for (uint256 i = 0; i < validCount; i++) {
relevantIds[i] = ids[i];
}
return relevantIds;
}

/**
* Returns the number of tokens that would be returned now when removing the liquidity
* from the corresponding v2 pair
*/
function quoteV2Migration(address _token0, address _token1, uint256 _lpAmount) public view returns (uint256, uint256) {
address pair = IUniswapV2Factory(_V2_FACTORY).getPair(_token0, _token1);
(uint112 amount0, uint112 amount1,) = IUniswapV2Pair(pair).getReserves();
uint256 totalSupply = IERC20Upgradeable(pair).totalSupply();
if (_token0 == IUniswapV2Pair(pair).token0()) {
// order matches v3
return (_lpAmount.mul(uint256(amount0)).div(totalSupply), _lpAmount.mul(uint256(amount1)).div(totalSupply));
} else {
// order is reversed
return (_lpAmount.mul(uint256(amount1)).div(totalSupply), _lpAmount.mul(uint256(amount0)).div(totalSupply));
}
}
}
28 changes: 28 additions & 0 deletions src/UniVaultProxy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pragma solidity 0.7.6;

import "@openzeppelin/contracts/proxy/UpgradeableProxy.sol";
import "./interface/IUniVaultV1.sol";

contract UniVaultProxy is UpgradeableProxy {
constructor(address _logic, bytes memory _data) UpgradeableProxy(_logic, _data) {}

/**
* The main logic. If the timer has elapsed and there is a schedule upgrade,
* the governance can upgrade the vault
*/
function upgrade() external {
(bool should, address newImplementation) = IUniVaultV1(address(this)).shouldUpgrade();
require(should, "Upgrade not scheduled");
_upgradeTo(newImplementation);

// the finalization needs to be executed on itself to update the storage of this proxy
// it also needs to be invoked by the governance, not by address(this), so delegatecall is needed
(bool success, bytes memory result) = address(this).delegatecall(abi.encodeWithSignature("finalizeUpgrade()"));

require(success, "Issue when finalizing the upgrade");
}

function implementation() external view returns (address) {
return _implementation();
}
}
104 changes: 104 additions & 0 deletions src/UniVaultStorageV1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//SPDX-License-Identifier: Unlicense
pragma solidity 0.7.6;

contract UniVaultStorageV1 {
uint256 public posId;
address public token0;
address public token1;
address public zapContract;
address public vault;
address public feeRewardForwarder;
uint256 public profitShareRatio;
address public platformTarget;
uint256 public platformRatio;

int24 public tickUpper;
int24 public tickLower;
uint24 public fee;
address public nextImplementation;
uint256 public nextImplementationTimestamp;
uint256 public nextImplementationDelay;

modifier onlyVault() {
require(msg.sender == vault, "Only vault");
_;
}

constructor(address _vault) {
vault = _vault;
}

function initializeByVault(
address _token0,
address _token1,
uint24 _fee,
int24 _tickLower,
int24 _tickUpper,
address _zapContract
) public onlyVault {
token0 = _token0;
token1 = _token1;
fee = _fee;
tickLower = _tickLower;
tickUpper = _tickUpper;
zapContract = _zapContract;
}

function setZapContract(address _zap) public onlyVault {
zapContract = _zap;
}

function setToken0(address _token) public onlyVault {
token0 = _token;
}

function setToken1(address _token) public onlyVault {
token1 = _token;
}

function setFee(uint24 _fee) public onlyVault {
fee = _fee;
}

function setTickUpper(int24 _tick) public onlyVault {
tickUpper = _tick;
}

function setTickLower(int24 _tick) public onlyVault {
tickLower = _tick;
}

function setPosId(uint256 _posId) public onlyVault {
posId = _posId;
}

function setVault(address _vault) public onlyVault {
vault = _vault;
}

function setFeeRewardForwarder(address _feeRewardForwarder) public onlyVault {
feeRewardForwarder = _feeRewardForwarder;
}

function configureFees(address _feeRewardForwarder, uint256 _feeRatio, address _platformTarget, uint256 _platformRatio)
public
onlyVault
{
feeRewardForwarder = _feeRewardForwarder;
profitShareRatio = _feeRatio;
platformTarget = _platformTarget;
platformRatio = _platformRatio;
}

function setNextImplementation(address _impl) public onlyVault {
nextImplementation = _impl;
}

function setNextImplementationTimestamp(uint256 _timestamp) public onlyVault {
nextImplementationTimestamp = _timestamp;
}

function setNextImplementationDelay(uint256 _delay) public onlyVault {
nextImplementationDelay = _delay;
}
}
Loading

0 comments on commit 2f2cd86

Please sign in to comment.