acta-maker-sdk 0.8.2

Rust SDK for the Acta options RFQ venue on Solana: maker and vault-operator WebSocket clients, order signing, chain builders
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
421
422
423
424
425
426
427
428
429
430
431
432
433
#[cfg(feature = "chain-rpc")]
use solana_rpc_client::nonblocking::rpc_client::RpcClient;
#[cfg(feature = "chain-rpc")]
use solana_sdk::account::Account;
use solana_sdk::{
    instruction::{AccountMeta, Instruction},
    pubkey::Pubkey,
};

#[cfg(feature = "chain-rpc")]
use super::ChainError;

/// Default amount left in the maker wallet after wrapping, for fee headroom.
pub const DEFAULT_SOL_RESERVE_LAMPORTS: u64 = 1_200_000;
pub const NATIVE_MINT: Pubkey = solana_sdk::pubkey!("So11111111111111111111111111111111111111112");

pub(crate) const TOKEN_PROGRAM_ID: Pubkey =
    solana_sdk::pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
const SYSTEM_PROGRAM_ID: Pubkey = solana_sdk::pubkey!("11111111111111111111111111111111");
const SPL_TOKEN_SYNC_NATIVE: u8 = 17;
const SPL_TOKEN_CLOSE_ACCOUNT: u8 = 9;
const SYSTEM_INSTRUCTION_TRANSFER: u32 = 2;
const SYSTEM_INSTRUCTION_CREATE_ACCOUNT_WITH_SEED: u32 = 3;
const SPL_TOKEN_TRANSFER: u8 = 3;
const SPL_TOKEN_INITIALIZE_ACCOUNT3: u8 = 18;
/// Rent-exempt minimum of a 165-byte token account.
const TOKEN_ACCOUNT_RENT_LAMPORTS: u64 = 2_039_280;
/// Seed of the owner's temporary wSOL account; it is created and closed in one transaction.
const UNWRAP_SEED: &str = "acta-unwrap";
#[cfg(feature = "chain-rpc")]
const TOKEN_ACCOUNT_MINT_OFFSET: usize = 0;
#[cfg(feature = "chain-rpc")]
const TOKEN_ACCOUNT_OWNER_OFFSET: usize = 32;
#[cfg(feature = "chain-rpc")]
const TOKEN_ACCOUNT_AMOUNT_OFFSET: usize = 64;
#[cfg(feature = "chain-rpc")]
const TOKEN_ACCOUNT_BASE_LEN: usize = 72;
#[cfg(feature = "chain-rpc")]
const TOKEN_ACCOUNT_RENT_LEN: usize = 165;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum NativeSolFunding {
    #[default]
    Disabled,
    WrapIfNeeded {
        reserve_lamports: u64,
    },
}

