satrush-client 0.1.4

Rust client to interact with SatRush's on-chain program.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Environment-free instruction builders.
//!
//! Thin composition helpers over the generated instruction structs: they derive
//! every PDA and associated token account from the program's fixed seeds, so a
//! caller only supplies the signing authority and the mint addresses. Shared by
//! the LiteSVM test-suite and the `satrush-cli` binary.

use crate::instructions::{
    CancelPublicAutomation, ClaimUsd, ClaimUsdInstructionArgs, CreateBoard, CreateEpochVault, CreateOneBtcVault,
    CreatePublicAutomation, CreatePublicAutomationInstructionArgs, CreateSatrushConfig,
    CreateSatrushConfigInstructionArgs, CreateSatsVault, CreateTreasury, DeployPublic, DeployPublicInstructionArgs,
    ExecutePublicAutomation, ExecutePublicAutomationInstructionArgs, RotateRound, SettleDeployPublic, SwapRoundStake,
    SwapRoundStakeInstructionArgs, TopUpPublicAutomation, TopUpPublicAutomationInstructionArgs,
    UpdateBoardRoundDuration, UpdateBoardRoundDurationInstructionArgs,
};
use crate::types::AutomationStrategy;
use crate::{
    get_board_address, get_epoch_vault_address, get_epoch_vault_iteration_address, get_event_authority_address,
    get_miner_address, get_one_btc_vault_address, get_one_btc_vault_iteration_address, get_public_automation_address,
    get_public_deployment_address, get_round_address, get_satrush_config_address, get_sats_vault_address,
    get_treasury_address,
};
use solana_instruction::{AccountMeta, Instruction};
use solana_pubkey::Pubkey;

/// SPL Token program.
pub const TOKEN_PROGRAM_ID: Pubkey = Pubkey::from_str_const("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
/// SPL Associated Token Account program.
pub const ASSOCIATED_TOKEN_PROGRAM_ID: Pubkey = Pubkey::from_str_const("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
/// System program.
pub const SYSTEM_PROGRAM_ID: Pubkey = Pubkey::from_str_const("11111111111111111111111111111111");
/// SlotHashes sysvar.
pub const SLOT_HASHES_ID: Pubkey = Pubkey::from_str_const("SysvarS1otHashes111111111111111111111111111");

/// Derive the canonical associated token account for `wallet` holding `mint`.
pub fn get_associated_token_address(wallet: &Pubkey, mint: &Pubkey) -> Pubkey {
    Pubkey::find_program_address(
        &[wallet.as_ref(), TOKEN_PROGRAM_ID.as_ref(), mint.as_ref()],
        &ASSOCIATED_TOKEN_PROGRAM_ID,
    )
    .0
}

/// `create_satrush_config`: the config PDA holding authorities, mints and fee
/// parameters. `authority` pays and must equal the program's upgrade authority.
pub fn get_create_satrush_config_instruction(
    authority: Pubkey,
    args: CreateSatrushConfigInstructionArgs,
) -> Instruction {
    CreateSatrushConfig {
        authority,
        satrush_config: get_satrush_config_address().0,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction(args)
}

/// `create_board`: the board singleton, its USD/BTC pools and the initial round
/// (id 1). Signed by the config's admin authority.
pub fn get_create_board_instruction(authority: Pubkey, usd_mint: Pubkey, btc_mint: Pubkey) -> Instruction {
    let board = get_board_address().0;
    CreateBoard {
        authority,
        satrush_config: get_satrush_config_address().0,
        board,
        initial_round: get_round_address(1).0,
        usd_mint,
        btc_mint,
        board_usd_ata: get_associated_token_address(&board, &usd_mint),
        board_btc_ata: get_associated_token_address(&board, &btc_mint),
        token_program: TOKEN_PROGRAM_ID,
        associated_token_program: ASSOCIATED_TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction()
}

/// `update_board_round_duration`: overwrite the board's `round_duration` (slots).
/// Applies to future round activations only; the active round keeps its window.
/// Signed by the config's admin authority.
pub fn get_update_board_round_duration_instruction(authority: Pubkey, new_round_duration: u32) -> Instruction {
    UpdateBoardRoundDuration {
        authority,
        satrush_config: get_satrush_config_address().0,
        board: get_board_address().0,
    }
    .instruction(UpdateBoardRoundDurationInstructionArgs { new_round_duration })
}

/// `create_epoch_vault`: the epoch vault singleton, its USD/BTC pools and the
/// first iteration (id 1).
pub fn get_create_epoch_vault_instruction(authority: Pubkey, usd_mint: Pubkey, btc_mint: Pubkey) -> Instruction {
    let epoch_vault = get_epoch_vault_address().0;
    CreateEpochVault {
        authority,
        satrush_config: get_satrush_config_address().0,
        epoch_vault,
        epoch_vault_iteration: get_epoch_vault_iteration_address(1).0,
        usd_mint,
        btc_mint,
        epoch_vault_usd_ata: get_associated_token_address(&epoch_vault, &usd_mint),
        epoch_vault_btc_ata: get_associated_token_address(&epoch_vault, &btc_mint),
        token_program: TOKEN_PROGRAM_ID,
        associated_token_program: ASSOCIATED_TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction()
}

/// `create_one_btc_vault`: the 1 BTC vault singleton, its USD/BTC pools and the
/// first iteration (id 1).
pub fn get_create_one_btc_vault_instruction(authority: Pubkey, usd_mint: Pubkey, btc_mint: Pubkey) -> Instruction {
    let one_btc_vault = get_one_btc_vault_address().0;
    CreateOneBtcVault {
        authority,
        satrush_config: get_satrush_config_address().0,
        one_btc_vault,
        one_btc_vault_iteration: get_one_btc_vault_iteration_address(1).0,
        usd_mint,
        btc_mint,
        one_btc_vault_usd_ata: get_associated_token_address(&one_btc_vault, &usd_mint),
        one_btc_vault_btc_ata: get_associated_token_address(&one_btc_vault, &btc_mint),
        token_program: TOKEN_PROGRAM_ID,
        associated_token_program: ASSOCIATED_TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction()
}

/// `create_treasury`: the treasury singleton and its USD fee pool.
pub fn get_create_treasury_instruction(authority: Pubkey, usd_mint: Pubkey) -> Instruction {
    let treasury = get_treasury_address().0;
    CreateTreasury {
        authority,
        satrush_config: get_satrush_config_address().0,
        treasury,
        usd_mint,
        treasury_usd_ata: get_associated_token_address(&treasury, &usd_mint),
        token_program: TOKEN_PROGRAM_ID,
        associated_token_program: ASSOCIATED_TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction()
}

/// `deploy_public`: stake `amount` USD (base units, gross of fees) on the tiles
/// in `selection_mask` for round `round_id`, as the miner `authority`. The round
/// must be the board's current one and open for deploys; the miner profile and
/// deployment record PDAs are created by the instruction.
pub fn get_deploy_public_instruction(
    authority: Pubkey,
    usd_mint: Pubkey,
    round_id: u32,
    selection_mask: u32,
    amount: u64,
) -> Instruction {
    let board = get_board_address().0;
    let epoch_vault = get_epoch_vault_address().0;
    let one_btc_vault = get_one_btc_vault_address().0;
    let treasury = get_treasury_address().0;

    DeployPublic {
        authority,
        satrush_config: get_satrush_config_address().0,
        board,
        usd_mint,
        board_usd_ata: get_associated_token_address(&board, &usd_mint),
        authority_usd_ata: get_associated_token_address(&authority, &usd_mint),
        round: get_round_address(round_id).0,
        public_deployment: get_public_deployment_address(authority, round_id).0,
        miner: get_miner_address(authority).0,
        epoch_vault,
        epoch_vault_usd_ata: get_associated_token_address(&epoch_vault, &usd_mint),
        one_btc_vault,
        one_btc_vault_usd_ata: get_associated_token_address(&one_btc_vault, &usd_mint),
        treasury,
        treasury_usd_ata: get_associated_token_address(&treasury, &usd_mint),
        token_program: TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction(DeployPublicInstructionArgs { selection_mask, amount })
}

/// `rotate_round`: reveal `current_round_id`'s winning tile and deploy round
/// `current_round_id + 1` as the board's new active round. Signed by the
/// config's round authority; valid once the current round's window elapsed.
pub fn get_rotate_round_instruction(authority: Pubkey, current_round_id: u32) -> Instruction {
    RotateRound {
        authority,
        satrush_config: get_satrush_config_address().0,
        board: get_board_address().0,
        current_round: get_round_address(current_round_id).0,
        next_round: get_round_address(current_round_id + 1).0,
        slot_hashes: SLOT_HASHES_ID,
        system_program: SYSTEM_PROGRAM_ID,
        event_authority: get_event_authority_address().0,
        program: crate::SATRUSH_ID,
    }
    .instruction()
}

/// `swap_round_stake`: relay a pre-built aggregator route that converts part of
/// the revealed round's USD into BTC. `swap_data` and `route_accounts` are the
/// route's opaque instruction data and account list; the on-chain handler
/// enforces the economics against observed balance deltas. Signed by the
/// config's round authority.
#[allow(clippy::too_many_arguments)]
pub fn get_swap_round_stake_instruction(
    authority: Pubkey,
    usd_mint: Pubkey,
    btc_mint: Pubkey,
    round_id: u32,
    min_btc_out: u64,
    swap_program: Pubkey,
    swap_data: Vec<u8>,
    route_accounts: &[AccountMeta],
) -> Instruction {
    let board = get_board_address().0;
    SwapRoundStake {
        authority,
        satrush_config: get_satrush_config_address().0,
        board,
        round: get_round_address(round_id).0,
        usd_mint,
        btc_mint,
        board_usd_ata: get_associated_token_address(&board, &usd_mint),
        board_btc_ata: get_associated_token_address(&board, &btc_mint),
        swap_program,
        token_program: TOKEN_PROGRAM_ID,
        event_authority: get_event_authority_address().0,
        program: crate::SATRUSH_ID,
    }
    .instruction_with_remaining_accounts(SwapRoundStakeInstructionArgs { min_btc_out, swap_data }, route_accounts)
}

/// `settle_deploy_public`: settle `deployment_authority`'s deployment in a
/// Settled round. `authority` signs (the owner, or the round authority for
/// automated deployments); `rent_recipient` must be the round authority for
/// automated deployments and the owner for manual ones.
pub fn get_settle_deploy_public_instruction(
    authority: Pubkey,
    deployment_authority: Pubkey,
    rent_recipient: Pubkey,
    usd_mint: Pubkey,
    btc_mint: Pubkey,
    round_id: u32,
) -> Instruction {
    let board = get_board_address().0;
    let sats_vault = get_sats_vault_address().0;
    let public_automation = get_public_automation_address(deployment_authority).0;
    SettleDeployPublic {
        authority,
        rent_recipient,
        satrush_config: get_satrush_config_address().0,
        round: get_round_address(round_id).0,
        board,
        public_deployment: get_public_deployment_address(deployment_authority, round_id).0,
        miner: get_miner_address(deployment_authority).0,
        public_automation,
        automation_usd_ata: get_associated_token_address(&public_automation, &usd_mint),
        board_usd_ata: get_associated_token_address(&board, &usd_mint),
        sats_vault,
        btc_mint,
        usd_mint,
        board_btc_ata: get_associated_token_address(&board, &btc_mint),
        sats_vault_btc_ata: get_associated_token_address(&sats_vault, &btc_mint),
        token_program: TOKEN_PROGRAM_ID,
        associated_token_program: ASSOCIATED_TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
        event_authority: get_event_authority_address().0,
        program: crate::SATRUSH_ID,
    }
    .instruction()
}

/// `create_public_automation`: escrow `deposit_usd_amount` and configure the
/// crank to deploy `per_round_usd_amount` per round with the given strategy.
/// One automation per authority.
#[allow(clippy::too_many_arguments)]
pub fn get_create_public_automation_instruction(
    authority: Pubkey,
    usd_mint: Pubkey,
    strategy: AutomationStrategy,
    selection_mask: u32,
    per_round_usd_amount: u64,
    reload: bool,
    deposit_usd_amount: u64,
) -> Instruction {
    let public_automation = get_public_automation_address(authority).0;
    CreatePublicAutomation {
        authority,
        satrush_config: get_satrush_config_address().0,
        public_automation,
        usd_mint,
        authority_usd_ata: get_associated_token_address(&authority, &usd_mint),
        automation_usd_ata: get_associated_token_address(&public_automation, &usd_mint),
        miner: get_miner_address(authority).0,
        token_program: TOKEN_PROGRAM_ID,
        associated_token_program: ASSOCIATED_TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction(CreatePublicAutomationInstructionArgs {
        strategy,
        selection_mask,
        per_round_usd_amount,
        reload,
        deposit_usd_amount,
    })
}

/// `top_up_public_automation`: move `amount` USD from the authority's account
/// into the automation's escrow.
pub fn get_top_up_public_automation_instruction(authority: Pubkey, usd_mint: Pubkey, amount: u64) -> Instruction {
    let public_automation = get_public_automation_address(authority).0;
    TopUpPublicAutomation {
        authority,
        satrush_config: get_satrush_config_address().0,
        public_automation,
        usd_mint,
        authority_usd_ata: get_associated_token_address(&authority, &usd_mint),
        automation_usd_ata: get_associated_token_address(&public_automation, &usd_mint),
        token_program: TOKEN_PROGRAM_ID,
    }
    .instruction(TopUpPublicAutomationInstructionArgs { amount })
}

/// `cancel_public_automation`: refund the full escrow balance and close the
/// automation and its token account. Unconditional; in-flight deployments
/// settle to the miner profile later.
pub fn get_cancel_public_automation_instruction(authority: Pubkey, usd_mint: Pubkey) -> Instruction {
    let public_automation = get_public_automation_address(authority).0;
    CancelPublicAutomation {
        authority,
        satrush_config: get_satrush_config_address().0,
        public_automation,
        usd_mint,
        automation_usd_ata: get_associated_token_address(&public_automation, &usd_mint),
        authority_usd_ata: get_associated_token_address(&authority, &usd_mint),
        token_program: TOKEN_PROGRAM_ID,
        associated_token_program: ASSOCIATED_TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction()
}

/// `execute_public_automation`: crank-signed per-round deploy funded from
/// `automation_authority`'s escrow. `selection_mask` must be `Some` for
/// Discretionary automations and `None` otherwise.
pub fn get_execute_public_automation_instruction(
    crank_authority: Pubkey,
    automation_authority: Pubkey,
    usd_mint: Pubkey,
    round_id: u32,
    selection_mask: Option<u32>,
) -> Instruction {
    let board = get_board_address().0;
    let epoch_vault = get_epoch_vault_address().0;
    let one_btc_vault = get_one_btc_vault_address().0;
    let treasury = get_treasury_address().0;
    let public_automation = get_public_automation_address(automation_authority).0;

    ExecutePublicAutomation {
        authority: crank_authority,
        satrush_config: get_satrush_config_address().0,
        board,
        round: get_round_address(round_id).0,
        public_automation,
        usd_mint,
        automation_usd_ata: get_associated_token_address(&public_automation, &usd_mint),
        board_usd_ata: get_associated_token_address(&board, &usd_mint),
        public_deployment: get_public_deployment_address(automation_authority, round_id).0,
        miner: get_miner_address(automation_authority).0,
        epoch_vault,
        epoch_vault_usd_ata: get_associated_token_address(&epoch_vault, &usd_mint),
        one_btc_vault,
        one_btc_vault_usd_ata: get_associated_token_address(&one_btc_vault, &usd_mint),
        treasury,
        treasury_usd_ata: get_associated_token_address(&treasury, &usd_mint),
        slot_hashes: SLOT_HASHES_ID,
        token_program: TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction(ExecutePublicAutomationInstructionArgs { selection_mask })
}

/// `claim_usd`: withdraw `amount` of the miner `authority`'s unclaimed USD
/// winnings from the board's USD pool to the authority's USD account. No exit
/// fee — the full amount transfers.
pub fn get_claim_usd_instruction(authority: Pubkey, usd_mint: Pubkey, amount: u64) -> Instruction {
    let board = get_board_address().0;
    ClaimUsd {
        authority,
        satrush_config: get_satrush_config_address().0,
        board,
        miner: get_miner_address(authority).0,
        usd_mint,
        board_usd_ata: get_associated_token_address(&board, &usd_mint),
        authority_usd_ata: get_associated_token_address(&authority, &usd_mint),
        token_program: TOKEN_PROGRAM_ID,
        associated_token_program: ASSOCIATED_TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction(ClaimUsdInstructionArgs { amount })
}

/// `create_sats_vault`: the sats vault singleton and its BTC reserve pool.
pub fn get_create_sats_vault_instruction(authority: Pubkey, btc_mint: Pubkey) -> Instruction {
    let sats_vault = get_sats_vault_address().0;
    CreateSatsVault {
        authority,
        satrush_config: get_satrush_config_address().0,
        sats_vault,
        btc_mint,
        sats_vault_btc_ata: get_associated_token_address(&sats_vault, &btc_mint),
        token_program: TOKEN_PROGRAM_ID,
        associated_token_program: ASSOCIATED_TOKEN_PROGRAM_ID,
        system_program: SYSTEM_PROGRAM_ID,
    }
    .instruction()
}