Search code examples
polkadot-jsrust-ink

What is causing this Invalid JSON ABI structure error?


I'm trying to deploy an ink! contract through polkadotJS.


var WASM = fs.readFileSync('./resources/flipper.wasm');
var ABI = fs.readFileSync('./resources/metadata.json');

const api = await ApiPromise.create();

const code = new CodePromise(api,ABI,WASM);

When executing, I get this error:

Error: Invalid JSON ABI structure supplied, expected a recent metadata version

According to Polkadot, the error is caused by a version lower than 3.0-rc1. Yet my version is ink! 3.0.0-rc3

my abi:

{
  "metadataVersion": "0.1.0",
  "source": {
    "hash": "0x7fbad529eb12d718da29468d27aa3f7b202bec25411f58d32999166ff614cf7f",
    "language": "ink! 3.0.0-rc3",
    "compiler": "rustc 1.53.0-nightly"
  },
  "contract": {
    "name": "flipper",
    "version": "0.1.0",
    "authors": [
      "[your_name] <[your_email]>"
    ]
  },
  "spec": {
    "constructors": [
      {
        "args": [
          {
            "name": "init_value",
            "type": {
              "displayName": [
                "bool"
              ],
              "type": 1
            }
          }
        ],
        "docs": [
          "Constructor that initializes the `bool` value to the given `init_value`."
        ],
        "name": [
          "new"
        ],
        "selector": "0x9bae9d5e"
      },
      {
        "args": [],
        "docs": [
          "Constructor that initializes the `bool` value to `false`.",
          "",
          "Constructors can delegate to other constructors."
        ],
        "name": [
          "default"
        ],
        "selector": "0xed4b9d1b"
      }
    ],
    "docs": [],
    "events": [],
    "messages": [
      {
        "args": [],
        "docs": [
          " A message that can be called on instantiated contracts.",
          " This one flips the value of the stored `bool` from `true`",
          " to `false` and vice versa."
        ],
        "mutates": true,
        "name": [
          "flip"
        ],
        "payable": false,
        "returnType": null,
        "selector": "0x633aa551"
      },
      {
        "args": [],
        "docs": [
          " Simply returns the current value of our `bool`."
        ],
        "mutates": false,
        "name": [
          "get"
        ],
        "payable": false,
        "returnType": {
          "displayName": [
            "bool"
          ],
          "type": 1
        },
        "selector": "0x2f865bd9"
      }
    ]
  },
  "storage": {
    "struct": {
      "fields": [
        {
          "layout": {
            "cell": {
              "key": "0x0000000000000000000000000000000000000000000000000000000000000000",
              "ty": 1
            }
          },
          "name": "value"
        }
      ]
    }
  },
  "types": [
    {
      "def": {
        "primitive": "bool"
      }
    }
  ]
}

So my ink! version is not the issue, what else is causing this? Or am I maybe doing something else wrong?


Solution

  • So when I imported the .contract file like this

    var contract = fs.readFileSync('./resources/flipper.contract');
    
    const json = u8aToString(contract);
    
    const ABI = new Abi(json, api.registry.getChainProperties());
    
    

    the error was solved! I used the contract blob instead of the wasm and abi json