armaniferrante 4 лет назад
Родитель
Сommit
1a0af7d8e5

+ 5 - 0
examples/references/.gitignore

@@ -0,0 +1,5 @@
+
+.anchor
+.DS_Store
+target
+**/*.rs.bk

+ 3 - 0
examples/references/Anchor.toml

@@ -0,0 +1,3 @@
+[provider]
+cluster = "localnet"
+wallet = "/home/armaniferrante/.config/solana/id.json"

+ 4 - 0
examples/references/Cargo.toml

@@ -0,0 +1,4 @@
+[workspace]
+members = [
+    "programs/*"
+]

+ 13 - 0
examples/references/migrations/deploy.js

@@ -0,0 +1,13 @@
+
+// Migrations are an early feature. Currently, they're nothing more than this
+// single deploy script that's invoked from the CLI, injecting a provider
+// configured from the workspace's Anchor.toml.
+
+const anchor = require("@project-serum/anchor");
+
+module.exports = async function (provider) {
+  // Configure client to use the provider.
+  anchor.setProvider(provider);
+
+  // Add your deploy script here.
+}

+ 18 - 0
examples/references/programs/references/Cargo.toml

@@ -0,0 +1,18 @@
+[package]
+name = "references"
+version = "0.1.0"
+description = "Created with Anchor"
+edition = "2018"
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "references"
+
+[features]
+no-entrypoint = []
+no-idl = []
+cpi = ["no-entrypoint"]
+default = []
+
+[dependencies]
+anchor-lang = { path = "../../../../lang" }

+ 2 - 0
examples/references/programs/references/Xargo.toml

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

+ 14 - 0
examples/references/programs/references/src/lib.rs

@@ -0,0 +1,14 @@
+use anchor_lang::prelude::*;
+
+#[program]
+pub mod references {
+    use super::*;
+    pub fn initialize(ctx: Context<Initialize>) -> ProgramResult {
+        Ok(())
+    }
+}
+
+#[derive(Accounts)]
+pub struct Initialize<'a, 'info> {
+    my_account: &'a AccountInfo<'info>,
+}

+ 14 - 0
examples/references/tests/references.js

@@ -0,0 +1,14 @@
+const anchor = require('@project-serum/anchor');
+
+describe('references', () => {
+
+  // Configure the client to use the local cluster.
+  anchor.setProvider(anchor.Provider.env());
+
+  it('Is initialized!', async () => {
+    // Add your test here.
+    const program = anchor.workspace.References;
+    const tx = await program.rpc.initialize();
+    console.log("Your transaction signature", tx);
+  });
+});

+ 38 - 0
lang/src/account_info.rs

@@ -21,6 +21,21 @@ impl<'info> Accounts<'info> for AccountInfo<'info> {
     }
 }
 
+impl<'a, 'b, 'c, 'd, 'info> Accounts<'info> for &'c AccountInfo<'info> {
+    fn try_accounts(
+        _program_id: &'a Pubkey,
+        accounts: &'b mut &'c [AccountInfo<'info>],
+        _ix_data: &'d [u8],
+    ) -> Result<Self, ProgramError> {
+        if accounts.is_empty() {
+            return Err(ErrorCode::AccountNotEnoughKeys.into());
+        }
+        let account = &accounts[0];
+        *accounts = &accounts[1..];
+        Ok(account)
+    }
+}
+
 impl<'info> AccountsInit<'info> for AccountInfo<'info> {
     fn try_accounts_init(
         _program_id: &Pubkey,
@@ -57,18 +72,41 @@ impl<'info> ToAccountMetas for AccountInfo<'info> {
     }
 }
 
+impl<'info> ToAccountMetas for &AccountInfo<'info> {
+    fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
+        let is_signer = is_signer.unwrap_or(self.is_signer);
+        let meta = match self.is_writable {
+            false => AccountMeta::new_readonly(*self.key, is_signer),
+            true => AccountMeta::new(*self.key, is_signer),
+        };
+        vec![meta]
+    }
+}
+
 impl<'info> ToAccountInfos<'info> for AccountInfo<'info> {
     fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
         vec![self.clone()]
     }
 }
 
+impl<'info> ToAccountInfos<'info> for &AccountInfo<'info> {
+    fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
+        vec![self.clone()]
+    }
+}
+
 impl<'info> ToAccountInfo<'info> for AccountInfo<'info> {
     fn to_account_info(&self) -> AccountInfo<'info> {
         self.clone()
     }
 }
 
+impl<'info> ToAccountInfo<'info> for &AccountInfo<'info> {
+    fn to_account_info(&self) -> AccountInfo<'info> {
+        self.clone()
+    }
+}
+
 impl<'info> AccountsExit<'info> for AccountInfo<'info> {
     fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
         // no-op

+ 34 - 12
lang/syn/src/parser/accounts/mod.rs

@@ -83,6 +83,10 @@ fn is_field_primitive(f: &syn::Field) -> ParseResult<bool> {
 fn parse_ty(f: &syn::Field) -> ParseResult<Ty> {
     let path = match &f.ty {
         syn::Type::Path(ty_path) => ty_path.path.clone(),
+        syn::Type::Reference(ty_ref) => match *ty_ref.elem.clone() {
+            syn::Type::Path(ty_path) => ty_path.path.clone(),
+            _ => return Err(ParseError::new(f.ty.span(), "invalid account type given")),
+        },
         _ => return Err(ParseError::new(f.ty.span(), "invalid account type given")),
     };
     let ty = match ident_string(f)?.as_str() {
@@ -100,20 +104,38 @@ fn parse_ty(f: &syn::Field) -> ParseResult<Ty> {
 }
 
 fn ident_string(f: &syn::Field) -> ParseResult<String> {
-    let path = match &f.ty {
-        syn::Type::Path(ty_path) => ty_path.path.clone(),
+    match &f.ty {
+        syn::Type::Path(ty_path) => {
+            let path = ty_path.path.clone();
+            // TODO: allow segmented paths.
+            if path.segments.len() != 1 {
+                return Err(ParseError::new(
+                    f.ty.span(),
+                    "segmented paths are not currently allowed",
+                ));
+            }
+
+            let segments = &path.segments[0];
+            Ok(segments.ident.to_string())
+        }
+        syn::Type::Reference(ty_ref) => match *ty_ref.elem.clone() {
+            syn::Type::Path(ty_path) => {
+                let path = ty_path.path.clone();
+                // TODO: allow segmented paths.
+                if path.segments.len() != 1 {
+                    return Err(ParseError::new(
+                        f.ty.span(),
+                        "segmented paths are not currently allowed",
+                    ));
+                }
+
+                let segments = &path.segments[0];
+                Ok(segments.ident.to_string())
+            }
+            _ => return Err(ParseError::new(f.ty.span(), "invalid type")),
+        },
         _ => return Err(ParseError::new(f.ty.span(), "invalid type")),
-    };
-    // TODO: allow segmented paths.
-    if path.segments.len() != 1 {
-        return Err(ParseError::new(
-            f.ty.span(),
-            "segmented paths are not currently allowed",
-        ));
     }
-
-    let segments = &path.segments[0];
-    Ok(segments.ident.to_string())
 }
 
 fn parse_program_state(path: &syn::Path) -> ParseResult<ProgramStateTy> {