oselezi / solana-volume-sdk

SDK to be used to generate trading volume and market making through various of AMMs on Solana
MIT License
30 stars 24 forks source link

Trying to run #6

Closed libinysolutions closed 3 days ago

libinysolutions commented 5 days ago

First of all, great script. I'm trying to do a npm run but it's not working. I get the message below.

import { AMM } from '@oselezi/solana-volume-sdk';
         ^^^
SyntaxError: Named export 'AMM' not found. The requested module '@oselezi/solana-volume-sdk' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@oselezi/solana-volume-sdk';
const { AMM } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v21.2.0
oselezi commented 3 days ago

Hi @libinysolutions ,

Thank you for reaching out and for your kind words about the script! I'm glad you're finding it useful.

I understand you're encountering the following error when running your script:

import { AMM } from '@oselezi/solana-volume-sdk';
         ^^^
SyntaxError: Named export 'AMM' not found. The requested module '@oselezi/solana-volume-sdk' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@oselezi/solana-volume-sdk';
const { AMM } = pkg;

This error occurs because the SDK is currently exported as a CommonJS module. When using ES module syntax (i.e., import statements), you might run into issues importing named exports from CommonJS modules.

Here's how you can fix the issue:

Your tsconfig.json can be like this:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true
  }
}

And you have to include type: "module" in your package.json file.

instead of

import { AMM } from '@oselezi/solana-volume-sdk';

please try

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

const SDK = require('@oselezi/solana-volume-sdk');
const { AMM } = SDK;

Let me know if you need further assistance or if you encounter any other issues.

libinysolutions commented 3 days ago

Thanks for the help. I'm not getting this error:

node index.ts
node:internal/modules/esm/get_format:160
  throw new ERR_UNKNOWN_FILE_EXTENSION(ext, filepath);
        ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/lucasguilherme/Meme/solana-volume-sdk/src/index.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
    at async link (node:internal/modules/esm/module_job:76:21) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Node.js v21.2.0
oselezi commented 3 days ago

@libinysolutions Please try to run it with tsx

npx tsx index.ts
oselezi commented 3 days ago

You can check the example folder for the usecase.