References

This commit is contained in:
armaniferrante 2021-07-10 23:28:03 -07:00
parent 5ef708e39b
commit 1a0af7d8e5
No known key found for this signature in database
GPG Key ID: 58BEF301E91F7828
10 changed files with 146 additions and 13 deletions

5
examples/references/.gitignore vendored Normal file
View File

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

View File

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

View File

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

View File

@ -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.
}

View File

@ -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" }

View File

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

View File

@ -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>,
}

View File

@ -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);
});
});

View File

@ -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

View File

@ -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(),
_ => 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",
));
}
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())
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")),
}
}
fn parse_program_state(path: &syn::Path) -> ParseResult<ProgramStateTy> {