1
function nextInitializedTickWithinOneWord(
    mapping(int16 => uint256) storage self,
    int24 tick,
    int24 tickSpacing,
    bool lte
) internal view returns (int24 next, bool initialized) {
    int24 compressed = tick / tickSpacing;
    if (lte) {
    (int16 wordPos, uint8 bitPos) = position(compressed);
    uint256 mask = (1 << bitPos) - 1 + (1 << bitPos);
    uint256 masked = self[wordPos] & mask;

From here for the mapping parameter it is said First arguments makes this function a method of mapping(int16 => uint256). One thing to note is: when calling this function nothing is passed as agrument. I could not understand how it works.Help me to understand this.

One more thing i did not understand is how the masked value is calculated. Help me on this too.

2
  • This would be ok question if you remove the second part about the masked value calculation, which has nothing to do with it. Please do try to follow the SO's guidelines, especially as you're asking so many questions.
    – kfx
    Apr 16 at 9:14
  • I thought it would be better if ask 2 question related to same topic together rather than asking it on 2 different question.
    – Hacek
    Apr 16 at 9:49

1 Answer 1

0

This function is used as operator for the type mapping(int16 => uint256).

Basically in solidity you can attach functions to a type using a Using A for B syntax. The first argument of these function is special, and it's easier to understand looking at some examples.

So, in your case the code will have a library with some functions that all perform some operation on mapping(int16 => uint256). This is type B, and will appear as the first argument in the functions:

library LibraryName {
    function nextInitializedTickWithinOneWord(
        mapping(int16 => uint256) storage self,
        int24 tick,
        int24 tickSpacing,
        bool lte
    ) internal view returns (int24 next, bool initialized) {
        ...
    }
}

The main contract has a variable ticks of this type. Importing the library and using the syntax Using A for B, we can perform all the functions like this:

using LibraryName for mapping(int16 => uint256);
mapping(int16 => uint256) public ticks;

ticks.nextInitializedTickWithinOneWord(
    _tick,
    _tickSpacing,
    _lte
);

which is equivalent of doing:

LibraryName.nextInitializedTickWithinOneWord(
    ticks,
    _tick,
    _tickSpacing,
    _lte
);

Check the docs here for a other examples. A common one is Using SafeERC20 for IERC20, you have a function function safeTransfer(IERC20 token, address to, uint256 amount) that you call with token.safeTransfer(to, amount). So the first argument of the function is written "outside" kinda.

Hope it's clear. I can't help with the second question, maybe you're asking the meaning of << and &? If so, 1 << bitPos is basically 2**bitPos, and & is bitwise AND. In any case I agree it's better to ask it in a separate question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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