CetusProtocol / cetus-clmm-sui-sdk

The clmm sdk on Sui.
Apache License 2.0
36 stars 25 forks source link

Update Docs for supporting Fetch #6

Closed nullbitx8 closed 4 months ago

nullbitx8 commented 6 months ago

I tried using the SDK for the first time but ran into the following error:

/home/dev/project/node_modules/@mysten/sui.js/dist/cjs/client/http-transport.js:61
      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 (/home/dev/project/node_modules/@mysten/sui.js/dist/cjs/client/http-transport.js:61:13)
    at SuiHTTPTransport.request (/home/dev/project/node_modules/@mysten/sui.js/dist/cjs/client/http-transport.js:69:28)
    at RpcModule.getObject (/home/dev/project/node_modules/@mysten/sui.js/dist/cjs/client/client.js:198:33)
    at PoolModule.getPool (/home/dev/project/node_modules/@cetusprotocol/cetus-sui-clmm-sdk/dist/index.js:4431:47)
    at retrieveAllPositionsOfPool (file:///home/dev/project/src/utils.js:4:41)
    at file:///home/dev/project/src/utils.js:15:1
    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)

I followed the installation instructions and config correctly. However the issue appears to be with the mysten sui sdk https://github.com/MystenLabs/sui/tree/main/sdk/typescript which the cetus sdk uses under the hood.

The problem is that the node-fetch module is being used but somehow it is not imported explicitly. So the easy fix for me was to import node-fetch in my script before importing the Cetus sdk.

Here are general instructions on what I had to do to fix it: https://github.com/node-fetch/node-fetch?tab=readme-ov-file#providing-global-access

So I created a file in my project named fetch-polyfill.js

// https://stackoverflow.com/a/73489195
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
}

And then I adjusted my cetus config to import that file before importing the cetus SDK. Here is how my cetus configuration file looks like now:

import "./fetch-polyfill.js";
import CetusClmmSDK from '@cetusprotocol/cetus-sui-clmm-sdk';

// TODO move these values out into environment variables
const SDKConfig = {
  clmmConfig: {
    pools_id: '0xf699e7f2276f5c9a75944b37a0c5b5d9ddfd2471bf6242483b03ab2887d198d0',
    global_config_id: '0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f',
    global_vault_id: '0xce7bceef26d3ad1f6d9b6f13a953f053e6ed3ca77907516481ce99ae8e588f2b',
    admin_cap_id: '0x89c1a321291d15ddae5a086c9abc533dff697fde3d89e0ca836c41af73e36a75',
  },
  cetusConfig: {
    coin_list_id: '0x8cbc11d9e10140db3d230f50b4d30e9b721201c0083615441707ffec1ef77b23',
    launchpad_pools_id: '0x1098fac992eab3a0ab7acf15bb654fc1cf29b5a6142c4ef1058e6c408dd15115',
    clmm_pools_id: '0x15b6a27dd9ae03eb455aba03b39e29aad74abd3757b8e18c0755651b2ae5b71e',
    admin_cap_id: '0x39d78781750e193ce35c45ff32c6c0c3f2941fa3ddaf8595c90c555589ddb113',
    global_config_id: '0x0408fa4e4a4c03cc0de8f23d0c2bbfe8913d178713c9a271ed4080973fe42d8f',
    coin_list_handle: '0x49136005e90e28c4695419ed4194cc240603f1ea8eb84e62275eaff088a71063',
    launchpad_pools_handle: '0x5e194a8efcf653830daf85a85b52e3ae8f65dc39481d54b2382acda25068375c',
    clmm_pools_handle: '0x37f60eb2d9d227949b95da8fea810db3c32d1e1fa8ed87434fc51664f87d83cb',
  },
}

export const mainnet = {
  fullRpcUrl: 'https://sui-mainnet-rpc.allthatnode.com',
  swapCountUrl: 'https://api-sui.cetus.zone/v2/sui/swap/count',
  simulationAccount: {
    address: ''
  },
  cetus_config: {
    package_id: '0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f',
    published_at: '0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f',
    config: SDKConfig.cetusConfig,
  },
  clmm_pool: {
    package_id: '0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb',
    published_at: '0x70968826ad1b4ba895753f634b0aea68d0672908ca1075a2abdf0fc9e0b2fc6a',
    config: {
      ...SDKConfig.clmmConfig,
      partners_id: '0xac30897fa61ab442f6bff518c5923faa1123c94b36bd4558910e9c783adfa204'
    }
  },
  integrate: {
    package_id: '0x996c4d9480708fb8b92aa7acf819fb0497b5ec8e65ba06601cae2fb6db3312c3',
    published_at: '0x6f5e582ede61fe5395b50c4a449ec11479a54d7ff8e0158247adfda60d98970b'
  },
  deepbook: {
    package_id: '0x000000000000000000000000000000000000000000000000000000000000dee9',
    published_at: '0x000000000000000000000000000000000000000000000000000000000000dee9'
  },
  deepbook_endpoint_v2: {
    package_id: '0x0dd416959739e1db3a4c6f9cac7f9e7202678f3b067d6d419e569a124fc35e0e',
    published_at: '0x0dd416959739e1db3a4c6f9cac7f9e7202678f3b067d6d419e569a124fc35e0e'
  },
  aggregatorUrl: 'https://api-sui.cetus.zone/router'
}

export const CetusMainnetSDK = new CetusClmmSDK.CetusClmmSDK(mainnet);

If you would like, I can add this to the documentation. I will also open an issue on the mysten sdk.

nullbitx8 commented 6 months ago

https://github.com/MystenLabs/sui/issues/17790

nullbitx8 commented 6 months ago

https://github.com/MystenLabs/sui/issues/17790#issuecomment-2117950756

This error is no longer an issue from node v18 onwards. If you like, I can add that as a tip in the README.