chainstacklabs / raydium-sdk-swap-example-typescript

An example to swap tokens on Solana using the Raydium SDK, TypeScript, and Chainstack
MIT License
91 stars 30 forks source link

Loaded pool keys #2

Open 0NighTWizard0 opened 4 months ago

0NighTWizard0 commented 4 months ago

hello. I don't know why it gives me this error when I exceute the swap. this error related to the pool and it's like the code can't read the pool list.

Loaded pool keys C:\raydium-sdk-swap-example-typescript\src\RaydiumSwap.ts:59 const poolData = this.allPoolKeysJson.find( ^ TypeError: Cannot read properties of undefined (reading 'find') at RaydiumSwap.findPoolInfoForTokens (C:\raydium-sdk-swap-example-typescript\src\RaydiumSwap.ts:59:43) at C:\raydium-sdk-swap-example-typescript\src\index.ts:30:32 at Generator.next () at fulfilled (C:\raydium-sdk-swap-example-typescript\src\index.ts:5:58) at processTicksAndRejections (node:internal/process/task_queues:95:5) error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

1500256797 commented 4 months ago

i met same error const directionIn = poolKeys.quoteMint.toString() == toToken ^ TypeError: Cannot read properties of null (reading 'quoteMint')

soos3d commented 4 months ago

I have been noticing this error with new coins. Are you trying to swap newly created coins?

0NighTWizard0 commented 4 months ago

its BOME token (ukHH6c7mMyiWCf1b9pnWe25TSpkDDt3H5pQZgZ74J82) it came about 6 days ago.

soos3d commented 4 months ago

just tried it now and the simulation worked fine!

Raydium swap initialized
Swapping 0.01 of So11111111111111111111111111111111111111112 for ukHH6c7mMyiWCf1b9pnWe25TSpkDDt3H5pQZgZ74J82...
Loaded pool keys
Found pool info
{
  minAmountOut: TokenAmount {
    numerator: <BN: 7c19b44>,
    denominator: <BN: f4240>,
    currency: Token {
      decimals: 6,
      symbol: 'UNKNOWN',
      name: 'UNKNOWN',
      programId: [PublicKey [PublicKey(TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA)]],
      mint: [PublicKey [PublicKey(ukHH6c7mMyiWCf1b9pnWe25TSpkDDt3H5pQZgZ74J82)]]
    },
    token: Token {
      decimals: 6,
      symbol: 'UNKNOWN',
      name: 'UNKNOWN',
      programId: [PublicKey [PublicKey(TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA)]],
      mint: [PublicKey [PublicKey(ukHH6c7mMyiWCf1b9pnWe25TSpkDDt3H5pQZgZ74J82)]]
    }
  },
  amountIn: TokenAmount {
    numerator: <BN: 989680>,
    denominator: <BN: 3b9aca00>,
    currency: Token {
      decimals: 9,
      symbol: 'UNKNOWN',
      name: 'UNKNOWN',
      programId: [PublicKey [PublicKey(TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA)]],
      mint: [PublicKey [PublicKey(So11111111111111111111111111111111111111112)]]
    },
    token: Token {
      decimals: 9,
      symbol: 'UNKNOWN',
      name: 'UNKNOWN',
      programId: [PublicKey [PublicKey(TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA)]],
      mint: [PublicKey [PublicKey(So11111111111111111111111111111111111111112)]]
    }
  }
}
{
  context: { apiVersion: '1.17.25', slot: 255567497 },
  value: {
    accounts: null,
    err: null,
    logs: [
      'Program ComputeBudget111111111111111111111111111111 invoke [1]',
      'Program ComputeBudget111111111111111111111111111111 success',
      'Program 11111111111111111111111111111111 invoke [1]',
      'Program 11111111111111111111111111111111 success',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]',
      'Program log: Instruction: InitializeAccount',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 3443 of 999700 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL invoke [1]',
      'Program log: Create',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: GetAccountDataSize',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1569 of 986390 compute units',
      'Program return: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA pQAAAAAAAAA=',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program 11111111111111111111111111111111 invoke [2]',
      'Program 11111111111111111111111111111111 success',
      'Program log: Initialize the associated token account',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: InitializeImmutableOwner',
      'Program log: Please upgrade to SPL Token 2022 for immutable owner support',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1405 of 979803 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: InitializeAccount3',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4188 of 975921 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL consumed 24807 of 996257 compute units',
      'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL success',
      'Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 invoke [1]',
      'Program log: ray_log: A4CWmAAAAAAARJvBBwAAAAABAAAAAAAAAICWmAAAAAAAeylzdg8AAACMAmQgAQAAAAjjJAgAAAAA',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: Transfer',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4736 of 953333 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: Transfer',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4645 of 945616 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 consumed 31282 of 971450 compute units',
      'Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 success',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]',
      'Program log: Instruction: CloseAccount',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2915 of 940168 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success'
    ],
    returnData: null,
    unitsConsumed: 62747
  }
}
✨  Done in 21.39s.
youngclaude commented 4 months ago

i met same error const directionIn = poolKeys.quoteMint.toString() == toToken ^ TypeError: Cannot read properties of null (reading 'quoteMint')

@soos3d Thanks for your help on this. Its clear the community will use it to trade the latest meme craze of the hour so the acceptance tests should be tight to that use case.

I was able to swap with your default example tokens with no issues using my mainnet Chainstack RPC.

However I am seeing the same issue as others after changing the 'tokenBAddress' address. Did you change anything to get it to work? Here is an example with the Raydium token RAY

image

mulerm commented 4 months ago

I have been noticing this error with new coins. Are you trying to swap newly created coins?

This is nice tool. Thanks! I'm trying this with newly created coins and I see the same error. Is this because it attempts to read the price of the token? Why not provide an option for the user to specify?

peonante commented 3 months ago

I have the same problem, it would be interesting to have a solution for swapping new tokens too

soos3d commented 3 months ago

i met same error const directionIn = poolKeys.quoteMint.toString() == toToken ^ TypeError: Cannot read properties of null (reading 'quoteMint')

@soos3d Thanks for your help on this. Its clear the community will use it to trade the latest meme craze of the hour so the acceptance tests should be tight to that use case.

I was able to swap with your default example tokens with no issues using my mainnet Chainstack RPC.

However I am seeing the same issue as others after changing the 'tokenBAddress' address. Did you change anything to get it to work? Here is an example with the Raydium token RAY

image

Hi @youngclaude , quoteMint should be an issue with the slippage. You can edit it from here: slippage line

soos3d commented 3 months ago

I'm working on adding retries to re-fetch the pools when they fail. might not fix the issue at 100%, but it should help.

peonante commented 3 months ago

i met same error const directionIn = poolKeys.quoteMint.toString() == toToken ^ TypeError: Cannot read properties of null (reading 'quoteMint')

@soos3d Thanks for your help on this. Its clear the community will use it to trade the latest meme craze of the hour so the acceptance tests should be tight to that use case. I was able to swap with your default example tokens with no issues using my mainnet Chainstack RPC. However I am seeing the same issue as others after changing the 'tokenBAddress' address. Did you change anything to get it to work? Here is an example with the Raydium token RAY image

Hi @youngclaude , quoteMint should be an issue with the slippage. You can edit it from here: slippage line

I tried changing the slippage but nothing changes, the error persists.

I believe the problem is due to the fact that new pairs are not immediately updated in the .. mainnet.json link

soos3d commented 3 months ago

Yes, I think that too. Adding retry logic to fetch the pairs should make it better. I'll work on it as soon as I have some time!

peonante commented 3 months ago

must there be something already existing that does this job? I can't find any other repositories or code anywhere that solves this, yet there are swaps and bots that do it

kiknaio commented 3 months ago

Hey folks :wave: I have same issue. I'm trying to get pool information from here https://api.raydium.io/v2/sdk/liquidity/mainnet.json and seems Raydium is updating file not so frequently and one can't get latest deployed pool information from there. Do you know any alternative services that provide that?

anisurrahman072 commented 3 months ago

Guys thanks a lot for your helps. Is there any way to test it first on devnet ?

Why my change "https://api.raydium.io/v2/sdk/liquidity/devnet.json" not works ? @soos3d could you please let me know how can i make the code compatible for Devnet ?

Doing test by Devnet is important really.

Krshivam25 commented 3 months ago

