hyperledger / web3j-maven-plugin

web3j Maven plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't generateContractWrappers for solidity 0.8.x due result json change format

longdt opened this issue · comments

I have simple solidity contract:


contract SimpleStorage {
    uint storedData;

    function set(uint x) public {
        storedData = x;
    }

    function get() public view returns (uint) {
        return storedData;
    }
}

when I run: mvn web3j:generate-sources -e -X command I got error:
class org.json.simple.JSONArray cannot be cast to class java.lang.String (org.json.simple.JSONArray is in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @51dbd6e4; java.lang.String is in module java.base of loader 'bootstrap')
Digging more plugin log, I found some change from result json of solidity compiler 0.8.x

DEBUG] Result: {"contracts":{"/home/longdt/IdeaProjects/bscexample/src/main/resources/SimpleStorage.sol:SimpleStorage":{"abi":[{"inputs":[],"name":"get","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"}],"bin":"608060405234801561001057600080fd5b5060c38061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806360fe47b11460375780636d4ce63c146048575b600080fd5b60466042366004606d565b6062565b005b604e6067565b604051605991906084565b60405180910390f35b600055565b60005490565b600060208284031215607d578081fd5b5035919050565b9081526020019056fea2646970667358221220c3e27a2753ab4a634d87f9c7953cf1214a691cd65ad224ea42b29f594c0e8a8e64736f6c63430008010033","metadata":"{"compiler":{"version":"0.8.1+commit.df193b15"},"language":"Solidity","output":{"abi":[{"inputs":[],"name":"get","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"}],"devdoc":{"kind":"dev","methods":{},"version":1},"userdoc":{"kind":"user","methods":{},"version":1}},"settings":{"compilationTarget":{"/home/longdt/IdeaProjects/bscexample/src/main/resources/SimpleStorage.sol":"SimpleStorage"},"evmVersion":"istanbul","libraries":{},"metadata":{"bytecodeHash":"ipfs"},"optimizer":{"enabled":true,"runs":200},"remappings":[]},"sources":{"/home/longdt/IdeaProjects/bscexample/src/main/resources/SimpleStorage.sol":{"keccak256":"0x866a2ad7baba979760a3f8399e5a67c5f84c28b1c4854a9f05b41de957d00907","urls":["bzz-raw://b054291d54d7d923a51e0c9254b37fc85e8907577eb82e623dd9c6cca804fe63","dweb:/ipfs/QmXDigRMzg7DTKZCHugyHJUqmPFbajRbF4yaCkoCDp1kRf"]}},"version":1}"}},"version":"0.8.1+commit.df193b15.Linux.g++"}

Specifically, abi field change data type from string (in version <= 0.7.x) to array (in version 0.8.x), so it cause following code in class JavaClassGeneratorMojo throws casting exception

List<AbiDefinition> functionDefinitions = loadContractDefinition(results.get(SolidityCompiler.Options.ABI.getName()));
or
String abiJson = contractResult.get(SolidityCompiler.Options.ABI.getName());

  • OS: Ubuntu 20.04
  • Java: 17
  • Solidity: 0.8.x

It seen low develop activity in this repo. I fixed by make a fork.

Thanks

I had the same issue and your fork fixed it for me. Thanks.

I have the same problem under Ubuntu, but not on Windows.

Using @longdt fork all is fine.

It works for me.Thanks very much! @longdt

here is also a PR part of this fix, it will be merged soon
#89

@gtebrean There is already a PR for this bug but they doesn't merge it #85 So don't hope It will be merged soon. And you should use my fork because this plugin doesn't work with solidity compiler version >= 0.8.10