Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sui Code Bug in Typescript SDK #17790

Closed
nullbitx8 opened this issue May 17, 2024 · 4 comments
Closed

Sui Code Bug in Typescript SDK #17790

nullbitx8 opened this issue May 17, 2024 · 4 comments
Assignees
Labels
ts-sdk Type: Bug Something isn't working

Comments

@nullbitx8
Copy link
Contributor

Steps to Reproduce Issue

  1. Install the sdk npm i @mysten/sui.js
  2. Create a file test.js with the following contents:
import { getFullnodeUrl, SuiClient } from '@mysten/sui.js/client';
 
// use getFullnodeUrl to define Devnet RPC location
const rpcUrl = getFullnodeUrl('mainnet');
 
// create a client connected to devnet
const client = new SuiClient({ url: rpcUrl });
 
// get coins owned by an address
// replace <OWNER_ADDRESS> with actual address in the form of 0x123...
await client.getCoins({
	owner: '0xb29c543ed7ad7169441c02fc14ffd57e486b7be7177e319de0e0933453a78d49',
});
  1. Run the code with node test.js

Expected Result

Code runs.

Actual Result

Code crashes with the following

file:///home/dev/project/node_modules/@mysten/sui.js/dist/esm/client/http-transport.js:38
      throw new Error(
            ^

Error: The current environment does not support fetch, you can provide a fetch implementation in the options for SuiHTTPTransport.
    at SuiHTTPTransport.fetch (file:///home/dev/project/node_modules/@mysten/sui.js/dist/esm/client/http-transport.js:38:13)
    at SuiHTTPTransport.request (file:///home/dev/project/node_modules/@mysten/sui.js/dist/esm/client/http-transport.js:46:28)
    at SuiClient.getCoins (file:///home/dev/project/node_modules/@mysten/sui.js/dist/esm/client/client.js:42:33)
    at file:///home/dev/project/src/testSuiClient.js:11:14
    at ModuleJob.run (node:internal/modules/esm/module_job:197:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

System Information

  • OS: Linux, ubuntu
  • Package version: "@mysten/sui.js": "^0.54.1",

It appears that the default configuration of the sdk is using fetch, but not importing node-fetch or another implementation.
To fix this, I followed this

I installed node-fetch and added the following to the top of my script:

// fetch-polyfill.js
import fetch, {
  Blob,
  blobFrom,
  blobFromSync,
  File,
  fileFrom,
  fileFromSync,
  FormData,
  Headers,
  Request,
  Response,
} from 'node-fetch'

if (!globalThis.fetch) {
  globalThis.fetch = fetch
  globalThis.Headers = Headers
  globalThis.Request = Request
  globalThis.Response = Response
}

// index.js
import './fetch-polyfill'

// ...

Perhaps this should be documented, or the SDK should import node-fetch explicitly before using it.
This may be affecting other packages as well.
I ran into this issue when using the cetus sdk, see issue here

@hayes-mysten
Copy link
Contributor

The easiest way to fix this is to just use a newer version of node that supports fetch, I believe node 16 was the last version that did not support node natively and reached end of life in october of last year, so upgrading node would be my recommendation

@nullbitx8
Copy link
Contributor Author

The easiest way to fix this is to just use a newer version of node that supports fetch, I believe node 16 was the last version that did not support node natively and reached end of life in october of last year, so upgrading node would be my recommendation

Thanks for the heads up!
Maybe adding Node 18 as a minimum requirement in the README or Docs would be helpful for newcomers.
I was chasing this down for a couple of hours

@nullbitx8
Copy link
Contributor Author

There is a note in the README for troubleshooting connection errors.

If you see errors like ECONNRESET or "socket hang up", run node -v to make sure your node version is v18.x.x. Refer to this [guide](https://blog.logrocket.com/how-switch-node-js-versions-nvm/) to switch node version.

Happy to add this error as one of the other examples of errors in there.

@stefan-mysten
Copy link
Contributor

@nullbitx8 feel free to submit a PR! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ts-sdk Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants