2

Within the uniswap v2 core contracts for a pair of coins, there is a mint() function. Within this mint function, there is an undeclared _mint() function. Does the underscore before the function name have some special meaning in solidity?

I have extracted the function below. I am having difficulty understanding what the _mint() function within the mint() function means and where it comes from if it is undeclared (as far as I can see)?

    // this low-level function should be called from a contract which performs important safety checks
    function mint(address to) external lock returns (uint liquidity) {
        (uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
        uint balance0 = IERC20(token0).balanceOf(address(this));
        uint balance1 = IERC20(token1).balanceOf(address(this));
        uint amount0 = balance0.sub(_reserve0);
        uint amount1 = balance1.sub(_reserve1);

        bool feeOn = _mintFee(_reserve0, _reserve1);
        uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
        if (_totalSupply == 0) {
            liquidity = Math.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);
           _mint(address(0), MINIMUM_LIQUIDITY); // permanently lock the first MINIMUM_LIQUIDITY tokens
        } else {
            liquidity = Math.min(amount0.mul(_totalSupply) / _reserve0, amount1.mul(_totalSupply) / _reserve1);
        }
        require(liquidity > 0, 'UniswapV2: INSUFFICIENT_LIQUIDITY_MINTED');
        _mint(to, liquidity);

        _update(balance0, balance1, _reserve0, _reserve1);
        if (feeOn) kLast = uint(reserve0).mul(reserve1); // reserve0 and reserve1 are up-to-date
        emit Mint(msg.sender, amount0, amount1);
    }

Source: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2Pair.sol#L121

Thanks in advance

2
  • It is usually a coding convention for indicating that the function is either private or internal (which basically means that you don't need to dwell on this function too much because there's nothing much that you can do with it anyway). Aug 18 '20 at 20:28
  • I guess the part I don't understand is how _mint() can work if it's not declared with a 'function _mint()'... anywhere. More for my learning than trying to use that internal function
    – Zakoff
    Aug 18 '20 at 20:43
4

The _mint function comes from one of the contract's parents, the UniswapV2ERC20 contract: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol#L40

The underscore prefix is a convention, as in some other languages, e.g. Python, that it is a "protected" function, i.e. can only be called from itself or a derived class. Solidity can actually enforce this through the compiler and the internal keyword, so this isn't strictly needed, but improves readability. Linters like solhint will complain if you don't begin an internal function with an underscore.

1
  • 1
    Thanks, this was the missing piece. It's from a parent contract, thanks!
    – Zakoff
    Aug 18 '20 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.