lib.rs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //! Anchor ⚓ is a framework for Solana's Sealevel runtime providing several
  2. //! convenient developer tools.
  3. //!
  4. //! - Rust eDSL for writing safe, secure, and high level Solana programs
  5. //! - [IDL](https://en.wikipedia.org/wiki/Interface_description_language) specification
  6. //! - TypeScript package for generating clients from IDL
  7. //! - CLI and workspace management for developing complete applications
  8. //!
  9. //! If you're familiar with developing in Ethereum's
  10. //! [Solidity](https://docs.soliditylang.org/en/v0.7.4/),
  11. //! [Truffle](https://www.trufflesuite.com/),
  12. //! [web3.js](https://github.com/ethereum/web3.js) or Parity's
  13. //! [Ink!](https://github.com/paritytech/ink), then the experience will be
  14. //! familiar. Although the syntax and semantics are targeted at Solana, the high
  15. //! level workflow of writing RPC request handlers, emitting an IDL, and
  16. //! generating clients from IDL is the same.
  17. //!
  18. //! For detailed tutorials and examples on how to use Anchor, see the guided
  19. //! [tutorials](https://project-serum.github.io/anchor) or examples in the GitHub
  20. //! [repository](https://github.com/project-serum/anchor).
  21. //!
  22. //! Presented here are the Rust primitives for building on Solana.
  23. extern crate self as anchor_lang;
  24. use solana_program::account_info::AccountInfo;
  25. use solana_program::instruction::AccountMeta;
  26. use solana_program::program_error::ProgramError;
  27. use solana_program::pubkey::Pubkey;
  28. use std::io::Write;
  29. mod account_info;
  30. mod boxed;
  31. mod context;
  32. mod cpi_account;
  33. mod ctor;
  34. mod error;
  35. pub mod idl;
  36. mod program_account;
  37. mod state;
  38. mod sysvar;
  39. pub use crate::context::{Context, CpiContext};
  40. pub use crate::cpi_account::CpiAccount;
  41. pub use crate::ctor::Ctor;
  42. pub use crate::program_account::ProgramAccount;
  43. pub use crate::state::ProgramState;
  44. pub use crate::sysvar::Sysvar;
  45. pub use anchor_attribute_access_control::access_control;
  46. pub use anchor_attribute_account::account;
  47. pub use anchor_attribute_error::error;
  48. pub use anchor_attribute_program::program;
  49. pub use anchor_attribute_state::state;
  50. pub use anchor_derive_accounts::Accounts;
  51. /// Default serialization format for anchor instructions and accounts.
  52. pub use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize};
  53. pub use error::Error;
  54. pub use solana_program;
  55. /// A data structure of validated accounts that can be deserialized from the
  56. /// input to a Solana program. Implementations of this trait should perform any
  57. /// and all requisite constraint checks on accounts to ensure the accounts
  58. /// maintain any invariants required for the program to run securely. In most
  59. /// cases, it's recommended to use the [`Accounts`](./derive.Accounts.html)
  60. /// derive macro to implement this trait.
  61. pub trait Accounts<'info>: ToAccountMetas + ToAccountInfos<'info> + Sized {
  62. /// Returns the validated accounts struct. What constitutes "valid" is
  63. /// program dependent. However, users of these types should never have to
  64. /// worry about account substitution attacks. For example, if a program
  65. /// expects a `Mint` account from the SPL token program in a particular
  66. /// field, then it should be impossible for this method to return `Ok` if any
  67. /// other account type is given--from the SPL token program or elsewhere.
  68. ///
  69. /// `program_id` is the currently executing program. `accounts` is the
  70. /// set of accounts to construct the type from. For every account used,
  71. /// the implementation should mutate the slice, consuming the used entry
  72. /// so that it cannot be used again.
  73. fn try_accounts(
  74. program_id: &Pubkey,
  75. accounts: &mut &[AccountInfo<'info>],
  76. ) -> Result<Self, ProgramError>;
  77. }
  78. /// The exit procedure for an account. Any cleanup or persistance to storage
  79. /// should be done here.
  80. pub trait AccountsExit<'info>: ToAccountMetas + ToAccountInfos<'info> {
  81. /// `program_id` is the currently executing program.
  82. fn exit(&self, program_id: &Pubkey) -> solana_program::entrypoint::ProgramResult;
  83. }
  84. /// A data structure of accounts providing a one time deserialization upon
  85. /// account initialization, i.e., when the data array for a given account is
  86. /// zeroed. Any subsequent call to `try_accounts_init` should fail. For all
  87. /// subsequent deserializations, it's expected that [`Accounts`] is used.
  88. pub trait AccountsInit<'info>: ToAccountMetas + ToAccountInfos<'info> + Sized {
  89. fn try_accounts_init(
  90. program_id: &Pubkey,
  91. accounts: &mut &[AccountInfo<'info>],
  92. ) -> Result<Self, ProgramError>;
  93. }
  94. /// Transformation to
  95. /// [`AccountMeta`](../solana_program/instruction/struct.AccountMeta.html)
  96. /// structs.
  97. pub trait ToAccountMetas {
  98. /// `is_signer` is given as an optional override for the signer meta field.
  99. /// This covers the edge case when a program-derived-address needs to relay
  100. /// a transaction from a client to another program but sign the transaction
  101. /// before the relay. The client cannot mark the field as a signer, and so
  102. /// we have to override the is_signer meta field given by the client.
  103. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta>;
  104. }
  105. /// Transformation to
  106. /// [`AccountInfo`](../solana_program/account_info/struct.AccountInfo.html)
  107. /// structs.
  108. pub trait ToAccountInfos<'info> {
  109. fn to_account_infos(&self) -> Vec<AccountInfo<'info>>;
  110. }
  111. /// Transformation to an `AccountInfo` struct.
  112. pub trait ToAccountInfo<'info> {
  113. fn to_account_info(&self) -> AccountInfo<'info>;
  114. }
  115. /// A data structure that can be serialized and stored into account storage,
  116. /// i.e. an
  117. /// [`AccountInfo`](../solana_program/account_info/struct.AccountInfo.html#structfield.data)'s
  118. /// mutable data slice.
  119. ///
  120. /// Implementors of this trait should ensure that any subsequent usage of the
  121. /// `AccountDeserialize` trait succeeds if and only if the account is of the
  122. /// correct type.
  123. ///
  124. /// In most cases, one can use the default implementation provided by the
  125. /// [`#[account]`](./attr.account.html) attribute.
  126. pub trait AccountSerialize {
  127. /// Serializes the account data into `writer`.
  128. fn try_serialize<W: Write>(&self, writer: &mut W) -> Result<(), ProgramError>;
  129. }
  130. /// A data structure that can be deserialized and stored into account storage,
  131. /// i.e. an
  132. /// [`AccountInfo`](../solana_program/account_info/struct.AccountInfo.html#structfield.data)'s
  133. /// mutable data slice.
  134. pub trait AccountDeserialize: Sized {
  135. /// Deserializes previously initialized account data. Should fail for all
  136. /// uninitialized accounts, where the bytes are zeroed. Implementations
  137. /// should be unique to a particular account type so that one can never
  138. /// successfully deserialize the data of one account type into another.
  139. /// For example, if the SPL token program where to implement this trait,
  140. /// it should impossible to deserialize a `Mint` account into a token
  141. /// `Account`.
  142. fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError>;
  143. /// Deserializes account data without checking the account discriminator.
  144. /// This should only be used on account initialization, when the bytes of
  145. /// the account are zeroed.
  146. fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, ProgramError>;
  147. }
  148. /// Calculates the data for an instruction invocation, where the data is
  149. /// `Sha256(<namespace>::<method_name>)[..8] || BorshSerialize(args)`.
  150. /// `args` is a borsh serialized struct of named fields for each argument given
  151. /// to an instruction.
  152. pub trait InstructionData: AnchorSerialize {
  153. fn data(&self) -> Vec<u8>;
  154. }
  155. /// The prelude contains all commonly used components of the crate.
  156. /// All programs should include it via `anchor_lang::prelude::*;`.
  157. pub mod prelude {
  158. pub use super::{
  159. access_control, account, error, program, state, AccountDeserialize, AccountSerialize,
  160. Accounts, AccountsExit, AccountsInit, AnchorDeserialize, AnchorSerialize, Context,
  161. CpiAccount, CpiContext, Ctor, ProgramAccount, ProgramState, Sysvar, ToAccountInfo,
  162. ToAccountInfos, ToAccountMetas,
  163. };
  164. pub use borsh;
  165. pub use solana_program::account_info::{next_account_info, AccountInfo};
  166. pub use solana_program::entrypoint::ProgramResult;
  167. pub use solana_program::instruction::AccountMeta;
  168. pub use solana_program::msg;
  169. pub use solana_program::program_error::ProgramError;
  170. pub use solana_program::pubkey::Pubkey;
  171. pub use solana_program::sysvar::clock::Clock;
  172. pub use solana_program::sysvar::epoch_schedule::EpochSchedule;
  173. pub use solana_program::sysvar::fees::Fees;
  174. pub use solana_program::sysvar::instructions::Instructions;
  175. pub use solana_program::sysvar::recent_blockhashes::RecentBlockhashes;
  176. pub use solana_program::sysvar::rent::Rent;
  177. pub use solana_program::sysvar::rewards::Rewards;
  178. pub use solana_program::sysvar::slot_hashes::SlotHashes;
  179. pub use solana_program::sysvar::slot_history::SlotHistory;
  180. pub use solana_program::sysvar::stake_history::StakeHistory;
  181. pub use solana_program::sysvar::Sysvar as SolanaSysvar;
  182. pub use thiserror;
  183. }