Skip to content

Commit

Permalink
add BNBPartyFactory contract
Browse files Browse the repository at this point in the history
  • Loading branch information
YouStillAlive committed Jul 1, 2024
1 parent aa75a69 commit 7f3054c
Show file tree
Hide file tree
Showing 12 changed files with 10,442 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: CI for Solidity Contracts

on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
uses: The-Poolz/solidity-workflows/.github/workflows/build.yml@v0.6.0
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
artifacts
cache
temp.zip
yarn.lock
gas-report.txt
typechain-types
coverage
coverage.json
.env
gasReport.md
73 changes: 73 additions & 0 deletions contracts/BNBPartyFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: MIT
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";

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

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

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

function createToken(
string memory name,
string memory 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);
}
}
186 changes: 186 additions & 0 deletions contracts/interfaces/INonfungiblePositionManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.7.5;

import "./IPoolInitializer.sol";

/// @title Non-fungible token for positions
/// @notice Wraps PancakeSwap V3 positions in a non-fungible token interface which allows for them to be transferred
/// and authorized.
interface INonfungiblePositionManager is IPoolInitializer {
/// @notice Emitted when liquidity is increased for a position NFT
/// @dev Also emitted when a token is minted
/// @param tokenId The ID of the token for which liquidity was increased
/// @param liquidity The amount by which liquidity for the NFT position was increased
/// @param amount0 The amount of token0 that was paid for the increase in liquidity
/// @param amount1 The amount of token1 that was paid for the increase in liquidity
event IncreaseLiquidity(
uint256 indexed tokenId,
uint128 liquidity,
uint256 amount0,
uint256 amount1
);
/// @notice Emitted when liquidity is decreased for a position NFT
/// @param tokenId The ID of the token for which liquidity was decreased
/// @param liquidity The amount by which liquidity for the NFT position was decreased
/// @param amount0 The amount of token0 that was accounted for the decrease in liquidity
/// @param amount1 The amount of token1 that was accounted for the decrease in liquidity
event DecreaseLiquidity(
uint256 indexed tokenId,
uint128 liquidity,
uint256 amount0,
uint256 amount1
);
/// @notice Emitted when tokens are collected for a position NFT
/// @dev The amounts reported may not be exactly equivalent to the amounts transferred, due to rounding behavior
/// @param tokenId The ID of the token for which underlying tokens were collected
/// @param recipient The address of the account that received the collected tokens
/// @param amount0 The amount of token0 owed to the position that was collected
/// @param amount1 The amount of token1 owed to the position that was collected
event Collect(
uint256 indexed tokenId,
address recipient,
uint256 amount0,
uint256 amount1
);

/// @notice Returns the position information associated with a given token ID.
/// @dev Throws if the token ID is not valid.
/// @param tokenId The ID of the token that represents the position
/// @return nonce The nonce for permits
/// @return operator The address that is approved for spending
/// @return token0 The address of the token0 for a specific pool
/// @return token1 The address of the token1 for a specific pool
/// @return fee The fee associated with the pool
/// @return tickLower The lower end of the tick range for the position
/// @return tickUpper The higher end of the tick range for the position
/// @return liquidity The liquidity of the position
/// @return feeGrowthInside0LastX128 The fee growth of token0 as of the last action on the individual position
/// @return feeGrowthInside1LastX128 The fee growth of token1 as of the last action on the individual position
/// @return tokensOwed0 The uncollected amount of token0 owed to the position as of the last computation
/// @return tokensOwed1 The uncollected amount of token1 owed to the position as of the last computation
function positions(
uint256 tokenId
)
external
view
returns (
uint96 nonce,
address operator,
address token0,
address token1,
uint24 fee,
int24 tickLower,
int24 tickUpper,
uint128 liquidity,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
);

struct MintParams {
address token0;
address token1;
uint24 fee;
int24 tickLower;
int24 tickUpper;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
uint256 amount1Min;
address recipient;
uint256 deadline;
}

/// @notice Creates a new position wrapped in a NFT
/// @dev Call this when the pool does exist and is initialized. Note that if the pool is created but not initialized
/// a method does not exist, i.e. the pool is assumed to be initialized.
/// @param params The params necessary to mint a position, encoded as `MintParams` in calldata
/// @return tokenId The ID of the token that represents the minted position
/// @return liquidity The amount of liquidity for this position
/// @return amount0 The amount of token0
/// @return amount1 The amount of token1
function mint(
MintParams calldata params
)
external
payable
returns (
uint256 tokenId,
uint128 liquidity,
uint256 amount0,
uint256 amount1
);

struct IncreaseLiquidityParams {
uint256 tokenId;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
uint256 amount1Min;
uint256 deadline;
}

/// @notice Increases the amount of liquidity in a position, with tokens paid by the `msg.sender`
/// @param params tokenId The ID of the token for which liquidity is being increased,
/// amount0Desired The desired amount of token0 to be spent,
/// amount1Desired The desired amount of token1 to be spent,
/// amount0Min The minimum amount of token0 to spend, which serves as a slippage check,
/// amount1Min The minimum amount of token1 to spend, which serves as a slippage check,
/// deadline The time by which the transaction must be included to effect the change
/// @return liquidity The new liquidity amount as a result of the increase
/// @return amount0 The amount of token0 to acheive resulting liquidity
/// @return amount1 The amount of token1 to acheive resulting liquidity
function increaseLiquidity(
IncreaseLiquidityParams calldata params
)
external
payable
returns (uint128 liquidity, uint256 amount0, uint256 amount1);

struct DecreaseLiquidityParams {
uint256 tokenId;
uint128 liquidity;
uint256 amount0Min;
uint256 amount1Min;
uint256 deadline;
}

/// @notice Decreases the amount of liquidity in a position and accounts it to the position
/// @param params tokenId The ID of the token for which liquidity is being decreased,
/// amount The amount by which liquidity will be decreased,
/// amount0Min The minimum amount of token0 that should be accounted for the burned liquidity,
/// amount1Min The minimum amount of token1 that should be accounted for the burned liquidity,
/// deadline The time by which the transaction must be included to effect the change
/// @return amount0 The amount of token0 accounted to the position's tokens owed
/// @return amount1 The amount of token1 accounted to the position's tokens owed
function decreaseLiquidity(
DecreaseLiquidityParams calldata params
) external payable returns (uint256 amount0, uint256 amount1);

struct CollectParams {
uint256 tokenId;
address recipient;
uint128 amount0Max;
uint128 amount1Max;
}

/// @notice Collects up to a maximum amount of fees owed to a specific position to the recipient
/// @param params tokenId The ID of the NFT for which tokens are being collected,
/// recipient The account that should receive the tokens,
/// amount0Max The maximum amount of token0 to collect,
/// amount1Max The maximum amount of token1 to collect
/// @return amount0 The amount of fees collected in token0
/// @return amount1 The amount of fees collected in token1
function collect(
CollectParams calldata params
) external payable returns (uint256 amount0, uint256 amount1);

/// @notice Burns a token ID, which deletes it from the NFT contract. The token must have 0 liquidity and all tokens
/// must be collected first.
/// @param tokenId The ID of the token that is being burned
function burn(uint256 tokenId) external payable;

function WETH9() external view returns (address);

}
21 changes: 21 additions & 0 deletions contracts/interfaces/IPoolInitializer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.5;

