Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mapping storage parameters for library functions #4635

Closed
chriseth opened this issue Aug 1, 2018 · 6 comments
Closed

Allow mapping storage parameters for library functions #4635

chriseth opened this issue Aug 1, 2018 · 6 comments
Assignees
Labels

Comments

@chriseth
Copy link
Contributor

chriseth commented Aug 1, 2018

Since library functions allow storage struct parameters, storage mapping parameters should also be allowed. This would enable us to modularize some things:

library Balances {
    function send(mapping(address => uint256) storage balances, address from, address to, uint amount) internal {
        require(balances[from] >= amount);
        require(balances[to] + amount >= balances[to]);
        balances[from] -= amount;
        balances[to] += amount;
    }
}

contract Token {
    mapping(address => uint256) balances;
    using Balances for *;
    mapping(address => mapping (address => uint256)) allowed;

    event Transfer(address from, address to, uint amount);
    event Approval(address owner, address spender, uint amount);

    function balanceOf(address tokenOwner) public constant returns (uint balance) {
        return balances[tokenOwner];
    }
    function transfer(address to, uint amount) public returns (bool success) {
        balances.send(msg.sender, to, amount);
        emit Transfer(msg.sender, to, amount);
        return true;

    }

    function transferFrom(address from, address to, uint amount) public returns (bool success) {
        require(allowed[from][msg.sender] >= amount);
        allowed[from][msg.sender] -= amount;
        balances.send(from, to, amount);
        emit Transfer(from, to, amount);
        return true;
    }

    function approve(address spender, uint tokens) public returns (bool success) {
        require(allowed[msg.sender][spender] == 0, "");
        allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        return true;
    }
}
@axic
Copy link
Member

axic commented Aug 1, 2018

I think this a good idea, extends the usability of libraries.

@chriseth chriseth changed the title Allow mapping storage parameters for internal library functions Allow mapping storage parameters for library functions Aug 1, 2018
@ekpyron ekpyron self-assigned this Aug 10, 2018
@ekpyron
Copy link
Member

ekpyron commented Aug 10, 2018

Should this only be the case for internal library functions?

Currently the following does not work either, and I don't see a good reason, why not:

contract C {
  function get(mapping(uint => uint) storage m, uint key) internal view returns (uint) {
    return m[key];
  }
}

This yields TypeError: Type is required to live outside storage.

Is there any good reason to require a type to live outside storage for the arguments of internal functions at all?

@ekpyron
Copy link
Member

ekpyron commented Aug 10, 2018

Somewhat related: #4670

@chriseth
Copy link
Contributor Author

This requires defining a type string for mappings for the function selector (as we did with strings).

@chriseth
Copy link
Contributor Author

(extended scope to public library functions)

@chriseth
Copy link
Contributor Author

Moved to 0.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants