sui icon indicating copy to clipboard operation
sui copied to clipboard

Sui Code Bug in Typescript SDK

Open nullbitx8 opened this issue 9 months ago • 3 comments

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

nullbitx8 avatar May 17 '24 05:05 nullbitx8

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

hayes-mysten avatar May 17 '24 16:05 hayes-mysten

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 avatar May 18 '24 02:05 nullbitx8

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.

nullbitx8 avatar May 18 '24 02:05 nullbitx8

@nullbitx8 feel free to submit a PR! Thanks!

stefan-mysten avatar May 23 '24 02:05 stefan-mysten