2021-01-14 22:35:50 -08:00
|
|
|
//! Anchor ⚓ is a framework for Solana's Sealevel runtime providing several
|
|
|
|
//! convenient developer tools.
|
|
|
|
//!
|
|
|
|
//! - Rust eDSL for writing safe, secure, and high level Solana programs
|
|
|
|
//! - [IDL](https://en.wikipedia.org/wiki/Interface_description_language) specification
|
|
|
|
//! - TypeScript package for generating clients from IDL
|
|
|
|
//! - CLI and workspace management for developing complete applications
|
|
|
|
//!
|
|
|
|
//! If you're familiar with developing in Ethereum's
|
|
|
|
//! [Solidity](https://docs.soliditylang.org/en/v0.7.4/),
|
|
|
|
//! [Truffle](https://www.trufflesuite.com/),
|
|
|
|
//! [web3.js](https://github.com/ethereum/web3.js) or Parity's
|
|
|
|
//! [Ink!](https://github.com/paritytech/ink), then the experience will be
|
|
|
|
//! familiar. Although the syntax and semantics are targeted at Solana, the high
|
|
|
|
//! level workflow of writing RPC request handlers, emitting an IDL, and
|
|
|
|
//! generating clients from IDL is the same.
|
|
|
|
//!
|
|
|
|
//! For detailed tutorials and examples on how to use Anchor, see the guided
|
|
|
|
//! [tutorials](https://project-serum.github.io/anchor) or examples in the GitHub
|
|
|
|
//! [repository](https://github.com/project-serum/anchor).
|
|
|
|
//!
|
|
|
|
//! Presented here are the Rust primitives for building on Solana.
|
|
|
|
|
2020-12-31 15:48:06 -08:00
|
|
|
use solana_sdk::account_info::AccountInfo;
|
2021-01-14 15:16:27 -08:00
|
|
|
use solana_sdk::instruction::AccountMeta;
|
2020-12-31 15:48:06 -08:00
|
|
|
use solana_sdk::program_error::ProgramError;
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2021-01-09 22:03:14 -08:00
|
|
|
use std::io::Write;
|
2020-12-31 15:48:06 -08:00
|
|
|
|
2021-01-14 22:35:50 -08:00
|
|
|
mod account_info;
|
|
|
|
mod context;
|
|
|
|
mod cpi_account;
|
|
|
|
mod program_account;
|
|
|
|
mod sysvar;
|
|
|
|
|
|
|
|
pub use crate::context::{Context, CpiContext};
|
|
|
|
pub use crate::cpi_account::CpiAccount;
|
|
|
|
pub use crate::program_account::ProgramAccount;
|
|
|
|
pub use crate::sysvar::Sysvar;
|
2021-01-09 22:03:14 -08:00
|
|
|
pub use anchor_attribute_access_control::access_control;
|
|
|
|
pub use anchor_attribute_account::account;
|
|
|
|
pub use anchor_attribute_program::program;
|
|
|
|
pub use anchor_derive_accounts::Accounts;
|
2021-01-14 15:16:27 -08:00
|
|
|
/// Default serialization format for anchor instructions and accounts.
|
2020-12-31 15:48:06 -08:00
|
|
|
pub use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize};
|
|
|
|
|
2021-01-14 22:35:50 -08:00
|
|
|
/// A data structure of accounts that can be deserialized from the input
|
2021-01-09 22:03:14 -08:00
|
|
|
/// of a Solana program. Due to the freewheeling nature of the accounts array,
|
|
|
|
/// implementations of this trait should perform any and all constraint checks
|
|
|
|
/// (in addition to any done within `AccountDeserialize`) on accounts to ensure
|
|
|
|
/// the accounts maintain any invariants required for the program to run
|
|
|
|
/// securely.
|
2021-01-14 15:16:27 -08:00
|
|
|
pub trait Accounts<'info>: ToAccountMetas + ToAccountInfos<'info> + Sized {
|
|
|
|
fn try_accounts(
|
|
|
|
program_id: &Pubkey,
|
2021-01-14 22:35:50 -08:00
|
|
|
accounts: &mut &[AccountInfo<'info>],
|
|
|
|
) -> Result<Self, ProgramError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A data structure of accounts providing a one time deserialization upon
|
|
|
|
/// initialization, i.e., when the data array for a given account is zeroed.
|
|
|
|
/// For all subsequent deserializations, it's expected that
|
|
|
|
/// [Accounts](trait.Accounts.html) is used.
|
|
|
|
pub trait AccountsInit<'info>: ToAccountMetas + ToAccountInfos<'info> + Sized {
|
|
|
|
fn try_accounts_init(
|
|
|
|
program_id: &Pubkey,
|
|
|
|
accounts: &mut &[AccountInfo<'info>],
|
2021-01-14 15:16:27 -08:00
|
|
|
) -> Result<Self, ProgramError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transformation to `AccountMeta` structs.
|
|
|
|
pub trait ToAccountMetas {
|
|
|
|
fn to_account_metas(&self) -> Vec<AccountMeta>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transformation to `AccountInfo` structs.
|
|
|
|
pub trait ToAccountInfos<'info> {
|
|
|
|
fn to_account_infos(&self) -> Vec<AccountInfo<'info>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transformation to an `AccountInfo` struct.
|
|
|
|
pub trait ToAccountInfo<'info> {
|
|
|
|
fn to_account_info(&self) -> AccountInfo<'info>;
|
2021-01-09 22:03:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A data structure that can be serialized and stored in an `AccountInfo` data
|
|
|
|
/// array.
|
|
|
|
///
|
|
|
|
/// Implementors of this trait should ensure that any subsequent usage the
|
|
|
|
/// `AccountDeserialize` trait succeeds if and only if the account is of the
|
|
|
|
/// correct type. For example, the implementation provided by the `#[account]`
|
|
|
|
/// attribute sets the first 8 bytes to be a unique account discriminator,
|
|
|
|
/// defined as the first 8 bytes of the SHA256 of the account's Rust ident.
|
|
|
|
/// Thus, any subsequent calls via `AccountDeserialize`'s `try_deserialize`
|
|
|
|
/// will check this discriminator. If it doesn't match, an invalid account
|
|
|
|
/// was given, and the program will exit with an error.
|
|
|
|
pub trait AccountSerialize {
|
|
|
|
/// Serilalizes the account data into `writer`.
|
|
|
|
fn try_serialize<W: Write>(&self, writer: &mut W) -> Result<(), ProgramError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A data structure that can be deserialized from an `AccountInfo` data array.
|
|
|
|
pub trait AccountDeserialize: Sized {
|
|
|
|
/// Deserializes the account data.
|
|
|
|
fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError>;
|
|
|
|
|
|
|
|
/// Deserializes account data without checking the account discriminator.
|
|
|
|
/// This should only be used on account initialization, when the
|
|
|
|
/// discriminator is not yet set (since the entire account data is zeroed).
|
|
|
|
fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, ProgramError>;
|
|
|
|
}
|
|
|
|
|
2021-01-14 22:35:50 -08:00
|
|
|
/// The prelude contains all commonly used components of the crate.
|
|
|
|
/// All programs should include it via `anchor_lang::prelude::*;`.
|
2020-12-31 15:48:06 -08:00
|
|
|
pub mod prelude {
|
|
|
|
pub use super::{
|
2021-01-09 22:03:14 -08:00
|
|
|
access_control, account, program, AccountDeserialize, AccountSerialize, Accounts,
|
2021-01-14 22:35:50 -08:00
|
|
|
AccountsInit, AnchorDeserialize, AnchorSerialize, Context, CpiAccount, CpiContext,
|
|
|
|
ProgramAccount, Sysvar, ToAccountInfo, ToAccountInfos, ToAccountMetas,
|
2020-12-31 15:48:06 -08:00
|
|
|
};
|
|
|
|
|
2021-01-01 23:01:31 -08:00
|
|
|
pub use solana_program::msg;
|
2021-01-14 15:16:27 -08:00
|
|
|
pub use solana_sdk::account_info::{next_account_info, AccountInfo};
|
2020-12-31 15:48:06 -08:00
|
|
|
pub use solana_sdk::entrypoint::ProgramResult;
|
2021-01-14 15:16:27 -08:00
|
|
|
pub use solana_sdk::instruction::AccountMeta;
|
2020-12-31 15:48:06 -08:00
|
|
|
pub use solana_sdk::program_error::ProgramError;
|
|
|
|
pub use solana_sdk::pubkey::Pubkey;
|
2021-01-11 09:22:25 -08:00
|
|
|
pub use solana_sdk::sysvar::clock::Clock;
|
|
|
|
pub use solana_sdk::sysvar::epoch_schedule::EpochSchedule;
|
|
|
|
pub use solana_sdk::sysvar::fees::Fees;
|
|
|
|
pub use solana_sdk::sysvar::instructions::Instructions;
|
|
|
|
pub use solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes;
|
|
|
|
pub use solana_sdk::sysvar::rent::Rent;
|
|
|
|
pub use solana_sdk::sysvar::rewards::Rewards;
|
|
|
|
pub use solana_sdk::sysvar::slot_hashes::SlotHashes;
|
|
|
|
pub use solana_sdk::sysvar::slot_history::SlotHistory;
|
|
|
|
pub use solana_sdk::sysvar::stake_history::StakeHistory;
|
2021-01-14 15:16:27 -08:00
|
|
|
pub use solana_sdk::sysvar::Sysvar as SolanaSysvar;
|
2020-12-31 15:48:06 -08:00
|
|
|
}
|