extern crate self as anchor_lang;
use bytemuck::{Pod, Zeroable};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
use solana_program::pubkey::Pubkey;
use std::collections::{BTreeMap, BTreeSet};
use std::io::Write;
mod account_meta;
pub mod accounts;
mod bpf_upgradeable_state;
mod bpf_writer;
mod common;
pub mod context;
mod ctor;
pub mod error;
#[doc(hidden)]
pub mod idl;
pub mod system_program;
mod vec;
pub use crate::bpf_upgradeable_state::*;
pub use anchor_attribute_access_control::access_control;
pub use anchor_attribute_account::{account, declare_id, zero_copy};
pub use anchor_attribute_constant::constant;
pub use anchor_attribute_error::*;
pub use anchor_attribute_event::{emit, event};
pub use anchor_attribute_interface::interface;
pub use anchor_attribute_program::program;
pub use anchor_attribute_state::state;
pub use anchor_derive_accounts::Accounts;
pub use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize};
pub use solana_program;
pub type Result<T> = std::result::Result<T, error::Error>;
pub trait Accounts<'info>: ToAccountMetas + ToAccountInfos<'info> + Sized {
fn try_accounts(
program_id: &Pubkey,
accounts: &mut &[AccountInfo<'info>],
ix_data: &[u8],
bumps: &mut BTreeMap<String, u8>,
reallocs: &mut BTreeSet<Pubkey>,
) -> Result<Self>;
}
pub trait AccountsExit<'info>: ToAccountMetas + ToAccountInfos<'info> {
fn exit(&self, _program_id: &Pubkey) -> Result<()> {
Ok(())
}
}
pub trait AccountsClose<'info>: ToAccountInfos<'info> {
fn close(&self, sol_destination: AccountInfo<'info>) -> Result<()>;
}
pub trait ToAccountMetas {
fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta>;
}
pub trait ToAccountInfos<'info> {
fn to_account_infos(&self) -> Vec<AccountInfo<'info>>;
}
pub trait ToAccountInfo<'info> {
fn to_account_info(&self) -> AccountInfo<'info>;
}
impl<'info, T> ToAccountInfo<'info> for T
where
T: AsRef<AccountInfo<'info>>,
{
fn to_account_info(&self) -> AccountInfo<'info> {
self.as_ref().clone()
}
}
pub trait AccountSerialize {
fn try_serialize<W: Write>(&self, _writer: &mut W) -> Result<()> {
Ok(())
}
}
pub trait AccountDeserialize: Sized {
fn try_deserialize(buf: &mut &[u8]) -> Result<Self> {
Self::try_deserialize_unchecked(buf)
}
fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self>;
}
pub trait ZeroCopy: Discriminator + Copy + Clone + Zeroable + Pod {}
pub trait InstructionData: AnchorSerialize {
fn data(&self) -> Vec<u8>;
}
pub trait Event: AnchorSerialize + AnchorDeserialize + Discriminator {
fn data(&self) -> Vec<u8>;
}
#[doc(hidden)]
#[deprecated(since = "0.4.2", note = "Please use Event instead")]
pub trait EventData: AnchorSerialize + Discriminator {
fn data(&self) -> Vec<u8>;
}
pub trait Discriminator {
fn discriminator() -> [u8; 8];
}
pub trait Bump {
fn seed(&self) -> u8;
}
pub trait Owner {
fn owner() -> Pubkey;
}
pub trait Id {
fn id() -> Pubkey;
}
pub trait Key {
fn key(&self) -> Pubkey;
}
impl Key for Pubkey {
fn key(&self) -> Pubkey {
*self
}
}
pub mod prelude {
pub use super::{
access_control, account, accounts::account::Account,
accounts::account_loader::AccountLoader, accounts::program::Program,
accounts::signer::Signer, accounts::system_account::SystemAccount,
accounts::sysvar::Sysvar, accounts::unchecked_account::UncheckedAccount, constant,
context::Context, context::CpiContext, declare_id, emit, err, error, event, interface,
program, require, require_eq, require_gt, require_gte, require_keys_eq, require_keys_neq,
require_neq, solana_program::bpf_loader_upgradeable::UpgradeableLoaderState, source, state,
system_program::System, zero_copy, AccountDeserialize, AccountSerialize, Accounts,
AccountsExit, AnchorDeserialize, AnchorSerialize, Id, Key, Owner, ProgramData, Result,
ToAccountInfo, ToAccountInfos, ToAccountMetas,
};
pub use anchor_attribute_error::*;
pub use borsh;
pub use error::*;
pub use solana_program::account_info::{next_account_info, AccountInfo};
pub use solana_program::instruction::AccountMeta;
pub use solana_program::msg;
pub use solana_program::program_error::ProgramError;
pub use solana_program::pubkey::Pubkey;
pub use solana_program::sysvar::clock::Clock;
pub use solana_program::sysvar::epoch_schedule::EpochSchedule;
pub use solana_program::sysvar::instructions::Instructions;
pub use solana_program::sysvar::rent::Rent;
pub use solana_program::sysvar::rewards::Rewards;
pub use solana_program::sysvar::slot_hashes::SlotHashes;
pub use solana_program::sysvar::slot_history::SlotHistory;
pub use solana_program::sysvar::stake_history::StakeHistory;
pub use solana_program::sysvar::Sysvar as SolanaSysvar;
pub use thiserror;
}
#[doc(hidden)]
pub mod __private {
use super::Result;
pub const CLOSED_ACCOUNT_DISCRIMINATOR: [u8; 8] = [255, 255, 255, 255, 255, 255, 255, 255];
pub use crate::ctor::Ctor;
pub use anchor_attribute_account::ZeroCopyAccessor;
pub use anchor_attribute_event::EventIndex;
pub use base64;
pub use bytemuck;
use solana_program::pubkey::Pubkey;
pub mod state {
pub use crate::accounts::state::*;
}
#[doc(hidden)]
pub trait AccountSize {
fn size(&self) -> Result<u64>;
}
#[doc(hidden)]
pub trait ZeroCopyAccessor<Ty> {
fn get(&self) -> Ty;
fn set(input: &Ty) -> Self;
}
#[doc(hidden)]
impl ZeroCopyAccessor<Pubkey> for [u8; 32] {
fn get(&self) -> Pubkey {
Pubkey::new(self)
}
fn set(input: &Pubkey) -> [u8; 32] {
input.to_bytes()
}
}
#[doc(hidden)]
pub use crate::accounts::state::PROGRAM_STATE_SEED;
}
#[macro_export]
macro_rules! require {
($invariant:expr, $error:tt $(,)?) => {
if !($invariant) {
return Err(anchor_lang::error!(crate::ErrorCode::$error));
}
};
($invariant:expr, $error:expr $(,)?) => {
if !($invariant) {
return Err(anchor_lang::error!($error));
}
};
}
#[macro_export]
macro_rules! require_eq {
($value1: expr, $value2: expr, $error_code:expr $(,)?) => {
if $value1 != $value2 {
return Err(error!($error_code).with_values(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 != $value2 {
return Err(error!(anchor_lang::error::ErrorCode::RequireEqViolated)
.with_values(($value1, $value2)));
}
};
}
#[macro_export]
macro_rules! require_neq {
($value1: expr, $value2: expr, $error_code: expr $(,)?) => {
if $value1 == $value2 {
return Err(error!($error_code).with_values(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 == $value2 {
return Err(error!(anchor_lang::error::ErrorCode::RequireNeqViolated)
.with_values(($value1, $value2)));
}
};
}
#[macro_export]
macro_rules! require_keys_eq {
($value1: expr, $value2: expr, $error_code:expr $(,)?) => {
if $value1 != $value2 {
return Err(error!($error_code).with_pubkeys(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 != $value2 {
return Err(error!(anchor_lang::error::ErrorCode::RequireKeysEqViolated)
.with_pubkeys(($value1, $value2)));
}
};
}
#[macro_export]
macro_rules! require_keys_neq {
($value1: expr, $value2: expr, $error_code: expr $(,)?) => {
if $value1 == $value2 {
return Err(error!($error_code).with_pubkeys(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 == $value2 {
return Err(
error!(anchor_lang::error::ErrorCode::RequireKeysNeqViolated)
.with_pubkeys(($value1, $value2)),
);
}
};
}
#[macro_export]
macro_rules! require_gt {
($value1: expr, $value2: expr, $error_code: expr $(,)?) => {
if $value1 <= $value2 {
return Err(error!($error_code).with_values(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 <= $value2 {
return Err(error!(anchor_lang::error::ErrorCode::RequireGtViolated)
.with_values(($value1, $value2)));
}
};
}
#[macro_export]
macro_rules! require_gte {
($value1: expr, $value2: expr, $error_code: expr $(,)?) => {
if $value1 < $value2 {
return Err(error!($error_code).with_values(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 < $value2 {
return Err(error!(anchor_lang::error::ErrorCode::RequireGteViolated)
.with_values(($value1, $value2)));
}
};
}
#[macro_export]
macro_rules! err {
($error:tt $(,)?) => {
Err(anchor_lang::error!(crate::ErrorCode::$error))
};
($error:expr $(,)?) => {
Err(anchor_lang::error!($error))
};
}
#[macro_export]
macro_rules! source {
() => {
anchor_lang::error::Source {
filename: file!(),
line: line!(),
}
};
}