Skip to content

Commit

Permalink
✨ Add component_id macro for external account
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco committed Feb 13, 2024
1 parent b69776f commit 1e984b4
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 22 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/bolt-lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bolt-attribute-bolt-component = { path = "./attribute/component", version = "0.0
bolt-attribute-bolt-system = { path = "./attribute/system", version = "0.0.1" }
bolt-attribute-bolt-system-input = { path = "./attribute/system-input", version = "0.0.1" }
bolt-attribute-bolt-component-deserialize = { path = "./attribute/component-deserialize", version = "0.0.1" }
bolt-attribute-bolt-component-id = { path = "./attribute/component-id", version = "0.0.1" }

# Bolt Programs
world = { path = "../../programs/world", features = ["cpi"], version = "0.0.1"}
Expand Down
14 changes: 14 additions & 0 deletions crates/bolt-lang/attribute/component-id/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "bolt-attribute-bolt-component-id"
version = "0.0.1"
edition = "2021"
description = "Bolt attribute-bolt-component-id"
license = "MIT"

[lib]
proc-macro = true

[dependencies]
syn = { version = "1.0", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"
13 changes: 13 additions & 0 deletions crates/bolt-lang/attribute/component-id/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{DeriveInput, parse_macro_input};

#[proc_macro_attribute]
pub fn component_id(_attr: TokenStream, item: TokenStream) -> TokenStream {
println!("run test");
let input = parse_macro_input!(item as DeriveInput);
let expanded = quote! {
#input
};
TokenStream::from(expanded)
}
35 changes: 34 additions & 1 deletion crates/bolt-lang/attribute/system-input/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro::TokenStream;
use quote::{quote};
use syn::{parse_macro_input, ItemStruct, Fields};
use syn::{parse_macro_input, ItemStruct, Fields, Meta, MetaNameValue, Lit};

/// This macro attribute is used to define a BOLT system input.
///
Expand Down Expand Up @@ -33,6 +33,38 @@ pub fn system_input(_attr: TokenStream, item: TokenStream) -> TokenStream {
};
let name = &input.ident;

// Impls Owner for each account and
let owners_impls = fields.iter().filter_map(|field| {
field.attrs.iter().find_map(|attr| {
if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
if meta_list.path.is_ident("component_id") {
for nested_meta in meta_list.nested.iter() {
if let syn::NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, lit: Lit::Str(lit_str), .. })) = nested_meta {
if path.is_ident("address") {
let address = lit_str.value();
let field_type = &field.ty;
return Some(quote! {
impl Owner for #field_type {

fn owner() -> Pubkey {
Pubkey::from_str(#address).unwrap()
}
}
impl AccountSerialize for #field_type {
fn try_serialize<W: Write>(&self, _writer: &mut W) -> Result<()> {
Ok(())
}
}
});
}
}
}
}
}
None
})
});

// Transform fields for the struct definition
let transformed_fields = fields.iter().map(|f| {
let field_name = &f.ident;
Expand Down Expand Up @@ -79,6 +111,7 @@ pub fn system_input(_attr: TokenStream, item: TokenStream) -> TokenStream {
let output = quote! {
#output_struct
#output_impl
#(#owners_impls)*
};

TokenStream::from(output)
Expand Down
7 changes: 0 additions & 7 deletions crates/bolt-lang/attribute/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ struct Extractor {
/// ```
#[proc_macro_attribute]
pub fn system(attr: TokenStream, item: TokenStream) -> TokenStream {

println!("xys: ");
let mut ast = parse_macro_input!(item as ItemMod);
let _attr = parse_macro_input!(attr as syn::AttributeArgs);

Expand All @@ -54,8 +52,6 @@ pub fn system(attr: TokenStream, item: TokenStream) -> TokenStream {
};
transform.visit_item_mod_mut(&mut ast);

println!("after transform: ");

// Add `#[program]` macro and try_to_vec implementation
let expanded = quote! {
#[program]
Expand All @@ -72,9 +68,7 @@ pub fn system(attr: TokenStream, item: TokenStream) -> TokenStream {
impl VisitMut for SystemTransform {
// Modify the return instruction to return Result<Vec<u8>>
fn visit_expr_mut(&mut self, expr: &mut Expr) {
println!("visit_expr: ");
if let Some(inner_variable) = Self::extract_inner_ok_expression(expr) {
println!("inner_variable: ");
let new_return_expr: Expr = match inner_variable {
Expr::Tuple(tuple_expr) => {
let tuple_elements = tuple_expr.elems.iter().map(|elem| {
Expand All @@ -98,7 +92,6 @@ impl VisitMut for SystemTransform {
// Modify the return type to Result<Vec<u8>> if necessary
if let ReturnType::Type(_, type_box) = &item_fn.sig.output {
if let Type::Path(type_path) = &**type_box {
println!("ret_values: {}", self.return_values);
if self.return_values > 1 {
item_fn.sig.ident = Ident::new(
format!("execute_{}", self.return_values).as_str(),
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 @@ -5,6 +5,7 @@ pub use bolt_attribute_bolt_program::bolt_program;
pub use bolt_attribute_bolt_component_deserialize::component_deserialize;
pub use bolt_attribute_bolt_system::system;
pub use bolt_attribute_bolt_system_input::system_input;
pub use bolt_attribute_bolt_component_id::component_id;

pub use bolt_system;
pub use world;
Expand Down
1 change: 1 addition & 0 deletions examples/system-fly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare_id!("HT2YawJjkNmqWcLNfPAMvNsLdWwPvvvbKA5bpMw4eUpq");

#[system]
pub mod system_fly {

pub fn execute(ctx: Context<Components>, _args: Vec<u8>) -> Result<Components> {
let pos = &mut ctx.accounts.position;
pos.z += 1;
Expand Down
16 changes: 2 additions & 14 deletions examples/system-simple-movement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub mod system_simple_movement {
}

#[system_input]
pub struct Components<'info> {
//#[component_id("Fn1JzzEdyb55fsyduWS94mYHizGhJZuhvjX6DVvrmGbQ")]
pub struct Components {
#[component_id(address = "Fn1JzzEdyb55fsyduWS94mYHizGhJZuhvjX6DVvrmGbQ")]
pub position: Position,
}

Expand All @@ -37,18 +37,6 @@ pub mod system_simple_movement {
pub z: i64,
}

impl AccountSerialize for Position {
fn try_serialize<W: Write>(&self, _writer: &mut W) -> Result<()> {
Ok(())
}
}

impl Owner for Position {
fn owner() -> Pubkey{
Pubkey::from_str("Fn1JzzEdyb55fsyduWS94mYHizGhJZuhvjX6DVvrmGbQ").unwrap()
}
}

// Define the structs to deserialize the arguments
#[derive(BoltSerialize, BoltDeserialize)]
struct Args {
Expand Down

0 comments on commit 1e984b4

Please sign in to comment.