eore_pool_api/
sdk.rs

1use drillx::Solution;
2use eore_api::{
3    consts::{CONFIG_ADDRESS, MINT_ADDRESS, TREASURY_ADDRESS, TREASURY_TOKENS_ADDRESS},
4    state::proof_pda,
5};
6use steel::*;
7
8use crate::{
9    consts::LEGACY_BOOST_PROGRAM_ID,
10    error::ApiError,
11    instruction::*,
12    state::{legacy_boost_pda, legacy_stake_pda, member_pda, pool_pda, pool_proof_pda, share_pda},
13};
14
15/// Builds a launch instruction.
16#[allow(deprecated)]
17pub fn launch(signer: Pubkey, miner: Pubkey, url: String) -> Result<Instruction, ApiError> {
18    let url = url_to_bytes(url.as_str())?;
19    let (pool_pda, pool_bump) = pool_pda(signer);
20    let (proof_pda, proof_bump) = pool_proof_pda(pool_pda);
21    let ix = Instruction {
22        program_id: crate::ID,
23        accounts: vec![
24            AccountMeta::new(signer, true),
25            AccountMeta::new_readonly(miner, false),
26            AccountMeta::new(pool_pda, false),
27            AccountMeta::new(proof_pda, false),
28            AccountMeta::new_readonly(eore_api::ID, false),
29            AccountMeta::new_readonly(eore_boost_api::ID, false),
30            AccountMeta::new_readonly(spl_token::ID, false),
31            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
32            AccountMeta::new_readonly(system_program::ID, false),
33            AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
34        ],
35        data: Launch {
36            pool_bump,
37            proof_bump,
38            url,
39        }
40        .to_bytes(),
41    };
42    Ok(ix)
43}
44
45/// Builds an join instruction.
46#[allow(deprecated)]
47pub fn join(member_authority: Pubkey, pool: Pubkey, payer: Pubkey) -> Instruction {
48    let (member_pda, member_bump) = member_pda(member_authority, pool);
49    Instruction {
50        program_id: crate::ID,
51        accounts: vec![
52            AccountMeta::new(payer, true),
53            AccountMeta::new_readonly(member_authority, false),
54            AccountMeta::new(member_pda, false),
55            AccountMeta::new(pool, false),
56            AccountMeta::new_readonly(system_program::ID, false),
57        ],
58        data: Join { member_bump }.to_bytes(),
59    }
60}
61
62/// Builds a claim instruction.
63#[allow(deprecated)]
64pub fn claim(
65    signer: Pubkey,
66    beneficiary: Pubkey,
67    pool_address: Pubkey,
68    amount: u64,
69) -> Instruction {
70    let (member_pda, _) = member_pda(signer, pool_address);
71    let (pool_proof_pda, _) = proof_pda(pool_address);
72    let pool_tokens_address =
73        spl_associated_token_account::get_associated_token_address(&pool_address, &MINT_ADDRESS);
74    Instruction {
75        program_id: crate::ID,
76        accounts: vec![
77            AccountMeta::new(signer, true),
78            AccountMeta::new(beneficiary, false),
79            AccountMeta::new(member_pda, false),
80            AccountMeta::new(pool_address, false),
81            AccountMeta::new(pool_tokens_address, false),
82            AccountMeta::new(pool_proof_pda, false),
83            AccountMeta::new_readonly(TREASURY_ADDRESS, false),
84            AccountMeta::new(TREASURY_TOKENS_ADDRESS, false),
85            AccountMeta::new_readonly(eore_api::ID, false),
86            AccountMeta::new_readonly(spl_token::ID, false),
87        ],
88        data: Claim {
89            amount: amount.to_le_bytes(),
90            pool_bump: 0,
91        }
92        .to_bytes(),
93    }
94}
95
96/// Builds an attribute instruction.
97pub fn attribute(signer: Pubkey, member_authority: Pubkey, total_balance: u64) -> Instruction {
98    let (pool_address, _) = pool_pda(signer);
99    let (proof_address, _) = pool_proof_pda(pool_address);
100    let (member_address, _) = member_pda(member_authority, pool_address);
101    let pool_tokens_address =
102        spl_associated_token_account::get_associated_token_address(&pool_address, &MINT_ADDRESS);
103    Instruction {
104        program_id: crate::ID,
105        accounts: vec![
106            AccountMeta::new(signer, true),
107            AccountMeta::new(pool_address, false),
108            AccountMeta::new(pool_tokens_address, false),
109            AccountMeta::new(proof_address, false),
110            AccountMeta::new(member_address, false),
111        ],
112        data: Attribute {
113            total_balance: total_balance.to_le_bytes(),
114        }
115        .to_bytes(),
116    }
117}
118
119/// Builds a commit instruction.
120#[deprecated(
121    since = "0.3.0",
122    note = "Staking has moved to the global boost program"
123)]
124#[allow(deprecated)]
125pub fn commit(_signer: Pubkey, _mint: Pubkey) -> Instruction {
126    panic!("Staking has moved to the global boost program");
127}
128
129/// Builds an submit instruction.
130pub fn submit(
131    signer: Pubkey,
132    solution: Solution,
133    attestation: [u8; 32],
134    bus: Pubkey,
135    // boost_accounts: Option<[Pubkey; 3]>,
136) -> Instruction {
137    let (pool_pda, _) = pool_pda(signer);
138    let (proof_pda, _) = pool_proof_pda(pool_pda);
139    let (boost_config, _) = eore_boost_api::state::config_pda();
140    let (boost_proof, _) = eore_api::state::proof_pda(boost_config);
141    let accounts = vec![
142        AccountMeta::new(signer, true),
143        AccountMeta::new(bus, false),
144        AccountMeta::new_readonly(CONFIG_ADDRESS, false),
145        AccountMeta::new(pool_pda, false),
146        AccountMeta::new(proof_pda, false),
147        AccountMeta::new_readonly(eore_api::ID, false),
148        AccountMeta::new_readonly(system_program::ID, false),
149        AccountMeta::new_readonly(sysvar::instructions::ID, false),
150        AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
151        AccountMeta::new_readonly(boost_config, false),
152        AccountMeta::new(boost_proof, false),
153    ];
154    Instruction {
155        program_id: crate::ID,
156        accounts,
157        data: Submit {
158            attestation,
159            digest: solution.d,
160            nonce: solution.n,
161        }
162        .to_bytes(),
163    }
164}
165
166/// Builds an unstake instruction for legacy boost program.
167pub fn unstake(
168    signer: Pubkey,
169    mint: Pubkey,
170    pool: Pubkey,
171    recipient: Pubkey,
172    amount: u64,
173) -> Instruction {
174    let (boost_pda, _) = legacy_boost_pda(mint);
175    let boost_tokens =
176        spl_associated_token_account::get_associated_token_address(&boost_pda, &mint);
177    let (member_pda, _) = member_pda(signer, pool);
178    let pool_tokens = spl_associated_token_account::get_associated_token_address(&pool, &mint);
179    let (share_pda, _) = share_pda(signer, pool, mint);
180    let (stake_pda, _) = legacy_stake_pda(pool, boost_pda);
181    Instruction {
182        program_id: crate::ID,
183        accounts: vec![
184            AccountMeta::new(signer, true),
185            AccountMeta::new(boost_pda, false),
186            AccountMeta::new(boost_tokens, false),
187            AccountMeta::new_readonly(mint, false),
188            AccountMeta::new_readonly(member_pda, false),
189            AccountMeta::new(pool, false),
190            AccountMeta::new(pool_tokens, false),
191            AccountMeta::new(recipient, false),
192            AccountMeta::new(share_pda, false),
193            AccountMeta::new(stake_pda, false),
194            AccountMeta::new_readonly(spl_token::ID, false),
195            AccountMeta::new_readonly(LEGACY_BOOST_PROGRAM_ID, false),
196        ],
197        data: Unstake {
198            amount: amount.to_le_bytes(),
199        }
200        .to_bytes(),
201    }
202}
203
204/// builds a stake instruction.
205#[deprecated(
206    since = "0.3.0",
207    note = "Staking has moved to the global boost program"
208)]
209#[allow(deprecated)]
210pub fn stake(
211    signer: Pubkey,
212    mint: Pubkey,
213    pool: Pubkey,
214    sender: Pubkey,
215    amount: u64,
216) -> Instruction {
217    let (member_pda, _) = member_pda(signer, pool);
218    let pool_tokens = spl_associated_token_account::get_associated_token_address(&pool, &mint);
219    let (share_pda, _) = share_pda(signer, pool, mint);
220    Instruction {
221        program_id: crate::ID,
222        accounts: vec![
223            AccountMeta::new(signer, true),
224            AccountMeta::new_readonly(mint, false),
225            AccountMeta::new_readonly(member_pda, false),
226            AccountMeta::new_readonly(pool, false),
227            AccountMeta::new(pool_tokens, false),
228            AccountMeta::new(sender, false),
229            AccountMeta::new(share_pda, false),
230            AccountMeta::new_readonly(spl_token::ID, false),
231        ],
232        data: Stake {
233            amount: amount.to_le_bytes(),
234        }
235        .to_bytes(),
236    }
237}
238
239/// Builds an open share instruction.
240#[deprecated(
241    since = "0.3.0",
242    note = "Staking has moved to the global boost program"
243)]
244#[allow(deprecated)]
245pub fn open_share(_signer: Pubkey, _mint: Pubkey, _pool: Pubkey) -> Instruction {
246    panic!("Staking has moved to the global boost program");
247}
248
249/// Builds an open stake instruction.
250#[deprecated(
251    since = "0.3.0",
252    note = "Staking has moved to the global boost program"
253)]
254#[allow(deprecated)]
255pub fn open_stake(_signer: Pubkey, _mint: Pubkey) -> Instruction {
256    panic!("Staking has moved to the global boost program");
257}
258
259fn url_to_bytes(input: &str) -> Result<[u8; 128], ApiError> {
260    let bytes = input.as_bytes();
261    let len = bytes.len();
262    if len > 128 {
263        Err(ApiError::UrlTooLarge)
264    } else {
265        let mut array = [0u8; 128];
266        array[..len].copy_from_slice(&bytes[..len]);
267        Ok(array)
268    }
269}