Skip to content

Add a library for handling short strings in a gas efficient way#4023

Merged
Amxx merged 21 commits intoOpenZeppelin:masterfrom
Amxx:feature/shortstring
Feb 6, 2023
Merged

Add a library for handling short strings in a gas efficient way#4023
Amxx merged 21 commits intoOpenZeppelin:masterfrom
Amxx:feature/shortstring

Conversation

@Amxx
Copy link
Copy Markdown
Collaborator

@Amxx Amxx commented Feb 1, 2023

Allow string → bytes32 with string storage fallback.

This provides an effective way to store short strings (<32chars) in immutable storage.

PR Checklist

  • Tests
  • Documentation
  • Changeset entry (run npx changeset add)

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 1, 2023

🦋 Changeset detected

Latest commit: fb9aac4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openzeppelin-solidity Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment thread contracts/utils/ShortStrings.sol Outdated
/**
* @dev Encode a string into a `ShortString`, or write it to storage if it is too long.
*/
function setWithFallback(string memory value, string storage store) internal returns (ShortString) {
Copy link
Copy Markdown
Contributor

@frangio frangio Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced by the naming. How do we expect this function be used? As a method with using for?

Copy link
Copy Markdown
Collaborator Author

@Amxx Amxx Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking

contract NamedContract {
    ShortStrings.ShortString private immutable _name;
    string private _nameFallback;
    
    constructor(string memory name_) {
        _name = ShortStrings.setWithFallback(name_, _nameFallback);
    }
    
    function name() public view returns (string memory) {
        return ShortStrings.getWithFallback(_name, _nameFallback);
    }
}

How would you do it?

Copy link
Copy Markdown
Collaborator Author

@Amxx Amxx Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you prefer

constructor(string memory name_) {
    _name = name_.toShortStringWithFallback(_nameFallback);
}
function name() public view returns (string memory) {
    return _name.toStringWithFallback(_nameFallback);
}

?

Copy link
Copy Markdown
Contributor

@frangio frangio Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the same. Those are good.

We should add that as an example snippet in the docs (without an underscore suffix though 😕).

Comment thread contracts/utils/ShortStrings.sol Outdated
Comment thread .changeset/violet-frogs-hide.md Outdated
Comment thread contracts/utils/ShortStrings.sol Outdated
Comment thread test/utils/ShortStrings.test.js Outdated
Comment thread test/utils/ShortStrings.test.js Outdated
@frangio
Copy link
Copy Markdown
Contributor

frangio commented Feb 2, 2023

Please review the PR title according to the guidelines: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/GUIDELINES.md#pull-requests

Comment thread contracts/utils/ShortStrings.sol Outdated
@Amxx Amxx changed the title Shortstring library Add a library for handling short strings in a gas efficient way Feb 3, 2023
Copy link
Copy Markdown
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Amxx Amxx merged commit 260e082 into OpenZeppelin:master Feb 6, 2023
@Amxx Amxx deleted the feature/shortstring branch February 6, 2023 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants