Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SPDX-License-Identifier: MIT
- pragma solidity ^0.8.4;
- pragma abicoder v2;
- import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
- import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
- import "@openzeppelin/contracts-upgradeable/utils/math/SafeMathUpgradeable.sol";
- import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol";
- interface IUniswap {
- function addLiquidityETH(
- address token,
- uint256 amountTokenDesired,
- uint256 amountTokenMin,
- uint256 amountETHMin,
- address to,
- uint256 deadline
- )
- external
- payable
- returns (
- uint256 amountToken,
- uint256 amountETH,
- uint256 liquidity
- );
- function addLiquidity(
- address tokenA,
- address tokenB,
- uint256 amountADesired,
- uint256 amountBDesired,
- uint256 amountAMin,
- uint256 amountBMin,
- address to,
- uint256 deadline
- )
- external
- returns (
- uint256 amountA,
- uint256 amountB,
- uint256 liquidity
- );
- function removeLiquidityETH(
- address token,
- uint256 liquidity,
- uint256 amountTokenMin,
- uint256 amountETHMin,
- address to,
- uint256 deadline
- ) external returns (uint256 amountToken, uint256 amountETH);
- function getAmountsIn(uint256 amountOut, address[] memory path)
- external
- view
- returns (uint256[] memory amounts);
- }
- interface IERC20 {
- function approve(address spender, uint256 amount) external returns (bool);
- }
- interface INotStaking {
- struct Liquidity {
- uint256 id;
- uint256 bnbAmount;
- uint256 notsAmount;
- uint256 liquidity;
- uint256 lockPeriod;
- }
- event Staked(
- address indexed user,
- uint256 amount,
- uint256 timestamp,
- Liquidity liquidity
- );
- }
- contract NotStaking is
- Initializable,
- OwnableUpgradeable,
- INotStaking
- {
- using SafeMathUpgradeable for uint256;
- using AddressUpgradeable for address;
- /*///////////////////////////////////////////////////////////////
- State variables
- //////////////////////////////////////////////////////////////*/
- address private UNISWAP_ROUTER_ADDRESS;
- uint256 liquidityId;
- address public _notCommunityAddress;
- /*///////////////////////////////////////////////////////////////
- Mappings
- //////////////////////////////////////////////////////////////*/
- // liquidityId -> liquidity
- mapping(uint256 => Liquidity) private _liquidities;
- /*///////////////////////////////////////////////////////////////
- Modifiers
- //////////////////////////////////////////////////////////////*/
- modifier onlyNotCommunity() {
- require(msg.sender == _notCommunityAddress, "Unauthorized");
- _;
- }
- /*///////////////////////////////////////////////////////////////
- Constructor + initializer logic
- //////////////////////////////////////////////////////////////*/
- /// @dev Initiliazes the contract, like a constructor.
- function initialize(
- address notCommunityAddress_,
- address uniswapRouterAddress_
- ) external initializer {
- // Initialize inherited contracts, most base-like -> most derived.
- __Ownable_init();
- _notCommunityAddress = notCommunityAddress_;
- UNISWAP_ROUTER_ADDRESS = uniswapRouterAddress_;
- liquidityId = 0;
- }
- function getPairPrice(uint256 _amountOut, address[] memory path)
- public
- view
- returns (uint256[] memory)
- {
- return IUniswap(UNISWAP_ROUTER_ADDRESS).getAmountsIn(_amountOut, path);
- }
- function addLiquidity(
- address token,
- uint256 amountTokenDesired,
- uint256 amountTokenMin,
- uint256 amountETHMin
- )
- external
- payable
- returns (
- uint256 amountA,
- uint256 amountB,
- uint256 liquidity
- )
- {
- // approve token transfer to cover all possible scenarios
- IERC20(token).approve(UNISWAP_ROUTER_ADDRESS, amountTokenDesired);
- (amountA, amountB, liquidity) = IUniswap(UNISWAP_ROUTER_ADDRESS)
- .addLiquidityETH(
- token,
- amountTokenDesired,
- amountTokenMin,
- amountETHMin,
- msg.sender,
- block.timestamp
- );
- liquidityId += 1;
- _liquidities[liquidityId] = Liquidity(
- liquidityId,
- amountA,
- amountB,
- liquidity,
- block.timestamp
- );
- emit Staked(
- msg.sender,
- amountA,
- block.timestamp,
- _liquidities[liquidityId]
- );
- }
- function removeLiquidity(
- address token,
- uint256 liquidity,
- uint256 amountTokenMin,
- uint256 amountETHMin,
- address to,
- uint256 deadline
- ) external {
- IERC20(token).approve(UNISWAP_ROUTER_ADDRESS, liquidity);
- IUniswap(UNISWAP_ROUTER_ADDRESS).removeLiquidityETH(
- token,
- liquidity,
- amountTokenMin,
- amountETHMin,
- to,
- deadline
- );
- }
- function setNotCommunityAddress(address __notCommunityAddress)
- external
- onlyOwner
- {
- _notCommunityAddress = __notCommunityAddress;
- }
- /// @dev Lets the contract receives native tokens e.g ETH
- receive() external payable {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement