wen-moon-ser / moonshot-sdk

Package for creating buy/sell transactions, and calculating the price of any moonshot token on Solana.
https://moonshot.cc
37 stars 9 forks source link

Question about account seeds #17

Closed labyla closed 1 month ago

labyla commented 1 month ago

Hello! I can't understand how you find seeds for accounts (for example seeds for curve account: "token" & mint address). Could you please explain it to me? I would be very grateful.

mavagio commented 1 month ago

hey @labyla here you can find how we derive the curve account for token https://github.com/wen-moon-ser/moonshot-sdk/blob/main/src/solana/utils/getCurveAccount.ts

import { PublicKey } from '@solana/web3.js';
import { TokenLaunchpadIdl } from '../program';
import { CurveAccount } from '../../domain';
import { convertBNtoBigInt } from './convertBNToBigInt';
import { convertContractEnums } from './convertContractCurrency';
import { BaseAnchorProvider } from '../provider';

export async function getCurveAccount(
  provider: BaseAnchorProvider<TokenLaunchpadIdl>,
  mintAddress: string,
): Promise<CurveAccount> {
  const [curveAccountKey] = PublicKey.findProgramAddressSync(
    [Buffer.from('token'), new PublicKey(mintAddress).toBytes()],
    provider.program.programId,
  );

  const curveAccount = await provider.program.account.curveAccount.fetch(
    curveAccountKey,
    provider.commitment,
  );

  if (curveAccount == null) {
    throw new Error('Curve account data not found');
  }
  const account = convertBNtoBigInt(curveAccount) as CurveAccount;
  return convertContractEnums(account);
}
mavagio commented 1 month ago

Feel free to re-open it in case you still have questions

labyla commented 1 month ago

Okay. Thanks a lot ♥