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:
- Change solidity pragma version
to ^0.5.0
. Add calldata
storage keywordto external
function complex parameters like arrays that lack an already defined storage location.Add memory
storage keyword to public, private and internal complex parameters that don’t already have a defined parameter storage location.- Rename constructor function definition
from function ContractName
to constructor
. - Add default function visibility
of public
to those functions which don’t have function visibility explicitly defined. - Add function visibility
of external
to fallback functions and all functions ofany interface. Convert constant
function state mutability modifierto view
. - 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 vianpm install --save-dev prettier prettier-plugin-solidity-refactorSecondly, 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 :).
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.
Seems like you are using truffle 4 which does not support solidity 0.5.x.
Please upgrade to truffle 5 to compile solidity 0.5.x contracts.