xf00f / web3x

Ethereum TypeScript Client Library - for perfect types and tiny builds.
211 stars 27 forks source link

internalType (Solidity 0.5.11+) with web3x-codegen - TS errors with ABI TS files #64

Closed uhhhh2 closed 4 years ago

uhhhh2 commented 5 years ago

In Solidity version 0.5.11 (https://github.com/ethereum/solidity/releases/tag/v0.5.11), an internalType key was added to the generated JSON ABI objects produced by the Solidity compiler.

When I compile contracts with version 0.5.10 of the Solidity compiler, I get entries like the following in the JSON ABI objects produced:

{
      "constant": true,
      "inputs": [
        {
          "name": "data",
          "type": "uint256[]"
        }
      ],
      "name": "myFunction",
      "outputs": [
        {
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "pure",
      "type": "function"
    }

When I compile contracts with version 0.5.11 of the Solidity compiler, I get entries like the following in the JSON ABI objects produced:

{
      "constant": true,
      "inputs": [
        {
          "internalType": "uint256[]",
          "name": "data",
          "type": "uint256[]"
        }
      ],
      "name": "myFunction",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "pure",
      "type": "function"
    }

web3x-codegen appears to prepare the TypeScript ABI files (the MyContractAbi.ts files generated from the Solidity contract MyContract) by copying the entries from the Solidity compiler's generated ABI JSON files, regardless of which subsets of each entry exist in the corresponding TypeScript type definitions in web3x. Thus, the new internalType field gets copied into the MyContractAbi.ts files even though the web3x AbiInput and AbiOutput types (what I think are the web3x types for the objects in the inputs and outputs arrays above) don't have internalType fields.

When I compile a project that uses the MyContract.ts and MyContractAbi.ts files produced when MyContract was compiled with Solidity 0.5.11, the presence of the new internalType key not mentioned in the web3x AbiInput and AbiOutput types causes the TypeScript compiler to output errors like the following:

Type '{ "internalType": string; "name": string; "type": string; }' is not assignable to type 'AbiInput'. 
 Type '{ "internalType": string; "name": string; "type": string; }' is not assignable to type 'AbiOutput'.

Since the entries of the inputs and outputs arrays for the smart contract functions are "not assignable" to the AbiInput and AbiOutput types, the TypeScript compiler throws additional "not assignable" errors with the enclosing ContractEntryDefinition objects.

uhhhh2 commented 5 years ago

What would be the best way to address this:

xf00f commented 5 years ago

Thanks for the info. Probably the former. There is PR submitted for this: https://github.com/xf00f/web3x/pull/65

I will attempt to merge later this week.

xf00f commented 4 years ago

Should be fixed in v4.0.6