loopring_sdk icon indicating copy to clipboard operation
loopring_sdk copied to clipboard

Importing sdk in express js project

Open fubelli opened this issue 3 years ago • 7 comments

I am trying to develop an expressjs app and inside my project I would like to use the following sdk: https://loopring-1.gitbook.io/loopring-dev-docs/sdk/sdk-guides

Following the instructions, I have installed the package via npm and I created a file inside my project named loop.js where I have pasted the starting code provided in the website:

import * as sdk from "@loopring-web/loopring-sdk";
export class LoopringAPIClass {
  public static userAPI: UserAPI;
  public static exchangeAPI: ExchangeAPI;
  public static ammpoolAPI: AmmpoolAPI;
  public static walletAPI: WalletAPI;
  public static wsAPI: WsAPI;
  public static nftAPI: NFTAPI;
  public static delegate: DelegateAPI;
  public static globalAPI: GlobalAPI;
  public static contractAPI: typeof ContractAPI;
  public static __chainId__: sdk.ChainId;
  public static InitApi = (chainId: sdk.ChainId) => {
    LoopringAPI.userAPI = new UserAPI({ chainId });
    LoopringAPI.exchangeAPI = new ExchangeAPI({ chainId });
    LoopringAPI.globalAPI = new GlobalAPI({ chainId });
    LoopringAPI.ammpoolAPI = new AmmpoolAPI({ chainId });
    LoopringAPI.walletAPI = new WalletAPI({ chainId });
    LoopringAPI.wsAPI = new WsAPI({ chainId });
    LoopringAPI.nftAPI = new NFTAPI({ chainId });
    LoopringAPI.delegate = new DelegateAPI({ chainId });
    LoopringAPI.__chainId__ = chainId;
    LoopringAPI.contractAPI = ContractAPI;
  };
}
/* env:
 * test:  sdk.ChainId.GOERLI 
 * eth:  sdk.ChainId.MAINNET 
 */
LoopringAPIClass.InitApi({sdk.ChainId.MAINNET}); 

When I try to run the code in nodejs I get the following error: error1

I have tried to add the "type": "module" line in package.json but then I got this different kind of error by running the import line:

error2

Could you please help me to integrate the sdk in my project?

fubelli avatar Dec 28 '22 19:12 fubelli

try removing LoopringAPIClass.InitApi({sdk.ChainId.MAINNET}); from above file and calling it from your server.js or index.js file

ZeeshanAhmadKhalil avatar Dec 29 '22 07:12 ZeeshanAhmadKhalil

Hello, I've tried the following setup but I keep getting the same error.

loop.js

import * as sdk from "@loopring-web/loopring-sdk";
export class LoopringAPIClass {
  public static userAPI: UserAPI;
  public static exchangeAPI: ExchangeAPI;
  public static ammpoolAPI: AmmpoolAPI;
  public static walletAPI: WalletAPI;
  public static wsAPI: WsAPI;
  public static nftAPI: NFTAPI;
  public static delegate: DelegateAPI;
  public static globalAPI: GlobalAPI;
  public static contractAPI: typeof ContractAPI;
  public static __chainId__: sdk.ChainId;
  public static InitApi = (chainId: sdk.ChainId) => {
    LoopringAPI.userAPI = new UserAPI({ chainId });
    LoopringAPI.exchangeAPI = new ExchangeAPI({ chainId });
    LoopringAPI.globalAPI = new GlobalAPI({ chainId });
    LoopringAPI.ammpoolAPI = new AmmpoolAPI({ chainId });
    LoopringAPI.walletAPI = new WalletAPI({ chainId });
    LoopringAPI.wsAPI = new WsAPI({ chainId });
    LoopringAPI.nftAPI = new NFTAPI({ chainId });
    LoopringAPI.delegate = new DelegateAPI({ chainId });
    LoopringAPI.__chainId__ = chainId;
    LoopringAPI.contractAPI = ContractAPI;
  };
}

server.js

const LoopringAPIClass = require("./loop")

And finally I've added the "type": "module" line in the package.json file in order to avoid the first kind of error.

The error I get by running the line inside server.js is the following:

screen3

fubelli avatar Dec 29 '22 10:12 fubelli

There are several issues with this library:

  1. The library is built as an ESM package, but the "main" in package.json points to a CommonJS file. This is what causes the original error for the author of this issue. FYI, the "module" entry in the manifest is not understood by Node, it's probably for the bundler.

    You would either need to replace the content of the main entry and point to the ESM version of the file. But if people rely on the CJS implementation that is imported from index.js, then you should probably remove the main entry and replace it with a modern exports entry, with separate import and require keys (for ESM version and CommonJS version respectively).

  2. The library imports a lot of core-js, but it's not in the source code, and it's not mentioned in the package.json's dependencies. This is mostly because the core-js is added during the bundling process. I don't know if it's required at all for Node. Anyway, when you fix the problem no. 1, you still cannot use the package, because the package does not define its dependencies correctly.

    You should either get rid of the core-js polyfill library or make it an explicit dependency somehow (not sure which is better here).

  3. The TS typings are wrong -- VSCode only highlights 4 top-level exports, nothing more. There's no way to correctly import the types of all the APIs and other utils that are exported from nested subdirectories.

    Screenshot 2023-11-17 at 18 03 04

    I don't yet know how to fix this issue.

akwodkiewicz avatar Nov 20 '23 10:11 akwodkiewicz

can you support a environment for me can reproduce your you problem

windatang avatar Nov 20 '23 18:11 windatang

Yes, I'll try to deliver it later this week. I also want to provide several PRs that should resolve some of these issues or improve DX of this project. But first, the simple example repo!

akwodkiewicz avatar Nov 21 '23 22:11 akwodkiewicz

Here's the example app. It's basically a "hello world" with a couple of imports.

https://github.com/akwodkiewicz/loopring-sdk-usage-example

There are 2 versions of the app on 2 branches: commonjs and esm. The issue is on both of them.

akwodkiewicz avatar Nov 22 '23 16:11 akwodkiewicz

old version package cjs with js wrap. new version 3.8+ will dirctory package with cjs. you can directory use required.

Code change in package.json

  "source": "src/index.ts",
  "exports": {
    "require": "./dist/index.cjs",
    "import": "./dist/index.esm.js"
  },```
  demo code 
  ```  info('imported default:', nodeFetch)
  const library = require('@loopring-web/loopring-sdk') //await import(); // runtime error
  const globalAPI = new library.GlobalAPI({ chainId: 5 })
  // Dynamic import of @loopring-web/loopring-sdk that claims to be ESM
  info('imported default:', globalAPI)```

windatang avatar Dec 05 '23 09:12 windatang