MoralisWeb3 / Moralis-JS-SDK

Moralis Official Javascript SDK
https://docs.moralis.io
Other
366 stars 257 forks source link

Moralis.Web3API type incompatibility #115

Closed emr closed 2 years ago

emr commented 2 years ago

Issue Description

I want to send requests to Moralis Web3Api from my nodejs application. For doing this, I use Moralis.Web3API.

First, I call Moralis.Web3API.initialize and then what I want to call from the API. It works but there is a type incompatibility for initialize method of Web3Api class.

Code

import Moralis from 'moralis/node';

Moralis.Web3API.initialize({
  serverUrl: 'https://ve...xm.usemoralis.com:2053/server',
  apiKey: 'dU...dr',
});

Moralis.Web3API.account.getNFTTransfers({
  chain: 'eth',
  address: '0xeC1...Aa8',
  order: 'desc',
}).then(console.log);

In the source code, the method expects an object with serverUrl and apiKey props. https://github.com/MoralisWeb3/Moralis-JS-SDK/blob/main/src/MoralisWeb3Api.js#L13

CleanShot 2021-11-12 at 13 40 11@2x

But in the generated type, the method expects only a string that will be used as server URL. https://github.com/MoralisWeb3/Moralis-JS-SDK/blob/main/types/generated/web3Api.d.ts#L1869

CleanShot 2021-11-12 at 13 36 47@2x

Result

Moralis.Web3API.initialize('https://ve...xm.usemoralis.com:2053/server');

If I want to call the method as above as declared in the type file, naturally, I get this error:

/Users/me/my_project/node_modules/moralis/lib/node/MoralisWeb3Api.js:37
      throw new Error('Web3Api.initialize failed: initialize with apiKey or serverUrl');
      ^

Error: Web3Api.initialize failed: initialize with apiKey or serverUrl
    at Function.initialize (/Users/me/my_project/node_modules/moralis/lib/node/MoralisWeb3Api.js:37:13)
    at Object.<anonymous> (/Users/me/my_project/my_file.js:3:17)

Actually, I haven't seen anything about initializing Moralis and sending API calls through the SDK with nodejs in documentations. So I tried to use SDK in this way. Am I using SDK in the wrong way? So what should I do? How can I use the SDK in nodejs properly?

Thank you all!

ErnoW commented 2 years ago

The types have been updated and should be correct now. You were correct that providing an object with apiKey and serverUrl is the right way. In fact, you should not have to use Moralis.Web3API.initialize manually. You can call Moralis.start which will initialize everything for you (setting serverUrl and appId as well for example).

If you want to use Moralis.start in a NodeJs environment, then you can use the moralisSecret (instead of manually providing the apiKey, this will also initialize web3 via the speedy-nodes). You can find this moralisSecret when you login to Moralis > account-settings > API. When you initialize moralis with this secret, then you can use the web3Api, and you don't even have to provide the serverUrl (but then you will have limited features, ex. no access to database functionalities)

We will add it to the documentation. You're right that we have not updated it there as it has just been released.

If you have more questions, please reach out to our forums, as the support team is more active over there :)