/// @title Creates and initializes V3 Pools
/// @notice Provides a method for creating and initializing a pool, if necessary, for bundling with other methods that
/// require the pool to exist.
interface IPoolInitializer {
/// @notice Creates a new pool if it does not exist, then initializes if not initialized
/// @dev This method can be bundled with others via IMulticall for the first action (e.g. mint) performed against a pool
/// @param token0 The contract address of token0 of the pool
/// @param token1 The contract address of token1 of the pool
/// @param fee The fee amount of the v3 pool for the specified token pair
/// @param sqrtPriceX96 The initial square root price of the pool as a Q64.96 value
/// @return pool Returns the pool address based on the pair of tokens and fee, will return the newly created pool address if necessary
function createAndInitializePoolIfNecessary(
address token0,
address token1,
uint24 fee,
uint160 sqrtPriceX96
) external payable returns (address pool);
}
15 changes: 15 additions & 0 deletions contracts/token/ERC20Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

contract ERC20Token is ERC20, ERC20Burnable {
constructor(
string memory name,
string memory symbol,
uint256 initialAmount
) ERC20(name, symbol) {
_mint(msg.sender, initialAmount);
}
}
71 changes: 71 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { HardhatUserConfig } from "hardhat/config";
import '@nomicfoundation/hardhat-verify';
import 'hardhat-gas-reporter';
import '@typechain/hardhat';
import 'solidity-coverage';
import '@nomicfoundation/hardhat-network-helpers';
import '@nomicfoundation/hardhat-ethers';
import '@nomicfoundation/hardhat-chai-matchers';
import "@truffle/dashboard-hardhat-plugin"

const config: HardhatUserConfig = {
defaultNetwork: "hardhat",
solidity: {
compilers: [
{
version: "0.8.24",
settings: {
evmVersion: "istanbul",
optimizer: {
enabled: true,
runs: 200,
},
},
},
],
},
networks: {
hardhat: {
blockGasLimit: 130_000_000,
},
bscTestnet: {
url: "https://data-seed-prebsc-1-s1.binance.org:8545",
chainId: 97,
//accounts: [ process.env.PRIVATE_KEY || ""],
},
bsc: {
url: "https://bsc-dataseed.binance.org/",
chainId: 56,
//accounts: [ process.env.PRIVATE_KEY || ""],
},
},
etherscan: {
apiKey: {
mainnet: process.env.ETHERSCAN_API_KEY || "",
bsc: process.env.BSCSCAN_API_KEY || "",
bscTestnet: process.env.BSCSCAN_API_KEY || "",
},
},
sourcify: {
// Disabled by default
// Doesn't need an API key
enabled: true,
},
gasReporter: {
enabled: true,
showMethodSig: true,
currency: 'USD',
token: 'BNB',
gasPriceApi: 'https://api.bscscan.com/api?module=proxy&action=eth_gasPrice&apikey=' + process.env.BSCSCAN_API_KEY,
coinmarketcap: process.env.CMC_API_KEY || '',
noColors: true,
reportFormat: "markdown",
outputFile: "gasReport.md",
forceTerminalOutput: true,
L1: "binance",
forceTerminalOutputFormat: "terminal",
showTimeSpent: true,
}
}

export default config
Loading

0 comments on commit 7f3054c

Please sign in to comment.