lib.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. use solana_program::account_info::AccountInfo;
  24. use solana_program::instruction::AccountMeta;
  25. use solana_program::program_error::ProgramError;
  26. use solana_program::pubkey::Pubkey;
  27. use std::io::Write;
  28. mod account_info;
  29. mod boxed;
  30. mod context;
  31. mod cpi_account;
  32. mod ctor;
  33. mod error;
  34. mod program_account;
  35. mod state;
  36. mod sysvar;
  37. pub use crate::context::{Context, CpiContext};
  38. pub use crate::cpi_account::CpiAccount;
  39. pub use crate::ctor::Ctor;
  40. pub use crate::program_account::ProgramAccount;
  41. pub use crate::state::{ProgramState, ProgramStateAccounts};
  42. pub use crate::sysvar::Sysvar;
  43. pub use anchor_attribute_access_control::access_control;
  44. pub use anchor_attribute_account::account;
  45. pub use anchor_attribute_error::error;
  46. pub use anchor_attribute_program::program;
  47. pub use anchor_attribute_state::state;
  48. pub use anchor_derive_accounts::Accounts;
  49. /// Default serialization format for anchor instructions and accounts.
  50. pub use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize};
  51. pub use error::Error;
  52. pub use solana_program;
  53. /// A data structure of accounts that can be deserialized from the input
  54. /// of a Solana program. Due to the freewheeling nature of the accounts array,
  55. /// implementations of this trait should perform any and all constraint checks
  56. /// (in addition to any done within `AccountDeserialize`) on accounts to ensure
  57. /// the accounts maintain any invariants required for the program to run
  58. /// securely.
  59. pub trait Accounts<'info>: ToAccountMetas + ToAccountInfos<'info> + Sized {
  60. fn try_accounts(
  61. program_id: &Pubkey,
  62. accounts: &mut &[AccountInfo<'info>],
  63. ) -> Result<Self, ProgramError>;
  64. }
  65. /// The exit procedure for an accounts object.
  66. pub trait AccountsExit<'info>: ToAccountMetas + ToAccountInfos<'info> {
  67. fn exit(&self, program_id: &Pubkey) -> solana_program::entrypoint::ProgramResult;
  68. }
  69. /// A data structure of accounts providing a one time deserialization upon
  70. /// initialization, i.e., when the data array for a given account is zeroed.
  71. /// For all subsequent deserializations, it's expected that
  72. /// [Accounts](trait.Accounts.html) is used.
  73. pub trait AccountsInit<'info>: ToAccountMetas + ToAccountInfos<'info> + Sized {
  74. fn try_accounts_init(
  75. program_id: &Pubkey,
  76. accounts: &mut &[AccountInfo<'info>],
  77. ) -> Result<Self, ProgramError>;
  78. }
  79. /// Transformation to `AccountMeta` structs.
  80. pub trait ToAccountMetas {
  81. /// `is_signer` is given as an optional override for the signer meta field.
  82. /// This covers the edge case when a program-derived-address needs to relay
  83. /// a transaction from a client to another program but sign the transaction
  84. /// before the relay. The client cannot mark the field as a signer, and so
  85. /// we have to override the is_signer meta field given by the client.
  86. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta>;
  87. }
  88. /// Transformation to `AccountInfo` structs.
  89. pub trait ToAccountInfos<'info> {
  90. fn to_account_infos(&self) -> Vec<AccountInfo<'info>>;
  91. }
  92. /// Transformation to an `AccountInfo` struct.
  93. pub trait ToAccountInfo<'info> {
  94. fn to_account_info(&self) -> AccountInfo<'info>;
  95. }
  96. /// A data structure that can be serialized and stored in an `AccountInfo` data
  97. /// array.
  98. ///
  99. /// Implementors of this trait should ensure that any subsequent usage the
  100. /// `AccountDeserialize` trait succeeds if and only if the account is of the
  101. /// correct type. For example, the implementation provided by the `#[account]`
  102. /// attribute sets the first 8 bytes to be a unique account discriminator,
  103. /// defined as the first 8 bytes of the SHA256 of the account's Rust ident.
  104. /// Thus, any subsequent calls via `AccountDeserialize`'s `try_deserialize`
  105. /// will check this discriminator. If it doesn't match, an invalid account
  106. /// was given, and the program will exit with an error.
  107. pub trait AccountSerialize {
  108. /// Serilalizes the account data into `writer`.
  109. fn try_serialize<W: Write>(&self, writer: &mut W) -> Result<(), ProgramError>;
  110. }
  111. /// A data structure that can be deserialized from an `AccountInfo` data array.
  112. pub trait AccountDeserialize: Sized {
  113. /// Deserializes the account data.
  114. fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError>;
  115. /// Deserializes account data without checking the account discriminator.
  116. /// This should only be used on account initialization, when the
  117. /// discriminator is not yet set (since the entire account data is zeroed).
  118. fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, ProgramError>;
  119. }
  120. /// The prelude contains all commonly used components of the crate.
  121. /// All programs should include it via `anchor_lang::prelude::*;`.
  122. pub mod prelude {
  123. pub use super::{
  124. access_control, account, error, program, state, AccountDeserialize, AccountSerialize,
  125. Accounts, AccountsExit, AccountsInit, AnchorDeserialize, AnchorSerialize, Context,
  126. CpiAccount, CpiContext, Ctor, ProgramAccount, ProgramState, Sysvar, ToAccountInfo,
  127. ToAccountInfos, ToAccountMetas,
  128. };
  129. pub use borsh;
  130. pub use solana_program::account_info::{next_account_info, AccountInfo};
  131. pub use solana_program::entrypoint::ProgramResult;
  132. pub use solana_program::instruction::AccountMeta;
  133. pub use solana_program::msg;
  134. pub use solana_program::program_error::ProgramError;
  135. pub use solana_program::pubkey::Pubkey;
  136. pub use solana_program::sysvar::clock::Clock;
  137. pub use solana_program::sysvar::epoch_schedule::EpochSchedule;
  138. pub use solana_program::sysvar::fees::Fees;
  139. pub use solana_program::sysvar::instructions::Instructions;
  140. pub use solana_program::sysvar::recent_blockhashes::RecentBlockhashes;
  141. pub use solana_program::sysvar::rent::Rent;
  142. pub use solana_program::sysvar::rewards::Rewards;
  143. pub use solana_program::sysvar::slot_hashes::SlotHashes;
  144. pub use solana_program::sysvar::slot_history::SlotHistory;
  145. pub use solana_program::sysvar::stake_history::StakeHistory;
  146. pub use solana_program::sysvar::Sysvar as SolanaSysvar;
  147. pub use thiserror;
  148. }