ocean.js icon indicating copy to clipboard operation
ocean.js copied to clipboard

Data NFT Publishing Code Example import errors

Open smejak opened this issue 2 years ago • 12 comments

Describe the bug I am trying to run the ocean.js Code Examples but am running into issues with the imports and the typescript syntax.

Initially, I had linting errors due to the javascript file having typescript syntax, which I solved by removing the types, but then when trying to run the marketplace.js script (created from the code examples), I got the following errors on the imports:

The imports were initially from the ocean.js repo, not the ocean npm library, so I replaced those.

import { SHA256 } from 'crypto-js'                                                                                                                                                                                                            
         ^^^^^^                                                                                                                                                                                                                               
SyntaxError: Named export 'SHA256' not found. The requested module 'crypto-js' is a CommonJS module, which may not support all module.exports as named exports.

This one I solved by removing the curly brackets.

file:///Users/jakub/Development/marketplace-quickstart/marketplace.js:4                                                                                                                                   
  AmountsOutMaxFee,                                                                                                                                                                                                                           
  ^^^^^^^^^^^^^^^^                                                                                                                                                                                                                            
SyntaxError: Named export 'AmountsOutMaxFee' not found. The requested module '@oceanprotocol/lib' is a CommonJS module, which may not support all module.exports as named exports.

Again curly brackets, attempted solution with import * as o from '@oceanprotocol/lib', although that resulted in the last error which I wasn't able to solve.

Initially, I had

To Reproduce Steps to reproduce the behavior:

  1. Go to https://github.com/oceanprotocol/ocean.js/blob/main/CodeExamples.md
  2. Go through the tutorial and put all javascript code into marketplace.js
  3. Create package.json as described in the tutorial, start barge, run npm i
  4. Add "type": "module" in package.json, it should look like this:
{
  "name": "marketplace-quickstart",
  "type": "module",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@oceanprotocol/contracts": "1.0.0-alpha.28",
    "@oceanprotocol/lib": "^1.0.0-next.37",
    "crypto-js": "^4.1.1",
    "web3": "^1.7.3"
  }
}
  1. Remove explicit type declarations to avoid syntax errors
  2. Remove the local imports (cannot work unless running in a clone of the ocean.js repo) and replace with
import SHA256 from 'crypto-js'
import * as o from '@oceanprotocol/lib'
  1. Add o. to every oceanlib function/class that is used in the script (can be found from the orginial imports)
  2. Run node marketplace.js in your terminal
  3. See the error https://gist.github.com/smejak/468cc1f635b60a6526fa9ec48eab8789

Expected behavior The marketplace publishing flow as described in Code Examples

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context Add any other context about the problem here.

  • npm version 8.12.1
  • Node.js v18.5.0

smejak avatar Jul 14 '22 06:07 smejak

@AnaLoznianu @jamiehewitt15 We also need to adapt the changes added in https://github.com/oceanprotocol/ocean.js/pull/1537 to the CodeExamples.md file to solve these issues.

miquelcabot avatar Jul 14 '22 11:07 miquelcabot

@miquelcabot I tried implementing the updates to CodeExamples.md from #1537 but I'm running into the same errors.

Firstly, where does the config

import { web3, getTestConfig, getAddresses } from '../config'

come from? Is it supposed to be from oceanlib as well or something else?

Also, CodeExamples.md in #1537 still says to create a marketplace.js, but then uses typescript syntax like type annotations or the ? modifier, which cause errors in the javascript linter (at least on my side).

smejak avatar Jul 19 '22 04:07 smejak

@smejak These files/imports come from the /test folder. And you are right referring to the .js/.ts problem. We need to review these issues in both CodeExamples.md and C2DExamples.md files. Thanks for your comments!

miquelcabot avatar Jul 19 '22 05:07 miquelcabot

@miquelcabot Thank you for the response! Regarding my earlier post where I modified the imports & resolved the ts/js problem, do you have an idea about what might be causing the error I linked here https://gist.github.com/smejak/468cc1f635b60a6526fa9ec48eab8789?

smejak avatar Jul 19 '22 05:07 smejak

@smejak They seem to be problems related to the package.json or tsconfig.json configuration files. You can check this repository https://github.com/miquelcabot/themap-ocean.js to see a simple use of the Ocean.js library. Warning: it's WIP!

miquelcabot avatar Jul 19 '22 05:07 miquelcabot

@AnaLoznianu This issue is solved in https://github.com/oceanprotocol/ocean.js/pull/1537

miquelcabot avatar Jul 20 '22 17:07 miquelcabot

@miquelcabot Thanks for letting me know. We will review the C2D PR in the following days

LoznianuAnamaria avatar Jul 21 '22 08:07 LoznianuAnamaria

Hey @miquelcabot, I'm following your #1537 and am getting the following error:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/test/config' is not defined by "exports" in /Users/jakub/Development/Algovera/Ocean/oceanjs_scripts/v4/node_modules/@oceanprotocol/lib/package.json

which is probably caused by this line:

import { getAddresses, getTestConfig, web3 } from '@oceanprotocol/lib/dist/test/config'

I have tried adding @oceanprotocol/lib/dist/test/config explicitly to my package.json but that didn't work. Any ideas on how I could fix this? Or are there perhaps examples on how I can specify rinkeby config for testing instead of using barge?

smejak avatar Jul 23 '22 10:07 smejak

@smejak Ok. I'm getting the same issue. I will fix it. Thanks for pointing out the error.

miquelcabot avatar Jul 23 '22 12:07 miquelcabot

@miquelcabot Thank you. Do you know of any examples for configuring ocean.js to rinkeby with a predefined wallet? I would like to ultimately use this flow in production but am running into an error in step 7. of the flow, specifically here:

const tx = await factory.createNftErc20WithFixedRate(
      publisherAccount,
      nftParams,
      erc20Params,
      freParams
 )

I initialized my config with rinkeby URLs, but I'm probably missing something because I'm getting the following error:

ERROR: Estimate gas failed! Error: Provided address [object Object] is invalid, the capitalization checksum test failed, or it's an indirect IBAN address which can't be converted.
/Users/jakub/Development/Algovera/Ocean/oceanjs_scripts/v4/node_modules/web3-core-helpers/lib/errors.js:28
        var err = new Error('Returned error: ' + message);
                  ^

Error: Returned error: {"context":{"id":1,"jsonrpc":"2.0","method":"eth_gasPrice","params":[]},"error":"405 Method Not Allowed: The method is not allowed for the requested URL."}

My WIP code is here https://github.com/smejak/oceanjs_scripts/blob/main/publish.js#L100 . Thanks for any suggestions!

smejak avatar Jul 23 '22 16:07 smejak

I think I'm looking for an alternative to getAddresses, because I think that requires all the contracts being deployed locally with barge running, although I think that's separate to the error above.

smejak avatar Jul 23 '22 18:07 smejak

@miquelcabot I have managed to solve the above error, but have been getting a JSON RPC error, which I traced back to my web3 provider, I changed it to the base address as shown here: https://github.com/oceanprotocol/ocean.js/blob/efa3839d10befdbc35e16e61c8e9bf310039970b/src/utils/ConfigHelper.ts

however, the JSON RPC error was raised when I used the rinkeby nodeUri from the same ConfigHelper, so I'm not sure whether that means that rinkeby v4 is not connecting correctly or whether it's something else.

smejak avatar Jul 23 '22 19:07 smejak

Am I right in thinking this issue is solved now?

jamiehewitt15 avatar Nov 17 '22 14:11 jamiehewitt15