Skip to content

Commit

Permalink
"section 1"
Browse files Browse the repository at this point in the history
  • Loading branch information
MxianD committed Aug 21, 2024
1 parent 9dfffb6 commit 9966e84
Show file tree
Hide file tree
Showing 7 changed files with 809 additions and 6 deletions.
34 changes: 34 additions & 0 deletions scripts/DeployDevelopment.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.14;

import "../lib/forge-std/src/console.sol";
import "../lib/forge-std/src/Script.sol";
import "../src/UniswapV3Pool.sol";
import "../src/UniswapV3Manager.sol";
import "../test/ERC20Mintable.sol";

contract DeployDevelopment is Script {
function run() public {
uint256 wethBalance = 1 ether;
uint256 usdcBalance = 5042 ether;
int24 currentTick = 85176;
uint160 currentSqrtP = 5602277097478614198912276234240;
vm.startBroadcast();
ERC20Mintable token0 = new ERC20Mintable("Wrapped Ether", "WETH" ,18);
ERC20Mintable token1 = new ERC20Mintable("USD coin", "USDC", 18);
UniswapV3Pool pool = new UniswapV3Pool(
address(token0),
address(token1),
currentSqrtP,
currentTick
);
UniswapV3Manager manager = new UniswapV3Manager();
token0.mint(msg.sender,wethBalance);
token1.mint(msg.sender,usdcBalance);
vm.stopBroadcast();
console.log("WETH address", address(token0));
console.log("USDC address", address(token1));
console.log("Pool address", address(pool));
console.log("Manager address", address(manager));
}
}
60 changes: 60 additions & 0 deletions src/UniswapV3Manager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.14;
import "../src/UniswapV3Pool.sol";
import "../src/interfaces/IERC20.sol";

contract UniswapV3Manager{
function mint(
address poolAddress_,
int24 lowerTick,
int24 upperTick,
uint128 liquidity,
bytes calldata data
)public returns(uint256, uint256){
return UniswapV3Pool(poolAddress_).mint(msg.sender, lowerTick, upperTick, liquidity, data);
}

function swap(address poolAddress_, bytes calldata data) public returns(int256, int256){
return UniswapV3Pool(poolAddress_).swap(msg.sender, data);
}
function uniswapV3MintCallback(
uint256 amount0,
uint256 amount1,
bytes calldata data
) public {
UniswapV3Pool.CallbackData memory extra = abi.decode(
data,
(UniswapV3Pool.CallbackData)
);

IERC20(extra.token0).transferFrom(extra.payer, msg.sender, amount0);
IERC20(extra.token1).transferFrom(extra.payer, msg.sender, amount1);
}

function uniswapV3SwapCallback(
int256 amount0,
int256 amount1,
bytes calldata data
) public {
UniswapV3Pool.CallbackData memory extra = abi.decode(
data,
(UniswapV3Pool.CallbackData)
);

if (amount0 > 0) {
IERC20(extra.token0).transferFrom(
extra.payer,
msg.sender,
uint256(amount0)
);
}

if (amount1 > 0) {
IERC20(extra.token1).transferFrom(
extra.payer,
msg.sender,
uint256(amount1)
);
}
}
}
48 changes: 48 additions & 0 deletions src/UniswapV3Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,29 @@ contract UniswapV3Pool {
uint256 amount0,
uint256 amount1
);

event Swap(
address indexed sender,
address indexed recipient,
int256 amount0,
int256 amount1,
uint160 sqrtPriceX96,
uint128 liquidity,
int24 tick
);

int24 internal constant MIN_TICK = -887272;
int24 internal constant MAX_TICK = -MIN_TICK;

address public immutable token0;
address public immutable token1;

struct CallbackData {
address token0;
address token1;
address payer;
}

struct Slot0 {
uint160 sqrtPriceX96;
int24 tick;
Expand Down Expand Up @@ -115,4 +132,35 @@ contract UniswapV3Pool {
function balance1() internal returns (uint256 balance) {
balance = IERC20(token1).balanceOf(address(this));
}

function swap(
address recipient, bytes calldata data
) public returns (int256 amount0, int256 amount1) {
int24 nextTick = 85184;
uint160 nextPrice = 5604469350942327889444743441197;

amount0 = -0.008396714242162444 ether;
amount1 = 42 ether;

(slot0.tick, slot0.sqrtPriceX96) = (nextTick, nextPrice);

IERC20(token0).transfer(recipient, uint256(-amount0));

uint256 balance1Before = balance1();
IUniswapV3SwapCallback(msg.sender).uniswapV3SwapCallback(amount0, amount1, data);
if(balance1Before + uint256(amount1) > balance1()) revert InsufficientInputAmount();

emit Swap(
msg.sender,
recipient,
amount0,
amount1,
slot0.sqrtPriceX96,
liquidity,
slot0.tick
);

}


}
2 changes: 1 addition & 1 deletion test/ERC20Mintable.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.14;

import "solmate/token/ERC20.sol";
import "../lib/solmate/src/tokens/ERC20.sol";

contract ERC20Mintable is ERC20{
constructor(
Expand Down
29 changes: 29 additions & 0 deletions test/TestUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.14;

import "../src/UniswapV3Pool.sol";

abstract contract TestUtils {
function encodeError(string memory error)
internal
pure
returns (bytes memory encoded)
{
encoded = abi.encodeWithSignature(error);
}

function encodeExtra(
address token0_,
address token1_,
address payer
) internal pure returns (bytes memory) {
return
abi.encode(
UniswapV3Pool.CallbackData({
token0: token0_,
token1: token1_,
payer: payer
})
);
}
}
Loading

0 comments on commit 9966e84

Please sign in to comment.