ETH Price: $2,332.44 (-4.14%)

Contract

0x6b74e5c4137AfD6a7Dbd9d43c9d7af2A258e57A7
 

Overview

ETH Balance

0 ETH

Eth Value

$0.00

Multichain Info

No addresses found
Transaction Hash
Method
Block
From
To
Set Fee To Addre...118012972021-02-06 7:04:091482 days ago1612595049IN
0x6b74e5c4...A258e57A7
0 ETH0.01116544130.00000145

Advanced mode:
Parent Transaction Hash Block
From
To
View All Internal Transactions
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
GraSwapFactory

Compiler Version
v0.6.12+commit.27d51765

Optimization Enabled:
Yes with 200 runs

Other Settings:
default evmVersion, None license

Contract Source Code (Solidity)

/**
 *Submitted for verification at Etherscan.io on 2021-02-06
*/

// SPDX-License-Identifier: GPL
pragma solidity 0.6.12;


interface IPairFeeDistribution {
    function addpair(address pair) external;
}

library Math {
    function min(uint x, uint y) internal pure returns (uint z) {
        z = x < y ? x : y;
    }

    // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
    function sqrt(uint y) internal pure returns (uint z) {
        if (y > 3) {
            z = y;
            uint x = y / 2 + 1;
            while (x < z) {
                z = x;
                x = (y / x + x) / 2;
            }
        } else if (y != 0) {
            z = 1;
        }
    }
}


library SafeMath256 {
    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     *
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");

        return c;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return sub(a, b, "SafeMath: subtraction overflow");
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        uint256 c = a - b;

        return c;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     *
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold

        return c;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * Reverts when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        return mod(a, b, "SafeMath: modulo by zero");
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * Reverts with custom message when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b != 0, errorMessage);
        return a % b;
    }
}


/*
This library defines a decimal floating point number. It has 8 decimal significant digits. Its maximum value is 9.9999999e+15.
And its minimum value is 1.0e-16. The following golang code explains its detail implementation.

func buildPrice(significant int, exponent int) uint32 {
	if !(10000000 <= significant && significant <= 99999999) {
		panic("Invalid significant")
	}
	if !(-16 <= exponent && exponent <= 15) {
		panic("Invalid exponent")
	}
	return uint32(((exponent+16)<<27)|significant);
}

func priceToFloat(price uint32) float64 {
	exponent := int(price>>27)
	significant := float64(price&((1<<27)-1))
	return significant * math.Pow10(exponent-23)
}

*/

// A price presented as a rational number
struct RatPrice {
    uint numerator;   // at most 54bits
    uint denominator; // at most 76bits
}

library DecFloat32 {
    uint32 public constant MANTISSA_MASK = (1<<27) - 1;
    uint32 public constant MAX_MANTISSA = 9999_9999;
    uint32 public constant MIN_MANTISSA = 1000_0000;
    uint32 public constant MIN_PRICE = MIN_MANTISSA;
    uint32 public constant MAX_PRICE = (31<<27)|MAX_MANTISSA;

    // 10 ** (i + 1)
    function powSmall(uint32 i) internal pure returns (uint) {
        uint x = 2695994666777834996822029817977685892750687677375768584125520488993233305610;
        return (x >> (32*i)) & ((1<<32)-1);
    }

    // 10 ** (i * 8)
    function powBig(uint32 i) internal pure returns (uint) {
        uint y = 3402823669209384634633746076162356521930955161600000001;
        return (y >> (64*i)) & ((1<<64)-1);
    }

    // if price32=( 0<<27)|12345678 then numerator=12345678 denominator=100000000000000000000000
    // if price32=( 1<<27)|12345678 then numerator=12345678 denominator=10000000000000000000000
    // if price32=( 2<<27)|12345678 then numerator=12345678 denominator=1000000000000000000000
    // if price32=( 3<<27)|12345678 then numerator=12345678 denominator=100000000000000000000
    // if price32=( 4<<27)|12345678 then numerator=12345678 denominator=10000000000000000000
    // if price32=( 5<<27)|12345678 then numerator=12345678 denominator=1000000000000000000
    // if price32=( 6<<27)|12345678 then numerator=12345678 denominator=100000000000000000
    // if price32=( 7<<27)|12345678 then numerator=12345678 denominator=10000000000000000
    // if price32=( 8<<27)|12345678 then numerator=12345678 denominator=1000000000000000
    // if price32=( 9<<27)|12345678 then numerator=12345678 denominator=100000000000000
    // if price32=(10<<27)|12345678 then numerator=12345678 denominator=10000000000000
    // if price32=(11<<27)|12345678 then numerator=12345678 denominator=1000000000000
    // if price32=(12<<27)|12345678 then numerator=12345678 denominator=100000000000
    // if price32=(13<<27)|12345678 then numerator=12345678 denominator=10000000000
    // if price32=(14<<27)|12345678 then numerator=12345678 denominator=1000000000
    // if price32=(15<<27)|12345678 then numerator=12345678 denominator=100000000
    // if price32=(16<<27)|12345678 then numerator=12345678 denominator=10000000
    // if price32=(17<<27)|12345678 then numerator=12345678 denominator=1000000
    // if price32=(18<<27)|12345678 then numerator=12345678 denominator=100000
    // if price32=(19<<27)|12345678 then numerator=12345678 denominator=10000
    // if price32=(20<<27)|12345678 then numerator=12345678 denominator=1000
    // if price32=(21<<27)|12345678 then numerator=12345678 denominator=100
    // if price32=(22<<27)|12345678 then numerator=12345678 denominator=10
    // if price32=(23<<27)|12345678 then numerator=12345678 denominator=1
    // if price32=(24<<27)|12345678 then numerator=123456780 denominator=1
    // if price32=(25<<27)|12345678 then numerator=1234567800 denominator=1
    // if price32=(26<<27)|12345678 then numerator=12345678000 denominator=1
    // if price32=(27<<27)|12345678 then numerator=123456780000 denominator=1
    // if price32=(28<<27)|12345678 then numerator=1234567800000 denominator=1
    // if price32=(29<<27)|12345678 then numerator=12345678000000 denominator=1
    // if price32=(30<<27)|12345678 then numerator=123456780000000 denominator=1
    // if price32=(31<<27)|12345678 then numerator=1234567800000000 denominator=1
    function expandPrice(uint32 price32) internal pure returns (RatPrice memory) {
        uint s = price32&((1<<27)-1);
        uint32 a = price32 >> 27;
        RatPrice memory price;
        if(a >= 24) {
            uint32 b = a - 24;
            price.numerator = s * powSmall(b);
            price.denominator = 1;
        } else if(a == 23) {
            price.numerator = s;
            price.denominator = 1;
        } else {
            uint32 b = 22 - a;
            price.numerator = s;
            price.denominator = powSmall(b&0x7) * powBig(b>>3);
        }
        return price;
    }

    function getExpandPrice(uint price) internal pure returns(uint numerator, uint denominator) {
        uint32 m = uint32(price) & MANTISSA_MASK;
        require(MIN_MANTISSA <= m && m <= MAX_MANTISSA, "Invalid Price");
        RatPrice memory actualPrice = expandPrice(uint32(price));
        return (actualPrice.numerator, actualPrice.denominator);
    }

}

library ProxyData {
    uint public constant COUNT = 5;
    uint public constant INDEX_FACTORY = 0;
    uint public constant INDEX_MONEY_TOKEN = 1;
    uint public constant INDEX_STOCK_TOKEN = 2;
    uint public constant INDEX_GRA = 3;
    uint public constant INDEX_OTHER = 4;
    uint public constant OFFSET_PRICE_DIV = 0;
    uint public constant OFFSET_PRICE_MUL = 64;
    uint public constant OFFSET_STOCK_UNIT = 64+64;
    uint public constant OFFSET_IS_ONLY_SWAP = 64+64+64;

    function factory(uint[5] memory proxyData) internal pure returns (address) {
         return address(proxyData[INDEX_FACTORY]);
    }

    function money(uint[5] memory proxyData) internal pure returns (address) {
         return address(proxyData[INDEX_MONEY_TOKEN]);
    }

    function stock(uint[5] memory proxyData) internal pure returns (address) {
         return address(proxyData[INDEX_STOCK_TOKEN]);
    }

    function graContract(uint[5] memory proxyData) internal pure returns (address) {
         return address(proxyData[INDEX_GRA]);
    }

    function priceMul(uint[5] memory proxyData) internal pure returns (uint64) {
        return uint64(proxyData[INDEX_OTHER]>>OFFSET_PRICE_MUL);
    }

    function priceDiv(uint[5] memory proxyData) internal pure returns (uint64) {
        return uint64(proxyData[INDEX_OTHER]>>OFFSET_PRICE_DIV);
    }

    function stockUnit(uint[5] memory proxyData) internal pure returns (uint64) {
        return uint64(proxyData[INDEX_OTHER]>>OFFSET_STOCK_UNIT);
    }

    function isOnlySwap(uint[5] memory proxyData) internal pure returns (bool) {
        return uint8(proxyData[INDEX_OTHER]>>OFFSET_IS_ONLY_SWAP) != 0;
    }

    function fill(uint[5] memory proxyData, uint expectedCallDataSize) internal pure {
        uint size;
        // solhint-disable-next-line no-inline-assembly
        assembly {
            size := calldatasize()
        }
        require(size == expectedCallDataSize, "INVALID_CALLDATASIZE");
        // solhint-disable-next-line no-inline-assembly
        assembly {
            let offset := sub(size, 160)
            calldatacopy(proxyData, offset, 160)
        }
    }
}

interface IGraSwapFactory {
    event PairCreated(address indexed pair, address stock, address money, bool isOnlySwap);

    function createPair(address stock, address money, bool isOnlySwap) external returns (address pair);
    function setFeeToAddresses(address _feeTo_1, address _feeTo_2, address _feeToPrivate) external;
    function setFeeToSetter(address) external;
    function setFeeBPS(uint32 bps) external;
    function setPairLogic(address implLogic) external;

    function allPairsLength() external view returns (uint);
    function feeTo_1() external view returns (address);
    function feeTo_2() external view returns (address);
    function feeToPrivate() external view returns (address);
    function feeToSetter() external view returns (address);
    function feeBPS() external view returns (uint32);
    function pairLogic() external returns (address);
    function getTokensFromPair(address pair) external view returns (address stock, address money);
    function tokensToPair(address stock, address money, bool isOnlySwap) external view returns (address pair);
}

interface IERC20 {
    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);
    function totalSupply() external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint value) external returns (bool);
    function transfer(address to, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);
}

interface IGraSwapBlackList {
    // event OwnerChanged(address);
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
    event AddedBlackLists(address[]);
    event RemovedBlackLists(address[]);

    function owner()external view returns (address);
    // function newOwner()external view returns (address);
    function isBlackListed(address)external view returns (bool);

    // function changeOwner(address ownerToSet) external;
    // function updateOwner() external;
    function transferOwnership(address newOwner) external;
    function addBlackLists(address[] calldata  accounts)external;
    function removeBlackLists(address[] calldata  accounts)external;
}

interface IGraWhiteList {
    event AppendWhiter(address adder);
    event RemoveWhiter(address remover);
    
    function appendWhiter(address account) external;
    function removeWhiter(address account) external;
    function isWhiter(address account) external;
    function isNotWhiter(address account) external;
}

interface IGraSwapToken is IERC20, IGraSwapBlackList, IGraWhiteList{
    function burn(uint256 amount) external;
    function burnFrom(address account, uint256 amount) external;
    function increaseAllowance(address spender, uint256 addedValue) external returns (bool);
    function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool);
    // function multiTransfer(uint256[] calldata mixedAddrVal) external returns (bool);
    function batchTransfer(address[] memory addressList, uint256[] memory amountList) external returns (bool);
}

interface IGraSwapERC20 {
    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

    function name() external view returns (string memory);
    function symbol() external returns (string memory);
    function decimals() external view returns (uint8);
    function totalSupply() external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint value) external returns (bool);
    function transfer(address to, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);
}

interface IGraSwapPool {
    // more liquidity was minted
    event Mint(address indexed sender, uint stockAndMoneyAmount, address indexed to);
    // liquidity was burned
    event Burn(address indexed sender, uint stockAndMoneyAmount, address indexed to);
    // amounts of reserved stock and money in this pair changed
    event Sync(uint reserveStockAndMoney);

    function internalStatus() external view returns(uint[3] memory res);
    function getReserves() external view returns (uint112 reserveStock, uint112 reserveMoney, uint32 firstSellID);
    function getBooked() external view returns (uint112 bookedStock, uint112 bookedMoney, uint32 firstBuyID);
    function stock() external returns (address);
    function money() external returns (address);
    function mint(address to) external returns (uint liquidity);
    function burn(address to) external returns (uint stockAmount, uint moneyAmount);
    function skim(address to) external;
    function sync() external;
}

interface IGraSwapPair {
    event NewLimitOrder(uint data); // new limit order was sent by an account
    event NewMarketOrder(uint data); // new market order was sent by an account
    event OrderChanged(uint data); // old orders in orderbook changed
    event DealWithPool(uint data); // new order deal with the AMM pool
    event RemoveOrder(uint data); // an order was removed from the orderbook
    