impl NativeSolFunding {
    #[must_use]
    pub const fn with_default_reserve() -> Self {
        Self::WrapIfNeeded {
            reserve_lamports: DEFAULT_SOL_RESERVE_LAMPORTS,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg(feature = "chain-rpc")]
pub(crate) enum WrapDecision {
    NotNeeded,
    Wrap {
        wrap_lamports: u64,
    },
    InsufficientNativeSol {
        needed_lamports: u64,
        available_lamports: u64,
    },
}

#[cfg(feature = "chain-rpc")]
pub(crate) enum WrapPlan {
    NotNeeded,
    Wrap {
        instructions: Vec<Instruction>,
        budget: NativeSolBudget,
    },
}

#[cfg(feature = "chain-rpc")]
pub(crate) struct WsolWrapRequest {
    pub(crate) owner: Pubkey,
    pub(crate) funding_ata: Pubkey,
    pub(crate) token_program: Pubkey,
    pub(crate) expected_settlement: u64,
    pub(crate) reserve_lamports: u64,
    pub(crate) allow_account_creation: bool,
    pub(crate) fee_payer: Pubkey,
    pub(crate) additional_ata_rent_lamports: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg(feature = "chain-rpc")]
pub(crate) struct NativeSolBudget {
    pub(crate) owner: Pubkey,
    wrap_lamports: u64,
    ata_rent_lamports: u64,
    reserve_lamports: u64,
}

#[cfg(feature = "chain-rpc")]
impl NativeSolBudget {
    fn required_lamports(self, fee_lamports: u64) -> Result<u64, ChainError> {
        self.wrap_lamports
            .checked_add(self.ata_rent_lamports)
            .and_then(|value| value.checked_add(fee_lamports))
            .and_then(|value| value.checked_add(self.reserve_lamports))
            .ok_or(ChainError::NativeSolBudgetOverflow)
    }

    pub(crate) fn validate(
        self,
        available_lamports: u64,
        fee_lamports: u64,
    ) -> Result<(), ChainError> {
        let needed_lamports = self.required_lamports(fee_lamports)?;
        if available_lamports < needed_lamports {
            return Err(ChainError::InsufficientNativeSol {
                needed_lamports,
                available_lamports,
            });
        }
        Ok(())
    }
}

#[cfg(feature = "chain-rpc")]
pub(crate) fn is_native_mint(mint: &Pubkey) -> bool {
    *mint == NATIVE_MINT
}

#[cfg(feature = "chain-rpc")]
pub(crate) fn decide_wrap(
    existing_wsol: u64,
    expected_settlement: u64,
    sol_balance: u64,
    reserve_lamports: u64,
    ata_rent_lamports: u64,
) -> WrapDecision {
    if existing_wsol >= expected_settlement {
        return WrapDecision::NotNeeded;
    }
    let wrap_lamports = expected_settlement - existing_wsol;
    let needed_lamports = wrap_lamports
        .saturating_add(ata_rent_lamports)
        .saturating_add(reserve_lamports);
    if sol_balance < needed_lamports {
        return WrapDecision::InsufficientNativeSol {
            needed_lamports,
            available_lamports: sol_balance,
        };
    }
    WrapDecision::Wrap { wrap_lamports }
}

#[cfg(feature = "chain-rpc")]
pub(crate) async fn plan_wsol_wrap(
    rpc: &RpcClient,
    account: Option<Account>,
    request: WsolWrapRequest,
) -> Result<WrapPlan, ChainError> {
    let WsolWrapRequest {
        owner,
        funding_ata,
        token_program,
        expected_settlement,
        reserve_lamports,
        allow_account_creation,
        fee_payer,
        additional_ata_rent_lamports,
    } = request;
    let (existing_wsol, ata_rent_lamports) = match account {
        Some(account) => (parse_wsol_balance(&account, &owner, &token_program)?, 0),
        None if allow_account_creation => {
            let rent = if fee_payer == owner {
                rpc.get_minimum_balance_for_rent_exemption(TOKEN_ACCOUNT_RENT_LEN)
                    .await?
            } else {
                0
            };
            (0, rent)
        }
        None => return Err(ChainError::MissingFundingAccount(funding_ata)),
    };
    if existing_wsol >= expected_settlement {
        return Ok(WrapPlan::NotNeeded);
    }
    let sol_balance = rpc.get_balance(&owner).await?;

    match decide_wrap(
        existing_wsol,
        expected_settlement,
        sol_balance,
        reserve_lamports,
        ata_rent_lamports.saturating_add(additional_ata_rent_lamports),
    ) {
        WrapDecision::NotNeeded => Ok(WrapPlan::NotNeeded),
        WrapDecision::InsufficientNativeSol {
            needed_lamports,
            available_lamports,
        } => Err(ChainError::InsufficientNativeSol {
            needed_lamports,
            available_lamports,
        }),
        WrapDecision::Wrap { wrap_lamports } => Ok(WrapPlan::Wrap {
            instructions: vec![
                build_system_transfer_ix(&owner, &funding_ata, wrap_lamports),
                build_sync_native_ix(&token_program, &funding_ata),
            ],
            budget: NativeSolBudget {
                owner,
                wrap_lamports,
                ata_rent_lamports: ata_rent_lamports
                    .checked_add(additional_ata_rent_lamports)
                    .ok_or(ChainError::NativeSolBudgetOverflow)?,
                reserve_lamports,
            },
        }),
    }
}

#[cfg(feature = "chain-rpc")]
pub(crate) async fn token_account_rent_lamports(rpc: &RpcClient) -> Result<u64, ChainError> {
    rpc.get_minimum_balance_for_rent_exemption(TOKEN_ACCOUNT_RENT_LEN)
        .await
        .map_err(ChainError::from)
}

#[cfg(feature = "chain-rpc")]
fn parse_wsol_balance(
    account: &Account,
    expected_owner: &Pubkey,
    token_program: &Pubkey,
) -> Result<u64, ChainError> {
    if account.owner != *token_program || account.data.len() < TOKEN_ACCOUNT_BASE_LEN {
        return Err(ChainError::InvalidAccountData);
    }
    if read_pubkey(&account.data, TOKEN_ACCOUNT_MINT_OFFSET)? != NATIVE_MINT
        || read_pubkey(&account.data, TOKEN_ACCOUNT_OWNER_OFFSET)? != *expected_owner
    {
        return Err(ChainError::InvalidAccountData);
    }
    read_u64(&account.data, TOKEN_ACCOUNT_AMOUNT_OFFSET)
}

#[cfg(feature = "chain-rpc")]
fn read_pubkey(data: &[u8], offset: usize) -> Result<Pubkey, ChainError> {
    let bytes: [u8; 32] = data
        .get(offset..offset + 32)
        .ok_or(ChainError::InvalidAccountData)?
        .try_into()
        .map_err(|_| ChainError::InvalidAccountData)?;
    Ok(Pubkey::new_from_array(bytes))
}

#[cfg(feature = "chain-rpc")]
fn read_u64(data: &[u8], offset: usize) -> Result<u64, ChainError> {
    let bytes: [u8; 8] = data
        .get(offset..offset + 8)
        .ok_or(ChainError::InvalidAccountData)?
        .try_into()
        .map_err(|_| ChainError::InvalidAccountData)?;
    Ok(u64::from_le_bytes(bytes))
}

/// Builds the explicit SOL-to-wSOL step; callers aggregate batch deficits and budget rent and fees.
pub fn build_wrap_sol_ixs(owner: Pubkey, fee_payer: Pubkey, lamports: u64) -> Vec<Instruction> {
    let ata = super::ix::derive_associated_token_address(&owner, &NATIVE_MINT, &TOKEN_PROGRAM_ID);
    vec![
        super::ix::create_associated_token_account_idempotent_ix(
            &fee_payer,
            &ata,
            &owner,
            &NATIVE_MINT,
            &TOKEN_PROGRAM_ID,
        ),
        build_system_transfer_ix(&owner, &ata, lamports),
        build_sync_native_ix(&TOKEN_PROGRAM_ID, &ata),
    ]
}

/// Wraps `lamports` of native SOL and deposits them into the Maker PDA custody; `maker_owner` signs.
pub fn build_maker_deposit_native_sol_ixs(
    program_id: &Pubkey,
    maker_owner: Pubkey,
    fee_payer: Pubkey,
    lamports: u64,
) -> Result<Vec<Instruction>, super::ChainIxError> {
    let args = super::MakerDepositIxArgs {
        maker_owner,
        amount: lamports,
        premium_mint: NATIVE_MINT,
        token_program: TOKEN_PROGRAM_ID,
        create_atas: true,
    };
    let mut ixs = super::build_maker_deposit_ixs(program_id, &args, fee_payer)?;
    let Some(deposit) = ixs.pop() else {
        return Ok(ixs);
    };
    let ata =
        super::ix::derive_associated_token_address(&maker_owner, &NATIVE_MINT, &TOKEN_PROGRAM_ID);
    ixs.push(build_system_transfer_ix(&maker_owner, &ata, lamports));
    ixs.push(build_sync_native_ix(&TOKEN_PROGRAM_ID, &ata));
    ixs.push(deposit);
    Ok(ixs)
}

/// Withdraws `lamports` of wSOL from the Maker PDA custody and unwraps exactly that amount: it
/// moves to a temporary wSOL account derived from the owner, which is closed in the same
/// transaction. Other wSOL in the owner's account stays wrapped. `maker_owner` signs and fronts
/// the temporary account's rent, which the close returns.
pub fn build_maker_withdraw_native_sol_ixs(
    program_id: &Pubkey,
    maker_owner: Pubkey,
    fee_payer: Pubkey,
    lamports: u64,
) -> Result<Vec<Instruction>, super::ChainIxError> {
    let args = super::MakerWithdrawIxArgs {
        maker_owner,
        amount: lamports,
        premium_mint: NATIVE_MINT,
        token_program: TOKEN_PROGRAM_ID,
        create_atas: true,
    };
    let mut ixs = super::build_maker_withdraw_ixs(program_id, &args, fee_payer)?;
    let ata =
        super::ix::derive_associated_token_address(&maker_owner, &NATIVE_MINT, &TOKEN_PROGRAM_ID);
    let temporary = unwrap_account(&maker_owner);
    ixs.extend([
        build_create_account_with_seed_ix(&maker_owner, &temporary),
        build_initialize_account3_ix(&temporary, &maker_owner),
        build_token_transfer_ix(&ata, &temporary, &maker_owner, lamports),
        build_close_account_ix(&TOKEN_PROGRAM_ID, &temporary, &maker_owner),
    ]);
    Ok(ixs)
}

/// The owner's temporary wSOL account used to unwrap an exact amount: `create_with_seed(owner,
/// "acta-unwrap", token program)`, hashed directly because the fixed seed cannot fail its checks.
pub fn unwrap_account(owner: &Pubkey) -> Pubkey {
    use sha2::{Digest, Sha256};
    let hash = Sha256::new()
        .chain_update(owner.as_ref())
        .chain_update(UNWRAP_SEED.as_bytes())
        .chain_update(TOKEN_PROGRAM_ID.as_ref())
        .finalize();
    Pubkey::new_from_array(hash.into())
}

fn build_create_account_with_seed_ix(owner: &Pubkey, account: &Pubkey) -> Instruction {
    let mut data = Vec::with_capacity(4 + 32 + 8 + UNWRAP_SEED.len() + 8 + 8 + 32);
    data.extend_from_slice(&SYSTEM_INSTRUCTION_CREATE_ACCOUNT_WITH_SEED.to_le_bytes());
    data.extend_from_slice(owner.as_ref());
    data.extend_from_slice(&(UNWRAP_SEED.len() as u64).to_le_bytes());
    data.extend_from_slice(UNWRAP_SEED.as_bytes());
    data.extend_from_slice(&TOKEN_ACCOUNT_RENT_LAMPORTS.to_le_bytes());
    data.extend_from_slice(&(TOKEN_ACCOUNT_RENT_LEN as u64).to_le_bytes());
    data.extend_from_slice(TOKEN_PROGRAM_ID.as_ref());
    Instruction {
        program_id: SYSTEM_PROGRAM_ID,
        accounts: vec![
            AccountMeta::new(*owner, true),
            AccountMeta::new(*account, false),
            AccountMeta::new_readonly(*owner, true),
        ],
        data,
    }
}

fn build_initialize_account3_ix(account: &Pubkey, owner: &Pubkey) -> Instruction {
    let mut data = Vec::with_capacity(33);
    data.push(SPL_TOKEN_INITIALIZE_ACCOUNT3);
    data.extend_from_slice(owner.as_ref());
    Instruction {
        program_id: TOKEN_PROGRAM_ID,
        accounts: vec![
            AccountMeta::new(*account, false),
            AccountMeta::new_readonly(NATIVE_MINT, false),
        ],
        data,
    }
}

fn build_token_transfer_ix(from: &Pubkey, to: &Pubkey, owner: &Pubkey, amount: u64) -> Instruction {
    let mut data = Vec::with_capacity(9);
    data.push(SPL_TOKEN_TRANSFER);
    data.extend_from_slice(&amount.to_le_bytes());
    Instruction {
        program_id: TOKEN_PROGRAM_ID,
        accounts: vec![
            AccountMeta::new(*from, false),
            AccountMeta::new(*to, false),
            AccountMeta::new_readonly(*owner, true),
        ],
        data,
    }
}

fn build_close_account_ix(token_program: &Pubkey, account: &Pubkey, owner: &Pubkey) -> Instruction {
    Instruction {
        program_id: *token_program,
        accounts: vec![
            AccountMeta::new(*account, false),
            AccountMeta::new(*owner, false),
            AccountMeta::new_readonly(*owner, true),
        ],
        data: vec![SPL_TOKEN_CLOSE_ACCOUNT],
    }
}

fn build_system_transfer_ix(from: &Pubkey, to: &Pubkey, lamports: u64) -> Instruction {
    let mut data = Vec::with_capacity(12);
    data.extend_from_slice(&SYSTEM_INSTRUCTION_TRANSFER.to_le_bytes());
    data.extend_from_slice(&lamports.to_le_bytes());
    Instruction {
        program_id: SYSTEM_PROGRAM_ID,
        accounts: vec![AccountMeta::new(*from, true), AccountMeta::new(*to, false)],
        data,
    }
}

fn build_sync_native_ix(token_program: &Pubkey, native_account: &Pubkey) -> Instruction {
    Instruction {
        program_id: *token_program,
        accounts: vec![AccountMeta::new(*native_account, false)],
        data: vec![SPL_TOKEN_SYNC_NATIVE],
    }
}

#[cfg(all(test, feature = "chain-rpc"))]
#[path = "wsol_tests.rs"]
mod tests;