Browse Source

:bug: Fixing extra accounts lifetime (#201)

Danilo Guanabara 1 month ago
parent
commit
981cfbfabc

+ 3 - 2
Anchor.toml

@@ -11,12 +11,14 @@ world = "WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n"
 bolt-component = "CmP2djJgABZ4cRokm4ndxuq6LerqpNHLBsaUv2XKEJua"
 bolt-system = "7X4EFsDJ5aYTcEjKzJ94rD8FRKgQeXC89fkpeTS4KaqP"
 component-small = "9yBADAhoTWCkNRB6hbfpwUgPpxyJiF9uEiWVPR6k7A4y"
+escrow-funding = "4Um2d8SvyfWyLLtfu2iJMFhM77DdjjyQusEy7K3VhPkd"
 position = "Fn1JzzEdyb55fsyduWS94mYHizGhJZuhvjX6DVvrmGbQ"
 system-apply-velocity = "6LHhFVwif6N9Po3jHtSmMVtPjF6zRfL3xMosSzcrQAS8"
 system-fly = "HT2YawJjkNmqWcLNfPAMvNsLdWwPvvvbKA5bpMw4eUpq"
 system-simple-movement = "FSa6qoJXFBR3a7ThQkTAMrC15p6NkchPEjBdd4n6dXxA"
 velocity = "CbHEFbSQdRN4Wnoby9r16umnJ1zWbULBHg4yqzGQonU1"
 with-1-component = "BsVKJF2H9GN1P9WrexdgEY4ztiweKvfQo6ydLWUEw6n7"
+with-10-components = "C69UYWaXBQXUbhHQGtG8pB7DHSgh2z5Sm9ifyAnM1kkt"
 with-2-components = "X5wTvz1i6ocNXzfrEB8JmhFCniojUZxqk3TXDq98fZX"
 with-3-components = "9R7rvEwCuZ6iow1Ch3sdUQKib4LBvftyBmyvSnPaAZkG"
 with-4-components = "2w9pkZoCfEciHLLDhG3zrZRprcYH7nojhyBQMnD3PtUU"
@@ -25,7 +27,6 @@ with-6-components = "3ndvNAg4moKeLhuWQtDmcN43PuvvGsigQWRBPthfWEN3"
 with-7-components = "4ESiD77Gjjfuywhw8NBnryHezXtwDSA27ustL29JdX7i"
 with-8-components = "EbTAEnrVV4f8W7Fd4TxW3jLjfpyhr74wQf7rSHRQ8u78"
 with-9-components = "GKdPXW7pGhFNRdMPHWNsrmqc7haXQk4VFCAyZKsrgYQG"
-with-10-components = "C69UYWaXBQXUbhHQGtG8pB7DHSgh2z5Sm9ifyAnM1kkt"
 
 [registry]
 url = "https://api.apr.dev"
@@ -35,7 +36,7 @@ cluster = "localnet"
 wallet = "./tests/fixtures/provider.json"
 
 [workspace]
-members = ["crates/programs/bolt-component", "crates/programs/bolt-system", "crates/programs/world", "examples/component-position", "examples/component-velocity", "examples/system-apply-velocity", "examples/system-fly", "examples/system-simple-movement", "examples/component-small", "examples/system-with-1-component", "examples/system-with-2-components", "examples/system-with-3-components", "examples/system-with-4-components", "examples/system-with-5-components", "examples/system-with-6-components", "examples/system-with-7-components", "examples/system-with-8-components", "examples/system-with-9-components", "examples/system-with-10-components"]
+members = ["crates/programs/bolt-component", "crates/programs/bolt-system", "crates/programs/world", "examples/component-position", "examples/component-velocity", "examples/system-apply-velocity", "examples/system-fly", "examples/system-simple-movement", "examples/component-small", "examples/system-with-1-component", "examples/system-with-2-components", "examples/system-with-3-components", "examples/system-with-4-components", "examples/system-with-5-components", "examples/system-with-6-components", "examples/system-with-7-components", "examples/system-with-8-components", "examples/system-with-9-components", "examples/system-with-10-components", "examples/escrow-funding"]
 
 [scripts]
 test = "tests/script.sh"

+ 9 - 0
Cargo.lock

@@ -1842,6 +1842,15 @@ dependencies = [
  "windows-sys 0.59.0",
 ]
 
+[[package]]
+name = "escrow-funding"
+version = "0.2.4"
+dependencies = [
+ "bolt-lang",
+ "component-small",
+ "serde",
+]
+
 [[package]]
 name = "event-listener"
 version = "2.5.3"

+ 10 - 6
crates/bolt-lang/attribute/system/src/lib.rs

@@ -188,7 +188,7 @@ impl VisitMut for SystemTransform {
 impl SystemTransform {
     fn add_variadic_execute_function(content: &mut Vec<syn::Item>) {
         content.push(syn::parse2(quote! {
-            pub fn bolt_execute<'info>(ctx: Context<'_, '_, 'info, 'info, VariadicBoltComponents<'info>>, args: Vec<u8>) -> Result<Vec<Vec<u8>>> {
+            pub fn bolt_execute<'a, 'b, 'info>(ctx: Context<'a, 'b, 'info, 'info, VariadicBoltComponents<'info>>, args: Vec<u8>) -> Result<Vec<Vec<u8>>> {
                 let mut components = Components::try_from(&ctx)?;
                 let bumps = ComponentsBumps {};
                 let context = Context::new(ctx.program_id, &mut components, ctx.remaining_accounts, bumps);
@@ -317,11 +317,15 @@ impl VisitMut for Extractor {
                     let last_segment = type_path.path.segments.last().unwrap();
                     if last_segment.ident == "Context" {
                         if let PathArguments::AngleBracketed(args) = &last_segment.arguments {
-                            if let Some(syn::GenericArgument::Type(syn::Type::Path(type_path))) =
-                                args.args.first()
-                            {
-                                let ident = &type_path.path.segments.first().unwrap().ident;
-                                self.context_struct_name = Some(ident.to_string());
+                            // Find the first generic argument that is a Type::Path (e.g., Components)
+                            for ga in args.args.iter() {
+                                if let syn::GenericArgument::Type(syn::Type::Path(type_path)) = ga {
+                                    if let Some(first_seg) = type_path.path.segments.first() {
+                                        self.context_struct_name =
+                                            Some(first_seg.ident.to_string());
+                                        break;
+                                    }
+                                }
                             }
                         }
                     }

+ 26 - 0
examples/escrow-funding/Cargo.toml

@@ -0,0 +1,26 @@
+[package]
+name = "escrow-funding"
+version = "0.2.4"
+description = "Created with Bolt"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "escrow_funding"
+
+[features]
+no-entrypoint = []
+no-idl = []
+no-log-ix-name = []
+cpi = ["no-entrypoint"]
+default = []
+idl-build = ["bolt-lang/idl-build"]
+anchor-debug = ["bolt-lang/anchor-debug"]
+custom-heap = []
+custom-panic = []
+
+
+[dependencies]
+bolt-lang.workspace = true
+serde = { version = "1.0", features = ["derive"] }
+component-small.workspace = true

+ 2 - 0
examples/escrow-funding/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 44 - 0
examples/escrow-funding/src/lib.rs

@@ -0,0 +1,44 @@
+use bolt_lang::anchor_lang::*;
+use bolt_lang::*;
+use component_small::Small;
+
+declare_id!("4Um2d8SvyfWyLLtfu2iJMFhM77DdjjyQusEy7K3VhPkd");
+
+#[system]
+pub mod escrow_funding {
+    pub fn execute<'info>(
+        ctx: Context<'_, '_, '_, 'info, Components<'info>>,
+        args: Args,
+    ) -> Result<Components<'info>> {
+        let receiver = ctx.accounts.receiver.to_account_info();
+        let sender = ctx.sender()?.clone();
+        let system_program = ctx.system_program()?.clone();
+
+        let cpi_accounts = system_program::Transfer {
+            from: sender,
+            to: receiver,
+        };
+        let cpi_ctx = CpiContext::new(system_program, cpi_accounts);
+        system_program::transfer(cpi_ctx, args.amount)?;
+
+        Ok(ctx.accounts)
+    }
+
+    #[system_input]
+    pub struct Components {
+        pub receiver: Small,
+    }
+
+    #[arguments]
+    pub struct Args {
+        amount: u64,
+    }
+
+    #[extra_accounts]
+    pub struct ExtraAccounts {
+        #[account(mut)]
+        pub sender: AccountInfo,
+        #[account(address = bolt_lang::solana_program::system_program::id())]
+        pub system_program: AccountInfo,
+    }
+}