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

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

Merged
merged 21 commits into from
Feb 6, 2023

Conversation

Amxx
Copy link
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

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

/**
* @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
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
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
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
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 😕).

contracts/utils/ShortStrings.sol Outdated Show resolved Hide resolved
.changeset/violet-frogs-hide.md Outdated Show resolved Hide resolved
contracts/utils/ShortStrings.sol Outdated Show resolved Hide resolved
test/utils/ShortStrings.test.js Outdated Show resolved Hide resolved
test/utils/ShortStrings.test.js Outdated Show resolved Hide resolved
@frangio
Copy link
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

Amxx and others added 4 commits February 3, 2023 10:20
Co-authored-by: Francisco <frangio.1@gmail.com>
Co-authored-by: Francisco <frangio.1@gmail.com>
Co-authored-by: Francisco <frangio.1@gmail.com>
Co-authored-by: Francisco <frangio.1@gmail.com>
@Amxx Amxx changed the title Shortstring library Add a library for handling short strings in a gas efficient way Feb 3, 2023
Copy link
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.

None yet

2 participants