Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading