Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
0cdb508
checkpoint
shahthepro Mar 1, 2023
ceaeb92
Merge branch 'master' into shah/uniswap-v3-strategy
shahthepro Mar 1, 2023
2da919c
Switch to timelock
shahthepro Mar 1, 2023
8fd2464
unblock myself
shahthepro Mar 1, 2023
84fc866
Merge branch 'master' into shah/uniswap-v3-strategy
shahthepro Mar 1, 2023
5c2d34a
More updates
shahthepro Mar 4, 2023
861726d
Add some comments
shahthepro Mar 4, 2023
2bc18cb
Document more functions
shahthepro Mar 4, 2023
700fd57
Fix bugs
shahthepro Mar 4, 2023
e30b62f
Update fork tests
shahthepro Mar 4, 2023
a08eb2f
Lint
shahthepro Mar 4, 2023
d4f982c
Add more fork tests
shahthepro Mar 4, 2023
91fd51d
Linter/prettier
shahthepro Mar 5, 2023
86523a5
Slither fixes
shahthepro Mar 6, 2023
cc29fc8
Updates
shahthepro Mar 8, 2023
bd8d3f9
Few more tweaks
shahthepro Mar 9, 2023
2fb3fe5
checkpoint
shahthepro Mar 14, 2023
8b55689
Start with splitting code
shahthepro Mar 14, 2023
8524569
prettify
shahthepro Mar 14, 2023
b1712f6
Split contracts and add swapAndRebalance method
shahthepro Mar 16, 2023
bbe2fb5
prettify
shahthepro Mar 16, 2023
ce15a7a
checkpoint
shahthepro Mar 17, 2023
87324d0
possible contract moves to circumvent delegate calls (#1263)
sparrowDom Mar 17, 2023
eed6212
cleanup
shahthepro Mar 17, 2023
47dc4b5
some basic unit tests
shahthepro Mar 17, 2023
875f0de
Sparrow dom/uniswap v3changes (#1264)
sparrowDom Mar 18, 2023
ef15c71
Make some changes
shahthepro Mar 18, 2023
53cd3c7
merge modifiers
shahthepro Mar 18, 2023
7bead43
Cleanup usage of PoolToken struct to save gas/contract size
shahthepro Mar 18, 2023
197d5ee
save 0.2KB by changing the modifier to a function
sparrowDom Mar 18, 2023
5cbd66b
Add net value loss threshold
shahthepro Mar 18, 2023
7f11995
Remove VaultValueChecker
shahthepro Mar 18, 2023
331f33c
Fork test for net value loss
shahthepro Mar 18, 2023
2c9c91e
Check event
shahthepro Mar 18, 2023
dedcbac
Cleanup
shahthepro Mar 18, 2023
81a02ad
allow for net lost value to reduce on favourable rebalances
sparrowDom Mar 18, 2023
95d20b6
prettify
shahthepro Mar 18, 2023
0e39f7e
Add more unit tests
shahthepro Mar 18, 2023
6e3128b
Fix increase liquidity tests
shahthepro Mar 19, 2023
8de11f3
Cleanup fixtures and add unit tests for Fees
shahthepro Mar 19, 2023
b5f98fe
Add some tests for net lost value
shahthepro Mar 19, 2023
2928ff5
Add more tests
shahthepro Mar 19, 2023
eef6d49
Even more tests
shahthepro Mar 19, 2023
5d1066b
Get rid off unwanted changes
shahthepro Mar 19, 2023
a614242
Fix vault tests
shahthepro Mar 19, 2023
ebbab24
Fix failing unit tests
shahthepro Mar 19, 2023
03cdc0c
Resolve global fixture conflict for fork tests
shahthepro Mar 19, 2023
60aa462
Cleanup helper functions
shahthepro Mar 19, 2023
51c050b
Add comments
shahthepro Mar 19, 2023
748cece
Fix fixtures bleed
shahthepro Mar 19, 2023
97546b2
Fix bleeding fixtures on fork tests
shahthepro Mar 19, 2023
1eb7709
More unit tests
shahthepro Mar 19, 2023
b4115ee
More unit tests
shahthepro Mar 19, 2023
a98aecb
fix async reverts
shahthepro Mar 19, 2023
d9b0a69
Add comment about slippage and fix failing unit test
shahthepro Mar 19, 2023
70aa199
Fix failing fork test
shahthepro Mar 19, 2023
2ddb8d0
add fork test for withdrawing when netLoss barrier is reached
sparrowDom Mar 20, 2023
df7f8e3
add test
sparrowDom Mar 20, 2023
99c6238
fix some tests
sparrowDom Mar 20, 2023
c40a955
intermittent commit
sparrowDom Mar 20, 2023
4b2e2c6
fix test
sparrowDom Mar 20, 2023
a4ec1b0
Merge branch 'master' into shah/uniswap-v3-strategy
shahthepro Apr 10, 2023
52ef747
Change deployment number
shahthepro Apr 10, 2023
6684429
Change reserve to Morpho Aave
shahthepro Apr 10, 2023
3c13846
Fix fork tests
shahthepro Apr 10, 2023
778c35f
Lint
shahthepro Apr 10, 2023
5e0191d
Merge branch 'master' into shah/uniswap-v3-strategy
shahthepro Apr 10, 2023
289a6dc
UniswapV3 strategy - M02 & M01 (#1284)
shahthepro Apr 21, 2023
a6e745a
Uniswap V3 - L01 (#1290)
shahthepro Apr 21, 2023
79fed4f
Collect fees even when closing empty positions (#1291)
shahthepro Apr 21, 2023
9d984e5
Fix return values (#1292)
shahthepro Apr 21, 2023
0446638
Check for duplicate tokenID when minting (#1293)
shahthepro Apr 21, 2023
7ea638f
Remove `payable` keyword (#1296)
shahthepro Apr 21, 2023
ec48808
Uniswap V3 - L08 (#1297)
shahthepro Apr 21, 2023
6c4bbf6
Use encodeWithSelector (#1298)
shahthepro Apr 21, 2023
0e8a804
Set Governor address to zero on implementation contracts (#1299)
shahthepro Apr 21, 2023
f71b05c
Uniswap - N14 - Remove unused imports (#1312)
shahthepro Apr 21, 2023
d3be84c
Uniswap audit fixes (#1300)
shahthepro Apr 21, 2023
88b341f
Merge branch 'master' into shah/uniswap-v3-strategy
shahthepro Apr 21, 2023
bc35c9c
Merge branch 'master' into shah/uniswap-v3-strategy
shahthepro May 24, 2023
af9e4a5
Update yarn.lock
shahthepro May 24, 2023
138ad8f
Merge master
shahthepro Jun 6, 2023
6c34043
Lint
shahthepro Jun 6, 2023
3829c63
Update deploy script ID
shahthepro Jun 6, 2023
c1faa2b
Merge branch 'master' into shah/uniswap-v3-strategy
shahthepro Jun 6, 2023
78103ee
Merge branch 'master' into shah/uniswap-v3-strategy
shahthepro Jun 7, 2023
ef873fa
Merge branch 'master' into shah/uniswap-v3-strategy
shahthepro Jun 21, 2023
5f2670b
Fix unit tests
shahthepro Jun 21, 2023
f0689e2
Use Aave Strategy as reserves
shahthepro Jun 21, 2023
79da02d
Fix fork tests
shahthepro Jun 21, 2023
0324ac1
Fix fork tests
shahthepro Jun 21, 2023
042a32b
Fix address
shahthepro Jun 22, 2023
5d4063d
Uniswap v3 strategy updates (#1652)
naddison36 Jun 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"solidity.defaultCompiler": "localNodeModule",
"solidity.packageDefaultDependenciesContractsDirectory": "contracts/contracts",
"solidity.packageDefaultDependenciesDirectory": "contracts/node_modules",
"solidity.compileUsingRemoteVersion": "v0.8.7+commit.e28d00a7",
Expand Down
14 changes: 14 additions & 0 deletions contracts/contracts/interfaces/IUniswapV3Strategy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.8.0;

import { IStrategy } from "./IStrategy.sol";

interface IUniswapV3Strategy is IStrategy {
function token0() external view returns (address);

function token1() external view returns (address);

function reserveStrategy(address token) external view returns (address);

function closeActivePositionOnlyVault() external;
}
9 changes: 9 additions & 0 deletions contracts/contracts/interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ interface IVault {

function approveStrategy(address _addr) external;

function approveUniswapV3Strategy(address _addr) external;

function removeStrategy(address _addr) external;

function isStrategySupported(address _addr) external view returns (bool);

function setAssetDefaultStrategy(address _asset, address _strategy)
external;

Expand Down Expand Up @@ -170,4 +174,9 @@ interface IVault {
function setNetOusdMintForStrategyThreshold(uint256 _threshold) external;

function netOusdMintedForStrategy() external view returns (int256);

function depositToUniswapV3Reserve(address asset, uint256 amount) external;

function withdrawFromUniswapV3Reserve(address asset, uint256 amount)
external;
}
13 changes: 13 additions & 0 deletions contracts/contracts/interfaces/IVaultValueChecker.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.8.0;

interface IVaultValueChecker {
function takeSnapshot() external;

function checkDelta(
int256 lowValueDelta,
int256 highValueDelta,
int256 lowSupplyDelta,
int256 highSupplyDelta
) external;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.7.5;
pragma abicoder v2;

/// @title Non-fungible token for positions
/// @notice Wraps Uniswap V3 positions in a non-fungible token interface which allows for them to be transferred
/// and authorized.
interface INonfungiblePositionManager {
/// @notice Returns the position information associated with a given token ID.
/// @dev Throws if the token ID is not valid.
/// @param tokenId The ID of the token that represents the position
/// @return nonce The nonce for permits
/// @return operator The address that is approved for spending
/// @return token0 The address of the token0 for a specific pool
/// @return token1 The address of the token1 for a specific pool
/// @return fee The fee associated with the pool
/// @return tickLower The lower end of the tick range for the position
/// @return tickUpper The higher end of the tick range for the position
/// @return liquidity The liquidity of the position
/// @return feeGrowthInside0LastX128 The fee growth of token0 as of the last action on the individual position
/// @return feeGrowthInside1LastX128 The fee growth of token1 as of the last action on the individual position
/// @return tokensOwed0 The uncollected amount of token0 owed to the position as of the last computation
/// @return tokensOwed1 The uncollected amount of token1 owed to the position as of the last computation
function positions(uint256 tokenId)
external
view
returns (
uint96 nonce,
address operator,
address token0,
address token1,
uint24 fee,
int24 tickLower,
int24 tickUpper,
uint128 liquidity,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
);

struct MintParams {
address token0;
address token1;
uint24 fee;
int24 tickLower;
int24 tickUpper;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
uint256 amount1Min;
address recipient;
uint256 deadline;
}

/// @notice Creates a new position wrapped in a NFT
/// @dev Call this when the pool does exist and is initialized. Note that if the pool is created but not initialized
/// a method does not exist, i.e. the pool is assumed to be initialized.
/// @param params The params necessary to mint a position, encoded as `MintParams` in calldata
/// @return tokenId The ID of the token that represents the minted position
/// @return liquidity The amount of liquidity for this position
/// @return amount0 The amount of token0
/// @return amount1 The amount of token1
function mint(MintParams calldata params)
external
payable
returns (
uint256 tokenId,
uint128 liquidity,
uint256 amount0,
uint256 amount1
);

struct IncreaseLiquidityParams {
uint256 tokenId;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
uint256 amount1Min;
uint256 deadline;
}

/// @notice Increases the amount of liquidity in a position, with tokens paid by the `msg.sender`
/// @param params tokenId The ID of the token for which liquidity is being increased,
/// amount0Desired The desired amount of token0 to be spent,
/// amount1Desired The desired amount of token1 to be spent,
/// amount0Min The minimum amount of token0 to spend, which serves as a slippage check,
/// amount1Min The minimum amount of token1 to spend, which serves as a slippage check,
/// deadline The time by which the transaction must be included to effect the change
/// @return liquidity The new liquidity amount as a result of the increase
/// @return amount0 The amount of token0 to acheive resulting liquidity
/// @return amount1 The amount of token1 to acheive resulting liquidity
function increaseLiquidity(IncreaseLiquidityParams calldata params)
external
payable
returns (
uint128 liquidity,
uint256 amount0,
uint256 amount1
);

struct DecreaseLiquidityParams {
uint256 tokenId;
uint128 liquidity;
uint256 amount0Min;
uint256 amount1Min;
uint256 deadline;
}

/// @notice Decreases the amount of liquidity in a position and accounts it to the position
/// @param params tokenId The ID of the token for which liquidity is being decreased,
/// amount The amount by which liquidity will be decreased,
/// amount0Min The minimum amount of token0 that should be accounted for the burned liquidity,
/// amount1Min The minimum amount of token1 that should be accounted for the burned liquidity,
/// deadline The time by which the transaction must be included to effect the change
/// @return amount0 The amount of token0 accounted to the position's tokens owed
/// @return amount1 The amount of token1 accounted to the position's tokens owed
function decreaseLiquidity(DecreaseLiquidityParams calldata params)
external
payable
returns (uint256 amount0, uint256 amount1);

struct CollectParams {
uint256 tokenId;
address recipient;
uint128 amount0Max;
uint128 amount1Max;
}

/// @notice Collects up to a maximum amount of fees owed to a specific position to the recipient
/// @param params tokenId The ID of the NFT for which tokens are being collected,
/// recipient The account that should receive the tokens,
/// amount0Max The maximum amount of token0 to collect,
/// amount1Max The maximum amount of token1 to collect
/// @return amount0 The amount of fees collected in token0
/// @return amount1 The amount of fees collected in token1
function collect(CollectParams calldata params)
external
payable
returns (uint256 amount0, uint256 amount1);

/// @notice Burns a token ID, which deletes it from the NFT contract. The token must have 0 liquidity and all tokens
/// must be collected first.
/// @param tokenId The ID of the token that is being burned
function burn(uint256 tokenId) external payable;
}
45 changes: 45 additions & 0 deletions contracts/contracts/interfaces/uniswap/v3/IUniswapV3Helper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.8.0;

import { INonfungiblePositionManager } from "./INonfungiblePositionManager.sol";

interface IUniswapV3Helper {
function getAmountsForLiquidity(
uint160 sqrtRatioX96,
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity
) external view returns (uint256 amount0, uint256 amount1);

function getLiquidityForAmounts(
uint160 sqrtRatioX96,
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint256 amount0,
uint256 amount1
) external view returns (uint128 liquidity);

function getSqrtRatioAtTick(int24 tick)
external
view
returns (uint160 sqrtPriceX96);

function positionFees(
INonfungiblePositionManager positionManager,
address poolAddress,
uint256 tokenId
) external view returns (uint256 amount0, uint256 amount1);

function positionTotal(
INonfungiblePositionManager positionManager,
address poolAddress,
uint256 tokenId,
uint160 sqrtRatioX96
) external view returns (uint256 amount0, uint256 amount1);

function positionPrincipal(
INonfungiblePositionManager positionManager,
uint256 tokenId,
uint160 sqrtRatioX96
) external view returns (uint256 amount0, uint256 amount1);
}
74 changes: 74 additions & 0 deletions contracts/contracts/mocks/MockStrategy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.8.0;

import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract MockStrategy {
using SafeERC20 for IERC20;

address public vaultAddress;
mapping(address => bool) public supportsAsset;
address[] internal allAssets;

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

constructor(address _vaultAddress, address[] memory assets) {
vaultAddress = _vaultAddress;
for (uint256 i = 0; i < assets.length; i++) {
address asset = assets[i];
supportsAsset[asset] = true;
allAssets.push(asset);
}
}

function deposit(address _asset, uint256 _amount) public onlyVault {
// Do nothing
}

function depositAll() public onlyVault {
// Do nothing
}

function withdraw(address _asset, uint256 _amount) public onlyVault {
IERC20(_asset).safeTransfer(vaultAddress, _amount);
}

function withdraw(
address _recipient,
address _asset,
uint256 _amount
) external onlyVault {
IERC20(_asset).safeTransfer(_recipient, _amount);
}

function checkBalance(address _asset)
external
view
returns (uint256 balance)
{
balance = IERC20(_asset).balanceOf(address(this));
}

function withdrawAll() public onlyVault {
for (uint256 i = 0; i < allAssets.length; i++) {
IERC20 asset = IERC20(allAssets[i]);
asset.safeTransfer(vaultAddress, asset.balanceOf(address(this)));
}
}

function collectRewardTokens() external {
// Do nothing
}

function getRewardTokenAddresses()
external
view
returns (address[] memory)
{
return new address[](0);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./MintableERC20.sol";
import "./../../MintableERC20.sol";
import "./MockUniswapPair.sol";

contract MockMintableUniswapPair is MockUniswapPair, MintableERC20 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { IUniswapV2Pair } from "../interfaces/uniswap/IUniswapV2Pair.sol";
import { IUniswapV2Pair } from "../../../interfaces/uniswap/IUniswapV2Pair.sol";

contract MockUniswapPair is IUniswapV2Pair {
address tok0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pragma solidity ^0.8.0;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import { IUniswapV2Router } from "../interfaces/uniswap/IUniswapV2Router02.sol";
import { Helpers } from "../utils/Helpers.sol";
import { StableMath } from "../utils/StableMath.sol";
import { IUniswapV2Router } from "../../../interfaces/uniswap/IUniswapV2Router02.sol";
import { Helpers } from "../../../utils/Helpers.sol";
import { StableMath } from "../../../utils/StableMath.sol";

// import "hardhat/console.sol";

Expand Down Expand Up @@ -121,4 +121,36 @@ contract MockUniswapRouter is IUniswapV2Router {
function WETH() external pure override returns (address) {
return address(0);
}

struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}

function exactInputSingle(ExactInputSingleParams calldata params)
external
payable
returns (uint256 amountOut)
{
amountOut = params.amountIn.scaleBy(
Helpers.getDecimals(params.tokenIn),
Helpers.getDecimals(params.tokenOut)
);
IERC20(params.tokenIn).transferFrom(
msg.sender,
address(this),
params.amountIn
);
IERC20(params.tokenOut).transfer(params.recipient, amountOut);
require(
amountOut >= params.amountOutMinimum,
"UniswapMock: amountOut less than amountOutMinimum"
);
}
}
Loading