Browse Source

feat: ✨ Add CLI commands to create registry/world (#79)

Naman Anand 1 year ago
parent
commit
fe3a235459
2 changed files with 73 additions and 2 deletions
  1. 58 1
      cli/src/instructions.rs
  2. 15 1
      cli/src/lib.rs

+ 58 - 1
cli/src/instructions.rs

@@ -8,7 +8,7 @@ use anchor_client::solana_sdk::system_program;
 use anchor_client::Client;
 use anyhow::{anyhow, Result};
 use std::rc::Rc;
-use world::{accounts, instruction, World, ID};
+use world::{accounts, instruction, Registry, World, ID};
 
 fn setup_client(cfg_override: &ConfigOverride) -> Result<(Client<Rc<Keypair>>, Keypair)> {
     let cfg = Config::discover(cfg_override)?.expect("Not in workspace.");
@@ -34,6 +34,63 @@ fn parse_pubkey(input: &str, error_message: &str) -> Result<Pubkey> {
         .map_err(|_| anyhow!(error_message.to_string()))
 }
 
+pub fn create_registry(cfg_override: &ConfigOverride) -> Result<()> {
+    let (client, payer) = setup_client(cfg_override)?;
+    let program = client.program(ID)?;
+
+    let (registry_pda, _) = Pubkey::find_program_address(&[Registry::seed()], &ID);
+
+    let signature = program
+        .request()
+        .accounts(accounts::InitializeRegistry {
+            registry: registry_pda,
+            payer: payer.pubkey(),
+            system_program: system_program::ID,
+        })
+        .args(instruction::InitializeRegistry {})
+        .signer(&payer)
+        .send()?;
+
+    println!(
+        "New registry {} created with signature {}",
+        registry_pda, signature
+    );
+
+    Ok(())
+}
+
+pub fn create_world(cfg_override: &ConfigOverride) -> Result<()> {
+    let (client, payer) = setup_client(cfg_override)?;
+    let program = client.program(ID)?;
+
+    let (registry_pda, _) = Pubkey::find_program_address(&[Registry::seed()], &ID);
+
+    let registry_account: Registry = program.account(registry_pda)?;
+    let world_id = registry_account.worlds;
+
+    let (world_pda, _) =
+        Pubkey::find_program_address(&[World::seed(), &world_id.to_be_bytes()], &ID);
+
+    let signature = program
+        .request()
+        .accounts(accounts::InitializeNewWorld {
+            payer: payer.pubkey(),
+            world: world_pda,
+            registry: registry_pda,
+            system_program: system_program::ID,
+        })
+        .args(instruction::InitializeNewWorld {})
+        .signer(&payer)
+        .send()?;
+
+    println!(
+        "New world created {} with signature {}",
+        world_pda, signature
+    );
+
+    Ok(())
+}
+
 pub fn authorize(
     cfg_override: &ConfigOverride,
     world: String,

+ 15 - 1
cli/src/lib.rs

@@ -6,7 +6,9 @@ mod templates;
 mod workspace;
 
 use crate::component::new_component;
-use crate::instructions::{approve_system, authorize, deauthorize, remove_system};
+use crate::instructions::{
+    approve_system, authorize, create_registry, create_world, deauthorize, remove_system,
+};
 use crate::rust_template::{create_component, create_system};
 use crate::system::new_system;
 use anchor_cli::config;
@@ -37,6 +39,10 @@ pub enum BoltCommand {
     // Include all existing commands from anchor_cli::Command
     #[clap(flatten)]
     Anchor(anchor_cli::Command),
+    #[clap(about = "Add a new registry instance")]
+    Registry(RegistryCommand),
+    #[clap(about = "Add a new world instance")]
+    World(WorldCommand),
     #[clap(about = "Add a new authority for a world instance")]
     Authorize(AuthorizeCommand),
     #[clap(about = "Remove an authority from a world instance")]
@@ -63,6 +69,12 @@ pub struct SystemCommand {
     pub name: String,
 }
 
+#[derive(Debug, Parser)]
+pub struct RegistryCommand {}
+
+#[derive(Debug, Parser)]
+pub struct WorldCommand {}
+
 #[derive(Debug, Parser)]
 pub struct AuthorizeCommand {
     pub world: String,
@@ -165,6 +177,8 @@ pub fn entry(opts: Opts) -> Result<()> {
         },
         BoltCommand::Component(command) => new_component(&opts.cfg_override, command.name),
         BoltCommand::System(command) => new_system(&opts.cfg_override, command.name),
+        BoltCommand::Registry(_command) => create_registry(&opts.cfg_override),
+        BoltCommand::World(_command) => create_world(&opts.cfg_override),
         BoltCommand::Authorize(command) => {
             authorize(&opts.cfg_override, command.world, command.new_authority)
         }