    // Return three prices in rational number form, i.e., numerator/denominator.
    // They are: the first sell order's price; the first buy order's price; the current price of the AMM pool.
    function getPrices() external returns (
        uint firstSellPriceNumerator,
        uint firstSellPriceDenominator,
        uint firstBuyPriceNumerator,
        uint firstBuyPriceDenominator,
        uint poolPriceNumerator,
        uint poolPriceDenominator);

    // This function queries a list of orders in orderbook. It starts from 'id' and iterates the single-linked list, util it reaches the end, 
    // or until it has found 'maxCount' orders. If 'id' is 0, it starts from the beginning of the single-linked list.
    // It may cost a lot of gas. So you'd not to call in on chain. It is mainly for off-chain query.
    // The first uint256 returned by this function is special: the lowest 24 bits is the first order's id and the the higher bits is block height.
    // THe other uint256s are all corresponding to an order record of the single-linked list.
    function getOrderList(bool isBuy, uint32 id, uint32 maxCount) external view returns (uint[] memory);

    // remove an order from orderbook and return its booked (i.e. frozen) money to maker
    // 'id' points to the order to be removed
    // prevKey points to 3 previous orders in the single-linked list
    function removeOrder(bool isBuy, uint32 id, uint72 positionID) external;

    function removeOrders(uint[] calldata rmList) external;

    // Try to deal a new limit order or insert it into orderbook
    // its suggested order id is 'id' and suggested positions are in 'prevKey'
    // prevKey points to 3 existing orders in the single-linked list
    // the order's sender is 'sender'. the order's amount is amount*stockUnit, which is the stock amount to be sold or bought.
    // the order's price is 'price32', which is decimal floating point value.
    function addLimitOrder(bool isBuy, address sender, uint64 amount, uint32 price32, uint32 id, uint72 prevKey) external payable;

    // Try to deal a new market order. 'sender' pays 'inAmount' of 'inputToken', in exchange of the other token kept by this pair
    function addMarketOrder(address inputToken, address sender, uint112 inAmount) external payable returns (uint);

    // Given the 'amount' of stock and decimal floating point price 'price32', calculate the 'stockAmount' and 'moneyAmount' to be traded
    function calcStockAndMoney(uint64 amount, uint32 price32) external pure returns (uint stockAmount, uint moneyAmount);
}

abstract contract GraSwapERC20 is IGraSwapERC20 {
    using SafeMath256 for uint;

    uint internal _unusedVar0;
    uint internal _unusedVar1;
    uint internal _unusedVar2;
    uint internal _unusedVar3;
    uint internal _unusedVar4;
    uint internal _unusedVar5;
    uint internal _unusedVar6;
    uint internal _unusedVar7;
    uint internal _unusedVar8;
    uint internal _unusedVar9;
    uint internal _unlocked = 1;

    modifier lock() {
        require(_unlocked == 1, "GraSwap: LOCKED");
        _unlocked = 0;
        _;
        _unlocked = 1;
    }

    string private constant _NAME = "GraSwap-Share";
    uint8 private constant _DECIMALS = 18;
    uint  public override totalSupply;
    mapping(address => uint) public override balanceOf;
    mapping(address => mapping(address => uint)) public override allowance;

    function symbol() virtual external override returns (string memory);

    function name() external view override returns (string memory) {
        return _NAME;
    }

    function decimals() external view override returns (uint8) {
        return _DECIMALS;
    }

    function _mint(address to, uint value) internal {
        totalSupply = totalSupply.add(value);
        balanceOf[to] = balanceOf[to].add(value);
        emit Transfer(address(0), to, value);
    }

    function _burn(address from, uint value) internal {
        balanceOf[from] = balanceOf[from].sub(value);
        totalSupply = totalSupply.sub(value);
        emit Transfer(from, address(0), value);
    }

    function _approve(address owner, address spender, uint value) private {
        allowance[owner][spender] = value;
        emit Approval(owner, spender, value);
    }

    function _transfer(address from, address to, uint value) private {
        balanceOf[from] = balanceOf[from].sub(value);
        balanceOf[to] = balanceOf[to].add(value);
        emit Transfer(from, to, value);
    }

    function approve(address spender, uint value) external override returns (bool) {
        _approve(msg.sender, spender, value);
        return true;
    }

    function transfer(address to, uint value) external override returns (bool) {
        _transfer(msg.sender, to, value);
        return true;
    }

    function transferFrom(address from, address to, uint value) external override returns (bool) {
        if (allowance[from][msg.sender] != uint(- 1)) {
            allowance[from][msg.sender] = allowance[from][msg.sender].sub(value);
        }
        _transfer(from, to, value);
        return true;
    }
}

// An order can be compressed into 256 bits and saved using one SSTORE instruction
// The orders form a single-linked list. The preceding order points to the following order with nextID
struct Order { //total 256 bits
    address sender; //160 bits, sender creates this order
    uint32 price; // 32-bit decimal floating point number
    uint64 amount; // 42 bits are used, the stock amount to be sold or bought
    uint32 nextID; // 22 bits are used
}

// When the match engine of orderbook runs, it uses follow context to cache data in memory
struct Context {
    // this order is a limit order
    bool isLimitOrder;
    // the new order's id, it is only used when a limit order is not fully dealt
    uint32 newOrderID;
    // for buy-order, it's remained money amount; for sell-order, it's remained stock amount
    uint remainAmount;
    // it points to the first order in the opposite order book against current order
    uint32 firstID;
    // it points to the first order in the buy-order book
    uint32 firstBuyID;
    // it points to the first order in the sell-order book
    uint32 firstSellID;
    // the amount goes into the pool, for buy-order, it's money amount; for sell-order, it's stock amount
    uint amountIntoPool;
    // the total dealt money and stock in the order book
    uint dealMoneyInBook;
    uint dealStockInBook;
    // cache these values from storage to memory
    uint reserveMoney;
    uint reserveStock;
    uint bookedMoney;
    uint bookedStock;
    // reserveMoney or reserveStock is changed
    bool reserveChanged;
    // the taker has dealt in the orderbook
    bool hasDealtInOrderBook;
    // the current taker order
    Order order;
    // the following data come from proxy
    uint64 stockUnit;
    uint64 priceMul;
    uint64 priceDiv;
    address stockToken;
    address moneyToken;
    address graContract;
    address factory;
}

// GraSwapPair combines a Uniswap-like AMM and an orderbook
abstract contract GraSwapPool is GraSwapERC20, IGraSwapPool {
    using SafeMath256 for uint;

    uint private constant _MINIMUM_LIQUIDITY = 10 ** 3;
    bytes4 internal constant _SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)")));

    // reserveMoney and reserveStock are both uint112, id is 22 bits; they are compressed into a uint256 word
    uint internal _reserveStockAndMoneyAndFirstSellID;
    // bookedMoney and bookedStock are both uint112, id is 22 bits; they are compressed into a uint256 word
    uint internal _bookedStockAndMoneyAndFirstBuyID;

    uint private _kLast;

    uint32 private constant _OS = 2; // owner's share
    uint32 private constant _LS = 3; // liquidity-provider's share

    function internalStatus() external override view returns(uint[3] memory res) {
        res[0] = _reserveStockAndMoneyAndFirstSellID;
        res[1] = _bookedStockAndMoneyAndFirstBuyID;
        res[2] = _kLast;
    }

    function stock() external override returns (address) {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+0));
        return ProxyData.stock(proxyData);
    }

    function money() external override returns (address) {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+0));
        return ProxyData.money(proxyData);
    }

    // the following 4 functions load&store compressed storage
    function getReserves() public override view returns (uint112 reserveStock, uint112 reserveMoney, uint32 firstSellID) {
        uint temp = _reserveStockAndMoneyAndFirstSellID;
        reserveStock = uint112(temp);
        reserveMoney = uint112(temp>>112);
        firstSellID = uint32(temp>>224);
    }
    function _setReserves(uint stockAmount, uint moneyAmount, uint32 firstSellID) internal {
        require(stockAmount < uint(1<<112) && moneyAmount < uint(1<<112), "GraSwap: OVERFLOW");
        uint temp = (moneyAmount<<112)|stockAmount;
        emit Sync(temp);
        temp = (uint(firstSellID)<<224)| temp;
        _reserveStockAndMoneyAndFirstSellID = temp;
    }
    function getBooked() public override view returns (uint112 bookedStock, uint112 bookedMoney, uint32 firstBuyID) {
        uint temp = _bookedStockAndMoneyAndFirstBuyID;
        bookedStock = uint112(temp);
        bookedMoney = uint112(temp>>112);
        firstBuyID = uint32(temp>>224);
    }
    function _setBooked(uint stockAmount, uint moneyAmount, uint32 firstBuyID) internal {
        require(stockAmount < uint(1<<112) && moneyAmount < uint(1<<112), "GraSwap: OVERFLOW");
        _bookedStockAndMoneyAndFirstBuyID = (uint(firstBuyID)<<224)|(moneyAmount<<112)|stockAmount;
    }

    function _myBalance(address token) internal view returns (uint) {
        if(token==address(0)) {
            return address(this).balance;
        } else {
            return IERC20(token).balanceOf(address(this));
        }
    }

    // safely transfer ERC20 tokens, or ETH (when token==0)
    function _safeTransfer(address token, address to, uint value, address graContract) internal {
        if(value==0) {return;}
        if(token==address(0)) {
            // limit gas to 9000 to prevent gastoken attacks
            // solhint-disable-next-line avoid-low-level-calls 
            to.call{value: value, gas: 9000}(new bytes(0)); //we ignore its return value purposely
            return;
        }
        // solhint-disable-next-line avoid-low-level-calls 
        (bool success, bytes memory data) = token.call(abi.encodeWithSelector(_SELECTOR, to, value));
        success = success && (data.length == 0 || abi.decode(data, (bool)));
        if(!success) { // for failsafe
            address graContractOwner = IGraSwapToken(graContract).owner();
            // solhint-disable-next-line avoid-low-level-calls 
            (success, data) = token.call(abi.encodeWithSelector(_SELECTOR, graContractOwner, value));
            require(success && (data.length == 0 || abi.decode(data, (bool))), "GraSwap: TRANSFER_FAILED");
        }
    }

    // Give feeToAddresses some liquidity tokens if K got increased since last liquidity-changing
    function _mintFee(uint112 _reserve0, uint112 _reserve1, uint[5] memory proxyData) private returns (bool feeOn) {
        address feeTo_1 = IGraSwapFactory(ProxyData.factory(proxyData)).feeTo_1();
        address feeTo_2 = IGraSwapFactory(ProxyData.factory(proxyData)).feeTo_2();
        address feeToPrivate = IGraSwapFactory(ProxyData.factory(proxyData)).feeToPrivate();
        feeOn = (feeTo_1 != address(0) && feeTo_2 != address(0) && feeToPrivate != address(0));
        uint kLast = _kLast;
        // gas savings to use cached kLast
        if (feeOn) {
            if (kLast != 0) {
                uint rootK = Math.sqrt(uint(_reserve0).mul(_reserve1));
                uint rootKLast = Math.sqrt(kLast);
                if (rootK > rootKLast) {
                    uint numerator = totalSupply.mul(rootK.sub(rootKLast)).mul(_OS);
                    uint denominator = rootK.mul(_LS).add(rootKLast.mul(_OS));
                    uint liquidity = numerator / denominator;
                    if (liquidity > 0) {
                        uint liquidity_p1 = liquidity.div(4); // 10%  
                        uint liquidity_p2 = liquidity.div(8); // 5%
                        uint liquidity_p3 = liquidity.mul(5).div(8); // 25%
                        if (liquidity_p1 > 0) {
                            _mint(feeTo_1, liquidity_p1);
                        }
                        if (liquidity_p2 > 0) {
                            _mint(feeTo_2, liquidity_p2);
                        }
                        if (liquidity_p2 > 0) {
                            _mint(feeToPrivate, liquidity_p3);
                        }
                    }
                }
            }
        } else if (kLast != 0) {
            _kLast = 0;
        }
    }

    // mint new liquidity tokens to 'to'
    function mint(address to) external override lock returns (uint liquidity) {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+1));
        (uint112 reserveStock, uint112 reserveMoney, uint32 firstSellID) = getReserves();
        (uint112 bookedStock, uint112 bookedMoney, ) = getBooked();
        uint stockBalance = _myBalance(ProxyData.stock(proxyData));
        uint moneyBalance = _myBalance(ProxyData.money(proxyData));
        require(stockBalance >= uint(bookedStock) + uint(reserveStock) &&
                moneyBalance >= uint(bookedMoney) + uint(reserveMoney), "GraSwap: INVALID_BALANCE");
        stockBalance -= uint(bookedStock);
        moneyBalance -= uint(bookedMoney);
        uint stockAmount = stockBalance - uint(reserveStock);
        uint moneyAmount = moneyBalance - uint(reserveMoney);

        bool feeOn = _mintFee(reserveStock, reserveMoney, proxyData);
        uint _totalSupply = totalSupply;
        // gas savings by caching totalSupply in memory,
        // must be defined here since totalSupply can update in _mintFee
        if (_totalSupply == 0) {
            liquidity = Math.sqrt(stockAmount.mul(moneyAmount)).sub(_MINIMUM_LIQUIDITY);
            _mint(address(0), _MINIMUM_LIQUIDITY);
            // permanently lock the first _MINIMUM_LIQUIDITY tokens
        } else {
            liquidity = Math.min(stockAmount.mul(_totalSupply) / uint(reserveStock),
                                 moneyAmount.mul(_totalSupply) / uint(reserveMoney));
        }
        require(liquidity > 0, "GraSwap: INSUFFICIENT_MINTED");
        _mint(to, liquidity);

        _setReserves(stockBalance, moneyBalance, firstSellID);
        if (feeOn) _kLast = stockBalance.mul(moneyBalance);
        emit Mint(msg.sender, (moneyAmount<<112)|stockAmount, to);
    }

    // burn liquidity tokens and send stock&money to 'to'
    function burn(address to) external override lock returns (uint stockAmount, uint moneyAmount) {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+1));
        (uint112 reserveStock, uint112 reserveMoney, uint32 firstSellID) = getReserves();
        (uint bookedStock, uint bookedMoney, ) = getBooked();
        uint stockBalance = _myBalance(ProxyData.stock(proxyData)).sub(bookedStock);
        uint moneyBalance = _myBalance(ProxyData.money(proxyData)).sub(bookedMoney);
        require(stockBalance >= uint(reserveStock) && moneyBalance >= uint(reserveMoney), "GraSwap: INVALID_BALANCE");

        bool feeOn = _mintFee(reserveStock, reserveMoney, proxyData);
        {
            uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
            uint liquidity = balanceOf[address(this)]; // we're sure liquidity < totalSupply
            stockAmount = liquidity.mul(stockBalance) / _totalSupply;
            moneyAmount = liquidity.mul(moneyBalance) / _totalSupply;
            require(stockAmount > 0 && moneyAmount > 0, "GraSwap: INSUFFICIENT_BURNED");

            //_burn(address(this), liquidity);
            balanceOf[address(this)] = 0;
            totalSupply = totalSupply.sub(liquidity);
            emit Transfer(address(this), address(0), liquidity);
        }

        address graContract = ProxyData.graContract(proxyData);
        _safeTransfer(ProxyData.stock(proxyData), to, stockAmount, graContract);
        _safeTransfer(ProxyData.money(proxyData), to, moneyAmount, graContract);

        stockBalance = stockBalance - stockAmount;
        moneyBalance = moneyBalance - moneyAmount;

        _setReserves(stockBalance, moneyBalance, firstSellID);
        if (feeOn) _kLast = stockBalance.mul(moneyBalance);
        emit Burn(msg.sender, (moneyAmount<<112)|stockAmount, to);
    }

    // take the extra money&stock in this pair to 'to'
    function skim(address to) external override lock {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+1));
        address stockToken = ProxyData.stock(proxyData);
        address moneyToken = ProxyData.money(proxyData);
        (uint112 reserveStock, uint112 reserveMoney, ) = getReserves();
        (uint bookedStock, uint bookedMoney, ) = getBooked();
        uint balanceStock = _myBalance(stockToken);
        uint balanceMoney = _myBalance(moneyToken);
        require(balanceStock >= uint(bookedStock) + uint(reserveStock) &&
                balanceMoney >= uint(bookedMoney) + uint(reserveMoney), "GraSwap: INVALID_BALANCE");
        address graContract = ProxyData.graContract(proxyData);
        _safeTransfer(stockToken, to, balanceStock-reserveStock-bookedStock, graContract);
        _safeTransfer(moneyToken, to, balanceMoney-reserveMoney-bookedMoney, graContract);
    }

    // sync-up reserve stock&money in pool according to real balance
    function sync() external override lock {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+0));
        (, , uint32 firstSellID) = getReserves();
        (uint bookedStock, uint bookedMoney, ) = getBooked();
        uint balanceStock = _myBalance(ProxyData.stock(proxyData));
        uint balanceMoney = _myBalance(ProxyData.money(proxyData));
        require(balanceStock >= bookedStock && balanceMoney >= bookedMoney, "GraSwap: INVALID_BALANCE");
        _setReserves(balanceStock-bookedStock, balanceMoney-bookedMoney, firstSellID);
    }

}

contract GraSwapPair is GraSwapPool, IGraSwapPair {
    // the orderbooks. Gas is saved when using array to store them instead of mapping
    uint[1<<22] private _sellOrders;
    uint[1<<22] private _buyOrders;

    uint32 private constant _MAX_ID = (1<<22)-1; // the maximum value of an order ID

    function _expandPrice(uint32 price32, uint[5] memory proxyData) private pure returns (RatPrice memory price) {
        price = DecFloat32.expandPrice(price32);
        price.numerator *= ProxyData.priceMul(proxyData);
        price.denominator *= ProxyData.priceDiv(proxyData);
    }

    function _expandPrice(Context memory ctx, uint32 price32) private pure returns (RatPrice memory price) {
        price = DecFloat32.expandPrice(price32);
        price.numerator *= ctx.priceMul;
        price.denominator *= ctx.priceDiv;
    }

    function symbol() external override returns (string memory) {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+0));
        string memory s = "ETH";
        address stock = ProxyData.stock(proxyData);
        if(stock != address(0)) {
            s = IERC20(stock).symbol();
        }
        string memory m = "ETH";
        address money = ProxyData.money(proxyData);
        if(money != address(0)) {
            m = IERC20(money).symbol();
        }
        return string(abi.encodePacked(s, "/", m));  //to concat strings
    }

    // when emitting events, solidity's ABI pads each entry to uint256, which is so wasteful
    // we compress the entries into one uint256 to save gas
    function _emitNewLimitOrder(
        uint64 addressLow, /*255~192*/
        uint64 totalStockAmount, /*191~128*/
        uint64 remainedStockAmount, /*127~64*/
        uint32 price, /*63~32*/
        uint32 orderID, /*31~8*/
        bool isBuy /*7~0*/) private {
        uint data = uint(addressLow);
        data = (data<<64) | uint(totalStockAmount);
        data = (data<<64) | uint(remainedStockAmount);
        data = (data<<32) | uint(price);
        data = (data<<32) | uint(orderID<<8);
        if(isBuy) {
            data = data | 1;
        }
        emit NewLimitOrder(data);
    }
    function _emitNewMarketOrder(
        uint136 addressLow, /*255~120*/
        uint112 amount, /*119~8*/
        bool isBuy /*7~0*/) private {
        uint data = uint(addressLow);
        data = (data<<112) | uint(amount);
        data = data<<8;
        if(isBuy) {
            data = data | 1;
        }
        emit NewMarketOrder(data);
    }
    function _emitOrderChanged(
        uint64 makerLastAmount, /*159~96*/
        uint64 makerDealAmount, /*95~32*/
        uint32 makerOrderID, /*31~8*/
        bool isBuy /*7~0*/) private {
        uint data = uint(makerLastAmount);
        data = (data<<64) | uint(makerDealAmount);
        data = (data<<32) | uint(makerOrderID<<8);
        if(isBuy) {
            data = data | 1;
        }
        emit OrderChanged(data);
    }
    function _emitDealWithPool(
        uint112 inAmount, /*131~120*/
        uint112 outAmount,/*119~8*/
        bool isBuy/*7~0*/) private {
        uint data = uint(inAmount);
        data = (data<<112) | uint(outAmount);
        data = data<<8;
        if(isBuy) {
            data = data | 1;
        }
        emit DealWithPool(data);
    }
    function _emitRemoveOrder(
        uint64 remainStockAmount, /*95~32*/
        uint32 orderID, /*31~8*/
        bool isBuy /*7~0*/) private {
        uint data = uint(remainStockAmount);
        data = (data<<32) | uint(orderID<<8);
        if(isBuy) {
            data = data | 1;
        }
        emit RemoveOrder(data);
    }

    // compress an order into a 256b integer
    function _order2uint(Order memory order) internal pure returns (uint) {
        uint n = uint(order.sender);
        n = (n<<32) | order.price;
        n = (n<<42) | order.amount;
        n = (n<<22) | order.nextID;
        return n;
    }

    // extract an order from a 256b integer
    function _uint2order(uint n) internal pure returns (Order memory) {
        Order memory order;
        order.nextID = uint32(n & ((1<<22)-1));
        n = n >> 22;
        order.amount = uint64(n & ((1<<42)-1));
        n = n >> 42;
        order.price = uint32(n & ((1<<32)-1));
        n = n >> 32;
        order.sender = address(n);
        return order;
    }

    // returns true if this order exists
    function _hasOrder(bool isBuy, uint32 id) internal view returns (bool) {
        if(isBuy) {
            return _buyOrders[id] != 0;
        } else {
            return _sellOrders[id] != 0;
        }
    }

    // load an order from storage, converting its compressed form into an Order struct
    function _getOrder(bool isBuy, uint32 id) internal view returns (Order memory order, bool findIt) {
        if(isBuy) {
            order = _uint2order(_buyOrders[id]);
            return (order, order.price != 0);
        } else {
            order = _uint2order(_sellOrders[id]);
            return (order, order.price != 0);
        }
    }

    // save an order to storage, converting it into compressed form
    function _setOrder(bool isBuy, uint32 id, Order memory order) internal {
        if(isBuy) {
            _buyOrders[id] = _order2uint(order);
        } else {
            _sellOrders[id] = _order2uint(order);
        }
    }

    // delete an order from storage
    function _deleteOrder(bool isBuy, uint32 id) internal {
        if(isBuy) {
            delete _buyOrders[id];
        } else {
            delete _sellOrders[id];
        }
    }

    function _getFirstOrderID(Context memory ctx, bool isBuy) internal pure returns (uint32) {
        if(isBuy) {
            return ctx.firstBuyID;
        }
        return ctx.firstSellID;
    }

    function _setFirstOrderID(Context memory ctx, bool isBuy, uint32 id) internal pure {
        if(isBuy) {
            ctx.firstBuyID = id;
        } else {
            ctx.firstSellID = id;
        }
    }

    function removeOrders(uint[] calldata rmList) external override lock {
        uint[5] memory proxyData;
        uint expectedCallDataSize = 4+32*(ProxyData.COUNT+2+rmList.length);
        ProxyData.fill(proxyData, expectedCallDataSize);
        for(uint i = 0; i < rmList.length; i++) {
            uint rmInfo = rmList[i];
            bool isBuy = uint8(rmInfo) != 0;
            uint32 id = uint32(rmInfo>>8);
            uint72 prevKey = uint72(rmInfo>>40);
            _removeOrder(isBuy, id, prevKey, proxyData);
        }
    }

    function removeOrder(bool isBuy, uint32 id, uint72 prevKey) external override lock {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+3));
        _removeOrder(isBuy, id, prevKey, proxyData);
    }

    function _removeOrder(bool isBuy, uint32 id, uint72 prevKey, uint[5] memory proxyData) private {
        Context memory ctx;
        (ctx.bookedStock, ctx.bookedMoney, ctx.firstBuyID) = getBooked();
        if(!isBuy) {
            (ctx.reserveStock, ctx.reserveMoney, ctx.firstSellID) = getReserves();
        }
        Order memory order = _removeOrderFromBook(ctx, isBuy, id, prevKey); // this is the removed order
        require(msg.sender == order.sender, "GraSwap: NOT_OWNER");
        uint64 stockUnit = ProxyData.stockUnit(proxyData);
        uint stockAmount = uint(order.amount)/*42bits*/ * uint(stockUnit);
        address graContract = ProxyData.graContract(proxyData);
        if(isBuy) {
            RatPrice memory price = _expandPrice(order.price, proxyData);
            uint moneyAmount = stockAmount * price.numerator/*54+64bits*/ / price.denominator;
            ctx.bookedMoney -= moneyAmount;
            _safeTransfer(ProxyData.money(proxyData), order.sender, moneyAmount, graContract);
        } else {
            ctx.bookedStock -= stockAmount;
            _safeTransfer(ProxyData.stock(proxyData), order.sender, stockAmount, graContract);
        }
        _setBooked(ctx.bookedStock, ctx.bookedMoney, ctx.firstBuyID);
    }

    // remove an order from orderbook and return it
    function _removeOrderFromBook(Context memory ctx, bool isBuy,
                                 uint32 id, uint72 prevKey) internal returns (Order memory) {
        (Order memory order, bool ok) = _getOrder(isBuy, id);
        require(ok, "GraSwap: NO_SUCH_ORDER");
        if(prevKey == 0) {
            uint32 firstID = _getFirstOrderID(ctx, isBuy);
            require(id == firstID, "GraSwap: NOT_FIRST");
            _setFirstOrderID(ctx, isBuy, order.nextID);
            if(!isBuy) {
                _setReserves(ctx.reserveStock, ctx.reserveMoney, ctx.firstSellID);
            }
        } else {
            (uint32 currID, Order memory prevOrder, bool findIt) = _getOrder3Times(isBuy, prevKey);
            require(findIt, "GraSwap: INVALID_POSITION");
            while(prevOrder.nextID != id) {
                currID = prevOrder.nextID;
                require(currID != 0, "GraSwap: REACH_END");
                (prevOrder, ) = _getOrder(isBuy, currID);
            }
            prevOrder.nextID = order.nextID;
            _setOrder(isBuy, currID, prevOrder);
        }
        _emitRemoveOrder(order.amount, id, isBuy);
        _deleteOrder(isBuy, id);
        return order;
    }

    // insert an order at the head of single-linked list
    // this function does not check price, use it carefully
    function _insertOrderAtHead(Context memory ctx, bool isBuy, Order memory order, uint32 id) private {
        order.nextID = _getFirstOrderID(ctx, isBuy);
        _setOrder(isBuy, id, order);
        _setFirstOrderID(ctx, isBuy, id);
    }

    // prevKey contains 3 orders. try to get the first existing order
    function _getOrder3Times(bool isBuy, uint72 prevKey) private view returns (
        uint32 currID, Order memory prevOrder, bool findIt) {
        currID = uint32(prevKey&_MAX_ID);
        (prevOrder, findIt) = _getOrder(isBuy, currID);
        if(!findIt) {
            currID = uint32((prevKey>>24)&_MAX_ID);
            (prevOrder, findIt) = _getOrder(isBuy, currID);
            if(!findIt) {
                currID = uint32((prevKey>>48)&_MAX_ID);
                (prevOrder, findIt) = _getOrder(isBuy, currID);
            }
        }
    }

    // Given a valid start position, find a proper position to insert order
    // prevKey contains three suggested order IDs, each takes 24 bits.
    // We try them one by one to find a valid start position
    // can not use this function to insert at head! if prevKey is all zero, it will return false
    function _insertOrderFromGivenPos(bool isBuy, Order memory order,
                                     uint32 id, uint72 prevKey) private returns (bool inserted) {
        (uint32 currID, Order memory prevOrder, bool findIt) = _getOrder3Times(isBuy, prevKey);
        if(!findIt) {
            return false;
        }
        return _insertOrder(isBuy, order, prevOrder, id, currID);
    }
    
    // Starting from the head of orderbook, find a proper position to insert order
    function _insertOrderFromHead(Context memory ctx, bool isBuy, Order memory order,
                                 uint32 id) private returns (bool inserted) {
        uint32 firstID = _getFirstOrderID(ctx, isBuy);
        bool canBeFirst = (firstID == 0);
        Order memory firstOrder;
        if(!canBeFirst) {
            (firstOrder, ) = _getOrder(isBuy, firstID);
            canBeFirst = (isBuy && (firstOrder.price < order.price)) ||
                (!isBuy && (firstOrder.price > order.price));
        }
        if(canBeFirst) {
            order.nextID = firstID;
            _setOrder(isBuy, id, order);
            _setFirstOrderID(ctx, isBuy, id);
            return true;
        }
        return _insertOrder(isBuy, order, firstOrder, id, firstID);
    }

    // starting from 'prevOrder', whose id is 'currID', find a proper position to insert order
    function _insertOrder(bool isBuy, Order memory order, Order memory prevOrder,
                         uint32 id, uint32 currID) private returns (bool inserted) {
        while(currID != 0) {
            bool canFollow = (isBuy && (order.price <= prevOrder.price)) ||
                (!isBuy && (order.price >= prevOrder.price));
            if(!canFollow) {break;} 
            Order memory nextOrder;
            if(prevOrder.nextID != 0) {
                (nextOrder, ) = _getOrder(isBuy, prevOrder.nextID);
                bool canPrecede = (isBuy && (nextOrder.price < order.price)) ||
                    (!isBuy && (nextOrder.price > order.price));
                canFollow = canFollow && canPrecede;
            }
            if(canFollow) {
                order.nextID = prevOrder.nextID;
                _setOrder(isBuy, id, order);
                prevOrder.nextID = id;
                _setOrder(isBuy, currID, prevOrder);
                return true;
            }
            currID = prevOrder.nextID;
            prevOrder = nextOrder;
        }
        return false;
    }

    // to query the first sell price, the first buy price and the price of pool
    function getPrices() external override returns (
        uint firstSellPriceNumerator,
        uint firstSellPriceDenominator,
        uint firstBuyPriceNumerator,
        uint firstBuyPriceDenominator,
        uint poolPriceNumerator,
        uint poolPriceDenominator) {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+0));
        (uint112 reserveStock, uint112 reserveMoney, uint32 firstSellID) = getReserves();
        poolPriceNumerator = uint(reserveMoney);
        poolPriceDenominator = uint(reserveStock);
        firstSellPriceNumerator = 0;
        firstSellPriceDenominator = 0;
        firstBuyPriceNumerator = 0;
        firstBuyPriceDenominator = 0;
        if(firstSellID!=0) {
            uint order = _sellOrders[firstSellID];
            RatPrice memory price = _expandPrice(uint32(order>>64), proxyData);
            firstSellPriceNumerator = price.numerator;
            firstSellPriceDenominator = price.denominator;
        }
        uint32 id = uint32(_bookedStockAndMoneyAndFirstBuyID>>224);
        if(id!=0) {
            uint order = _buyOrders[id];
            RatPrice memory price = _expandPrice(uint32(order>>64), proxyData);
            firstBuyPriceNumerator = price.numerator;
            firstBuyPriceDenominator = price.denominator;
        }
    }

    // Get the orderbook's content, starting from id, to get no more than maxCount orders
    function getOrderList(bool isBuy, uint32 id, uint32 maxCount) external override view returns (uint[] memory) {
        if(id == 0) {
            if(isBuy) {
                id = uint32(_bookedStockAndMoneyAndFirstBuyID>>224);
            } else {
                id = uint32(_reserveStockAndMoneyAndFirstSellID>>224);
            }
        }
        uint[1<<22] storage orderbook;
        if(isBuy) {
            orderbook = _buyOrders;
        } else {
            orderbook = _sellOrders;
        }
        //record block height at the first entry
        uint order = (block.number<<24) | id;
        uint addrOrig; // start of returned data
        uint addrLen; // the slice's length is written at this address
        uint addrStart; // the address of the first entry of returned slice
        uint addrEnd; // ending address to write the next order
        uint count = 0; // the slice's length
        // solhint-disable-next-line no-inline-assembly
        assembly {
            addrOrig := mload(0x40) // There is a “free memory pointer” at address 0x40 in memory
            mstore(addrOrig, 32) //the meaningful data start after offset 32
        }
        addrLen = addrOrig + 32;
        addrStart = addrLen + 32;
        addrEnd = addrStart;
        while(count < maxCount) {
            // solhint-disable-next-line no-inline-assembly
            assembly {
                mstore(addrEnd, order) //write the order
            }
            addrEnd += 32;
            count++;
            if(id == 0) {break;}
            order = orderbook[id];
            require(order!=0, "GraSwap: INCONSISTENT_BOOK");
            id = uint32(order&_MAX_ID);
        }
        // solhint-disable-next-line no-inline-assembly
        assembly {
            mstore(addrLen, count) // record the returned slice's length
            let byteCount := sub(addrEnd, addrOrig)
            return(addrOrig, byteCount)
        }
    }

    // Get an unused id to be used with new order
    function _getUnusedOrderID(bool isBuy, uint32 id) internal view returns (uint32) {
        if(id == 0) { // 0 is reserved
            // solhint-disable-next-line avoid-tx-origin
            id = uint32(uint(blockhash(block.number-1))^uint(tx.origin)) & _MAX_ID; //get a pseudo random number
        }
        for(uint32 i = 0; i < 100 && id <= _MAX_ID; i++) { //try 100 times
            if(!_hasOrder(isBuy, id)) {
                return id;
            }
            id++;
        }
        require(false, "GraSwap: CANNOT_FIND_VALID_ID");
        return 0;
    }

    function calcStockAndMoney(uint64 amount, uint32 price32) external pure override returns (uint stockAmount, uint moneyAmount) {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+2));
        (stockAmount, moneyAmount, ) = _calcStockAndMoney(amount, price32, proxyData);
    }

    function _calcStockAndMoney(uint64 amount, uint32 price32, uint[5] memory proxyData) private pure returns (uint stockAmount, uint moneyAmount, RatPrice memory price) {
        price = _expandPrice(price32, proxyData);
        uint64 stockUnit = ProxyData.stockUnit(proxyData);
        stockAmount = uint(amount)/*42bits*/ * uint(stockUnit);
        moneyAmount = stockAmount * price.numerator/*54+64bits*/ /price.denominator;
    }

    function addLimitOrder(bool isBuy, address sender, uint64 amount, uint32 price32,
                           uint32 id, uint72 prevKey) external payable override lock {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+6));
        require(ProxyData.isOnlySwap(proxyData)==false, "GraSwap: LIMIT_ORDER_NOT_SUPPORTED");
        Context memory ctx;
        ctx.stockUnit = ProxyData.stockUnit(proxyData);
        ctx.graContract = ProxyData.graContract(proxyData);
        ctx.factory = ProxyData.factory(proxyData);
        ctx.stockToken = ProxyData.stock(proxyData);
        ctx.moneyToken = ProxyData.money(proxyData);
        ctx.priceMul = ProxyData.priceMul(proxyData);
        ctx.priceDiv = ProxyData.priceDiv(proxyData);
        ctx.hasDealtInOrderBook = false;
        ctx.isLimitOrder = true;
        ctx.order.sender = sender;
        ctx.order.amount = amount;
        ctx.order.price = price32;

        ctx.newOrderID = _getUnusedOrderID(isBuy, id);
        RatPrice memory price;
    
        {// to prevent "CompilerError: Stack too deep, try removing local variables."
            require((amount >> 42) == 0, "GraSwap: INVALID_AMOUNT");
            uint32 m = price32 & DecFloat32.MANTISSA_MASK;
            require(DecFloat32.MIN_MANTISSA <= m && m <= DecFloat32.MAX_MANTISSA, "GraSwap: INVALID_PRICE");

            uint stockAmount;
            uint moneyAmount;
            (stockAmount, moneyAmount, price) = _calcStockAndMoney(amount, price32, proxyData);
            if(isBuy) {
                ctx.remainAmount = moneyAmount;
            } else {
                ctx.remainAmount = stockAmount;
            }
        }

        require(ctx.remainAmount < uint(1<<112), "GraSwap: OVERFLOW");
        (ctx.reserveStock, ctx.reserveMoney, ctx.firstSellID) = getReserves();
        (ctx.bookedStock, ctx.bookedMoney, ctx.firstBuyID) = getBooked();
        _checkRemainAmount(ctx, isBuy);
        if(prevKey != 0) { // try to insert it
            bool inserted = _insertOrderFromGivenPos(isBuy, ctx.order, ctx.newOrderID, prevKey);
            if(inserted) { //  if inserted successfully, record the booked tokens
                _emitNewLimitOrder(uint64(ctx.order.sender), amount, amount, price32, ctx.newOrderID, isBuy);
                if(isBuy) {
                    ctx.bookedMoney += ctx.remainAmount;
                } else {
                    ctx.bookedStock += ctx.remainAmount;
                }
                _setBooked(ctx.bookedStock, ctx.bookedMoney, ctx.firstBuyID);
                if(ctx.reserveChanged) {
                    _setReserves(ctx.reserveStock, ctx.reserveMoney, ctx.firstSellID);
                }
                return;
            }
            // if insertion failed, we try to match this order and make it deal
        }
        _addOrder(ctx, isBuy, price);
    }

    function addMarketOrder(address inputToken, address sender,
                            uint112 inAmount) external payable override lock returns (uint) {
        uint[5] memory proxyData;
        ProxyData.fill(proxyData, 4+32*(ProxyData.COUNT+3));
        Context memory ctx;
        ctx.moneyToken = ProxyData.money(proxyData);
        ctx.stockToken = ProxyData.stock(proxyData);
        require(inputToken == ctx.moneyToken || inputToken == ctx.stockToken, "GraSwap: INVALID_TOKEN");
        bool isBuy = inputToken == ctx.moneyToken;
        ctx.stockUnit = ProxyData.stockUnit(proxyData);
        ctx.priceMul = ProxyData.priceMul(proxyData);
        ctx.priceDiv = ProxyData.priceDiv(proxyData);
        ctx.graContract = ProxyData.graContract(proxyData);
        ctx.factory = ProxyData.factory(proxyData);
        ctx.hasDealtInOrderBook = false;
        ctx.isLimitOrder = false;
        ctx.remainAmount = inAmount;
        (ctx.reserveStock, ctx.reserveMoney, ctx.firstSellID) = getReserves();
        (ctx.bookedStock, ctx.bookedMoney, ctx.firstBuyID) = getBooked();
        _checkRemainAmount(ctx, isBuy);
        ctx.order.sender = sender;
        if(isBuy) {
            ctx.order.price = DecFloat32.MAX_PRICE;
        } else {
            ctx.order.price = DecFloat32.MIN_PRICE;
        }

        RatPrice memory price; // leave it to zero, actually it will not be used;
        _emitNewMarketOrder(uint136(ctx.order.sender), inAmount, isBuy);
        return _addOrder(ctx, isBuy, price);
    }

    // Check router contract did send me enough tokens.
    // If Router sent to much tokens, take them as reserve money&stock
    function _checkRemainAmount(Context memory ctx, bool isBuy) private view {
        ctx.reserveChanged = false;
        uint diff;
        if(isBuy) {
            uint balance = _myBalance(ctx.moneyToken);
            require(balance >= ctx.bookedMoney + ctx.reserveMoney, "GraSwap: MONEY_MISMATCH");
            diff = balance - ctx.bookedMoney - ctx.reserveMoney;
            if(ctx.remainAmount < diff) {
                ctx.reserveMoney += (diff - ctx.remainAmount);
                ctx.reserveChanged = true;
            }
        } else {
            uint balance = _myBalance(ctx.stockToken);
            require(balance >= ctx.bookedStock + ctx.reserveStock, "GraSwap: STOCK_MISMATCH");
            diff = balance - ctx.bookedStock - ctx.reserveStock;
            if(ctx.remainAmount < diff) {
                ctx.reserveStock += (diff - ctx.remainAmount);
                ctx.reserveChanged = true;
            }
        }
        require(ctx.remainAmount <= diff, "GraSwap: DEPOSIT_NOT_ENOUGH");
    }

    // internal helper function to add new limit order & market order
    // returns the amount of tokens which were sent to the taker (from AMM pool and booked tokens)
    function _addOrder(Context memory ctx, bool isBuy, RatPrice memory price) private returns (uint) {
        (ctx.dealMoneyInBook, ctx.dealStockInBook) = (0, 0);
        ctx.firstID = _getFirstOrderID(ctx, !isBuy);
        uint32 currID = ctx.firstID;
        ctx.amountIntoPool = 0;
        while(currID != 0) { // while not reaching the end of single-linked 
            (Order memory orderInBook, ) = _getOrder(!isBuy, currID);
            bool canDealInOrderBook = (isBuy && (orderInBook.price <= ctx.order.price)) ||
                (!isBuy && (orderInBook.price >= ctx.order.price));
            if(!canDealInOrderBook) {break;} // no proper price in orderbook, stop here

            // Deal in liquid pool
            RatPrice memory priceInBook = _expandPrice(ctx, orderInBook.price);
            bool allDeal = _tryDealInPool(ctx, isBuy, priceInBook);
            if(allDeal) {break;}

            // Deal in orderbook
            _dealInOrderBook(ctx, isBuy, currID, orderInBook, priceInBook);

            // if the order in book did NOT fully deal, then this new order DID fully deal, so stop here
            if(orderInBook.amount != 0) {
                _setOrder(!isBuy, currID, orderInBook);
                break;
            }
            // if the order in book DID fully deal, then delete this order from storage and move to the next
            _deleteOrder(!isBuy, currID);
            currID = orderInBook.nextID;
        }
        // Deal in liquid pool
        if(ctx.isLimitOrder) {
            // use current order's price to deal with pool
            _tryDealInPool(ctx, isBuy, price);
            // If a limit order did NOT fully deal, we add it into orderbook
            // Please note a market order always fully deals
            _insertOrderToBook(ctx, isBuy, price);
        } else {
            // the AMM pool can deal with orders with any amount
            ctx.amountIntoPool += ctx.remainAmount; // both of them are less than 112 bits
            ctx.remainAmount = 0;
        }
        uint amountToTaker = _dealWithPoolAndCollectFee(ctx, isBuy);
        if(isBuy) {
            ctx.bookedStock -= ctx.dealStockInBook; //If this subtraction overflows, _setBooked will fail
        } else {
            ctx.bookedMoney -= ctx.dealMoneyInBook; //If this subtraction overflows, _setBooked will fail
        }
        if(ctx.firstID != currID) { //some orders DID fully deal, so the head of single-linked list change
            _setFirstOrderID(ctx, !isBuy, currID);
        }
        // write the cached values to storage
        _setBooked(ctx.bookedStock, ctx.bookedMoney, ctx.firstBuyID);
        _setReserves(ctx.reserveStock, ctx.reserveMoney, ctx.firstSellID);
        return amountToTaker;
    }

    // Given reserveMoney and reserveStock in AMM pool, calculate how much tokens will go into the pool if the
    // final price is 'price'
    function _intopoolAmountTillPrice(bool isBuy, uint reserveMoney, uint reserveStock,
                                     RatPrice memory price) private pure returns (uint result) {
        // sqrt(Pold/Pnew) = sqrt((2**32)*M_old*PnewDenominator / (S_old*PnewNumerator)) / (2**16)
        // sell, stock-into-pool, Pold > Pnew
        uint numerator = reserveMoney/*112bits*/ * price.denominator/*76+64bits*/;
        uint denominator = reserveStock/*112bits*/ * price.numerator/*54+64bits*/;
        if(isBuy) { // buy, money-into-pool, Pold < Pnew
            // sqrt(Pnew/Pold) = sqrt((2**32)*S_old*PnewNumerator / (M_old*PnewDenominator)) / (2**16)
            (numerator, denominator) = (denominator, numerator);
        }
        while(numerator >= (1<<192)) { // can not equal to (1<<192) !!!
            numerator >>= 16;
            denominator >>= 16;
        }
        require(denominator != 0, "GraSwapPair: DIV_BY_ZERO");
        numerator = numerator * (1<<64);
        uint quotient = numerator / denominator;
        if(quotient <= (1<<64)) {
            return 0;
        } else if(quotient <= ((1<<64)*5/4)) {
            // Taylor expansion: x/2 - x*x/8 + x*x*x/16
            uint x = quotient - (1<<64);
            uint y = x*x;
            y = x/2 - y/(8*(1<<64)) + y*x/(16*(1<<128));
            if(isBuy) {
                result = reserveMoney * y;
            } else {
                result = reserveStock * y;
            }
            result /= (1<<64);
            return result;
        }
        uint root = Math.sqrt(quotient); //root is at most 110bits
        uint diff =  root - (1<<32);  //at most 110bits
        if(isBuy) {
            result = reserveMoney * diff;
        } else {
            result = reserveStock * diff;
        }
        result /= (1<<32);
        return result;
    }

    // Current order tries to deal against the AMM pool. Returns whether current order fully deals.
    function _tryDealInPool(Context memory ctx, bool isBuy, RatPrice memory price) private pure returns (bool) {
        uint currTokenCanTrade = _intopoolAmountTillPrice(isBuy, ctx.reserveMoney, ctx.reserveStock, price);
        require(currTokenCanTrade < uint(1<<112), "GraSwap: CURR_TOKEN_TOO_LARGE");
        // all the below variables are less than 112 bits
        if(!isBuy) {
            currTokenCanTrade /= ctx.stockUnit; //to round
            currTokenCanTrade *= ctx.stockUnit;
        }
        if(currTokenCanTrade > ctx.amountIntoPool) {
            uint diffTokenCanTrade = currTokenCanTrade - ctx.amountIntoPool;
            bool allDeal = diffTokenCanTrade >= ctx.remainAmount;
            if(allDeal) {
                diffTokenCanTrade = ctx.remainAmount;
            }
            ctx.amountIntoPool += diffTokenCanTrade;
            ctx.remainAmount -= diffTokenCanTrade;
            return allDeal;
        }
        return false;
    }

    // Current order tries to deal against the orders in book
    function _dealInOrderBook(Context memory ctx, bool isBuy, uint32 currID,
                             Order memory orderInBook, RatPrice memory priceInBook) internal {
        ctx.hasDealtInOrderBook = true;
        uint stockAmount;
        if(isBuy) {
            uint a = ctx.remainAmount/*112bits*/ * priceInBook.denominator/*76+64bits*/;
            uint b = priceInBook.numerator/*54+64bits*/ * ctx.stockUnit/*64bits*/;
            stockAmount = a/b;
        } else {
            stockAmount = ctx.remainAmount/ctx.stockUnit;
        }
        if(uint(orderInBook.amount) < stockAmount) {
            stockAmount = uint(orderInBook.amount);
        }
        require(stockAmount < (1<<42), "GraSwap: STOCK_TOO_LARGE");
        uint stockTrans = stockAmount/*42bits*/ * ctx.stockUnit/*64bits*/;
        uint moneyTrans = stockTrans * priceInBook.numerator/*54+64bits*/ / priceInBook.denominator/*76+64bits*/;

        _emitOrderChanged(orderInBook.amount, uint64(stockAmount), currID, isBuy);
        orderInBook.amount -= uint64(stockAmount);
        if(isBuy) { //subtraction cannot overflow: moneyTrans and stockTrans are calculated from remainAmount
            ctx.remainAmount -= moneyTrans;
        } else {
            ctx.remainAmount -= stockTrans;
        }
        // following accumulations can not overflow, because stockTrans(moneyTrans) at most 106bits(160bits)
        // we know for sure that dealStockInBook and dealMoneyInBook are less than 192 bits
        ctx.dealStockInBook += stockTrans;
        ctx.dealMoneyInBook += moneyTrans;
        if(isBuy) {
            _safeTransfer(ctx.moneyToken, orderInBook.sender, moneyTrans, ctx.graContract);
        } else {
            _safeTransfer(ctx.stockToken, orderInBook.sender, stockTrans, ctx.graContract);
        }
    }

    // make real deal with the pool and then collect fee, which will be added to AMM pool
    function _dealWithPoolAndCollectFee(Context memory ctx, bool isBuy) internal returns (uint) {
        (uint outpoolTokenReserve, uint inpoolTokenReserve, uint otherToTaker) = (
              ctx.reserveMoney, ctx.reserveStock, ctx.dealMoneyInBook);
        if(isBuy) {
            (outpoolTokenReserve, inpoolTokenReserve, otherToTaker) = (
                ctx.reserveStock, ctx.reserveMoney, ctx.dealStockInBook);
        }

        // all these 4 varialbes are less than 112 bits
        // outAmount is sure to less than outpoolTokenReserve (which is ctx.reserveStock or ctx.reserveMoney)
        uint outAmount = (outpoolTokenReserve*ctx.amountIntoPool)/(inpoolTokenReserve+ctx.amountIntoPool);
        if(ctx.amountIntoPool > 0) {
            _emitDealWithPool(uint112(ctx.amountIntoPool), uint112(outAmount), isBuy);
        }
        uint32 feeBPS = IGraSwapFactory(ctx.factory).feeBPS();
        // the token amount that should go to the taker, 
        // for buy-order, it's stock amount; for sell-order, it's money amount
        uint amountToTaker = outAmount + otherToTaker;
        require(amountToTaker < uint(1<<112), "GraSwap: AMOUNT_TOO_LARGE");
        uint fee = (amountToTaker * feeBPS + 9999) / 10000;
        amountToTaker -= fee;

        if(isBuy) {
            ctx.reserveMoney = ctx.reserveMoney + ctx.amountIntoPool;
            ctx.reserveStock = ctx.reserveStock - outAmount + fee;
        } else {
            ctx.reserveMoney = ctx.reserveMoney - outAmount + fee;
            ctx.reserveStock = ctx.reserveStock + ctx.amountIntoPool;
        }

        address token = ctx.moneyToken;
        if(isBuy) {
            token = ctx.stockToken;
        }
        _safeTransfer(token, ctx.order.sender, amountToTaker, ctx.graContract);
        return amountToTaker;
    }

    // Insert a not-fully-deal limit order into orderbook
    function _insertOrderToBook(Context memory ctx, bool isBuy, RatPrice memory price) internal {
        (uint smallAmount, uint moneyAmount, uint stockAmount) = (0, 0, 0);
        if(isBuy) {
            uint tempAmount1 = ctx.remainAmount /*112bits*/ * price.denominator /*76+64bits*/;
            uint temp = ctx.stockUnit * price.numerator/*54+64bits*/;
            stockAmount = tempAmount1 / temp;
            uint tempAmount2 = stockAmount * temp; // Now tempAmount1 >= tempAmount2
            moneyAmount = (tempAmount2+price.denominator-1)/price.denominator; // round up
            if(ctx.remainAmount > moneyAmount) {
                // smallAmount is the gap where remainAmount can not buy an integer of stocks
                smallAmount = ctx.remainAmount - moneyAmount;
            } else {
                moneyAmount = ctx.remainAmount;
            } //Now ctx.remainAmount >= moneyAmount
        } else {
            // for sell orders, remainAmount were always decreased by integral multiple of StockUnit
            // and we know for sure that ctx.remainAmount % StockUnit == 0
            stockAmount = ctx.remainAmount / ctx.stockUnit;
            smallAmount = ctx.remainAmount - stockAmount * ctx.stockUnit;
        }
        ctx.amountIntoPool += smallAmount; // Deal smallAmount with pool
        //ctx.reserveMoney += smallAmount; // If this addition overflows, _setReserves will fail
        _emitNewLimitOrder(uint64(ctx.order.sender), ctx.order.amount, uint64(stockAmount),
                           ctx.order.price, ctx.newOrderID, isBuy);
        if(stockAmount != 0) {
            ctx.order.amount = uint64(stockAmount);
            if(ctx.hasDealtInOrderBook) {
                // if current order has ever dealt, it has the best price and can be inserted at head
                _insertOrderAtHead(ctx, isBuy, ctx.order, ctx.newOrderID);
            } else {
                // if current order has NEVER dealt, we must find a proper position for it.
                // we may scan a lot of entries in the single-linked list and run out of gas
                _insertOrderFromHead(ctx, isBuy, ctx.order, ctx.newOrderID);
            }
        }
        // Any overflow/underflow in following calculation will be caught by _setBooked
        if(isBuy) {
            ctx.bookedMoney += moneyAmount;
        } else {
            ctx.bookedStock += (ctx.remainAmount - smallAmount);
        }
    }
}

// solhint-disable-next-line max-states-count
contract GraSwapPairProxy {
    uint internal _unusedVar0;
    uint internal _unusedVar1;
    uint internal _unusedVar2;
    uint internal _unusedVar3;
    uint internal _unusedVar4;
    uint internal _unusedVar5;
    uint internal _unusedVar6;
    uint internal _unusedVar7;
    uint internal _unusedVar8;
    uint internal _unusedVar9;
    uint internal _unlocked;

    uint internal immutable _immuFactory;
    uint internal immutable _immuMoneyToken;
    uint internal immutable _immuStockToken;
    uint internal immutable _immuGras;
    uint internal immutable _immuOther;

    constructor(address stockToken, address moneyToken, bool isOnlySwap, uint64 stockUnit, uint64 priceMul, uint64 priceDiv, address graContract) public {
        _immuFactory = uint(msg.sender);
        _immuMoneyToken = uint(moneyToken);
        _immuStockToken = uint(stockToken);
        _immuGras = uint(graContract);
        uint temp = 0;
        if(isOnlySwap) {
            temp = 1;
        }
        temp = (temp<<64) | stockUnit;
        temp = (temp<<64) | priceMul;
        temp = (temp<<64) | priceDiv;
        _immuOther = temp;
        _unlocked = 1;
    }

    receive() external payable { }
    // solhint-disable-next-line no-complex-fallback
    fallback() payable external {
        uint factory     = _immuFactory;
        uint moneyToken  = _immuMoneyToken;
        uint stockToken  = _immuStockToken;
        uint graContract        = _immuGras;
        uint other       = _immuOther;
        address impl = IGraSwapFactory(address(_immuFactory)).pairLogic();
        // solhint-disable-next-line no-inline-assembly
        assembly {
            let ptr := mload(0x40)
            let size := calldatasize()
            calldatacopy(ptr, 0, size)
            let end := add(ptr, size)
            // append immutable variables to the end of calldata
            mstore(end, factory)
            end := add(end, 32)
            mstore(end, moneyToken)
            end := add(end, 32)
            mstore(end, stockToken)
            end := add(end, 32)
            mstore(end, graContract)
            end := add(end, 32)
            mstore(end, other)
            size := add(size, 160)
            let result := delegatecall(gas(), impl, ptr, size, 0, 0)
            size := returndatasize()
            returndatacopy(ptr, 0, size)

            switch result
            case 0 { revert(ptr, size) }
            default { return(ptr, size) }
        }
    }
}


contract GraSwapFactory is IGraSwapFactory {
    struct TokensInPair {
        address stock;
        address money;
    }

    address public override feeTo_1;
    address public override feeTo_2;
    address public override feeToPrivate;
    address public override feeToSetter;
    address public immutable gov;
    address public immutable graContract;
    uint32 public override feeBPS = 40;
    address public override pairLogic;
    mapping(address => TokensInPair) private _pairWithToken;
    mapping(bytes32 => address) private _tokensToPair;
    address[] public allPairs;
    IPairFeeDistribution pfd; // pairFeeDistribution

    constructor(address _feeToSetter, address _gov, address _graContract, address _pairLogic, address _distribution) public {
        feeToSetter = _feeToSetter;
        gov = _gov;
        graContract = _graContract;
        pairLogic = _pairLogic;
        pfd = IPairFeeDistribution(_distribution);
    }

    function createPair(address stock, address money, bool isOnlySwap) external override returns (address pair) {
        require(stock != money, "GraSwapFactory: IDENTICAL_ADDRESSES");
        // not necessary //require(stock != address(0) || money != address(0), "GraSwapFactory: ZERO_ADDRESS");
        uint moneyDec = _getDecimals(money);
        uint stockDec = _getDecimals(stock);
        require(23 >= stockDec && stockDec >= 0, "GraSwapFactory: STOCK_DECIMALS_NOT_SUPPORTED");
        uint dec = 0;
        if (stockDec >= 4) {
            dec = stockDec - 4; // now 19 >= dec && dec >= 0
        }
        // 10**19 = 10000000000000000000
        //  1<<64 = 18446744073709551616
        uint64 priceMul = 1;
        uint64 priceDiv = 1;
        bool differenceTooLarge = false;
        if (moneyDec > stockDec) {
            if (moneyDec > stockDec + 19) {
                differenceTooLarge = true;
            } else {
                priceMul = uint64(uint(10)**(moneyDec - stockDec));
            }
        }
        if (stockDec > moneyDec) {
            if (stockDec > moneyDec + 19) {
                differenceTooLarge = true;
            } else {
                priceDiv = uint64(uint(10)**(stockDec - moneyDec));
            }
        }
        require(!differenceTooLarge, "GraSwapFactory: DECIMALS_DIFF_TOO_LARGE");
        bytes32 salt = keccak256(abi.encodePacked(stock, money, isOnlySwap));
        require(_tokensToPair[salt] == address(0), "GraSwapFactory: PAIR_EXISTS");
        GraSwapPairProxy Graswap = new GraSwapPairProxy{salt: salt}(stock, money, isOnlySwap, uint64(uint(10)**dec), priceMul, priceDiv, graContract);

        pair = address(Graswap);
        allPairs.push(pair);
        _tokensToPair[salt] = pair;
        _pairWithToken[pair] = TokensInPair(stock, money);
        emit PairCreated(pair, stock, money, isOnlySwap);

        // save pair info in pairFeeDistribution contract
        pfd.addpair(pair);
    }

    function _getDecimals(address token) private view returns (uint) {
        if (token == address(0)) { return 18; }
        return uint(IERC20(token).decimals());
    }

    function allPairsLength() external override view returns (uint) {
        return allPairs.length;
    }

    function setFeeToAddresses(address _feeTo_1, address _feeTo_2, address _feeToPrivate) external override {
        require(msg.sender == feeToSetter, "GraSwapFactory: FORBIDDEN");
        feeTo_1 = _feeTo_1;
        feeTo_2 = _feeTo_2;
        feeToPrivate = _feeToPrivate;
    }

    function setFeeToSetter(address _feeToSetter) external override {
        require(msg.sender == feeToSetter, "GraSwapFactory: FORBIDDEN");
        feeToSetter = _feeToSetter;
    }

    function setPairLogic(address implLogic) external override {
        require(msg.sender == gov, "GraSwapFactory: SETTER_MISMATCH");
        pairLogic = implLogic;
    }

    function setFeeBPS(uint32 _bps) external override {
        require(msg.sender == gov, "GraSwapFactory: SETTER_MISMATCH");
        require(0 <= _bps && _bps <= 50 , "GraSwapFactory: BPS_OUT_OF_RANGE");
        feeBPS = _bps;
    }
    function setpdf(address _newpfd) external {
        require(msg.sender == feeToSetter, "GraSwapFactory: FORBIDDEN");
        pfd = IPairFeeDistribution(_newpfd);
    }
    function getTokensFromPair(address pair) external view override returns (address stock, address money) {
        stock = _pairWithToken[pair].stock;
        money = _pairWithToken[pair].money;
    }

    function tokensToPair(address stock, address money, bool isOnlySwap) external view override returns (address pair) {
        bytes32 key = keccak256(abi.encodePacked(stock, money, isOnlySwap));
        return _tokensToPair[key];
    }
}

