Skip to content

Commit

Permalink
progression
Browse files Browse the repository at this point in the history
  • Loading branch information
YouStillAlive committed Jul 2, 2024
1 parent 7f3054c commit d2892e1
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 45 deletions.
61 changes: 18 additions & 43 deletions contracts/BNBPartyFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,45 @@ pragma solidity ^0.8.0;
import "./token/ERC20Token.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@pancakeswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "./interfaces/INonfungiblePositionManager.sol";
import "./PoolCreation.sol";

contract BNBPartyFactory {
address public internalRouter;
ISwapRouter public swapRouter;
INonfungiblePositionManager public positionManager;
uint256 public fee;
uint256 public buyLimit;
uint256 public initialTokenAmount;
contract BNBPartyFactory is PoolCreation {
address public immutable internalRouter;
ISwapRouter public immutable swapRouter;
uint256 public immutable buyLimit;
uint256 public immutable initialTokenAmount;

event TokenCreated(address tokenAddress, string name, string symbol);
event TokenCreated(
address indexed tokenAddress,
string name,
string symbol
);

constructor(
address _internalRouter,
address _swapRouter,
address _positionManager,
uint256 _fee,
uint24 _fee,
uint256 _buyLimit,
uint256 _initialTokenAmount
uint256 _initialTokenAmount,
uint160 _sqrtPriceLimitX96
) {
internalRouter = _internalRouter;
swapRouter = ISwapRouter(_swapRouter);
positionManager = INonfungiblePositionManager(_positionManager);
fee = _fee;
buyLimit = _buyLimit;
initialTokenAmount = _initialTokenAmount;
sqrtPriceLimitX96 = _sqrtPriceLimitX96;
}

function createToken(
string memory name,
string memory symbol
string calldata name,
string calldata symbol
) public payable returns (ERC20Token newToken) {
require(msg.value >= fee, "Insufficient BNB for fee");
newToken = new ERC20Token(name, symbol, initialTokenAmount);
emit TokenCreated(address(newToken), name, symbol);
addInitialLiquidity(address(newToken), initialTokenAmount);
}

function addInitialLiquidity(
address tokenAddress,
uint256 amount
) internal {
ERC20 token = ERC20(tokenAddress);
token.approve(address(positionManager), amount);
positionManager.createAndInitializePoolIfNecessary(
tokenAddress,
positionManager.WETH9(),
3000, // Fee tier
1 ether // Price
);
INonfungiblePositionManager.MintParams
memory params = INonfungiblePositionManager.MintParams({
token0: tokenAddress,
token1: positionManager.WETH9(),
fee: 3000, // Fee tier
tickLower: -887272, // Lower tick
tickUpper: 887272, // Upper tick
amount0Desired: amount,
amount1Desired: 0,
amount0Min: 0,
amount1Min: 0,
recipient: address(this),
deadline: block.timestamp
});
positionManager.mint{value: 0}(params);
_addInitialLiquidity(address(newToken), initialTokenAmount);
}
}
87 changes: 87 additions & 0 deletions contracts/InternalRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@pancakeswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./interfaces/INonfungiblePositionManager.sol";
import "./PoolCreation.sol";

contract InternalRouter is PoolCreation {
ISwapRouter public pancakeRouter;
address public factory;
uint256 public buyLimit;
address pool;
mapping(address => bool) public reachedLimit;

event SwapOperation(address indexed user, uint256 amount);
event LiquidityTransferred(address token, uint256 amount);

constructor(
address _pancakeRouter,
address _pool,
address _positionManager,
uint256 _buyLimit,
address _factory
) {
pancakeRouter = ISwapRouter(_pancakeRouter);
positionManager = INonfungiblePositionManager(_positionManager);
buyLimit = _buyLimit;
factory = _factory;
pool = _pool;
}

function swap(address token, uint256 amount) external payable {
require(msg.value >= amount, "Insufficient BNB sent");

checkAndTransferLiquidity(token);

ISwapRouter.ExactInputSingleParams memory params = ISwapRouter
.ExactInputSingleParams({
tokenIn: positionManager.WETH9(), // WETH9() can be replaced with address(0) for BNB
tokenOut: token,
fee: 3000,
recipient: msg.sender,
deadline: block.timestamp,
amountIn: msg.value,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0
});

pancakeRouter.exactInputSingle{value: msg.value}(params);

emit SwapOperation(msg.sender, amount);
}

function checkAndTransferLiquidity(address token) internal {
if (IERC20(token).balanceOf(pool) >= buyLimit && !reachedLimit[token]) {
transferToRealPancakeSwap(token);
reachedLimit[token] = true;
}
}

function transferToRealPancakeSwap(address token) internal {
uint256 contractTokenBalance = IERC20(token).balanceOf(address(this));
IERC20(token).approve(address(positionManager), contractTokenBalance);

INonfungiblePositionManager.MintParams
memory params = INonfungiblePositionManager.MintParams({
token0: address(0), // WETH9() can be replaced with address(0) for BNB
token1: token,
fee: 3000,
tickLower: -887272, // Adjust these values as necessary
tickUpper: 887272, // Adjust these values as necessary
amount0Desired: contractTokenBalance,
amount1Desired: 0,
amount0Min: 0,
amount1Min: 0,
recipient: factory,
deadline: block.timestamp
});

positionManager.mint{value: 0}(params);

emit LiquidityTransferred(token, contractTokenBalance);
}

receive() external payable {}
}
40 changes: 40 additions & 0 deletions contracts/PoolCreation.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./token/ERC20Token.sol";
import "./interfaces/INonfungiblePositionManager.sol";

contract PoolCreation {
INonfungiblePositionManager public immutable positionManager;
uint24 public immutable fee;
uint160 public immutable sqrtPriceLimitX96;

function _addInitialLiquidity(
address tokenAddress,
uint256 amount
) internal {
ERC20 token = ERC20(tokenAddress);
token.approve(address(positionManager), amount);
positionManager.createAndInitializePoolIfNecessary(
tokenAddress,
positionManager.WETH9(),
fee, // Fee tier
sqrtPriceLimitX96
);
INonfungiblePositionManager.MintParams
memory params = INonfungiblePositionManager.MintParams({
token0: tokenAddress,
token1: positionManager.WETH9(),
fee: fee, // Fee tier
tickLower: -887272, // Lower tick
tickUpper: 887272, // Upper tick
amount0Desired: amount,
amount1Desired: 0,
amount0Min: amount,
amount1Min: 0,
recipient: address(this),
deadline: block.timestamp
});
positionManager.mint{value: 0}(params);
}
}
3 changes: 1 addition & 2 deletions contracts/interfaces/INonfungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,5 @@ interface INonfungiblePositionManager is IPoolInitializer {
/// @param tokenId The ID of the token that is being burned
function burn(uint256 tokenId) external payable;

function WETH9() external view returns (address);

function WETH9() external view returns (address);
}

0 comments on commit d2892e1

Please sign in to comment.