polkadot-js / api

Promise and RxJS APIs around Polkadot and Substrate based chains via RPC calls. It is dynamically generated based on what the Substrate runtime provides in terms of metadata.
Apache License 2.0
1.07k stars 354 forks source link

Unable to initialize the API: You need to pass a valid Metadata instance to Decorated #2885

Closed heipacker closed 3 years ago

heipacker commented 3 years ago

// Required imports const {ApiPromise, WsProvider} = require('@polkadot/api');

async function main() { // Initialise the provider to connect to the local node const provider = new WsProvider('wss://wspolkadot.mytokenpocket.vip');

// Create the API and wait until ready
const api = await ApiPromise.create({provider});

// Retrieve the chain & node information information via rpc calls
const [chain, nodeName, nodeVersion] = await Promise.all([
    api.rpc.system.chain(),
    api.rpc.system.name(),
    api.rpc.system.version()
]);

console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`);

}

main().catch(console.error).finally(() => process.exit());

v2.7.1 or v2.8.1

jacogr commented 3 years ago

Honestly, no idea - the same URL works correctly via the apps UI. Can you please share your package.json?

So this error You need to pass a valid Metadata instance to Decorated would point to a @polkadot/types not matching with the @polkadot/api (The versions need to align between the two)

heipacker commented 3 years ago

{ "name": "server", "version": "0.0.1", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "@babel/runtime": "^7.10.2", "@digicat/ethereum-contract-watcher": "^1.0.0", "@polkadot/api": "^2.7.1", "async": "^2.1.4", "axios": "^0.19.0", "base64-arraybuffer": "^0.1.5", "bluebird": "^3.4.6", "body-parser": "^1.19.0", "config": "^1.24.0", "connect-redis": "^3.2.0", "cookie-parser": "^1.4.4", "cookie-session": "^1.3.3", "debug": "~0.7.4", "express": "^4.17.1", "express-cluster": "0.0.4", "express-domain-middleware": "^0.1.0", "express-ipv4": "0.0.1", "express-session": "^1.14.2", "express-ws": "^2.0.0", "express-xml-bodyparser": "^0.3.0", "log4js": "^1.0.1", "md5": "^2.2.1", "moment": "^2.18.1", "moment-timezone": "^0.5.10", "morgan": "^1.9.1", "multer": "^1.2.0", "node-cache": "^4.1.0", "node-schedule": "^1.2.0", "node-summary": "^1.1.0", "nodemailer": "^3.1.8", "querystring": "^0.2.0", "redis-clustr": "^1.5.2", "request": "^2.79.0", "sha1": "^1.1.1", "static-favicon": "~1.0.0", "string": "^3.3.3", "stringformat": "0.0.5", "xml": "^1.0.1", "xml2js": "^0.4.17", "xss": "^0.3.3" } }

heipacker commented 3 years ago

same error when change to "@polkadot/api": "^2.8.1",

jacogr commented 3 years ago

No explicit installs of @polkadot/types. What does npm list @polkadot/types yield?

heipacker commented 3 years ago

└─┬ @polkadot/api@2.8.1 ├─┬ @polkadot/api-derive@2.8.1 │ └── @polkadot/types@2.8.1 deduped ├─┬ @polkadot/metadata@2.8.1 │ ├── @polkadot/types@2.8.1 deduped │ └─┬ @polkadot/types-known@2.8.1 │ └── @polkadot/types@2.8.1 deduped ├─┬ @polkadot/rpc-core@2.8.1 │ └── @polkadot/types@2.8.1 deduped ├─┬ @polkadot/rpc-provider@2.8.1 │ └── @polkadot/types@2.8.1 deduped ├─┬ @polkadot/types@2.8.1 │ └─┬ @polkadot/metadata@2.8.1 │ ├── @polkadot/types@2.8.1 deduped │ └─┬ @polkadot/types-known@2.8.1 │ └── @polkadot/types@2.8.1 deduped └─┬ @polkadot/types-known@2.8.1 └── @polkadot/types@2.8.1 deduped

heipacker commented 3 years ago

It doesn't look unusual

jacogr commented 3 years ago

At a loss what is wrong there. My next step would be to remove node_modules and try, something is very weird with the installed deps there.

This is what I just did:

  1. init a simple clean package
$ mkdir -p npm-test
$ cd npm-test 
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (npm-test) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to test/npm-test/package.json:

{
  "name": "npm-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Is this OK? (yes) 
  1. add @polkadot/api (2.8.1 as per above)
$ npm add @polkadot/api@2.8.1

> bufferutil@4.0.2 install /Users/jaco/Projects/polkadot-js/test/npm-test/node_modules/bufferutil
> node-gyp-build

> utf-8-validate@5.0.3 install /Users/jaco/Projects/polkadot-js/test/npm-test/node_modules/utf-8-validate
> node-gyp-build

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN npm-test@1.0.0 No description
npm WARN npm-test@1.0.0 No repository field.

+ @polkadot/api@2.8.1
added 84 packages from 104 contributors and audited 84 packages in 30.22s

1 package is looking for funding
  run `npm fund` for details

found 0 vulnerabilities
  1. created index.js with the exact contents above
$ node ./index.js 
You are connected to chain Polkadot using Parity Polkadot v0.8.26-1-803da90f7-x86_64-linux-gnu
heipacker commented 3 years ago

delete this deps "@digicat/ethereum-contract-watcher": "^1.0.0"

ok now

polkadot-js-bot commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.