Contract Security Audit

Contract ABI

API
[{"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"},{"internalType":"address","name":"_gov","type":"address"},{"internalType":"address","name":"_graContract","type":"address"},{"internalType":"address","name":"_pairLogic","type":"address"},{"internalType":"address","name":"_distribution","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pair","type":"address"},{"indexed":false,"internalType":"address","name":"stock","type":"address"},{"indexed":false,"internalType":"address","name":"money","type":"address"},{"indexed":false,"internalType":"bool","name":"isOnlySwap","type":"bool"}],"name":"PairCreated","type":"event"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"allPairs","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"allPairsLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"stock","type":"address"},{"internalType":"address","name":"money","type":"address"},{"internalType":"bool","name":"isOnlySwap","type":"bool"}],"name":"createPair","outputs":[{"internalType":"address","name":"pair","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"feeBPS","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeToPrivate","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeToSetter","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeTo_1","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeTo_2","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"pair","type":"address"}],"name":"getTokensFromPair","outputs":[{"internalType":"address","name":"stock","type":"address"},{"internalType":"address","name":"money","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"gov","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"graContract","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pairLogic","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint32","name":"_bps","type":"uint32"}],"name":"setFeeBPS","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_feeTo_1","type":"address"},{"internalType":"address","name":"_feeTo_2","type":"address"},{"internalType":"address","name":"_feeToPrivate","type":"address"}],"name":"setFeeToAddresses","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"name":"setFeeToSetter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"implLogic","type":"address"}],"name":"setPairLogic","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newpfd","type":"address"}],"name":"setpdf","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"stock","type":"address"},{"internalType":"address","name":"money","type":"address"},{"internalType":"bool","name":"isOnlySwap","type":"bool"}],"name":"tokensToPair","outputs":[{"internalType":"address","name":"pair","type":"address"}],"stateMutability":"view","type":"function"}]

60c06040526003805463ffffffff60a01b1916600560a31b17905534801561002657600080fd5b5060405161117d38038061117d833981810160405260a081101561004957600080fd5b50805160208201516040830151606080850151608095860151600380546001600160a01b03199081166001600160a01b039889161790915585841b6001600160601b03199081169098529284901b90961660a0526004805483169186169190911790556008805490911694841694909417909355811691166110906100ed600039806105fe52806108ac5250806103e952806104f95280610c1c52506110906000f3fe608060405234801561001057600080fd5b50600436106101165760003560e01c806355f4cc5a116100a257806387dcb20e1161007157806387dcb20e146102aa5780638a7bfa46146102b2578063a2e74af6146102ba578063c0225aa1146102e0578063ca2152251461033557610116565b806355f4cc5a14610218578063574f2ba3146102205780636c87813e1461023a57806382dfdce41461027257610116565b8063160c8dde116100e9578063160c8dde146101a75780631a1c6e53146101af5780631e3dd18b146101d05780633be9d930146101ed57806344402ac41461021057610116565b8063094b74151461011b578063100c2ba21461013f57806312d43a5114610167578063137ea99d1461016f575b600080fd5b61012361035b565b604080516001600160a01b039092168252519081900360200190f35b6101656004803603602081101561015557600080fd5b50356001600160a01b031661036a565b005b6101236103e7565b6101656004803603606081101561018557600080fd5b506001600160a01b03813581169160208101358216916040909101351661040b565b6101236104a5565b6101b76104b4565b6040805163ffffffff9092168252519081900360200190f35b610123600480360360208110156101e657600080fd5b50356104c7565b6101656004803603602081101561020357600080fd5b503563ffffffff166104ee565b6101236105ed565b6101236105fc565b610228610620565b60408051918252519081900360200190f35b6101236004803603606081101561025057600080fd5b506001600160a01b038135811691602081013590911690604001351515610626565b6101236004803603606081101561028857600080fd5b506001600160a01b038135811691602081013590911690604001351515610691565b610123610b4c565b610123610b5b565b610165600480360360208110156102d057600080fd5b50356001600160a01b0316610b6a565b610306600480360360208110156102f657600080fd5b50356001600160a01b0316610be7565b60405180836001600160a01b03168152602001826001600160a01b031681526020019250505060405180910390f35b6101656004803603602081101561034b57600080fd5b50356001600160a01b0316610c11565b6003546001600160a01b031681565b6003546001600160a01b031633146103c5576040805162461bcd60e51b815260206004820152601960248201527823b930a9bbb0b82330b1ba37b93c9d102327a92124a22222a760391b604482015290519081900360640190fd5b600880546001600160a01b0319166001600160a01b0392909216919091179055565b7f000000000000000000000000000000000000000000000000000000000000000081565b6003546001600160a01b03163314610466576040805162461bcd60e51b815260206004820152601960248201527823b930a9bbb0b82330b1ba37b93c9d102327a92124a22222a760391b604482015290519081900360640190fd5b600080546001600160a01b039485166001600160a01b031991821617909155600180549385169382169390931790925560028054919093169116179055565b6001546001600160a01b031681565b600354600160a01b900463ffffffff1681565b600781815481106104d457fe5b6000918252602090912001546001600160a01b0316905081565b336001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000161461056b576040805162461bcd60e51b815260206004820152601f60248201527f47726153776170466163746f72793a205345545445525f4d49534d4154434800604482015290519081900360640190fd5b60328163ffffffff1611156105c7576040805162461bcd60e51b815260206004820181905260248201527f47726153776170466163746f72793a204250535f4f55545f4f465f52414e4745604482015290519081900360640190fd5b6003805463ffffffff909216600160a01b0263ffffffff60a01b19909216919091179055565b6000546001600160a01b031681565b7f000000000000000000000000000000000000000000000000000000000000000081565b60075490565b604080516bffffffffffffffffffffffff19606095861b81166020808401919091529490951b909416603485015290151560f81b604884015280516029818503018152604990930181528251928201929092206000908152600690915220546001600160a01b031690565b6000826001600160a01b0316846001600160a01b031614156106e45760405162461bcd60e51b81526004018080602001828103825260238152602001806110386023913960400191505060405180910390fd5b60006106ef84610cb0565b905060006106fc86610cb0565b90508060171015801561070d575060015b6107485760405162461bcd60e51b815260040180806020018281038252602c815260200180610fe5602c913960400191505060405180910390fd5b600060048210610759575060031981015b600180600084861115610782578460130186111561077957506001610782565b848603600a0a92505b858511156107a6578560130185111561079d575060016107a6565b858503600a0a91505b80156107e35760405162461bcd60e51b81526004018080602001828103825260278152602001806110116027913960400191505060405180910390fd5b6040805160608c811b6bffffffffffffffffffffffff19908116602080850191909152918d901b1660348301528a151560f81b6048830152825180830360290181526049909201835281519181019190912060008181526006909252919020546001600160a01b03161561089e576040805162461bcd60e51b815260206004820152601b60248201527f47726153776170466163746f72793a20504149525f4558495354530000000000604482015290519081900360640190fd5b6000818c8c8c89600a0a89897f00000000000000000000000000000000000000000000000000000000000000006040516108d790610d38565b6001600160a01b039788168152958716602087015293151560408087019190915267ffffffffffffffff9384166060870152918316608086015290911660a0840152921660c08201529051829181900360e001906000f5905080158015610942573d6000803e3d6000fd5b5090508098506007899080600181540180825580915050600190039060005260206000200160009091909190916101000a8154816001600160a01b0302191690836001600160a01b03160217905550886006600084815260200190815260200160002060006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060405180604001604052808d6001600160a01b031681526020018c6001600160a01b0316815250600560008b6001600160a01b03166001600160a01b0316815260200190815260200160002060008201518160000160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060208201518160010160006101000a8154816001600160a01b0302191690836001600160a01b03160217905550905050886001600160a01b03167fd6b196957cdc774a3f446a0dcd127f49e6352507784aa7fd6137c1d13326b6eb8d8d8d60405180846001600160a01b03168152602001836001600160a01b031681526020018215158152602001935050505060405180910390a2600854604080516317d4a88d60e31b81526001600160a01b038c811660048301529151919092169163bea5446891602480830192600092919082900301818387803b158015610b2557600080fd5b505af1158015610b39573d6000803e3d6000fd5b5050505050505050505050509392505050565b6004546001600160a01b031681565b6002546001600160a01b031681565b6003546001600160a01b03163314610bc5576040805162461bcd60e51b815260206004820152601960248201527823b930a9bbb0b82330b1ba37b93c9d102327a92124a22222a760391b604482015290519081900360640190fd5b600380546001600160a01b0319166001600160a01b0392909216919091179055565b6001600160a01b039081166000908152600560205260409020805460019091015490821692911690565b336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614610c8e576040805162461bcd60e51b815260206004820152601f60248201527f47726153776170466163746f72793a205345545445525f4d49534d4154434800604482015290519081900360640190fd5b600480546001600160a01b0319166001600160a01b0392909216919091179055565b60006001600160a01b038216610cc857506012610d33565b816001600160a01b031663313ce5676040518163ffffffff1660e01b815260040160206040518083038186803b158015610d0157600080fd5b505afa158015610d15573d6000803e3d6000fd5b505050506040513d6020811015610d2b57600080fd5b505160ff1690505b919050565b61029f80610d468339019056fe61012060405234801561001157600080fd5b5060405161029f38038061029f833981810160405260e081101561003457600080fd5b50805160208201516040830151606084015160808086015160a08088015160c098890151339094526001600160a01b03808816909252818816909852821660e052949593949293919291906000851561008b575060015b604090811b6001600160401b0395861617811b9385169390931790921b9216919091176101005250506001600a55505060805160a05160c05160e051610100516101aa6100f56000398060a552508060835250806061525080603f525080601d52506101aa6000f3fe60806040523661000b57005b604080516343ee590760e11b815290517f0000000000000000000000000000000000000000000000000000000000000000917f0000000000000000000000000000000000000000000000000000000000000000917f0000000000000000000000000000000000000000000000000000000000000000917f0000000000000000000000000000000000000000000000000000000000000000917f0000000000000000000000000000000000000000000000000000000000000000916000916001600160a01b038816916387dcb20e91600480830192602092919082900301818787803b1580156100f957600080fd5b505af115801561010d573d6000803e3d6000fd5b505050506040513d602081101561012357600080fd5b505160405190915036806000833781810188815260208101889052604081018790526060810186905260800184905260a0016000808284865af43d9150816000843e808015610170578284f35b8284fdfea264697066735822122041a05d90ed222a6e7ed4d35a5abb37f7c70f4e5a81d51bccbf4c47d22f8b8b5a64736f6c634300060c003347726153776170466163746f72793a2053544f434b5f444543494d414c535f4e4f545f535550504f5254454447726153776170466163746f72793a20444543494d414c535f444946465f544f4f5f4c4152474547726153776170466163746f72793a204944454e544943414c5f414444524553534553a26469706673582212200439385f35a18b2b67bdb8c5a4d1e895f17c1c6133b3b3ae7782796a5444194164736f6c634300060c0033000000000000000000000000a19c059c61605ba41efc407df5f27dafaf6d5c58000000000000000000000000598c30abb336948f1b8131e2468c8bcbbc7de9ae0000000000000000000000005a23b7e3bb936c7753b5e7a6c304a8fb43979d200000000000000000000000000baecbb404828f5ef2169d480d70c40bdd3d1449000000000000000000000000c705612b0ca8c026854f38e866bce2701828486a

Deployed Bytecode

0x608060405234801561001057600080fd5b50600436106101165760003560e01c806355f4cc5a116100a257806387dcb20e1161007157806387dcb20e146102aa5780638a7bfa46146102b2578063a2e74af6146102ba578063c0225aa1146102e0578063ca2152251461033557610116565b806355f4cc5a14610218578063574f2ba3146102205780636c87813e1461023a57806382dfdce41461027257610116565b8063160c8dde116100e9578063160c8dde146101a75780631a1c6e53146101af5780631e3dd18b146101d05780633be9d930146101ed57806344402ac41461021057610116565b8063094b74151461011b578063100c2ba21461013f57806312d43a5114610167578063137ea99d1461016f575b600080fd5b61012361035b565b604080516001600160a01b039092168252519081900360200190f35b6101656004803603602081101561015557600080fd5b50356001600160a01b031661036a565b005b6101236103e7565b6101656004803603606081101561018557600080fd5b506001600160a01b03813581169160208101358216916040909101351661040b565b6101236104a5565b6101b76104b4565b6040805163ffffffff9092168252519081900360200190f35b610123600480360360208110156101e657600080fd5b50356104c7565b6101656004803603602081101561020357600080fd5b503563ffffffff166104ee565b6101236105ed565b6101236105fc565b610228610620565b60408051918252519081900360200190f35b6101236004803603606081101561025057600080fd5b506001600160a01b038135811691602081013590911690604001351515610626565b6101236004803603606081101561028857600080fd5b506001600160a01b038135811691602081013590911690604001351515610691565b610123610b4c565b610123610b5b565b610165600480360360208110156102d057600080fd5b50356001600160a01b0316610b6a565b610306600480360360208110156102f657600080fd5b50356001600160a01b0316610be7565b60405180836001600160a01b03168152602001826001600160a01b031681526020019250505060405180910390f35b6101656004803603602081101561034b57600080fd5b50356001600160a01b0316610c11565b6003546001600160a01b031681565b6003546001600160a01b031633146103c5576040805162461bcd60e51b815260206004820152601960248201527823b930a9bbb0b82330b1ba37b93c9d102327a92124a22222a760391b604482015290519081900360640190fd5b600880546001600160a01b0319166001600160a01b0392909216919091179055565b7f000000000000000000000000598c30abb336948f1b8131e2468c8bcbbc7de9ae81565b6003546001600160a01b03163314610466576040805162461bcd60e51b815260206004820152601960248201527823b930a9bbb0b82330b1ba37b93c9d102327a92124a22222a760391b604482015290519081900360640190fd5b600080546001600160a01b039485166001600160a01b031991821617909155600180549385169382169390931790925560028054919093169116179055565b6001546001600160a01b031681565b600354600160a01b900463ffffffff1681565b600781815481106104d457fe5b6000918252602090912001546001600160a01b0316905081565b336001600160a01b037f000000000000000000000000598c30abb336948f1b8131e2468c8bcbbc7de9ae161461056b576040805162461bcd60e51b815260206004820152601f60248201527f47726153776170466163746f72793a205345545445525f4d49534d4154434800604482015290519081900360640190fd5b60328163ffffffff1611156105c7576040805162461bcd60e51b815260206004820181905260248201527f47726153776170466163746f72793a204250535f4f55545f4f465f52414e4745604482015290519081900360640190fd5b6003805463ffffffff909216600160a01b0263ffffffff60a01b19909216919091179055565b6000546001600160a01b031681565b7f0000000000000000000000005a23b7e3bb936c7753b5e7a6c304a8fb43979d2081565b60075490565b604080516bffffffffffffffffffffffff19606095861b81166020808401919091529490951b909416603485015290151560f81b604884015280516029818503018152604990930181528251928201929092206000908152600690915220546001600160a01b031690565b6000826001600160a01b0316846001600160a01b031614156106e45760405162461bcd60e51b81526004018080602001828103825260238152602001806110386023913960400191505060405180910390fd5b60006106ef84610cb0565b905060006106fc86610cb0565b90508060171015801561070d575060015b6107485760405162461bcd60e51b815260040180806020018281038252602c815260200180610fe5602c913960400191505060405180910390fd5b600060048210610759575060031981015b600180600084861115610782578460130186111561077957506001610782565b848603600a0a92505b858511156107a6578560130185111561079d575060016107a6565b858503600a0a91505b80156107e35760405162461bcd60e51b81526004018080602001828103825260278152602001806110116027913960400191505060405180910390fd5b6040805160608c811b6bffffffffffffffffffffffff19908116602080850191909152918d901b1660348301528a151560f81b6048830152825180830360290181526049909201835281519181019190912060008181526006909252919020546001600160a01b03161561089e576040805162461bcd60e51b815260206004820152601b60248201527f47726153776170466163746f72793a20504149525f4558495354530000000000604482015290519081900360640190fd5b6000818c8c8c89600a0a89897f0000000000000000000000005a23b7e3bb936c7753b5e7a6c304a8fb43979d206040516108d790610d38565b6001600160a01b039788168152958716602087015293151560408087019190915267ffffffffffffffff9384166060870152918316608086015290911660a0840152921660c08201529051829181900360e001906000f5905080158015610942573d6000803e3d6000fd5b5090508098506007899080600181540180825580915050600190039060005260206000200160009091909190916101000a8154816001600160a01b0302191690836001600160a01b03160217905550886006600084815260200190815260200160002060006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060405180604001604052808d6001600160a01b031681526020018c6001600160a01b0316815250600560008b6001600160a01b03166001600160a01b0316815260200190815260200160002060008201518160000160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060208201518160010160006101000a8154816001600160a01b0302191690836001600160a01b03160217905550905050886001600160a01b03167fd6b196957cdc774a3f446a0dcd127f49e6352507784aa7fd6137c1d13326b6eb8d8d8d60405180846001600160a01b03168152602001836001600160a01b031681526020018215158152602001935050505060405180910390a2600854604080516317d4a88d60e31b81526001600160a01b038c811660048301529151919092169163bea5446891602480830192600092919082900301818387803b158015610b2557600080fd5b505af1158015610b39573d6000803e3d6000fd5b5050505050505050505050509392505050565b6004546001600160a01b031681565b6002546001600160a01b031681565b6003546001600160a01b03163314610bc5576040805162461bcd60e51b815260206004820152601960248201527823b930a9bbb0b82330b1ba37b93c9d102327a92124a22222a760391b604482015290519081900360640190fd5b600380546001600160a01b0319166001600160a01b0392909216919091179055565b6001600160a01b039081166000908152600560205260409020805460019091015490821692911690565b336001600160a01b037f000000000000000000000000598c30abb336948f1b8131e2468c8bcbbc7de9ae1614610c8e576040805162461bcd60e51b815260206004820152601f60248201527f47726153776170466163746f72793a205345545445525f4d49534d4154434800604482015290519081900360640190fd5b600480546001600160a01b0319166001600160a01b0392909216919091179055565b60006001600160a01b038216610cc857506012610d33565b816001600160a01b031663313ce5676040518163ffffffff1660e01b815260040160206040518083038186803b158015610d0157600080fd5b505afa158015610d15573d6000803e3d6000fd5b505050506040513d6020811015610d2b57600080fd5b505160ff1690505b919050565b61029f80610d468339019056fe61012060405234801561001157600080fd5b5060405161029f38038061029f833981810160405260e081101561003457600080fd5b50805160208201516040830151606084015160808086015160a08088015160c098890151339094526001600160a01b03808816909252818816909852821660e052949593949293919291906000851561008b575060015b604090811b6001600160401b0395861617811b9385169390931790921b9216919091176101005250506001600a55505060805160a05160c05160e051610100516101aa6100f56000398060a552508060835250806061525080603f525080601d52506101aa6000f3fe60806040523661000b57005b604080516343ee590760e11b815290517f0000000000000000000000000000000000000000000000000000000000000000917f0000000000000000000000000000000000000000000000000000000000000000917f0000000000000000000000000000000000000000000000000000000000000000917f0000000000000000000000000000000000000000000000000000000000000000917f0000000000000000000000000000000000000000000000000000000000000000916000916001600160a01b038816916387dcb20e91600480830192602092919082900301818787803b1580156100f957600080fd5b505af115801561010d573d6000803e3d6000fd5b505050506040513d602081101561012357600080fd5b505160405190915036806000833781810188815260208101889052604081018790526060810186905260800184905260a0016000808284865af43d9150816000843e808015610170578284f35b8284fdfea264697066735822122041a05d90ed222a6e7ed4d35a5abb37f7c70f4e5a81d51bccbf4c47d22f8b8b5a64736f6c634300060c003347726153776170466163746f72793a2053544f434b5f444543494d414c535f4e4f545f535550504f5254454447726153776170466163746f72793a20444543494d414c535f444946465f544f4f5f4c4152474547726153776170466163746f72793a204944454e544943414c5f414444524553534553a26469706673582212200439385f35a18b2b67bdb8c5a4d1e895f17c1c6133b3b3ae7782796a5444194164736f6c634300060c0033

Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)

