[Tool] Refactor your solidity 0.4.x code to solidity 0.5.x code

Solidity 0.5.X is here with a lot of features and breaking changes. Making all the changes to your solidity code manually, especially adding the explicitness requirements can be really cumbersome (and boring) for large projects. This is why I decided to make a codemod for Solidity that does most of the work for you. It’s still under testing and is buggy but it gets the job done for the most part. You may see its magic in the underworks polymath-core repo branch.

The plugin is based on the solidity prettier plugin that extends the features of prettier, a well-known code formatter. It is available as an NPM package and the source code is hosted on GitHub.

Features

The plugin has the following features:

  1. Change solidity pragma version to ^0.5.0.
  2. Add calldata storage keyword to external function complex parameters like arrays that lack an already defined storage location.
  3. Add memory storage keyword to public, private and internal complex parameters that don’t already have a defined parameter storage location.
  4. Rename constructor function definition from function ContractName to constructor.
  5. Add default function visibility of public to those functions which don’t have function visibility explicitly defined.
  6. Add function visibility of external to fallback functions and all functions of any interface.
  7. Convert constant function state mutability modifier to view.
  8. Prettify your code using prettier.

Usage

Using this plugin is very straightforward and only requires two simple commands:

Firstly, you need to install the npm plugin via
npm install --save-dev prettier prettier-plugin-solidity-refactor
Secondly, you need to execute the plugin using
./node_modules/.bin/prettier --write --tab-width 4 --print-width 140 '**/*.sol'

More detailed usage instructions and information is available on the GitHub repo.

Feel free to play around with the plugin and let me know if you find any bug. Refactoring your solidity code should be less boring now 🙂

Disclaimer
Remember that this plugin is still in testing and lacks a few features. Nevertheless, it should save a decent chunk of time if you are planning on refactoring your code. Read more about its limitations and known bugs on GitHub and always manually verify all your code. I won’t be liable if your cat loses one of their live or the ethereum blockchain hangs due to the use of this plugin :).

Spread the love

2 thoughts on “[Tool] Refactor your solidity 0.4.x code to solidity 0.5.x code”

  1. I ran your tool on http://github.com/marbleprotocol/flash-lending, but now I’m getting this error:
    “`
    > truffle test

    Error parsing /contracts/bank/contracts/example/Arbitrage.sol: ParsedContract.sol:60:86: ParserError: Expected ‘,’ but got identifier
    function submitTrade(address token, uint256 amount, address dest, bytes calldata data) external {
    ^–^
    Compilation failed. See above.
    “`

    Removing “calldata” just shifts the error over to other parts of my code that also had `bytes data` before.

    “`
    ParserError: Expected ‘,’ but got identifier
    function submitTrade(address token, uint256 amount, address dest, bytes calldata data) external {
    “`

    It doesn’t like the “calldata” being added to bytes.

Leave a Comment

Your email address will not be published. Required fields are marked *