context.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. /// Be very careful when using this directly.
  12. pub remaining_accounts: &'c [AccountInfo<'info>],
  13. }
  14. impl<'a, 'b, 'c, 'info, T: Accounts<'info>> Context<'a, 'b, 'c, 'info, T> {
  15. pub fn new(
  16. program_id: &'a Pubkey,
  17. accounts: &'b mut T,
  18. remaining_accounts: &'c [AccountInfo<'info>],
  19. ) -> Self {
  20. Self {
  21. accounts,
  22. program_id,
  23. remaining_accounts,
  24. }
  25. }
  26. }
  27. /// Context speciying non-argument inputs for cross-program-invocations.
  28. pub struct CpiContext<'a, 'b, 'c, 'info, T>
  29. where
  30. T: ToAccountMetas + ToAccountInfos<'info>,
  31. {
  32. pub accounts: T,
  33. pub program: AccountInfo<'info>,
  34. pub signer_seeds: &'a [&'b [&'c [u8]]],
  35. }
  36. impl<'a, 'b, 'c, 'info, T> CpiContext<'a, 'b, 'c, 'info, T>
  37. where
  38. T: ToAccountMetas + ToAccountInfos<'info>,
  39. {
  40. pub fn new(program: AccountInfo<'info>, accounts: T) -> Self {
  41. Self {
  42. accounts,
  43. program,
  44. signer_seeds: &[],
  45. }
  46. }
  47. pub fn new_with_signer(
  48. program: AccountInfo<'info>,
  49. accounts: T,
  50. signer_seeds: &'a [&'b [&'c [u8]]],
  51. ) -> Self {
  52. Self {
  53. accounts,
  54. program,
  55. signer_seeds,
  56. }
  57. }
  58. pub fn with_signer(mut self, signer_seeds: &'a [&'b [&'c [u8]]]) -> Self {
  59. self.signer_seeds = signer_seeds;
  60. self
  61. }
  62. }