context.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. use crate::{Accounts, ToAccountInfos, ToAccountMetas};
  2. use solana_program::account_info::AccountInfo;
  3. use solana_program::pubkey::Pubkey;
  4. /// Provides non-argument inputs to the program.
  5. pub struct Context<'a, 'b, 'c, 'info, T> {
  6. /// Currently executing program id.
  7. pub program_id: &'a Pubkey,
  8. /// Deserialized accounts.
  9. pub accounts: &'b mut T,
  10. /// Remaining accounts given but not deserialized or validated.
  11. pub remaining_accounts: &'c [AccountInfo<'info>],
  12. }
  13. impl<'a, 'b, 'c, 'info, T: Accounts<'info>> Context<'a, 'b, 'c, 'info, T> {
  14. pub fn new(
  15. program_id: &'a Pubkey,
  16. accounts: &'b mut T,
  17. remaining_accounts: &'c [AccountInfo<'info>],
  18. ) -> Self {
  19. Self {
  20. accounts,
  21. program_id,
  22. remaining_accounts,
  23. }
  24. }
  25. }
  26. /// Context speciying non-argument inputs for cross-program-invocations.
  27. pub struct CpiContext<'a, 'b, 'c, 'info, T>
  28. where
  29. T: ToAccountMetas + ToAccountInfos<'info>,
  30. {
  31. pub accounts: T,
  32. pub program: AccountInfo<'info>,
  33. pub signer_seeds: &'a [&'b [&'c [u8]]],
  34. }
  35. impl<'a, 'b, 'c, 'info, T> CpiContext<'a, 'b, 'c, 'info, T>
  36. where
  37. T: ToAccountMetas + ToAccountInfos<'info>,
  38. {
  39. pub fn new(program: AccountInfo<'info>, accounts: T) -> Self {
  40. Self {
  41. accounts,
  42. program,
  43. signer_seeds: &[],
  44. }
  45. }
  46. pub fn new_with_signer(
  47. program: AccountInfo<'info>,
  48. accounts: T,
  49. signer_seeds: &'a [&'b [&'c [u8]]],
  50. ) -> Self {
  51. Self {
  52. accounts,
  53. program,
  54. signer_seeds,
  55. }
  56. }
  57. pub fn with_signer(mut self, signer_seeds: &'a [&'b [&'c [u8]]]) -> Self {
  58. self.signer_seeds = signer_seeds;
  59. self
  60. }
  61. }