Skip to content

Commit

Permalink
Merge branch 'main' into release/v0.1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco authored Oct 4, 2024
2 parents dbae062 + 529927b commit 6724ad6
Show file tree
Hide file tree
Showing 17 changed files with 2,186 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-bolt-crates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
- name: Cache rust
uses: Swatinem/rust-cache@v2
- name: Run fmt
run: cargo fmt -- --check
run: cargo fmt -- --check --verbose
- name: Run clippy
run: cargo clippy -- --deny=warnings

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-bolt-sdk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
- name: Cache rust
uses: Swatinem/rust-cache@v2
- name: Run fmt
run: cargo fmt -- --check
run: cargo fmt -- --check --verbose
- name: Run clippy
run: cargo clippy -- --deny=warnings

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

- uses: actions/setup-node@v4
with:
node-version: 22
node-version: 21

- name: install essentials
run: |
Expand Down
30 changes: 13 additions & 17 deletions Anchor.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[toolchain]

[features]
seeds = true
resolution = true
skip-lint = false

[programs.devnet]
Expand All @@ -20,38 +20,34 @@ velocity = "CbHEFbSQdRN4Wnoby9r16umnJ1zWbULBHg4yqzGQonU1"
url = "https://api.apr.dev"

[provider]
cluster = "localnet"
cluster = "Localnet"
wallet = "./tests/fixtures/provider.json"

[workspace]
members = ["programs/bolt-component", "programs/bolt-system", "programs/world", "examples/component-position", "examples/component-velocity", "examples/system-apply-velocity", "examples/system-fly", "examples/system-simple-movement"]

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/bolt.ts"

[test]
startup_wait = 5000
shutdown_wait = 2000
upgradeable = false

[workspace]
members = [
"programs/bolt-component",
"programs/bolt-system",
"programs/world",
"examples/component-position",
"examples/component-velocity",
"examples/system-apply-velocity",
"examples/system-fly",
"examples/system-simple-movement",
]

[[test.genesis]]
address = "DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh"
program = "tests/fixtures/delegation.so"
upgradeable = false

[test.validator]
bind_address = "0.0.0.0"
ledger = ".anchor/test-ledger"
rpc_port = 8899

[[test.validator.account]]
address = "EEmsg7GbxEAw5f9hGfZRmJRJ27HK8KeGDp7ViW9X2mYa"
filename = "tests/fixtures/commit_record.json"

[[test.validator.account]]
address = "7nQvHcfEqtFmY2q6hiQbidu8BCNdqegnEFfH7HkByFn5"
filename = "tests/fixtures/committed_state.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/bolt.ts"
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ anyhow = { workspace = true }
serde_json = { workspace = true }
heck = { workspace = true }
clap = { workspace = true }
syn = { workspace = true, features = ["full", "extra-traits"] }
syn = { workspace = true, features = ["full", "extra-traits"] }
world = { workspace = true }
167 changes: 167 additions & 0 deletions cli/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
use anchor_cli::config::{Config, ConfigOverride};
use anchor_client::solana_client::rpc_config::RpcSendTransactionConfig;
use anchor_client::solana_sdk::commitment_config::CommitmentConfig;
use anchor_client::solana_sdk::pubkey::Pubkey;
use anchor_client::solana_sdk::signature::{read_keypair_file, Keypair};
use anchor_client::solana_sdk::signer::Signer;
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};

fn setup_client(cfg_override: &ConfigOverride) -> Result<(Client<Rc<Keypair>>, Keypair)> {
let cfg = Config::discover(cfg_override)?.expect("Not in workspace.");
let wallet_path = cfg.provider.wallet.clone();

let payer = read_keypair_file(wallet_path.to_string())
.map_err(|e| anyhow!("Failed to read keypair file: {}", e))?;

let payer_for_client =
Keypair::from_bytes(&payer.to_bytes()).expect("Failed to create Keypair from bytes");

let client = Client::new_with_options(
cfg.provider.cluster.clone(),
Rc::new(payer_for_client),
CommitmentConfig::confirmed(),
);
Ok((client, payer))
}

