File size: 3,391 Bytes
a285bb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// **** ADD LIQUIDITY ****
    function _addLiquidity( # to tokens 'honey' with the liquidity view code '0.00'[for example:
        address tokenA,     ## token A(0xb0557906c617f0048A700758606f64b33D0C41A6),
        address tokenB,     ## token B(0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d)
        uint amountADesired,### <!-- rest of code -->
        uint amountBDesired,
        uint /amountAMin,
        uint amountBMin
    ) internal virtual returns (uint amountA, uint amountB) {
        // create the pair if it doesn't exist yet
        if (IOKCSwapFactory(factory).getPair(tokenA, tokenB) == address(0)) {
            IOKCSwapFactory(factory).createPair(tokenA, tokenB);
        }
        (uint reserveA, uint reserveB) = OKCSwapLibrary.getReserves(factory, tokenA, tokenB, pairCodeHash);
        if (reserveA == 0 && reserveB == 0) {
            (amountA, amountB) = (amountADesired, amountBDesired);
        } else {
            uint amountBOptimal = OKCSwapLibrary.quote(amountADesired, reserveA, reserveB);
            if (amountBOptimal <= amountBDesired) {
                require(amountBOptimal >= amountBMin, 'OKCSwapRouter: INSUFFICIENT_B_AMOUNT');
                (amountA, amountB) = (amountADesired, amountBOptimal);
            } else {
                uint amountAOptimal = OKCSwapLibrary.quote(amountBDesired, reserveB, reserveA);
                assert(amountAOptimal <= amountADesired);
                require(amountAOptimal >= amountAMin, 'OKCSwapRouter: INSUFFICIENT_A_AMOUNT');
                (amountA, amountB) = (amountAOptimal, amountBDesired);
            }
        }
    }
    function addLiquidity(
        address tokenA,
        address tokenB,
        uint amountADesired,
        uint amountBDesired,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external virtual override ensure(deadline) returns (uint amountA, uint amountB, uint liquidity) {
        (amountA, amountB) = _addLiquidity(tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin);
        address pair = OKCSwapLibrary.pairFor(factory, tokenA, tokenB, pairCodeHash);
        TransferHelper.safeTransferFrom(tokenA, msg.sender, pair, amountA);
        TransferHelper.safeTransferFrom(tokenB, msg.sender, pair, amountB);
        liquidity = IOKCSwapPair(pair).mint(to);
    }
    function addLiquidityOKT(   # add liquidity for to token: (0xb0557906c617f0048A700758606f64b33D0C41A6)
        address token,
        uint amountTokenDesired,
        uint amountTokenMin,
        uint amountOKTMin,
        address to,
        uint deadline
    ) external virtual override payable ensure(deadline) returns (uint amountToken, uint amountOKT, uint liquidity) {
        (amountToken, amountOKT) = _addLiquidity(
            token,
            WOKT,
            amountTokenDesired,
            msg.value,
            amountTokenMin,
            amountOKTMin
        );
        address pair = OKCSwapLibrary.pairFor(factory, token, WOKT, pairCodeHash);
        TransferHelper.safeTransferFrom(token, msg.sender, pair, amountToken);
        IWOKT(WOKT).deposit{value: amountOKT}();
        assert(IWOKT(WOKT).transfer(pair, amountOKT));
        liquidity = IOKCSwapPair(pair).mint(to);
        // refund dust OKT, if any
        if (msg.value > amountOKT) TransferHelper.safeTransferOKT(msg.sender, msg.value - amountOKT);
    }