// Derive PDAs and other accounts
const [tokenOwnerPDA, pdaTokenNonce] = PublicKey.findProgramAddressSync(
[Buffer.from("token_owner"), tokenMint.toBuffer()],
SC_BONDING_CURVE_PROGRAM_ID
)
const [propertiesAccount] = PublicKey.findProgramAddressSync(
[Buffer.from("settings")],
SC_BONDING_CURVE_PROGRAM_ID
)
const [logAuthority, logAuthorityBump] = PublicKey.findProgramAddressSync(
[Buffer.from("logging_authority")],
SC_BONDING_CURVE_PROGRAM_ID
)
// Get token accounts
const ownerTokenAccount = getAssociatedTokenAddressSync(tokenMint, tokenOwnerPDA, true)
const userTokenAccount = getAssociatedTokenAddressSync(tokenMint, wallet.publicKey, false)
// Create instruction data buffer
const data = Buffer.alloc(18)
data.writeUint8(2, 0) // Instruction code for BuyToken
data.writeUint8(pdaTokenNonce, 1) // PDA nonce
data.writeBigUInt64LE(solToSpend, 2) // SOL amount
data.writeBigUInt64LE(minTokenToReceive, 10) // Minimum tokens
data.writeUint8(logAuthorityBump, 18) // Log authority bump
const buyTokenIx = new TransactionInstruction({
programId: SC_BONDING_CURVE_PROGRAM_ID,
keys: [
{ pubkey: wallet.publicKey, isSigner: true, isWritable: true },
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: tokenOwnerPDA, isSigner: false, isWritable: true },
{ pubkey: tokenMint, isSigner: false, isWritable: false },
{ pubkey: ownerTokenAccount, isSigner: false, isWritable: true },
{ pubkey: userTokenAccount, isSigner: false, isWritable: true },
{ pubkey: FEE_ACCOUNT, isSigner: false, isWritable: true },
{ pubkey: propertiesAccount, isSigner: false, isWritable: false },
{ pubkey: SC_BONDING_CURVE_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: logAuthority, isSigner: false, isWritable: false },
],
data: data,
})