Web3.swift icon indicating copy to clipboard operation
Web3.swift copied to clipboard

Loading Dynamic Contract: "Expected to decode Array<Any> but found a dictionary instead."

Open fflach opened this issue 6 years ago • 9 comments

Hey Ybrin, I am currently working with your example for dynamic contracts (Boilertalk - Dynamic Contracts) but I get an error when I am trying to load the contract.

 // Reading json ABI
    if let path = Bundle.main.path(forResource: "BlockRunner", ofType: "json") {
        do {
            let web3 = Web3(rpcURL: RPC)
            let contractAddress = try! EthereumAddress(hex: "--------", eip55: false)
            let data = try String(contentsOf: URL(fileURLWithPath: path))
            let contractJsonABI = data.data(using: .utf8)!
            // Line where the error occurs
            // type(of: contractJsonABI) = Foundation.Data
            let contract = try! web3.eth.Contract(json: contractJsonABI, abiKey: nil, address: contractAddress)

Error: Swift.DecodingError.typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))

fflach avatar Aug 21 '18 12:08 fflach

So I could solve this error by following the loadStub function. Unfortunately I ran directly in a new problem I can´t solve. It seems to be an issue with the json file:

debugDescription: "No value associated with key CodingKeys(stringValue: "inputs", intValue: nil) ("inputs")."

Thank you!

if let path = Bundle.main.path(forResource: "BlockRunner", ofType: "json") {
        do {
            let web3 = Web3(rpcURL: RPC)
            let contractAddress = try! EthereumAddress(hex: "-------", eip55: false)
            let url = URL(fileURLWithPath: path)
            let data = try? Data(contentsOf: url)
            // Line where also the new error occurs
            let contract = try! web3.eth.Contract(json: data!, abiKey: "abi", address: contractAddress)

Complete Error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "inputs", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 1", intValue: 1)], debugDescription: "No value associated with key CodingKeys(stringValue: "inputs", intValue: nil) ("inputs").", underlyingError: nil))

JSON Structure:

{
  "contractName": "BlockRunner",
  "abi": [
    {
      "inputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "constructor"
    },
    {
      "payable": true,
      "stateMutability": "payable",
      "type": "fallback"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "date",
          "type": "uint256"
        },
        {
          "name": "numberOfSteps",
          "type": "uint256"
        }
      ],
      "name": "newSteps",
      "outputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "addr",
          "type": "address"
        },
        { ......

fflach avatar Aug 21 '18 14:08 fflach

Hey @fflach

Which version of Web3 are you using in your example above? I'm gonna look into this in detail tomorrow...

koraykoska avatar Aug 21 '18 21:08 koraykoska

Thank you for your quick response! When installing the pod the listed version is Web3 (0.3.0) . I deployed the contract with the current version of truffle. The contract is written in solidity version 0.4.23.

fflach avatar Aug 22 '18 08:08 fflach

I think this is a bug in the ABIObject decoder.

In this line decode should be replaced with decodeIfPresent to make inputs really optional.

I couldn't test it yet as I can't directly reproduce the error. @fflach Could you send me either the full json abi or an example project?

@pixelmatrix What do you think? Is this the cause for this error?

@fflach In the meantime you could try and add empty inputs arrays into all abi objects in your json and report back whether it resolves this bug or not.

koraykoska avatar Aug 22 '18 12:08 koraykoska

@Ybrin has this PR been released yet? I believe this should fix the inputs issue on fallback functions: https://github.com/Boilertalk/Web3.swift/pull/50

pixelmatrix avatar Aug 22 '18 18:08 pixelmatrix

@pixelmatrix Oh sorry, I missed that. I'm gonna release it now...

koraykoska avatar Aug 22 '18 18:08 koraykoska

Awesome. Hopefully that will solve it. The original issue is because you need to pass "abi" as the key so it can find the abi inside the json. Might be worth updating the documentation to mention that since compiled Truffle artifacts always have the abi nested.

pixelmatrix avatar Aug 22 '18 18:08 pixelmatrix

I don't know how to use the Function or Event that In Contract,Please tell me In your free,Thank you very much

MrCodeHauler avatar Aug 27 '18 01:08 MrCodeHauler

@Mr-Lin-Old Please join our Telegram chat with the link below if you have a question which is not an issue with the library.

https://t.me/joinchat/BPk3DE6CTFaiOolSIZNLyg

koraykoska avatar Sep 01 '18 15:09 koraykoska