Skip to main content

oil_chat_api/
sdk.rs

1use steel::*;
2use crate::instruction::*;
3use crate::state::*;
4use solana_program::pubkey::Pubkey;
5
6/// Build an Initialize instruction
7/// Creates the Config account with next_message_id = 0
8pub fn initialize(signer: &Pubkey) -> Instruction {
9    let (config_pda, _) = config_pda();
10    
11    Instruction {
12        program_id: crate::ID,
13        accounts: vec![
14            AccountMeta::new(*signer, true), // signer
15            AccountMeta::new(config_pda, false), // config (writable)
16            AccountMeta::new_readonly(solana_program::system_program::ID, false),
17        ],
18        data: Initialize {}.to_bytes(),
19    }
20}
21
22/// Build a SendMessage instruction
23/// 
24/// For regular wallets: signer == authority
25/// For FOGO sessions: client should add program_signer and payer accounts after authority
26pub fn send_message(
27    signer: &Pubkey,
28    authority: &Pubkey,
29    message_hash: [u8; 32],
30) -> Instruction {
31    let (user_chat_config_pda, _) = user_chat_config_pda(*authority);
32    let (message_pda, _) = message_pda(*authority, 0); // message_id will be set by program from per-authority counter
33    
34    Instruction {
35        program_id: crate::ID,
36        accounts: vec![
37            AccountMeta::new(*signer, true), // signer
38            AccountMeta::new(*authority, false), // authority (writable)
39            // For FOGO sessions, client adds: program_signer, payer here
40            AccountMeta::new(user_chat_config_pda, false), // user_chat_config (writable)
41            AccountMeta::new(message_pda, false), // message (writable)
42            AccountMeta::new_readonly(solana_program::system_program::ID, false),
43        ],
44        data: SendMessage { message_hash }.to_bytes(),
45    }
46}
47
48/// Build a ReplyToMessage instruction
49/// 
50/// For regular wallets: signer == authority
51/// For FOGO sessions: client should add program_signer and payer accounts after authority
52pub fn reply_to_message(
53    signer: &Pubkey,
54    authority: &Pubkey,
55    parent_message_pda: &Pubkey,
56    message_hash: [u8; 32],
57) -> Instruction {
58    let (user_chat_config_pda, _) = user_chat_config_pda(*authority);
59    let (message_pda, _) = message_pda(*authority, 0); // message_id will be set by program from per-authority counter
60    
61    Instruction {
62        program_id: crate::ID,
63        accounts: vec![
64            AccountMeta::new(*signer, true), // signer
65            AccountMeta::new(*authority, false), // authority (writable)
66            // For FOGO sessions, client adds: program_signer, payer here
67            AccountMeta::new_readonly(*parent_message_pda, false), // parent message (read-only)
68            AccountMeta::new(user_chat_config_pda, false), // user_chat_config (writable)
69            AccountMeta::new(message_pda, false), // message (writable)
70            AccountMeta::new_readonly(solana_program::system_program::ID, false),
71        ],
72        data: ReplyToMessage {
73            parent_message_pda: parent_message_pda.to_bytes(),
74            message_hash,
75        }.to_bytes(),
76    }
77}
78
79/// Build an AddReaction instruction
80/// 
81/// For regular wallets: signer == authority
82/// For FOGO sessions: client should add program_signer and payer accounts after authority
83pub fn add_reaction(
84    signer: &Pubkey,
85    authority: &Pubkey,
86    message_pda: &Pubkey,
87    emoji: [u8; 4],
88) -> Instruction {
89    let (reaction_pda, _) = reaction_pda(*message_pda, *authority, &emoji);
90    
91    Instruction {
92        program_id: crate::ID,
93        accounts: vec![
94            AccountMeta::new(*signer, true), // signer
95            AccountMeta::new(*authority, false), // authority (writable)
96            // For FOGO sessions, client adds: program_signer, payer here
97            AccountMeta::new_readonly(*message_pda, false), // message (writable to update reaction_count)
98            AccountMeta::new(reaction_pda, false), // reaction (writable)
99            AccountMeta::new_readonly(solana_program::system_program::ID, false),
100        ],
101        data: AddReaction {
102            message_pda: message_pda.to_bytes(),
103            emoji,
104        }.to_bytes(),
105    }
106}
107
108/// Build a RemoveReaction instruction
109/// 
110/// For regular wallets: signer == authority
111/// For FOGO sessions: client should add program_signer account after authority
112pub fn remove_reaction(
113    signer: &Pubkey,
114    authority: &Pubkey,
115    message_pda: &Pubkey,
116    emoji: [u8; 4],
117) -> Instruction {
118    let (reaction_pda, _) = reaction_pda(*message_pda, *authority, &emoji);
119    
120    Instruction {
121        program_id: crate::ID,
122        accounts: vec![
123            AccountMeta::new(*signer, true), // signer
124            AccountMeta::new(*authority, false), // authority (writable)
125            // For FOGO sessions, client adds: program_signer here
126            AccountMeta::new_readonly(*message_pda, false), // message (writable to update reaction_count)
127            AccountMeta::new(reaction_pda, false), // reaction (writable, will be closed)
128            AccountMeta::new(*authority, false), // destination for rent (authority)
129            AccountMeta::new_readonly(solana_program::system_program::ID, false),
130        ],
131        data: RemoveReaction {
132            message_pda: message_pda.to_bytes(),
133            emoji,
134        }.to_bytes(),
135    }
136}