// 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(3, 0) // Instruction index for SellToken
data.writeUint8(pdaTokenNonce, 1) // PDA nonce
data.writeBigUInt64LE(tokenToSpend, 2) // Token amount
data.writeBigUInt64LE(minSolToReceive, 10) // Minimum SOL
data.writeUint8(logAuthorityBump, 18) // Log authority bump
const sellTokenIx = new TransactionInstruction({
programId: SC_BONDING_CURVE_PROGRAM_ID,
keys: [
{ pubkey: wallet.publicKey, isSigner: true, isWritable: true },
{ 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: SC_BONDING_CURVE_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: propertiesAccount, isSigner: false, isWritable: false },
{ pubkey: logAuthority, isSigner: false, isWritable: false },
],
data: data,
})