ore_api/
sdk.rs

1use drillx::Solution;
2use steel::*;
3
4use crate::{
5    consts::*,
6    instruction::*,
7    state::{bus_pda, config_pda, proof_pda, treasury_pda},
8};
9
10/// Builds an auth instruction.
11pub fn auth(proof: Pubkey) -> Instruction {
12    Instruction {
13        program_id: NOOP_PROGRAM_ID,
14        accounts: vec![],
15        data: proof.to_bytes().to_vec(),
16    }
17}
18
19/// Builds a claim instruction.
20pub fn claim(signer: Pubkey, beneficiary: Pubkey, amount: u64) -> Instruction {
21    let proof = proof_pda(signer).0;
22    Instruction {
23        program_id: crate::ID,
24        accounts: vec![
25            AccountMeta::new(signer, true),
26            AccountMeta::new(beneficiary, false),
27            AccountMeta::new(proof, false),
28            AccountMeta::new_readonly(TREASURY_ADDRESS, false),
29            AccountMeta::new(TREASURY_TOKENS_ADDRESS, false),
30            AccountMeta::new_readonly(spl_token::ID, false),
31        ],
32        data: Claim {
33            amount: amount.to_le_bytes(),
34        }
35        .to_bytes(),
36    }
37}
38
39/// Builds a close instruction.
40pub fn close(signer: Pubkey) -> Instruction {
41    let proof = proof_pda(signer).0;
42    Instruction {
43        program_id: crate::ID,
44        accounts: vec![
45            AccountMeta::new(signer, true),
46            AccountMeta::new(proof, false),
47            AccountMeta::new_readonly(solana_program::system_program::ID, false),
48        ],
49        data: Close {}.to_bytes(),
50    }
51}
52
53/// Builds a mine instruction.
54pub fn mine(
55    signer: Pubkey,
56    authority: Pubkey,
57    bus: Pubkey,
58    solution: Solution,
59    boost_config: Pubkey,
60) -> Instruction {
61    let proof = proof_pda(authority).0;
62    let accounts = vec![
63        AccountMeta::new(signer, true),
64        AccountMeta::new(bus, false),
65        AccountMeta::new_readonly(CONFIG_ADDRESS, false),
66        AccountMeta::new(proof, false),
67        AccountMeta::new_readonly(sysvar::instructions::ID, false),
68        AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
69        AccountMeta::new_readonly(boost_config, false),
70        AccountMeta::new(proof_pda(boost_config).0, false),
71    ];
72    Instruction {
73        program_id: crate::ID,
74        accounts,
75        data: Mine {
76            digest: solution.d,
77            nonce: solution.n,
78        }
79        .to_bytes(),
80    }
81}
82
83/// Builds an open instruction.
84pub fn open(signer: Pubkey, miner: Pubkey, payer: Pubkey) -> Instruction {
85    let proof_pda = proof_pda(signer);
86    Instruction {
87        program_id: crate::ID,
88        accounts: vec![
89            AccountMeta::new(signer, true),
90            AccountMeta::new_readonly(miner, false),
91            AccountMeta::new(payer, true),
92            AccountMeta::new(proof_pda.0, false),
93            AccountMeta::new_readonly(solana_program::system_program::ID, false),
94            AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
95        ],
96        data: Open {}.to_bytes(),
97    }
98}
99
100/// Builds a reset instruction.
101pub fn reset(signer: Pubkey) -> Instruction {
102    Instruction {
103        program_id: crate::ID,
104        accounts: vec![
105            AccountMeta::new(signer, true),
106            AccountMeta::new(BUS_ADDRESSES[0], false),
107            AccountMeta::new(BUS_ADDRESSES[1], false),
108            AccountMeta::new(BUS_ADDRESSES[2], false),
109            AccountMeta::new(BUS_ADDRESSES[3], false),
110            AccountMeta::new(BUS_ADDRESSES[4], false),
111            AccountMeta::new(BUS_ADDRESSES[5], false),
112            AccountMeta::new(BUS_ADDRESSES[6], false),
113            AccountMeta::new(BUS_ADDRESSES[7], false),
114            AccountMeta::new(CONFIG_ADDRESS, false),
115            AccountMeta::new(MINT_ADDRESS, false),
116            AccountMeta::new(TREASURY_ADDRESS, false),
117            AccountMeta::new(TREASURY_TOKENS_ADDRESS, false),
118            AccountMeta::new_readonly(spl_token::ID, false),
119        ],
120        data: Reset {}.to_bytes(),
121    }
122}
123
124// Build an update instruction.
125pub fn update(signer: Pubkey, miner: Pubkey) -> Instruction {
126    let proof = proof_pda(signer).0;
127    Instruction {
128        program_id: crate::ID,
129        accounts: vec![
130            AccountMeta::new(signer, true),
131            AccountMeta::new_readonly(miner, false),
132            AccountMeta::new(proof, false),
133        ],
134        data: Update {}.to_bytes(),
135    }
136}
137
138/// Builds an initialize instruction.
139pub fn initialize(signer: Pubkey) -> Instruction {
140    let bus_pdas = [
141        bus_pda(0),
142        bus_pda(1),
143        bus_pda(2),
144        bus_pda(3),
145        bus_pda(4),
146        bus_pda(5),
147        bus_pda(6),
148        bus_pda(7),
149    ];
150    let config_pda = config_pda();
151    let mint_pda = Pubkey::find_program_address(&[MINT, MINT_NOISE.as_slice()], &crate::ID);
152    let treasury_pda = treasury_pda();
153    let metadata_pda = Pubkey::find_program_address(
154        &[
155            METADATA,
156            mpl_token_metadata::ID.as_ref(),
157            mint_pda.0.as_ref(),
158        ],
159        &mpl_token_metadata::ID,
160    );
161    Instruction {
162        program_id: crate::ID,
163        accounts: vec![
164            AccountMeta::new(signer, true),
165            AccountMeta::new(bus_pdas[0].0, false),
166            AccountMeta::new(bus_pdas[1].0, false),
167            AccountMeta::new(bus_pdas[2].0, false),
168            AccountMeta::new(bus_pdas[3].0, false),
169            AccountMeta::new(bus_pdas[4].0, false),
170            AccountMeta::new(bus_pdas[5].0, false),
171            AccountMeta::new(bus_pdas[6].0, false),
172            AccountMeta::new(bus_pdas[7].0, false),
173            AccountMeta::new(config_pda.0, false),
174            AccountMeta::new(metadata_pda.0, false),
175            AccountMeta::new(mint_pda.0, false),
176            AccountMeta::new(treasury_pda.0, false),
177            AccountMeta::new(TREASURY_TOKENS_ADDRESS, false),
178            AccountMeta::new_readonly(system_program::ID, false),
179            AccountMeta::new_readonly(spl_token::ID, false),
180            AccountMeta::new_readonly(spl_associated_token_account::ID, false),
181            AccountMeta::new_readonly(mpl_token_metadata::ID, false),
182            AccountMeta::new_readonly(sysvar::rent::ID, false),
183        ],
184        data: Initialize {}.to_bytes(),
185    }
186}