tape_api/
sdk.rs

1use steel::*;
2use crankx::Solution;
3
4use crate::{
5    consts::*, 
6    instruction::*, 
7    pda::*,
8    utils,
9};
10
11pub fn build_create_ix(
12    signer: Pubkey,
13    name: &str,
14) -> Instruction {
15    let name = utils::to_name(name);
16    let (tape_address, _tape_bump) = tape_pda(signer, &name);
17    let (writer_address, _writer_bump) = writer_pda(tape_address);
18
19    Instruction {
20        program_id: crate::ID,
21        accounts: vec![
22            AccountMeta::new(signer, true),
23            AccountMeta::new(tape_address, false),
24            AccountMeta::new(writer_address, false),
25            AccountMeta::new_readonly(solana_program::system_program::ID, false),
26            AccountMeta::new_readonly(sysvar::rent::ID, false),
27            AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
28        ],
29        data: Create {
30            name,
31        }.to_bytes(),
32    }
33}
34
35pub fn build_write_ix(
36    signer: Pubkey,
37    tape: Pubkey,
38    writer: Pubkey,
39    prev_segment: Option<[u8; 64]>,
40    data: &[u8],
41) -> Instruction {
42
43    let prev_segment = match prev_segment {
44        Some(sig) => sig,
45        None => [0; 64],
46    };
47
48    Instruction {
49        program_id: crate::ID,
50        accounts: vec![
51            AccountMeta::new(signer, true),
52            AccountMeta::new(tape, false),
53            AccountMeta::new(writer, false),
54        ],
55        data: Write::new(
56            prev_segment
57        ).pack(data),
58    }
59}
60
61pub fn build_finalize_ix(
62    signer: Pubkey, 
63    tape: Pubkey,
64    writer: Pubkey,
65    tail_segment: [u8; 64],
66) -> Instruction {
67
68    Instruction {
69        program_id: crate::ID,
70        accounts: vec![
71            AccountMeta::new(signer, true),
72            AccountMeta::new(tape, false),
73            AccountMeta::new(writer, false),
74            AccountMeta::new(ARCHIVE_ADDRESS, false),
75            AccountMeta::new_readonly(solana_program::system_program::ID, false),
76            AccountMeta::new_readonly(sysvar::rent::ID, false),
77        ],
78        data: Finalize {
79            tail: tail_segment,
80        }.to_bytes(),
81    }
82}
83
84pub fn build_register_ix(
85    signer: Pubkey, 
86    name: &str
87) -> Instruction {
88    let name = utils::to_name(name);
89    let (miner_address, _bump) = miner_pda(signer, name);
90
91    Instruction {
92        program_id: crate::ID,
93        accounts: vec![
94            AccountMeta::new(signer, true),
95            AccountMeta::new(miner_address, false),
96            AccountMeta::new_readonly(ARCHIVE_ADDRESS, false),
97            AccountMeta::new_readonly(solana_program::system_program::ID, false),
98            AccountMeta::new_readonly(sysvar::rent::ID, false),
99            AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
100        ],
101        data: Register {
102            name,
103        }.to_bytes(),
104    }
105}
106
107pub fn build_mine_ix(
108    signer: Pubkey,
109    miner: Pubkey,
110    spool: Pubkey,
111    tape: Pubkey,
112    solution: Solution,
113    recall_chunk: [u8; CHUNK_SIZE],
114    recall_proof: [[u8;32]; PROOF_LEN],
115) -> Instruction {
116    Instruction {
117        program_id: crate::ID,
118        accounts: vec![
119            AccountMeta::new(signer, true),
120            AccountMeta::new(spool, false),
121            AccountMeta::new(miner, false),
122            AccountMeta::new_readonly(tape, false),
123            AccountMeta::new_readonly(EPOCH_ADDRESS, false),
124            AccountMeta::new_readonly(ARCHIVE_ADDRESS, false),
125            AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
126        ],
127        data: Mine {
128            digest: solution.d,
129            nonce: solution.n,
130            recall_chunk,
131            recall_proof,
132        }.to_bytes(),
133    }
134}
135
136pub fn build_claim_ix(
137    signer: Pubkey, 
138    miner: Pubkey,
139    beneficiary: Pubkey, 
140    amount: u64
141) -> Instruction {
142    Instruction {
143        program_id: crate::ID,
144        accounts: vec![
145            AccountMeta::new(signer, true),
146            AccountMeta::new(beneficiary, false),
147            AccountMeta::new(miner, false),
148            AccountMeta::new_readonly(TREASURY_ADDRESS, false),
149            AccountMeta::new(TREASURY_ATA, false),
150            AccountMeta::new_readonly(spl_token::ID, false),
151        ],
152        data: Claim {
153            amount: amount.to_le_bytes(),
154        }.to_bytes(),
155    }
156}
157
158pub fn build_close_ix(
159    signer: Pubkey,
160    miner: Pubkey,
161) -> Instruction {
162    Instruction {
163        program_id: crate::ID,
164        accounts: vec![
165            AccountMeta::new(signer, true),
166            AccountMeta::new(miner, false),
167            AccountMeta::new_readonly(solana_program::system_program::ID, false),
168        ],
169        data: Close {}.to_bytes(),
170    }
171}
172
173pub fn build_initialize_ix(
174    signer: Pubkey
175) -> Instruction {
176    let spool_pdas = [
177        spool_pda(0).0,
178        spool_pda(1).0,
179        spool_pda(2).0,
180        spool_pda(3).0,
181        spool_pda(4).0,
182        spool_pda(5).0,
183        spool_pda(6).0,
184        spool_pda(7).0,
185    ];
186
187    let (archive_pda, _archive_bump) = archive_pda();
188    let (epoch_pda, _epoch_bump) = epoch_pda();
189    let (mint_pda, _mint_bump) = mint_pda();
190    let (treasury_pda, _treasury_bump) = treasury_pda();
191    let (treasury_ata, _treasury_ata_bump) = treasury_ata();
192    let (metadata_pda, _metadata_bump) = metadata_pda(mint_pda);
193
194    assert_eq!(archive_pda, ARCHIVE_ADDRESS);
195    assert_eq!(epoch_pda, EPOCH_ADDRESS);
196    assert_eq!(mint_pda, MINT_ADDRESS);
197    assert_eq!(treasury_pda, TREASURY_ADDRESS);
198    assert_eq!(treasury_ata, TREASURY_ATA);
199
200    Instruction {
201        program_id: crate::ID,
202        accounts: vec![
203            AccountMeta::new(signer, true),
204            AccountMeta::new(spool_pdas[0], false),
205            AccountMeta::new(spool_pdas[1], false),
206            AccountMeta::new(spool_pdas[2], false),
207            AccountMeta::new(spool_pdas[3], false),
208            AccountMeta::new(spool_pdas[4], false),
209            AccountMeta::new(spool_pdas[5], false),
210            AccountMeta::new(spool_pdas[6], false),
211            AccountMeta::new(spool_pdas[7], false),
212            AccountMeta::new(archive_pda, false),
213            AccountMeta::new(epoch_pda, false),
214            AccountMeta::new(metadata_pda, false),
215            AccountMeta::new(mint_pda, false),
216            AccountMeta::new(treasury_pda, false),
217            AccountMeta::new(treasury_ata, false),
218            AccountMeta::new_readonly(system_program::ID, false),
219            AccountMeta::new_readonly(spl_token::ID, false),
220            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
221            AccountMeta::new_readonly(mpl_token_metadata::ID, false),
222            AccountMeta::new_readonly(sysvar::rent::ID, false),
223        ],
224        data: Initialize {}.to_bytes(),
225    }
226}
227
228pub fn build_advance_ix(
229    signer: Pubkey
230) -> Instruction {
231    Instruction {
232        program_id: crate::ID,
233        accounts: vec![
234            AccountMeta::new(signer, true),
235            AccountMeta::new(SPOOL_ADDRESSES[0], false),
236            AccountMeta::new(SPOOL_ADDRESSES[1], false),
237            AccountMeta::new(SPOOL_ADDRESSES[2], false),
238            AccountMeta::new(SPOOL_ADDRESSES[3], false),
239            AccountMeta::new(SPOOL_ADDRESSES[4], false),
240            AccountMeta::new(SPOOL_ADDRESSES[5], false),
241            AccountMeta::new(SPOOL_ADDRESSES[6], false),
242            AccountMeta::new(SPOOL_ADDRESSES[7], false),
243            AccountMeta::new(EPOCH_ADDRESS, false),
244            AccountMeta::new(MINT_ADDRESS, false),
245            AccountMeta::new(TREASURY_ADDRESS, false),
246            AccountMeta::new(TREASURY_ATA, false),
247            AccountMeta::new_readonly(spl_token::ID, false),
248        ],
249        data: Advance {}.to_bytes(),
250    }
251}