pool.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. use steel::*;
  2. use crate::{
  3. consts::{AUTHORITY_SEED, LIQUIDITY_SEED},
  4. error::TokenSwapError,
  5. };
  6. use super::TokenSwapAccount;
  7. /// Fetch PDA of the pool account.
  8. pub fn pool_pda(amm: Pubkey, mint_a: Pubkey, mint_b: Pubkey) -> (Pubkey, u8) {
  9. Pubkey::find_program_address(
  10. &[amm.as_ref(), mint_a.as_ref(), mint_b.as_ref()],
  11. &crate::id(),
  12. )
  13. }
  14. pub fn validate_pool_account(pool: &AccountInfo, mint_a: Pubkey, mint_b: Pubkey) -> ProgramResult {
  15. let pool_info_data = pool.as_account::<Pool>(&crate::id())?;
  16. pool.has_owner(&crate::id())?.has_seeds(
  17. &[
  18. pool_info_data.amm.as_ref(),
  19. pool_info_data.mint_a.as_ref(),
  20. pool_info_data.mint_b.as_ref(),
  21. ],
  22. &crate::id(),
  23. )?;
  24. if pool_info_data.mint_a != mint_a || pool_info_data.mint_b != mint_b {
  25. return Err(TokenSwapError::InvalidAccount.into());
  26. }
  27. Ok(())
  28. }
  29. pub fn pool_authority_pda(amm: Pubkey, mint_a: Pubkey, mint_b: Pubkey) -> (Pubkey, u8) {
  30. Pubkey::find_program_address(
  31. &[
  32. amm.as_ref(),
  33. mint_a.as_ref(),
  34. mint_b.as_ref(),
  35. AUTHORITY_SEED,
  36. ],
  37. &crate::id(),
  38. )
  39. }
  40. pub fn validate_pool_authority(
  41. pool: &Pool,
  42. pool_authority: &AccountInfo,
  43. mint_a: Pubkey,
  44. mint_b: Pubkey,
  45. ) -> ProgramResult {
  46. pool_authority.has_seeds(
  47. &[
  48. pool.amm.as_ref(),
  49. mint_a.as_ref(),
  50. mint_b.as_ref(),
  51. AUTHORITY_SEED,
  52. ],
  53. &crate::id(),
  54. )?;
  55. Ok(())
  56. }
  57. pub fn mint_liquidity_pda(amm: Pubkey, mint_a: Pubkey, mint_b: Pubkey) -> (Pubkey, u8) {
  58. Pubkey::find_program_address(
  59. &[
  60. amm.as_ref(),
  61. mint_a.as_ref(),
  62. mint_b.as_ref(),
  63. LIQUIDITY_SEED,
  64. ],
  65. &crate::id(),
  66. )
  67. }
  68. pub fn validate_mint_liquidity(
  69. pool: &Pool,
  70. mint_liquidity: &AccountInfo,
  71. mint_a: Pubkey,
  72. mint_b: Pubkey,
  73. ) -> ProgramResult {
  74. mint_liquidity
  75. .is_writable()?
  76. .has_seeds(
  77. &[
  78. pool.amm.as_ref(),
  79. mint_a.as_ref(),
  80. mint_b.as_ref(),
  81. LIQUIDITY_SEED,
  82. ],
  83. &crate::id(),
  84. )?
  85. .as_mint()?;
  86. Ok(())
  87. }
  88. #[repr(C)]
  89. #[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
  90. pub struct Pool {
  91. /// Primary key of the AMM
  92. pub amm: Pubkey,
  93. /// Mint of token A
  94. pub mint_a: Pubkey,
  95. /// Mint of token B
  96. pub mint_b: Pubkey,
  97. pub pool_authority_bump: u8,
  98. }
  99. account!(TokenSwapAccount, Pool);