truffle-flattener: Could not parse smart contract with "unchecked block" syntax
When I flatten a certain smart contract that uses openzeppelin’s SafeMath library I’m getting this error:
Error: Could not parse @openzeppelin/contracts/utils/math/SafeMath.sol for extracting its imports: ParserError: missing ';' at '{'
Upon investigation, flattening fails when encountering an unchecked block.
unchecked block is a valid solidity syntax introduced since 0.8.0, check reference here.
Sample code to reproduce the error:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
library SafeMath {
. . .
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a / b);
}
}
. . .
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 11
- Comments: 18 (2 by maintainers)
You can also do
npm i https://github.com/jplew/truffle-flattenerthat will replace currenttruffle-flattenerpackage with branch linked in PR #81. It works fineThe problem is due to an outdated version of @solidity-parser/parser. See this PR: https://github.com/nomiclabs/truffle-flattener/pull/81
If you can’t wait for the maintainers to rerelease the package, just clone mine, npm install, then run
./index.js MyContract.solinstead oftruffle-flattener MyContract.sol.you can also npm package, then npm install the artifact and still use truffle-flattener system wide 😃 in case you got scripts depending on it
I still got the same issue today. I’m using the truffle-flattener 1.5.0 now
This is not working as I am getting the same error
The parser lib (dependency) is outdated. Those PRs need to be merged to upgrade it. This one should work https://github.com/ngduc/flatten-sol