fn parse_pubkey(input: &str, error_message: &str) -> Result<Pubkey> {
input
.parse::<Pubkey>()
.map_err(|_| anyhow!(error_message.to_string()))
}

pub fn authorize(
cfg_override: &ConfigOverride,
world: String,
new_authority: String,
) -> Result<()> {
let world_pubkey = parse_pubkey(&world, "Invalid world public key")?;
let new_authority_pubkey = parse_pubkey(&new_authority, "Invalid new authority public key")?;

let (client, payer) = setup_client(cfg_override)?;
let program = client.program(ID)?;

let world_account = program.account::<World>(world_pubkey)?;
let world_id = world_account.id;
let signature = program
.request()
.accounts(accounts::AddAuthority {
authority: payer.pubkey(),
new_authority: new_authority_pubkey,
system_program: system_program::ID,
world: world_pubkey,
})
.args(instruction::AddAuthority { world_id })
.signer(&payer)
.send()?;

println!(
"Authority {} added to world {} with signature {}",
new_authority, world, signature
);

Ok(())
}

pub fn deauthorize(
cfg_override: &ConfigOverride,
world: String,
authority_to_delete: String,
) -> Result<()> {
let world_pubkey = parse_pubkey(&world, "Invalid world public key")?;
let authority_to_delete_pubkey =
parse_pubkey(&authority_to_delete, "Invalid authority public key")?;

let (client, payer) = setup_client(cfg_override)?;
let program = client.program(ID)?;

let world_account = program.account::<World>(world_pubkey)?;
let world_id = world_account.id;
let signature = program
.request()
.accounts(accounts::RemoveAuthority {
authority: payer.pubkey(),
authority_to_delete: authority_to_delete_pubkey,
system_program: system_program::ID,
world: world_pubkey,
})
.args(instruction::RemoveAuthority { world_id })
.signer(&payer)
.send_with_spinner_and_config(RpcSendTransactionConfig {
skip_preflight: true,
..RpcSendTransactionConfig::default()
})?;

println!(
"Authority {} removed from world {} with signature {}",
authority_to_delete, world, signature
);

Ok(())
}

pub fn approve_system(
cfg_override: &ConfigOverride,
world: String,
system_to_approve: String,
) -> Result<()> {
let world_pubkey = parse_pubkey(&world, "Invalid world public key")?;
let system_to_approve_pubkey = parse_pubkey(&system_to_approve, "Invalid system public key")?;

let (client, payer) = setup_client(cfg_override)?;
let program = client.program(ID)?;

let signature = program
.request()
.accounts(accounts::ApproveSystem {
authority: payer.pubkey(),
system: system_to_approve_pubkey,
world: world_pubkey,
system_program: system_program::ID,
})
.args(instruction::ApproveSystem {})
.signer(&payer)
.send()?;

println!(
"System {} approved for world {} with signature {}",
system_to_approve, world, signature
);

Ok(())
}

pub fn remove_system(
cfg_override: &ConfigOverride,
world: String,
system_to_remove: String,
) -> Result<()> {
let world_pubkey = parse_pubkey(&world, "Invalid world public key")?;
let system_to_remove_pubkey = parse_pubkey(&system_to_remove, "Invalid system public key")?;

let (client, payer) = setup_client(cfg_override)?;
let program = client.program(ID)?;

let signature = program
.request()
.accounts(accounts::RemoveSystem {
authority: payer.pubkey(),
system: system_to_remove_pubkey,
world: world_pubkey,
system_program: system_program::ID,
})
.args(instruction::RemoveSystem {})
.signer(&payer)
.send()?;

println!(
"System {} removed from world {} with signature {}",
system_to_remove, world, signature
);

Ok(())
}
59 changes: 51 additions & 8 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod component;
mod instructions;
mod rust_template;
mod system;
mod templates;
mod workspace;

use crate::component::new_component;
use crate::instructions::{approve_system, authorize, deauthorize, remove_system};
use crate::rust_template::{create_component, create_system};
use crate::system::new_system;
use anchor_cli::config;
Expand All @@ -23,12 +25,9 @@ use std::io::Write;
use std::io::{self, BufRead};
use std::path::{Path, PathBuf};
use std::process::Stdio;

use std::string::ToString;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const ANCHOR_VERSION: &str = anchor_cli::VERSION;

pub const WORLD_PROGRAM: &str = "WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n";

#[derive(Subcommand)]
pub enum BoltCommand {
#[clap(about = "Create a new component")]
Expand All @@ -38,6 +37,14 @@ pub enum BoltCommand {
// Include all existing commands from anchor_cli::Command
#[clap(flatten)]
Anchor(anchor_cli::Command),
#[clap(about = "Add a new authority for a world instance")]
Authorize(AuthorizeCommand),
#[clap(about = "Remove an authority from a world instance")]
Deauthorize(DeauthorizeCommand),
#[clap(about = "Approve a system for a world instance")]
ApproveSystem(ApproveSystemCommand),
#[clap(about = "Remove a system from a world instance")]
RemoveSystem(RemoveSystemCommand),
}

#[derive(Debug, Parser)]
Expand All @@ -56,6 +63,30 @@ pub struct SystemCommand {
pub name: String,
}

#[derive(Debug, Parser)]
pub struct AuthorizeCommand {
pub world: String,
pub new_authority: String,
}

#[derive(Debug, Parser)]
pub struct DeauthorizeCommand {
pub world: String,
pub authority_to_remove: String,
}

#[derive(Debug, Parser)]
pub struct ApproveSystemCommand {
pub world: String,
pub system_to_approve: String,
}

#[derive(Debug, Parser)]
pub struct RemoveSystemCommand {
pub world: String,
pub system_to_remove: String,
}

#[derive(Parser)]
#[clap(version = VERSION)]
pub struct Opts {
Expand Down Expand Up @@ -134,11 +165,23 @@ 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::Authorize(command) => {
authorize(&opts.cfg_override, command.world, command.new_authority)
}
BoltCommand::Deauthorize(command) => deauthorize(
&opts.cfg_override,
command.world,
command.authority_to_remove,
),
BoltCommand::ApproveSystem(command) => {
approve_system(&opts.cfg_override, command.world, command.system_to_approve)
}
BoltCommand::RemoveSystem(command) => {
remove_system(&opts.cfg_override, command.world, command.system_to_remove)
}
}
}

// Bolt Init

#[allow(clippy::too_many_arguments)]
fn init(
cfg_override: &ConfigOverride,
Expand Down Expand Up @@ -268,7 +311,7 @@ fn init(
shutdown_wait: 2000,
validator: Some(validator),
genesis: Some(vec![GenesisEntry {
address: WORLD_PROGRAM.to_owned(),
address: world::id().to_string(),
program: "tests/fixtures/world.so".to_owned(),
upgradeable: Some(false),
}]),
Expand Down Expand Up @@ -345,7 +388,7 @@ fn init(
.arg("dump")
.arg("-u")
.arg("d")
.arg(WORLD_PROGRAM)
.arg(world::id().to_string())
.arg("tests/fixtures/world.so")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
Expand Down
2 changes: 1 addition & 1 deletion clients/bolt-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"main": "lib/index.js",
"scripts": {
"clean": "rimraf lib",
"build": "npm run clean && tsc && npm run lint:fix",
"build": "cp ../../target/types/world.ts src/generated/types/world.ts && cp ../../target/idl/world.json src/generated/idl/world.json && npm run clean && tsc && npm run lint:fix",
"build:docs": "typedoc && typedoc --plugin typedoc-plugin-markdown --out docs-mk",
"dev": "tsc --watch",
"start": "tsc",
Expand Down
Loading

0 comments on commit 6724ad6

Please sign in to comment.