universalsettle-api 0.2.1

X402-inspired settlement program for any token on Solana
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
use steel::*;

use crate::{
    instruction::*,
    state::{
        authority_transfer_pda, config_pda, fee_shard_pda, fee_shard_sol_storage_pda,
        split_vault_pda, vault_sol_storage_pda,
    },
};

/// Builds an instruction to create a SplitVault for a seller.
///
/// # Arguments
///
/// * `payer` - Fee payer for the account creation (signer)
/// * `seller` - Resource owner wallet address
pub fn create_vault(payer: Pubkey, seller: Pubkey) -> Instruction {
    let (vault_pda, _) = split_vault_pda(&seller);
    let (vault_sol_storage, _) = vault_sol_storage_pda(vault_pda);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(payer, true),
            AccountMeta::new(vault_pda, false),
            AccountMeta::new(vault_sol_storage, false),
            AccountMeta::new_readonly(solana_program::system_program::ID, false),
        ],
        data: CreateVault { seller }.to_bytes(),
    }
}

/// Builds an instruction to sweep funds from a vault to the seller and facilitator.
///
/// # Arguments
///
/// * `payer` - The fee payer and authorized signer (Facilitator)
/// * `vault` - The SplitVault PDA
/// * `seller` - The seller's wallet
/// * `fee_destination` - The facilitator's fee wallet
/// * `token_mint` - For SOL: Pubkey::default(), For SPL: the mint address
/// * `amount` - Amount to sweep (0 = all)
/// * `is_sol` - Whether to sweep native SOL or SPL tokens
/// * `vault_tokens` - The vault's token account (for SPL)
/// * `seller_tokens` - The seller's token account (for SPL)
/// * `fee_dest_tokens` - The facilitator's token account (for SPL)
#[allow(clippy::too_many_arguments)]
pub fn sweep(
    payer: Pubkey,
    vault: Pubkey,
    seller: Pubkey,
    fee_destination: Pubkey,
    token_mint: Pubkey,
    amount: u64,
    is_sol: bool,
    vault_tokens: Option<Pubkey>,
    seller_tokens: Option<Pubkey>,
    fee_dest_tokens: Option<Pubkey>,
    token_program: Option<Pubkey>,
) -> Instruction {
    let (config_pda, _) = config_pda();
    let mut accounts = vec![
        AccountMeta::new(payer, true),
        // vault (SplitVault PDA) must be writable to update recovery status
        AccountMeta::new(vault, false),
        AccountMeta::new_readonly(config_pda, false),
    ];

    if is_sol {
        let (vault_sol_storage, _) = vault_sol_storage_pda(vault);
        accounts.push(AccountMeta::new(vault_sol_storage, false));
        accounts.push(AccountMeta::new(seller, false));
        accounts.push(AccountMeta::new(fee_destination, false));
        accounts.push(AccountMeta::new_readonly(
            solana_program::system_program::ID,
            false,
        ));
    } else {
        accounts.push(AccountMeta::new(
            vault_tokens.expect("vault_tokens is required for SPL sweep"),
            false,
        ));
        accounts.push(AccountMeta::new(
            seller_tokens.expect("seller_tokens is required for SPL sweep"),
            false,
        ));
        accounts.push(AccountMeta::new(
            fee_dest_tokens.expect("fee_dest_tokens is required for SPL sweep"),
            false,
        ));
        accounts.push(AccountMeta::new_readonly(token_mint, false));
        accounts.push(AccountMeta::new_readonly(
            token_program.unwrap_or(spl_token::ID),
            false,
        ));
    }

    Instruction {
        program_id: crate::ID,
        accounts,
        data: Sweep {
            token_mint,
            amount: amount.to_le_bytes(),
            is_sol: if is_sol { [1] } else { [0] },
            _padding: [0; 7],
        }
        .to_bytes(),
    }
}

/// Create an initialize instruction
#[allow(clippy::too_many_arguments)]
pub fn initialize(
    authority: Pubkey,
    fee_destination: Pubkey,
    fee_bps: Option<u16>,
    min_fee_amount: Option<u64>,
    min_fee_amount_sol: Option<u64>,
    provisioning_fee_sol: Option<u64>,
    provisioning_fee_spl: Option<u64>,
    discounted_fee_bps: Option<u16>,
    use_fee_shard: Option<u8>,
    shard_count: Option<u8>,
) -> Instruction {
    let (config_pda, _) = config_pda();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(config_pda, false),
            AccountMeta::new_readonly(solana_program::system_program::ID, false),
            AccountMeta::new_readonly(solana_program::sysvar::rent::ID, false),
        ],
        data: Initialize {
            fee_destination,
            min_fee_amount: (min_fee_amount.unwrap_or(10_000)).to_le_bytes(),
            min_fee_amount_sol: (min_fee_amount_sol.unwrap_or(200_000)).to_le_bytes(),
            provisioning_fee_sol: (provisioning_fee_sol.unwrap_or(10_000_000)).to_le_bytes(),
            provisioning_fee_spl: (provisioning_fee_spl.unwrap_or(1_000_000)).to_le_bytes(),
            fee_bps: (fee_bps.unwrap_or(100)).to_le_bytes(),
            discounted_fee_bps: (discounted_fee_bps.unwrap_or(95)).to_le_bytes(),
            use_fee_shard: use_fee_shard.unwrap_or(0),
            shard_count: shard_count.unwrap_or(8),
            _padding: [0; 2],
        }
        .to_bytes(),
    }
}

/// Create an update authority instruction (Step 1: Propose)
pub fn update_authority(current_authority: Pubkey, new_authority: Pubkey) -> Instruction {
    let (config_pda, _) = config_pda();
    let (transfer_pda, _) = authority_transfer_pda(config_pda);

    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(current_authority, true),
            AccountMeta::new_readonly(config_pda, false),
            AccountMeta::new(transfer_pda, false),
            AccountMeta::new_readonly(solana_program::system_program::ID, false),
        ],
        data: UpdateAuthority { new_authority }.to_bytes(),
    }
}

/// Create an accept authority instruction (Step 2: Accept)
pub fn accept_authority(new_authority: Pubkey) -> Instruction {
    let (config_pda, _) = config_pda();
    let (transfer_pda, _) = authority_transfer_pda(config_pda);

    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(new_authority, true), // signer, writable
            AccountMeta::new(config_pda, false),   // writable
            AccountMeta::new(transfer_pda, false), // writable
        ],
        data: AcceptAuthority {}.to_bytes(),
    }
}

/// Create a cancel authority proposal instruction
pub fn cancel_authority_proposal(current_authority: Pubkey) -> Instruction {
    let (config_pda, _) = config_pda();
    let (transfer_pda, _) = authority_transfer_pda(config_pda);

    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(current_authority, true), // signer, writable
            AccountMeta::new_readonly(config_pda, false),
            AccountMeta::new(transfer_pda, false), // writable
        ],
        data: CancelAuthorityProposal {}.to_bytes(),
    }
}

/// Create an update fee rate instruction
pub fn update_fee_rate(authority: Pubkey, new_fee_bps: u16) -> Instruction {
    let (config_pda, _) = config_pda();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(config_pda, false),
        ],
        data: UpdateFeeRate {
            new_fee_bps: new_fee_bps.to_le_bytes(),
            _padding: [0; 6],
        }
        .to_bytes(),
    }
}

/// Create an update fee destination instruction
pub fn update_fee_destination(authority: Pubkey, new_fee_destination: Pubkey) -> Instruction {
    let (config_pda, _) = config_pda();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(config_pda, false),
        ],
        data: UpdateFeeDestination {
            new_fee_destination,
        }
        .to_bytes(),
    }
}

/// Create an update min fee amount instruction
pub fn update_min_fee_amount(authority: Pubkey, new_min_fee_amount: u64) -> Instruction {
    let (config_pda, _) = config_pda();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(config_pda, false),
        ],
        data: UpdateMinFeeAmount {
            new_min_fee_amount: new_min_fee_amount.to_le_bytes(),
        }
        .to_bytes(),
    }
}

/// Create an update provisioning fee instruction
pub fn update_provisioning_fee(
    authority: Pubkey,
    new_provisioning_fee_sol: u64,
    new_provisioning_fee_spl: u64,
) -> Instruction {
    let (config_pda, _) = config_pda();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(config_pda, false),
        ],
        data: UpdateProvisioningFee {
            new_provisioning_fee_sol: new_provisioning_fee_sol.to_le_bytes(),
            new_provisioning_fee_spl: new_provisioning_fee_spl.to_le_bytes(),
        }
        .to_bytes(),
    }
}

/// Create an update discounted fee rate instruction
pub fn update_discounted_fee_rate(authority: Pubkey, new_discounted_fee_bps: u16) -> Instruction {
    let (config_pda, _) = config_pda();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(config_pda, false),
        ],
        data: UpdateDiscountedFeeRate {
            new_discounted_fee_bps: new_discounted_fee_bps.to_le_bytes(),
            _padding: [0; 6],
        }
        .to_bytes(),
    }
}

