Skip to content

Commit

Permalink
"first"
Browse files Browse the repository at this point in the history
  • Loading branch information
MxianD committed Aug 20, 2024
1 parent ec9381e commit 0ebcf9e
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 57 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"solidity.packageDefaultDependenciesContractsDirectory": "src",
"solidity.packageDefaultDependenciesDirectory": "lib"
}
1 change: 1 addition & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
forge-std/=lib/forge-std/src/
19 changes: 0 additions & 19 deletions script/Counter.s.sol

This file was deleted.

14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

118 changes: 118 additions & 0 deletions src/UniswapV3Pool.sol
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));
}
}
12 changes: 12 additions & 0 deletions src/interfaces/IERC20.sol
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;
}
6 changes: 6 additions & 0 deletions src/interfaces/IUniswapV3MintCallback.sol
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;
}
7 changes: 7 additions & 0 deletions src/interfaces/IUniswapV3SwapCallback.sol
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;
}
23 changes: 23 additions & 0 deletions src/lib/Position.sol
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))
];
}
}
23 changes: 23 additions & 0 deletions src/lib/Tick.sol
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;
}
}
24 changes: 0 additions & 24 deletions test/Counter.t.sol

This file was deleted.

16 changes: 16 additions & 0 deletions test/ERC20Mintable.sol
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);
}
}
11 changes: 11 additions & 0 deletions test/UniswapV3Pool.t.sol
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);
}
}

0 comments on commit 0ebcf9e

Please sign in to comment.