000000000000000000000000a19c059c61605ba41EFC407dF5F27DAfAf6d5C58000000000000000000000000598C30ABb336948F1B8131e2468C8BCbbC7de9ae0000000000000000000000005a23b7E3bb936C7753B5e7A6C304a8FB43979d200000000000000000000000000BaECBB404828F5eF2169D480D70C40BDD3D1449000000000000000000000000c705612B0CA8c026854f38E866Bce2701828486a

-----Decoded View---------------
Arg [0] : _feeToSetter (address): 0xa19c059c61605ba41EFC407dF5F27DAfAf6d5C58
Arg [1] : _gov (address): 0x598C30ABb336948F1B8131e2468C8BCbbC7de9ae
Arg [2] : _graContract (address): 0x5a23b7E3bb936C7753B5e7A6C304a8FB43979d20
Arg [3] : _pairLogic (address): 0x0BaECBB404828F5eF2169D480D70C40BDD3D1449
Arg [4] : _distribution (address): 0xc705612B0CA8c026854f38E866Bce2701828486a

-----Encoded View---------------
5 Constructor Arguments found :
Arg [0] : 000000000000000000000000a19c059c61605ba41EFC407dF5F27DAfAf6d5C58
Arg [1] : 000000000000000000000000598C30ABb336948F1B8131e2468C8BCbbC7de9ae
Arg [2] : 0000000000000000000000005a23b7E3bb936C7753B5e7A6C304a8FB43979d20
Arg [3] : 0000000000000000000000000BaECBB404828F5eF2169D480D70C40BDD3D1449
Arg [4] : 000000000000000000000000c705612B0CA8c026854f38E866Bce2701828486a


Deployed Bytecode Sourcemap

76494:4798:0:-:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;76747:35;;;:::i;:::-;;;;-1:-1:-1;;;;;76747:35:0;;;;;;;;;;;;;;80667:170;;;;;;;;;;;;;;;;-1:-1:-1;80667:170:0;-1:-1:-1;;;;;80667:170:0;;:::i;:::-;;76789:28;;;:::i;79766:283::-;;;;;;;;;;;;;;;;-1:-1:-1;;;;;;79766:283:0;;;;;;;;;;;;;;;;;;;:::i;76666:31::-;;;:::i;76867:34::-;;;:::i;:::-;;;;;;;;;;;;;;;;;;;77066:25;;;;;;;;;;;;;;;;-1:-1:-1;77066:25:0;;:::i;80427:234::-;;;;;;;;;;;;;;;;-1:-1:-1;80427:234:0;;;;:::i;76628:31::-;;;:::i;76824:36::-;;;:::i;79653:105::-;;;:::i;:::-;;;;;;;;;;;;;;;;81052:237;;;;;;;;;;;;;;;;-1:-1:-1;;;;;;81052:237:0;;;;;;;;;;;;;;;;;;;:::i;77470:1997::-;;;;;;;;;;;;;;;;-1:-1:-1;;;;;;77470:1997:0;;;;;;;;;;;;;;;;;;;:::i;76908:33::-;;;:::i;76704:36::-;;;:::i;80057:183::-;;;;;;;;;;;;;;;;-1:-1:-1;80057:183:0;-1:-1:-1;;;;;80057:183:0;;:::i;80843:201::-;;;;;;;;;;;;;;;;-1:-1:-1;80843:201:0;-1:-1:-1;;;;;80843:201:0;;:::i;:::-;;;;;-1:-1:-1;;;;;80843:201:0;;;;;;-1:-1:-1;;;;;80843:201:0;;;;;;;;;;;;;;;;80248:171;;;;;;;;;;;;;;;;-1:-1:-1;80248:171:0;-1:-1:-1;;;;;80248:171:0;;:::i;76747:35::-;;;-1:-1:-1;;;;;76747:35:0;;:::o;80667:170::-;80742:11;;-1:-1:-1;;;;;80742:11:0;80728:10;:25;80720:63;;;;;-1:-1:-1;;;80720:63:0;;;;;;;;;;;;-1:-1:-1;;;80720:63:0;;;;;;;;;;;;;;;80794:3;:35;;-1:-1:-1;;;;;;80794:35:0;-1:-1:-1;;;;;80794:35:0;;;;;;;;;;80667:170::o;76789:28::-;;;:::o;79766:283::-;79903:11;;-1:-1:-1;;;;;79903:11:0;79889:10;:25;79881:63;;;;;-1:-1:-1;;;79881:63:0;;;;;;;;;;;;-1:-1:-1;;;79881:63:0;;;;;;;;;;;;;;;79955:7;:18;;-1:-1:-1;;;;;79955:18:0;;;-1:-1:-1;;;;;;79955:18:0;;;;;;;;79984;;;;;;;;;;;;;;;80013:12;:28;;;;;;;;;;;79766:283::o;76666:31::-;;;-1:-1:-1;;;;;76666:31:0;;:::o;76867:34::-;;;-1:-1:-1;;;76867:34:0;;;;;:::o;77066:25::-;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;77066:25:0;;-1:-1:-1;77066:25:0;:::o;80427:234::-;80496:10;-1:-1:-1;;;;;80510:3:0;80496:17;;80488:61;;;;;-1:-1:-1;;;80488:61:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;80589:2;80581:4;:10;;;;80560:69;;;;;-1:-1:-1;;;80560:69:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;80640:6;:13;;;;;;-1:-1:-1;;;80640:13:0;-1:-1:-1;;;;80640:13:0;;;;;;;;;80427:234::o;76628:31::-;;;-1:-1:-1;;;;;76628:31:0;;:::o;76824:36::-;;;:::o;79653:105::-;79735:8;:15;79653:105;:::o;81052:237::-;81202:42;;;-1:-1:-1;;81202:42:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;81192:53;;;;;;;;;-1:-1:-1;81263:18:0;;;:13;:18;;;;;-1:-1:-1;;;;;81263:18:0;;81052:237::o;77470:1997::-;77564:12;77606:5;-1:-1:-1;;;;;77597:14:0;:5;-1:-1:-1;;;;;77597:14:0;;;77589:62;;;;-1:-1:-1;;;77589:62:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;77775:13;77791:19;77804:5;77791:12;:19::i;:::-;77775:35;;77821:13;77837:19;77850:5;77837:12;:19::i;:::-;77821:35;;77881:8;77875:2;:14;;:31;;;;-1:-1:-1;77893:13:0;77875:31;77867:88;;;;-1:-1:-1;;;77867:88:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;77966:8;78005:1;77993:8;:13;77989:93;;-1:-1:-1;;;78029:12:0;;77989:93;78194:1;;78176:15;78282:19;;;78278:232;;;78333:8;78344:2;78333:13;78322:8;:24;78318:181;;;-1:-1:-1;78388:4:0;78318:181;;;78473:8;78462;:19;78456:2;78451:31;78433:50;;78318:181;78535:8;78524;:19;78520:232;;;78575:8;78586:2;78575:13;78564:8;:24;78560:181;;;-1:-1:-1;78630:4:0;78560:181;;;78715:8;78704;:19;78698:2;78693:31;78675:50;;78560:181;78771:18;78770:19;78762:71;;;;-1:-1:-1;;;78762:71:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;78869:42;;;;;;;-1:-1:-1;;78869:42:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;78859:53;;;;;;;;;-1:-1:-1;78931:19:0;;;:13;:19;;;;;;;-1:-1:-1;;;;;78931:19:0;:33;78923:73;;;;;-1:-1:-1;;;78923:73:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;79007:24;79061:4;79067:5;79074;79081:10;79110:3;79105:2;79100:13;79116:8;79126;79136:11;79034:114;;;;;:::i;:::-;-1:-1:-1;;;;;79034:114:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;79034:114:0;;;;;;;;;;;;;;;;;;79007:141;;79176:7;79161:23;;79195:8;79209:4;79195:19;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;79195:19:0;;;;;-1:-1:-1;;;;;79195:19:0;;;;;;79247:4;79225:13;:19;79239:4;79225:19;;;;;;;;;;;;:26;;;;;-1:-1:-1;;;;;79225:26:0;;;;;-1:-1:-1;;;;;79225:26:0;;;;;;79285;;;;;;;;79298:5;-1:-1:-1;;;;;79285:26:0;;;;;79305:5;-1:-1:-1;;;;;79285:26:0;;;;79262:14;:20;79277:4;-1:-1:-1;;;;;79262:20:0;-1:-1:-1;;;;;79262:20:0;;;;;;;;;;;;:49;;;;;;;;;;;;;-1:-1:-1;;;;;79262:49:0;;;;;-1:-1:-1;;;;;79262:49:0;;;;;;;;;;;;;;;;;;;-1:-1:-1;;;;;79262:49:0;;;;;-1:-1:-1;;;;;79262:49:0;;;;;;;;;79339:4;-1:-1:-1;;;;;79327:43:0;;79345:5;79352;79359:10;79327:43;;;;-1:-1:-1;;;;;79327:43:0;;;;;;-1:-1:-1;;;;;79327:43:0;;;;;;;;;;;;;;;;;;;;;;;;79442:3;;:17;;;-1:-1:-1;;;79442:17:0;;-1:-1:-1;;;;;79442:17:0;;;;;;;;;:3;;;;;:11;;:17;;;;;:3;;:17;;;;;;;:3;;:17;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;77470:1997;;;;;;;;;;;;;:::o;76908:33::-;;;-1:-1:-1;;;;;76908:33:0;;:::o;76704:36::-;;;-1:-1:-1;;;;;76704:36:0;;:::o;80057:183::-;80154:11;;-1:-1:-1;;;;;80154:11:0;80140:10;:25;80132:63;;;;;-1:-1:-1;;;80132:63:0;;;;;;;;;;;;-1:-1:-1;;;80132:63:0;;;;;;;;;;;;;;;80206:11;:26;;-1:-1:-1;;;;;;80206:26:0;-1:-1:-1;;;;;80206:26:0;;;;;;;;;;80057:183::o;80843:201::-;-1:-1:-1;;;;;80965:20:0;;;80916:13;80965:20;;;:14;:20;;;;;:26;;;81010;;;;80965;;;;81010;;;80843:201::o;80248:171::-;80326:10;-1:-1:-1;;;;;80340:3:0;80326:17;;80318:61;;;;;-1:-1:-1;;;80318:61:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;80390:9;:21;;-1:-1:-1;;;;;;80390:21:0;-1:-1:-1;;;;;80390:21:0;;;;;;;;;;80248:171::o;79475:170::-;79534:4;-1:-1:-1;;;;;79555:19:0;;79551:39;;-1:-1:-1;79585:2:0;79578:9;;79551:39;79619:5;-1:-1:-1;;;;;79612:22:0;;:24;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-1:-1:-1;79612:24:0;79607:30;;;-1:-1:-1;79475:170:0;;;;:::o;-1:-1:-1:-;;;;;;;;:::o

Swarm Source

ipfs://0439385f35a18b2b67bdb8c5a4d1e895f17c1c6133b3b3ae7782796a54441941

Block Transaction Difficulty Gas Used Reward
View All Blocks Produced

Block Uncle Number Difficulty Gas Used Reward
View All Uncles
Loading...
Loading
Loading...
Loading

Validator Index Block Amount
View All Withdrawals

Transaction Hash Block Value Eth2 PubKey Valid
View All Deposits
Loading...
Loading
[ Download: CSV Export  ]

A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.