/// Create an update min fee amount sol instruction
pub fn update_min_fee_amount_sol(authority: Pubkey, new_min_fee_amount_sol: u64) -> Instruction {
    let (config_pda, _) = config_pda();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(config_pda, false),
        ],
        data: UpdateMinFeeAmountSol {
            new_min_fee_amount_sol: new_min_fee_amount_sol.to_le_bytes(),
        }
        .to_bytes(),
    }
}

/// Create an init_shard instruction
pub fn init_shard(payer: Pubkey, authority: Pubkey, index: u64) -> Instruction {
    let (config_pda, _) = config_pda();
    let (shard_pda, _) = fee_shard_pda(index);
    let (shard_sol_storage, _) = fee_shard_sol_storage_pda(shard_pda);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(payer, true),
            AccountMeta::new_readonly(authority, true),
            AccountMeta::new_readonly(config_pda, false),
            AccountMeta::new(shard_pda, false),
            AccountMeta::new(shard_sol_storage, false),
            AccountMeta::new_readonly(solana_program::system_program::ID, false),
        ],
        data: InitShard {
            index: index.to_le_bytes(),
        }
        .to_bytes(),
    }
}

/// Create a collect_from_shard instruction
pub fn collect_from_shard(
    authority: Pubkey,
    index: u64,
    treasury: Pubkey,
    mint: Option<Pubkey>,
    amount: u64,
    token_program: Option<Pubkey>,
) -> Instruction {
    let (config_pda, _) = config_pda();
    let (shard_pda, _) = fee_shard_pda(index);
    let is_sol = mint.is_none() || mint == Some(Pubkey::default());

    let mut accounts = vec![
        AccountMeta::new_readonly(authority, true),
        AccountMeta::new_readonly(config_pda, false),
        AccountMeta::new_readonly(shard_pda, false),
    ];

    if is_sol {
        let (shard_sol_storage, _) = fee_shard_sol_storage_pda(shard_pda);
        accounts.push(AccountMeta::new(shard_sol_storage, false));
        accounts.push(AccountMeta::new(treasury, false));
        accounts.push(AccountMeta::new_readonly(
            solana_program::system_program::ID,
            false,
        ));
    } else {
        let mint = mint.unwrap();
        let shard_tokens =
            spl_associated_token_account::get_associated_token_address(&shard_pda, &mint);
        let treasury_tokens =
            spl_associated_token_account::get_associated_token_address(&treasury, &mint);
        accounts.push(AccountMeta::new(shard_tokens, false));
        accounts.push(AccountMeta::new(treasury_tokens, false));
        accounts.push(AccountMeta::new_readonly(
            token_program.unwrap_or(spl_token::ID),
            false,
        ));
    }

    Instruction {
        program_id: crate::ID,
        accounts,
        data: CollectFromShard {
            index: index.to_le_bytes(),
            amount: amount.to_le_bytes(),
            is_sol: [if is_sol { 1 } else { 0 }],
            _padding: [0; 7],
        }
        .to_bytes(),
    }
}

/// Create an update_shard_config instruction
pub fn update_shard_config(authority: Pubkey, use_fee_shard: u8, shard_count: u8) -> Instruction {
    let (config_pda, _) = config_pda();
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new_readonly(authority, true),
            AccountMeta::new(config_pda, false),
        ],
        data: UpdateShardConfig {
            use_fee_shard,
            shard_count,
            _padding: [0; 6],
        }
        .to_bytes(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn spl_sweep_uses_token_mint_as_mint_account() {
        let payer = Pubkey::new_unique();
        let vault = Pubkey::new_unique();
        let seller = Pubkey::new_unique();
        let fee_destination = Pubkey::new_unique();
        let token_mint = Pubkey::new_unique();
        let vault_tokens = Pubkey::new_unique();
        let seller_tokens = Pubkey::new_unique();
        let fee_dest_tokens = Pubkey::new_unique();

        let ix = sweep(
            payer,
            vault,
            seller,
            fee_destination,
            token_mint,
            0,
            false,
            Some(vault_tokens),
            Some(seller_tokens),
            Some(fee_dest_tokens),
            Some(spl_token::ID),
        );

        assert_eq!(ix.accounts[6].pubkey, token_mint);
        assert_eq!(ix.accounts[7].pubkey, spl_token::ID);
    }
}