Skip to content

Commit

Permalink
feat: ✨ Add CLI commands to create registry/world (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamnamananand996 authored Oct 18, 2024
1 parent 2d08db1 commit fe3a235
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
59 changes: 58 additions & 1 deletion cli/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")]
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit fe3a235

Please sign in to comment.