Skip to content

Commit

Permalink
✨ Component Authority owned by the world program
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco committed Feb 6, 2024
1 parent ef647d0 commit 48fed65
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 31 deletions.
17 changes: 6 additions & 11 deletions crates/bolt-lang/attribute/bolt-program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,10 @@ fn generate_initialize(component_type: &Type) -> (TokenStream2, TokenStream2) {
(
quote! {
#[automatically_derived]
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
ctx.accounts.data.set_inner(<#component_type>::default());
if let Some(authority) = &ctx.accounts.authority {
if authority.key != ctx.accounts.payer.key {
panic!("The authority does not match the payer.");
}
ctx.accounts.data.bolt_metadata.authority = *authority.key;
}
Ok(())
pub fn initialize(ctx: Context<Initialize>) -> Result<Vec<u8>> {
let mut serialized_data = Vec::new();
<#component_type>::default().serialize(&mut serialized_data).expect("Failed to serialize");
Ok(serialized_data)
}
},
quote! {
Expand All @@ -127,8 +122,8 @@ fn generate_initialize(component_type: &Type) -> (TokenStream2, TokenStream2) {
pub struct Initialize<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(init_if_needed, payer = payer, space = <#component_type>::size(), seeds = [<#component_type>::seed(), entity.key().as_ref()], bump)]
pub data: Account<'info, #component_type>,
#[account(init_if_needed, owner=World::id(), payer = payer, space = <#component_type>::size(), seeds = [<#component_type>::seed(), entity.key().as_ref()], bump)]
pub data: AccountInfo<'info>,
#[account()]
pub entity: Account<'info, Entity>,
#[account()]
Expand Down
2 changes: 0 additions & 2 deletions crates/bolt-lang/attribute/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ struct SystemTransform;
/// ```ignore
/// #[system]
/// pub mod system_fly {
/// use super::*;
///
/// pub fn execute(ctx: Context<Component>, _args: Vec<u8>) -> Result<Position> {
/// let pos = Position {
/// x: ctx.accounts.position.x,
Expand Down
1 change: 1 addition & 0 deletions crates/bolt-lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use bolt_attribute_bolt_system::system;
pub use bolt_system;
pub use world;
pub use world::Entity;
pub use world::program::World;

pub use serde;
pub use serde::{Deserialize as BoltDeserialize, Serialize as BoltSerialize};
Expand Down
2 changes: 1 addition & 1 deletion examples/component-position/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ declare_id!("Fn1JzzEdyb55fsyduWS94mYHizGhJZuhvjX6DVvrmGbQ");
pub struct Position {
pub x: i64,
pub y: i64,
pub z: i64,
pub z: i64
}
2 changes: 0 additions & 2 deletions examples/system-apply-velocity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ pub mod system_apply_velocity {

#[derive(Accounts)]
pub struct Component<'info> {
#[account()]
pub velocity: Account<'info, Velocity>,
#[account()]
pub position: Account<'info, Position>,
}
4 changes: 2 additions & 2 deletions examples/system-simple-movement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ declare_id!("FSa6qoJXFBR3a7ThQkTAMrC15p6NkchPEjBdd4n6dXxA");

#[system]
pub mod system_simple_movement {

pub fn execute(ctx: Context<Component>, args_p: Vec<u8>) -> Result<Position> {
let args = parse_args::<Args>(&args_p);

let mut position = Position::from_account_info(&ctx.accounts.position)?;

// Compute the new position based on the direction
Expand All @@ -26,7 +27,6 @@ pub mod system_simple_movement {
// Define the Account to parse from the component
#[derive(Accounts)]
pub struct Component<'info> {
/// CHECK: check that the component is the expected account
pub position: AccountInfo<'info>,
}

Expand Down
21 changes: 10 additions & 11 deletions programs/bolt-component/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use anchor_lang::prelude::*;
use std::str::FromStr;

declare_id!("CmP2djJgABZ4cRokm4ndxuq6LerqpNHLBsaUv2XKEJua");

#[program]
pub mod bolt_component {
use super::*;

pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
// ctx.accounts.data.to_account_info().assign(::ID); TODO: Test delegate to the world program
if let Some(authority) = &ctx.accounts.authority {
if authority.key != ctx.accounts.payer.key {
panic!("Authority mismatch");
}
ctx.accounts.data.bolt_metadata.authority = *authority.key;
}
Ok(())
pub fn initialize(_ctx: Context<Initialize>) -> Result<Vec<u8>> {
let mut component = Component::default();
component.bolt_metadata.authority = Pubkey::from_str("WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n").unwrap();
let mut serialized_data = Vec::new();
anchor_lang::AccountSerialize::try_serialize(&component, &mut serialized_data).expect("Failed to serialize");
//component.serialize(&mut serialized_data).expect("Failed to serialize");
Ok(serialized_data)
}

pub fn apply(_ctx: Context<Apply>, _args: Vec<u8>) -> Result<()> {
Expand Down Expand Up @@ -56,8 +55,8 @@ pub mod bolt_component {
pub struct Initialize<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(init_if_needed, payer = payer, space = Component::size(), seeds = [Component::seed(), entity.key().as_ref()], bump)]
pub data: Account<'info, Component>,
#[account(init_if_needed, owner = Pubkey::from_str("WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n").unwrap(), payer = payer, space = Component::size(), seeds = [Component::seed(), entity.key().as_ref()], bump)]
pub data: AccountInfo<'info>,
#[account()]
/// CHECK: A generic entity account
pub entity: AccountInfo<'info>,
Expand Down
1 change: 0 additions & 1 deletion programs/bolt-system/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anchor_lang::prelude::borsh::{BorshDeserialize, BorshSerialize};
use anchor_lang::prelude::*;
//use bolt_helpers_system_template::*;

declare_id!("7X4EFsDJ5aYTcEjKzJ94rD8FRKgQeXC89fkpeTS4KaqP");

Expand Down
4 changes: 3 additions & 1 deletion programs/world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub mod world {
}

pub fn initialize_component(ctx: Context<InitializeComponent>) -> Result<()> {
bolt_component::cpi::initialize(ctx.accounts.build())?;
let data = bolt_component::cpi::initialize(ctx.accounts.build())?;
let component_data = &mut *ctx.accounts.data.data.borrow_mut();
component_data.copy_from_slice(&data.get());
Ok(())
}

Expand Down

0 comments on commit 48fed65

Please sign in to comment.