I swapped the token A (HUMP). to token B (SOL) the terminal show no error and generate the transaction URL but TX-URL_SOLSCAN nothing is there

soos3d commented 3 months ago

Guys thanks a lot for your helps. Is there any way to test it first on devnet ?

Why my change "https://api.raydium.io/v2/sdk/liquidity/devnet.json" not works ? @soos3d could you please let me know how can i make the code compatible for Devnet ?

Doing test by Devnet is important really.

Let me look into that!

soos3d commented 3 months ago

I swapped the token A (HUMP). to token B (SOL) the terminal show no error and generate the transaction URL but TX-URL_SOLSCAN nothing is there

yeah this happens when the chain is really congested; the transactions are simply dropped. Unfortunately, there is not too much we can do at the moment besides increasing priority fees and sens transactions continuously until one goes through🥲

Krshivam25 commented 3 months ago

@soos3d adding to this code need lot of more improvement as even the user dont have the SOL balance on wallet the transaction getting executed and there is no check for this. which is wrong.

soos3d commented 3 months ago

even the user dont have the SOL balance on wallet the transaction getting executed and there is no check for this

true, this can be a good feature to add; feel free to contribute and open a PR if you want to implement it!

Krshivam25 commented 3 months ago

even the user dont have the SOL balance on wallet the transaction getting executed and there is no check for this

true, this can be a good feature to add; feel free to contribute and open a PR if you want to implement it!

Sure, I am working on it , will update it here soon : )

AMIYA8597 commented 3 months ago

hello. I don't know why it gives me this error when I exceute the swap. this error related to the pool and it's like the code can't read the pool list.

Loaded pool keys C:\raydium-sdk-swap-example-typescript\src\RaydiumSwap.ts:59 const poolData = this.allPoolKeysJson.find( ^ TypeError: Cannot read properties of undefined (reading 'find') at RaydiumSwap.findPoolInfoForTokens (C:\raydium-sdk-swap-example-typescript\src\RaydiumSwap.ts:59:43) at C:\raydium-sdk-swap-example-typescript\src\index.ts:30:32 at Generator.next () at fulfilled (C:\raydium-sdk-swap-example-typescript\src\index.ts:5:58) at processTicksAndRejections (node:internal/process/task_queues:95:5) error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

here I am facing other error

throw new Error('Non-base' + BASE + ' character')
      ^

Error: Non-base58 character

brianyeap commented 3 months ago

Guys thanks a lot for your helps. Is there any way to test it first on devnet ? Why my change "https://api.raydium.io/v2/sdk/liquidity/devnet.json" not works ? @soos3d could you please let me know how can i make the code compatible for Devnet ? Doing test by Devnet is important really.

Let me look into that!

i have this issue to, is it becasue raydium does not provide the poolkeys?

alexandr-kazakov commented 2 months ago

The TypeError: Cannot read properties of null (reading 'quoteMint' appears if the app can't load any pools keys for your tokens pairs(for example pair: SOL and USDC).

So you can write instead of

 const poolInfo = raydiumSwap.findPoolInfoForTokens(swapConfig.tokenAAddress, swapConfig.tokenBAddress);
 console.log('Found pool info');

this:

  const poolInfo = raydiumSwap.findPoolInfoForTokens(swapConfig.tokenAAddress, swapConfig.tokenBAddress);
    if (!poolInfo) {
      console.error('Pool not found');
      return 'Pool not found';
    }
alexandr-kazakov commented 2 months ago

I'm searching myself ho to make swap for newly created coins. Current this repo solution doesn't work for this. I implemented this https://solana.stackexchange.com/a/10020/2840 by LIQUIDITY_STATE_LAYOUT_V4 solution too and it doesn't help. I'm thinking about yellowstone-grpc solution now.

Any ideas?

brooke007 commented 3 weeks ago

Hey folks 👋 I have same issue. I'm trying to get pool information from here https://api.raydium.io/v2/sdk/liquidity/mainnet.json and seems Raydium is updating file not so frequently and one can't get latest deployed pool information from there. Do you know any alternative services that provide that?

have you solved it?

alexandr-kazakov commented 3 weeks ago

@brooke007 I solved it https://github.com/alexandr-kazakov/solana-crendel-bot In my bot pools data is not get from mainnet.json. From the first second of pool creation you will know about it.