Advertisement
dennoh

Adding Liquidity with a s.c

Jun 9th, 2022
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.80 KB | None | 0 0
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.4;
  3.  
  4. pragma abicoder v2;
  5.  
  6. import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
  7.  
  8. import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
  9.  
  10. import "@openzeppelin/contracts-upgradeable/utils/math/SafeMathUpgradeable.sol";
  11.  
  12. import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol";
  13.  
  14. interface IUniswap {
  15.     function addLiquidityETH(
  16.         address token,
  17.         uint256 amountTokenDesired,
  18.         uint256 amountTokenMin,
  19.         uint256 amountETHMin,
  20.         address to,
  21.         uint256 deadline
  22.     )
  23.         external
  24.         payable
  25.         returns (
  26.             uint256 amountToken,
  27.             uint256 amountETH,
  28.             uint256 liquidity
  29.         );
  30.  
  31.     function addLiquidity(
  32.         address tokenA,
  33.         address tokenB,
  34.         uint256 amountADesired,
  35.         uint256 amountBDesired,
  36.         uint256 amountAMin,
  37.         uint256 amountBMin,
  38.         address to,
  39.         uint256 deadline
  40.     )
  41.         external
  42.         returns (
  43.             uint256 amountA,
  44.             uint256 amountB,
  45.             uint256 liquidity
  46.         );
  47.  
  48.     function removeLiquidityETH(
  49.         address token,
  50.         uint256 liquidity,
  51.         uint256 amountTokenMin,
  52.         uint256 amountETHMin,
  53.         address to,
  54.         uint256 deadline
  55.     ) external returns (uint256 amountToken, uint256 amountETH);
  56.  
  57.     function getAmountsIn(uint256 amountOut, address[] memory path)
  58.         external
  59.         view
  60.         returns (uint256[] memory amounts);
  61. }
  62.  
  63. interface IERC20 {
  64.     function approve(address spender, uint256 amount) external returns (bool);
  65. }
  66.  
  67. interface INotStaking {
  68.     struct Liquidity {
  69.         uint256 id;
  70.         uint256 bnbAmount;
  71.         uint256 notsAmount;
  72.         uint256 liquidity;
  73.         uint256 lockPeriod;
  74.     }
  75.  
  76.     event Staked(
  77.         address indexed user,
  78.         uint256 amount,
  79.         uint256 timestamp,
  80.         Liquidity liquidity
  81.     );
  82. }
  83.  
  84. contract NotStaking is
  85.     Initializable,
  86.     OwnableUpgradeable,
  87.     INotStaking
  88. {
  89.     using SafeMathUpgradeable for uint256;
  90.     using AddressUpgradeable for address;
  91.  
  92.     /*///////////////////////////////////////////////////////////////
  93.                             State variables
  94.     //////////////////////////////////////////////////////////////*/
  95.     address private UNISWAP_ROUTER_ADDRESS;
  96.  
  97.     uint256 liquidityId;
  98.  
  99.     address public _notCommunityAddress;
  100.  
  101.     /*///////////////////////////////////////////////////////////////
  102.                                 Mappings
  103.     //////////////////////////////////////////////////////////////*/
  104.  
  105.     // liquidityId -> liquidity
  106.     mapping(uint256 => Liquidity) private _liquidities;
  107.  
  108.     /*///////////////////////////////////////////////////////////////
  109.                                 Modifiers
  110.     //////////////////////////////////////////////////////////////*/
  111.  
  112.     modifier onlyNotCommunity() {
  113.         require(msg.sender == _notCommunityAddress, "Unauthorized");
  114.  
  115.         _;
  116.     }
  117.  
  118.     /*///////////////////////////////////////////////////////////////
  119.                     Constructor + initializer logic
  120.     //////////////////////////////////////////////////////////////*/
  121.  
  122.     /// @dev Initiliazes the contract, like a constructor.
  123.     function initialize(
  124.         address notCommunityAddress_,
  125.         address uniswapRouterAddress_
  126.     ) external initializer {
  127.         // Initialize inherited contracts, most base-like -> most derived.
  128.         __Ownable_init();
  129.  
  130.         _notCommunityAddress = notCommunityAddress_;
  131.         UNISWAP_ROUTER_ADDRESS = uniswapRouterAddress_;
  132.         liquidityId = 0;
  133.     }
  134.  
  135.     function getPairPrice(uint256 _amountOut, address[] memory path)
  136.         public
  137.         view
  138.         returns (uint256[] memory)
  139.     {
  140.         return IUniswap(UNISWAP_ROUTER_ADDRESS).getAmountsIn(_amountOut, path);
  141.     }
  142.  
  143.     function addLiquidity(
  144.         address token,
  145.         uint256 amountTokenDesired,
  146.         uint256 amountTokenMin,
  147.         uint256 amountETHMin    
  148.     )
  149.         external
  150.         payable
  151.         returns (
  152.             uint256 amountA,
  153.             uint256 amountB,
  154.             uint256 liquidity
  155.         )
  156.     {
  157.         // approve token transfer to cover all possible scenarios
  158.         IERC20(token).approve(UNISWAP_ROUTER_ADDRESS, amountTokenDesired);
  159.  
  160.         (amountA, amountB, liquidity) = IUniswap(UNISWAP_ROUTER_ADDRESS)
  161.             .addLiquidityETH(
  162.                 token,
  163.                 amountTokenDesired,
  164.                 amountTokenMin,
  165.                 amountETHMin,
  166.                 msg.sender,
  167.                 block.timestamp
  168.             );
  169.  
  170.         liquidityId += 1;
  171.  
  172.         _liquidities[liquidityId] = Liquidity(
  173.             liquidityId,
  174.             amountA,
  175.             amountB,
  176.             liquidity,
  177.             block.timestamp
  178.         );
  179.  
  180.         emit Staked(
  181.             msg.sender,
  182.             amountA,
  183.             block.timestamp,
  184.             _liquidities[liquidityId]
  185.         );
  186.     }
  187.  
  188.     function removeLiquidity(
  189.         address token,
  190.         uint256 liquidity,
  191.         uint256 amountTokenMin,
  192.         uint256 amountETHMin,
  193.         address to,
  194.         uint256 deadline
  195.     ) external {
  196.         IERC20(token).approve(UNISWAP_ROUTER_ADDRESS, liquidity);
  197.  
  198.         IUniswap(UNISWAP_ROUTER_ADDRESS).removeLiquidityETH(
  199.             token,
  200.             liquidity,
  201.             amountTokenMin,
  202.             amountETHMin,
  203.             to,
  204.             deadline
  205.         );
  206.     }
  207.  
  208.     function setNotCommunityAddress(address __notCommunityAddress)
  209.         external
  210.         onlyOwner
  211.     {
  212.         _notCommunityAddress = __notCommunityAddress;
  213.     }
  214.  
  215.     /// @dev Lets the contract receives native tokens e.g ETH
  216.     receive() external payable {}
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement