-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
221 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"solidity.packageDefaultDependenciesContractsDirectory": "src", | ||
"solidity.packageDefaultDependenciesDirectory": "lib" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
forge-std/=lib/forge-std/src/ |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.14; | ||
|
||
import "./interfaces/IERC20.sol"; | ||
import "./interfaces/IUniswapV3MintCallback.sol"; | ||
import "./interfaces/IUniswapV3SwapCallback.sol"; | ||
|
||
import "./lib/Tick.sol"; | ||
import "./lib/Position.sol"; | ||
contract UniswapV3Pool { | ||
using Tick for mapping(int24 => Tick.Info); | ||
using Position for mapping(bytes32 => Position.Info); | ||
using Position for Position.Info; | ||
|
||
error InsufficientInputAmount(); | ||
error InvalidTickRange(); | ||
error ZeroLiquidity(); | ||
|
||
event Mint( | ||
address sender, | ||
address indexed owner, | ||
int24 indexed tickLower, | ||
int24 indexed tickUpper, | ||
uint128 amount, | ||
uint256 amount0, | ||
uint256 amount1 | ||
); | ||
int24 internal constant MIN_TICK = -887272; | ||
int24 internal constant MAX_TICK = -MIN_TICK; | ||
|
||
address public immutable token0; | ||
address public immutable token1; | ||
|
||
struct Slot0 { | ||
uint160 sqrtPriceX96; | ||
int24 tick; | ||
} | ||
Slot0 public slot0; | ||
|
||
uint128 public liquidity; | ||
|
||
// Ticks Info | ||
mapping(int24 => Tick.Info) public ticks; | ||
// Position Info | ||
mapping(bytes32 => Position.Info) public positions; | ||
|
||
constructor( | ||
address token0_, | ||
address token1_, | ||
uint160 sqrtPriceX96, | ||
int24 tick | ||
) { | ||
token0 = token0_; | ||
token1 = token1_; | ||
slot0 = Slot0({sqrtPriceX96: sqrtPriceX96, tick: tick}); | ||
} | ||
|
||
function mint( | ||
address owner, | ||
int24 lowerTick, | ||
int24 upperTick, | ||
uint128 amount, | ||
bytes calldata data | ||
) external returns (uint256 amount0, uint256 amount1) { | ||
if ( | ||
lowerTick >= upperTick || | ||
lowerTick < MIN_TICK || | ||
upperTick > MAX_TICK | ||
) revert InvalidTickRange(); | ||
if (amount == 0) revert ZeroLiquidity(); | ||
ticks.update(lowerTick, amount); | ||
ticks.update(upperTick, amount); | ||
|
||
Position.Info storage position = positions.get( | ||
owner, | ||
lowerTick, | ||
upperTick | ||
); | ||
|
||
position.update(amount); | ||
|
||
amount0 = 0.998976618347425280 ether; | ||
amount1 = 5000 ether; | ||
|
||
liquidity += uint128(amount); | ||
|
||
uint256 balance0Before; | ||
uint256 balance1Before; | ||
if (amount0 > 0) balance0Before = balance0(); | ||
if (amount1 > 0) balance1Before = balance1(); | ||
IUniswapV3MintCallback(msg.sender).uniswapV3MintCallback( | ||
amount0, | ||
amount1, | ||
data | ||
); | ||
if (amount0 > 0 && balance0Before + amount0 > balance0()) | ||
revert InsufficientInputAmount(); | ||
if (amount1 > 0 && balance1Before + amount1 > balance1()) | ||
revert InsufficientInputAmount(); | ||
|
||
emit Mint( | ||
msg.sender, | ||
owner, | ||
lowerTick, | ||
upperTick, | ||
amount, | ||
amount0, | ||
amount1 | ||
); | ||
} | ||
function balance0() internal returns (uint256 balance) { | ||
balance = IERC20(token0).balanceOf(address(this)); | ||
} | ||
|
||
function balance1() internal returns (uint256 balance) { | ||
balance = IERC20(token1).balanceOf(address(this)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.14; | ||
|
||
interface IERC20 { | ||
function approve(address, uint256) external; | ||
|
||
function balanceOf(address) external returns (uint256); | ||
|
||
function transfer(address, uint256) external; | ||
|
||
function transferFrom(address, address, uint256) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.14; | ||
|
||
interface IUniswapV3MintCallback { | ||
function uniswapV3MintCallback(uint256 amount0, uint256 amount1, bytes calldata data) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.14; | ||
|
||
interface IUniswapV3SwapCallback { | ||
function uniswapV3SwapCallback(int256 amount0, int256 amount1, bytes calldata data) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.14; | ||
library Position{ | ||
struct Info{ | ||
uint128 liquidity; | ||
} | ||
function update(Info storage self, uint128 liquidityDelta) internal{ | ||
uint128 liquidityBefore = self.liquidity; | ||
uint128 liquidityAfter = liquidityBefore + liquidityDelta; | ||
|
||
self.liquidity = liquidityAfter; | ||
} | ||
function get( | ||
mapping(bytes32=>Info) storage self, | ||
address owner, | ||
int24 lowerTick, | ||
int24 upperTick | ||
)internal view returns(Position.Info storage position){ | ||
position = self[ | ||
keccak256(abi.encodePacked(owner, lowerTick, upperTick)) | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.14; | ||
library Tick { | ||
struct Info { | ||
bool initalized; | ||
uint128 liquidity; | ||
} | ||
function update( | ||
mapping(int24 => Tick.Info) storage self, | ||
int24 tick, | ||
uint128 liquidityDelta | ||
) internal { | ||
Tick.Info storage tickInfo = self[tick]; | ||
uint128 liquidityBefore = tickInfo.liquidity; | ||
uint128 liquidityAfter = liquidityBefore + liquidityDelta; | ||
|
||
if (liquidityBefore == 0) { | ||
tickInfo.initalized = true; | ||
} | ||
|
||
tickInfo.liquidity = liquidityAfter; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.14; | ||
|
||
import "solmate/token/ERC20.sol"; | ||
|
||
contract ERC20Mintable is ERC20{ | ||
constructor( | ||
string memory _name, | ||
string memory _symbol, | ||
uint8 _decimals | ||
) ERC20(_name, _symbol, _decimals){} | ||
|
||
function mint(address to, uint256 amount) public { | ||
_mint(to, amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.14; | ||
import "forge-std/Test.sol"; | ||
|
||
contract UniswapV3PoolTest is Test{ | ||
function setUp() public{} | ||
|
||
function testExample() public{ | ||
assertTrue(true); | ||
} | ||
} |