tensor_eigen/commands/whitelist/
create.rs1use super::*;
2
3use tensor_whitelist::{
4 instructions::{CreateWhitelistV2, CreateWhitelistV2InstructionArgs},
5 types::Condition,
6};
7use uuid::Uuid;
8
9pub struct CreateWhitelistV2Params {
10 pub keypair_path: Option<PathBuf>,
11 pub rpc_url: Option<String>,
12 pub whitelist_config_path: PathBuf,
13 pub namespace_path: Option<PathBuf>,
14}
15
16#[serde_as]
17#[derive(Debug, Serialize, Deserialize)]
18struct CreateWhitelistV2Config {
19 uuid: Option<[u8; 32]>,
20 #[serde_as(as = "Option<DisplayFromStr>")]
21 freeze_authority: Option<Pubkey>,
22 conditions: Vec<Condition>,
23}
24
25pub fn create_whitelist_v2(args: CreateWhitelistV2Params) -> Result<()> {
26 let config = CliConfig::new(args.keypair_path, args.rpc_url)?;
27
28 let payer = config.keypair.pubkey();
29 let owner = config.keypair.pubkey();
30
31 let namespace_signer = if let Some(namespace_path) = args.namespace_path {
32 read_keypair_file(namespace_path).map_err(|_| anyhow!("Unable to read keypair file"))?
33 } else {
34 Keypair::new()
35 };
36
37 let create_whitelist_config: CreateWhitelistV2Config =
38 serde_json::from_reader(std::fs::File::open(args.whitelist_config_path)?)?;
39
40 let uuid = create_whitelist_config.uuid.unwrap_or_else(|| {
41 let uuid1 = Uuid::new_v4();
42 let uuid2 = Uuid::new_v4();
43 let mut extended_uuid = [0u8; 32];
44 extended_uuid[..16].copy_from_slice(uuid1.as_bytes());
45 extended_uuid[16..].copy_from_slice(uuid2.as_bytes());
46 extended_uuid
47 });
48
49 let whitelist = WhitelistV2::find_pda(&namespace_signer.pubkey(), uuid).0;
50
51 let args = CreateWhitelistV2InstructionArgs {
52 uuid,
53 freeze_authority: create_whitelist_config.freeze_authority,
54 conditions: create_whitelist_config.conditions,
55 };
56
57 let ix = CreateWhitelistV2 {
58 payer,
59 update_authority: owner,
60 namespace: namespace_signer.pubkey(),
61 whitelist,
62 system_program: solana_sdk::system_program::id(),
63 }
64 .instruction(args);
65
66 let tx = transaction!(&[&config.keypair, &namespace_signer], &[ix], &config.client);
67
68 config.client.send_and_confirm_transaction(&tx)?;
69
70 println!("Whitelist created: {}", whitelist);
71 println!("Namespace: {}", namespace_signer.pubkey());
72
73 Ok(())
74}