Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: SwapRouter integration #6

Open
wants to merge 2 commits into
base: delegateCallPool
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
85 changes: 56 additions & 29 deletions contracts/Factory.sol
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol';

import"./Pool.sol";
import"./interfaces/IDesireSwapV0Factory.sol";

contract DesireSwapV0Factory is IDesireSwapV0Factory {
address public override owner;
contract DesireSwapV0Factory is IUniswapV3Factory, IDesireSwapV0Factory {
address public override(IUniswapV3Factory, IDesireSwapV0Factory) owner;
address public override feeCollector;
address public override body;

bool public override protocolFeeIsOn;
uint256 public override protocolFeePart;

struct poolTypeData{
uint256 sqrtPositionMultiplier;
uint256 fee;
}
poolTypeData[] poolType;
uint8 public poolTypeCount;
mapping(uint256 => uint8) feeToPoolTypeNumber;
mapping(address => mapping(address => mapping(uint8 => address))) public poolAddress;
// struct poolTypeData{
// uint256 sqrtPositionMultiplier;
// uint256 fee;
// }
// poolTypeData[] poolType;
// uint8 public poolTypeCount;
// mapping(address => mapping(address => mapping(uint8 => address))) public poolAddress;
mapping(address => mapping(address => mapping(uint24 => address))) public getPool;

modifier onlyBy(address _account) {
require(msg.sender == owner, "DesireSwapV0Factory: SENDER_IS_NOT_THE_OWNER");
Expand All @@ -35,31 +37,35 @@ contract DesireSwapV0Factory is IDesireSwapV0Factory {
}


function addPoolType(uint256 _sqrtPositionMultiplier, uint256 _fee) external override onlyBy(owner) {
require(feeToPoolTypeNumber[_fee] == 0);
poolType[poolTypeCount] = poolTypeData({
sqrtPositionMultiplier: _sqrtPositionMultiplier,
fee: _fee
});
emit NewPoolType (poolTypeCount, _sqrtPositionMultiplier, _fee);
poolTypeCount++;
}
// function addPoolType(uint256 _sqrtPositionMultiplier, uint256 _fee) external override onlyBy(owner) {
// // require(feeToPoolTypeNumber[_fee] == 0);
// poolType[poolTypeCount] = poolTypeData({
// sqrtPositionMultiplier: _sqrtPositionMultiplier,
// fee: _fee
// });
// emit NewPoolType(poolTypeCount, _sqrtPositionMultiplier, _fee);
// poolTypeCount++;
// }

function createPool(address _tokenA, address _tokenB, uint8 _poolTypeNumber, uint256 _startingSqrtBottomPrice)
external override onlyBy(owner) {
function createPool(address _tokenA, address _tokenB
//, uint8 _poolTypeNumber, uint256 _startingSqrtBottomPrice
, uint24 fee
)
external override onlyBy(owner) returns (address pool) {
require(_tokenA != _tokenB);
require(_tokenA != address(0) && _tokenB != address(0));
(address token0, address token1) = _tokenA < _tokenB ? (_tokenA, _tokenB) : (_tokenB, _tokenA);
require(token0 != address(0) && token1 != address(0));
require(poolAddress[token0][token1][_poolTypeNumber] == address(0));
require(getPool[token0][token1][fee] == address(0));
address pool = address(new DesireSwapV0Pool(address(this), token0, token1,
poolType[_poolTypeNumber].sqrtPositionMultiplier, poolType[_poolTypeNumber].fee,
_startingSqrtBottomPrice));
poolAddress[token0][token1][_poolTypeNumber] = pool;
poolAddress[token1][token0][_poolTypeNumber] = pool;
emit PoolCreated(token0, token1, _poolTypeNumber, pool);
// poolType[_poolTypeNumber].sqrtPositionMultiplier, poolType[_poolTypeNumber].fee,
// _startingSqrtBottomPrice));
fee));
getPool[token0][token1][fee] = pool;
getPool[token1][token0][fee] = pool;
emit PoolCreated(token0, token1, fee, pool);
}

function setOwner(address _owner) external override onlyBy(owner) {
function setOwner(address _owner) external override(IUniswapV3Factory, IDesireSwapV0Factory) onlyBy(owner) {
emit OwnerChanged(owner, _owner);
owner = _owner;
}
Expand All @@ -73,4 +79,25 @@ contract DesireSwapV0Factory is IDesireSwapV0Factory {
emit BodyChanged(body, _body);
body = _body;
}

// UV3Factory
function feeAmountTickSpacing(uint24 fee) external override view returns (int24) {}

// function getPool(
// address tokenA,
// address tokenB,
// uint24 fee
// ) external override view returns (address pool) {
// require(fee == uint24(uint8(fee)));
// // uint8 = feeToPoolTypeNumber[fee];
// return poolAddress[tokenA][tokenB][uint8(0)];
// }

// function createPool(
// address tokenA,
// address tokenB,
// uint24 fee
// ) external override returns (address pool) {}

function enableFeeAmount(uint24 fee, int24 tickSpacing) external override {}
}
92 changes: 92 additions & 0 deletions contracts/FactoryV3.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol';

import "./interfaces/IDesireSwapV0Factory.sol";

contract FactoryV3 is IUniswapV3Factory, IDesireSwapV0Factory {

/* EVENTS
event OwnerChanged(address indexed oldOwner, address indexed newOwner);
event PoolCreated(address indexed token0, address indexed token1, uint24 indexed fee, int24 tickSpacing, address pool);
event FeeAmountEnabled(uint24 indexed fee, int24 indexed tickSpacing);
*/

address public override(IUniswapV3Factory, IDesireSwapV0Factory) owner;

// IDESIRESWAP
address public override feeCollector;
address public override body;
bool public override protocolFeeIsOn;
uint256 public override protocolFeePart;

struct poolTypeData{
uint256 sqrtPositionMultiplier;
uint256 fee;
}
poolTypeData[] poolType;
uint8 public poolTypeCount;
mapping(uint256 => uint8) feeToPoolTypeNumber;
mapping(address => mapping(address => mapping(uint8 => address))) public poolAddress;

modifier onlyBy(address _account) {
require(msg.sender == owner, "DesireSwapV0Factory: SENDER_IS_NOT_THE_OWNER");
_;
}

constructor(address _body){
owner = msg.sender;
feeCollector = msg.sender;
emit OwnerChanged(address(0), msg.sender);
body = _body;
emit BodyChanged(address(0), body);
}

function feeAmountTickSpacing(uint24 fee) external override view returns (int24) {

}

function getPool(
address tokenA,
address tokenB,
uint24 fee
) external override view returns (address pool) {
require(fee == uint24(uint8(fee)));
return poolAddress[tokenA][tokenB][uint8(fee)];
}

// Incompatible signature
function createPool(address _tokenA, address _tokenB, uint8 _poolType, uint256 _startingSqrtBottomPrice) external override {}
function createPool(
address tokenA,
address tokenB,
uint24 fee
) external override returns (address pool) {

}

function setOwner(address _owner) external override(IUniswapV3Factory, IDesireSwapV0Factory) onlyBy(owner) {
emit OwnerChanged(owner, _owner);
owner = _owner;
}

function enableFeeAmount(uint24 fee, int24 tickSpacing) external override {

}

// IDESIRESWAP FUNCS
function setFeeCollector(address _feeCollector) external override {

}
function setBody(address _body) external override {

}

// analogic to UV3Factory `deploy`
function addPoolType(uint256 _sqrtPositionMultiplier, uint256 _fee) external override {

}


}
32 changes: 23 additions & 9 deletions contracts/Pool.sol
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol';

import "./Ticket.sol";
import "./library/TransferHelper.sol";
import "./interfaces/IERC20.sol";
import './interfaces/IDesireSwapV0Factory.sol';
import './interfaces/IDesireSwapV0Pool.sol';

contract DesireSwapV0Pool is Ticket, IDesireSwapV0Pool {
address public immutable factory;
address public immutable token0;
address public immutable token1;
contract DesireSwapV0Pool is Ticket, IDesireSwapV0Pool, IUniswapV3Pool {
uint256 private constant DEFAULT_SQRT_POSITION_MULTIPLIER = 1; // to be established

address public override immutable factory;
address public override immutable token0;
address public override immutable token1;

// UV3Pool
uint24 public override fee;
int24 public override tickSpacing;
uint128 public override maxLiquidityPerTick;

uint256 public immutable sqrtPositionMultiplier; // example: 100100000.... is 1.001 (* 10**36)
uint256 public immutable feePercentage; // 0 fee is 0 // 100% fee is 1* 10**36;
uint24 public immutable feePercentage; // 0 fee is 0 // 100% fee is 1* 10**36;
uint256 private totalReserve0;
uint256 private totalReserve1;
uint256 private lastBalance0;
Expand All @@ -35,19 +44,24 @@ contract DesireSwapV0Pool is Ticket, IDesireSwapV0Pool {

constructor(
address _factory, address _token0, address _token1,
uint256 _sqrtPositionMultiplier, uint256 _feePercentage,
uint256 _startingSqrtPriceBottom
uint24 _feePercentage
){
factory = _factory;
token0 = _token0;
token1 = _token1;
sqrtPositionMultiplier = _sqrtPositionMultiplier;
feePercentage = _feePercentage;
}

function initialize(uint256 _startingSqrtPriceBottom, uint256 _sqrtPositionMultiplier) public {
sqrtPositionMultiplier = _sqrtPositionMultiplier;
positions[0].sqrtPriceBottom = _startingSqrtPriceBottom;
positions[0].sqrtPriceTop = _startingSqrtPriceBottom*_sqrtPositionMultiplier/10**18;
positions[0].activated = true;
}

function initialize(uint160 sqrtPriceX96) external override {
initialize(uint256(sqrtPriceX96), DEFAULT_SQRT_POSITION_MULTIPLIER);
}

function getLastBalances() external override view returns (uint256 _lastBalance0, uint256 _lastBalance1) {
_lastBalance0 = lastBalance0;
Expand Down Expand Up @@ -105,7 +119,7 @@ contract DesireSwapV0Pool is Ticket, IDesireSwapV0Pool {
uint256 amount0,
uint256 amount1,
bytes calldata data
) external override{
) external override(IDesireSwapV0Pool, IUniswapV3PoolActions){
address body = IDesireSwapV0Factory(factory).body();
(bool success, bytes memory data) = body.delegatecall(
abi.encodeWithSignature("flash(address to, uint256 amount0, uint256 amount1, bytes calldata data)",to, amount0, amount1, data)
Expand Down
Loading