raydium-io/raydium-sdk

parse pool example error

fatratkiller opened this issue · 0 comments

when running the code example:

`
import { Connection, PublicKey } from "@solana/web3.js";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { OpenOrders } from "@project-serum/serum";
import BN from "bn.js";
import { Client } from "discord.js"; // Import Discord.js client
import pkg from '@raydium-io/raydium-sdk';
const { TokenAccount, SPL_ACCOUNT_LAYOUT, LIQUIDITY_STATE_LAYOUT_V4 } = pkg;

const discordClient = new Client({
intents: []
});

// Initialize Discord Bot
discordClient.login('xxx').then(() => {
console.log('Logged in to Discord!');
}).catch(console.error);

async function getTokenAccounts(connection, owner) {
const tokenResp = await connection.getTokenAccountsByOwner(owner, {
programId: TOKEN_PROGRAM_ID,
});

const accounts = [];
for (const { pubkey, account } of tokenResp.value) {
accounts.push({
pubkey,
accountInfo: SPL_ACCOUNT_LAYOUT.decode(account.data),
});
}
return accounts;
}

const SOL_USDC_POOL_ID = "58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2";
const OPENBOOK_PROGRAM_ID = new PublicKey(
"srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX"
);

export async function parsePoolInfo() {
// Replace "http://localhost:8899" with your actual Solana RPC node URL
const connection = new Connection("http://localhost:8899", "confirmed");
const owner = new PublicKey("VnxDzsZ7chE88e9rB6UKztCt2HUwrkgCTx8WieWf5mM");

const tokenAccounts = await getTokenAccounts(connection, owner);

// example to get pool info
const info = await connection.getAccountInfo(new PublicKey(SOL_USDC_POOL_ID));
if (!info) return;

const poolState = LIQUIDITY_STATE_LAYOUT_V4.decode(info.data);
const openOrders = await OpenOrders.load(
connection,
poolState.openOrders,
OPENBOOK_PROGRAM_ID // OPENBOOK_PROGRAM_ID(marketProgramId) of each pool can get from api: https://api.raydium.io/v2/sdk/liquidity/mainnet.json
);

const baseDecimal = 10 ** poolState.baseDecimal.toNumber(); // e.g. 10 ^ 6
const quoteDecimal = 10 ** poolState.quoteDecimal.toNumber();

const baseTokenAmount = await connection.getTokenAccountBalance(
poolState.baseVault
);
const quoteTokenAmount = await connection.getTokenAccountBalance(
poolState.quoteVault
);

const basePnl = poolState.baseNeedTakePnl.toNumber() / baseDecimal;
const quotePnl = poolState.quoteNeedTakePnl.toNumber() / quoteDecimal;

const openOrdersBaseTokenTotal =
openOrders.baseTokenTotal.toNumber() / baseDecimal;
const openOrdersQuoteTokenTotal =
openOrders.quoteTokenTotal.toNumber() / quoteDecimal;

const base =
(baseTokenAmount.value?.uiAmount || 0) + openOrdersBaseTokenTotal - basePnl;
const quote =
(quoteTokenAmount.value?.uiAmount || 0) +
openOrdersQuoteTokenTotal -
quotePnl;

const denominator = new BN(10).pow(poolState.baseDecimal);

const addedLpAccount = tokenAccounts.find((a) =>
a.accountInfo.mint.equals(poolState.lpMint)
);

console.log(
"SOL_USDC pool info:",
"pool total base " + base,
"pool total quote " + quote,

"base vault balance " + baseTokenAmount.value.uiAmount,
"quote vault balance " + quoteTokenAmount.value.uiAmount,

"base tokens in openorders " + openOrdersBaseTokenTotal,
"quote tokens in openorders  " + openOrdersQuoteTokenTotal,

"base token decimals " + poolState.baseDecimal.toNumber(),
"quote token decimals " + poolState.quoteDecimal.toNumber(),
"total lp " + poolState.lpReserve.div(denominator).toString(),

"addedLpAmount " +
  (addedLpAccount?.accountInfo.amount.toNumber() || 0) / baseDecimal

);
}

parsePoolInfo();

async function postToDiscord(message) {
if (!discordClient.isReady()) {
console.log('Discord client is not ready.');
return;
}

const channel = await discordClient.channels.fetch('xxxx');
if (!channel) {
console.log('Channel not found.');
return;
}

channel.send(message);
}

export async function parsePoolInfoAndPost() {
// Call your existing parsePoolInfo function
const poolInfo = await parsePoolInfo();

// Prepare a message based on the poolInfo (customize this as needed)
const message = SOL_USDC pool information:\n${JSON.stringify(poolInfo, null, 2)};

// Post the message to Discord
await postToDiscord(message);
}

parsePoolInfoAndPost();
`

It always show as:

(node:33374) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Usenode --trace-warnings ...` to show where the warning was created)
/Users/apple/solbot/node_modules/@raydium-io/raydium-sdk/lib/esm/index.js:1
export * from './base';
^^^^^^

SyntaxError: Unexpected token 'export'
at internalCompileFunction (node:internal/vm:77:18)
at wrapSafe (node:internal/modules/cjs/loader:1288:20)
at Module._compile (node:internal/modules/cjs/loader:1340:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at cjsLoader (node:internal/modules/esm/translators:356:17)
at ModuleWrap. (node:internal/modules/esm/translators:305:7)
at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
`

even my package set as "type: module" or i change the file to 'mjs', it still doest work!>

lol, what's the problem?