Error: Unexpected string in JSON at position 23357

=======
EDITS:
I found where the issue is:

I have a file called IERC20.sol

    pragma solidity ^0.5.10;

    interface IERC20 {
        function transfer(address to, uint256 value) external returns (bool);
        function approve(address spender, uint256 value) external returns (bool);
        function transferFrom(address from, address to, uint256 value) external returns (bool);
        function balanceOf(address who) external view returns (uint256);
        function allowance(address owner, address spender) external view returns (uint256);
    }

Somehow the .json that gets generated for this interface has this problem: End of file expected

But somehow this error was not there initially, and then it crept up whilst I was coding and using oz CLI. I cannot remember the exact steps that led to this error suddenly appearing. I did not change the IERC20.sol file itself. I only delete one import of it in one file.

=========
I have recently ran into an issue on multiple occasions when trying to oz add or oz create after making some contract changes and running oz compile again. Somehow this Unexpected string in JSON at position 23357 comes creeping up. Even though everything oz compile s successfully.

:computer: Environment
macOS Catalina
oz/CL: 2.5.3
the rest:
β”œβ”€β”€ @openzeppelin/contracts@2.3.0

β”œβ”€β”€ @openzeppelin/upgrades@2.5.3

β”œβ”€β”€ @truffle/hdwallet-provider@1.0.22

└── dotenv@8.2.0

nothing else

:memo:Details
I used the oz cli as normal. I do some stuff. I was able to oz create. I do some more stuff. I want to oz create again. This error creeps up. I delete the .openzeppelin folder and I oz init fresh again. I copy paste my contracts/. I oz compile successfully. Still oz create or oz add fail with Unexpected string in JSON at position 23357.

:1234: Code to reproduce
It’s very hard to reproduce. I guess you need to have macOS Catalina and

  • mkdir test
  • cd test
  • npm init -y
  • oz init
  • make some contracts
  • oz compile
  • oz create
  • do stuff
  • do stuff
  • rename a contract
  • do stuff
  • oz compile
  • oz create

and maybe you get the error too.

This seems like a tough one. I would appreciate your help.

1 Like

Hi @gitpusha,

I can reproduce intermittently.

It appears to occur when you have two contracts with the same name with different contents in the project result in a single json which contains the two artifacts with the shorter one on top of the longer one.

I suggest you check if you are importing another IERC20 contract (e.g. IERC20 from openzeppelin-contracts-ethereum-package https://github.com/OpenZeppelin/openzeppelin-contracts-ethereum-package/blob/master/contracts/token/ERC20/IERC20.sol) as well as having one of your own.

There is an open issue for two contracts with the same name resulting in invalid JSON output, I have added my steps to reproduce:

1 Like

Hi @gitpusha (and for future community members reading this)

As per: Error: Cannot find module 'create-hash'

Looking at your packages installed, you currently have @openzeppelin/contracts rather than @openzeppelin/contracts-ethereum-package :

https://docs.openzeppelin.com/sdk/2.5/linking
Make sure you install @openzeppelin/contracts-ethereum-package and not the vanilla @openzeppelin/contracts . The latter is set up for general usage, while @openzeppelin/contracts-ethereum-package is tailored for being used with the OpenZeppelin SDK. This means that its contracts are already set up to be upgradeable.

Though use version 2.2.3 due to OpenZeppelin/openzeppelin-contracts-ethereum-package#70

oz link @openzeppelin/contracts-ethereum-package@2.2.3