dex.rs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. use anchor_lang::solana_program::account_info::AccountInfo;
  2. use anchor_lang::solana_program::entrypoint::ProgramResult;
  3. use anchor_lang::{Accounts, CpiContext, ToAccountInfos};
  4. use serum_dex::instruction::SelfTradeBehavior;
  5. use serum_dex::matching::{OrderType, Side};
  6. use std::num::NonZeroU64;
  7. pub use serum_dex;
  8. #[cfg(not(feature = "devnet"))]
  9. anchor_lang::solana_program::declare_id!("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin");
  10. #[cfg(feature = "devnet")]
  11. anchor_lang::solana_program::declare_id!("DESVgJVGajEgKGXhb6XmqDHGz3VjdgP7rEVESBgxmroY");
  12. #[allow(clippy::too_many_arguments)]
  13. pub fn new_order_v3<'info>(
  14. ctx: CpiContext<'_, '_, '_, 'info, NewOrderV3<'info>>,
  15. side: Side,
  16. limit_price: NonZeroU64,
  17. max_coin_qty: NonZeroU64,
  18. max_native_pc_qty_including_fees: NonZeroU64,
  19. self_trade_behavior: SelfTradeBehavior,
  20. order_type: OrderType,
  21. client_order_id: u64,
  22. limit: u16,
  23. ) -> ProgramResult {
  24. let referral = ctx.remaining_accounts.get(0);
  25. let ix = serum_dex::instruction::new_order(
  26. ctx.accounts.market.key,
  27. ctx.accounts.open_orders.key,
  28. ctx.accounts.request_queue.key,
  29. ctx.accounts.event_queue.key,
  30. ctx.accounts.market_bids.key,
  31. ctx.accounts.market_asks.key,
  32. ctx.accounts.order_payer_token_account.key,
  33. ctx.accounts.open_orders_authority.key,
  34. ctx.accounts.coin_vault.key,
  35. ctx.accounts.pc_vault.key,
  36. ctx.accounts.token_program.key,
  37. ctx.accounts.rent.key,
  38. referral.map(|r| r.key),
  39. &ID,
  40. side,
  41. limit_price,
  42. max_coin_qty,
  43. order_type,
  44. client_order_id,
  45. self_trade_behavior,
  46. limit,
  47. max_native_pc_qty_including_fees,
  48. )?;
  49. solana_program::program::invoke_signed(
  50. &ix,
  51. &ToAccountInfos::to_account_infos(&ctx),
  52. ctx.signer_seeds,
  53. )?;
  54. Ok(())
  55. }
  56. pub fn cancel_order_v2<'info>(
  57. ctx: CpiContext<'_, '_, '_, 'info, CancelOrderV2<'info>>,
  58. side: Side,
  59. order_id: u128,
  60. ) -> ProgramResult {
  61. let ix = serum_dex::instruction::cancel_order(
  62. &ID,
  63. ctx.accounts.market.key,
  64. ctx.accounts.market_bids.key,
  65. ctx.accounts.market_asks.key,
  66. ctx.accounts.open_orders.key,
  67. ctx.accounts.open_orders_authority.key,
  68. ctx.accounts.event_queue.key,
  69. side,
  70. order_id,
  71. )?;
  72. solana_program::program::invoke_signed(
  73. &ix,
  74. &ToAccountInfos::to_account_infos(&ctx),
  75. ctx.signer_seeds,
  76. )?;
  77. Ok(())
  78. }
  79. pub fn settle_funds<'info>(
  80. ctx: CpiContext<'_, '_, '_, 'info, SettleFunds<'info>>,
  81. ) -> ProgramResult {
  82. let referral = ctx.remaining_accounts.get(0);
  83. let ix = serum_dex::instruction::settle_funds(
  84. &ID,
  85. ctx.accounts.market.key,
  86. ctx.accounts.token_program.key,
  87. ctx.accounts.open_orders.key,
  88. ctx.accounts.open_orders_authority.key,
  89. ctx.accounts.coin_vault.key,
  90. ctx.accounts.coin_wallet.key,
  91. ctx.accounts.pc_vault.key,
  92. ctx.accounts.pc_wallet.key,
  93. referral.map(|r| r.key),
  94. ctx.accounts.vault_signer.key,
  95. )?;
  96. solana_program::program::invoke_signed(
  97. &ix,
  98. &ToAccountInfos::to_account_infos(&ctx),
  99. ctx.signer_seeds,
  100. )?;
  101. Ok(())
  102. }
  103. pub fn init_open_orders<'info>(
  104. ctx: CpiContext<'_, '_, '_, 'info, InitOpenOrders<'info>>,
  105. ) -> ProgramResult {
  106. let ix = serum_dex::instruction::init_open_orders(
  107. &ID,
  108. ctx.accounts.open_orders.key,
  109. ctx.accounts.authority.key,
  110. ctx.accounts.market.key,
  111. )?;
  112. solana_program::program::invoke_signed(
  113. &ix,
  114. &ToAccountInfos::to_account_infos(&ctx),
  115. ctx.signer_seeds,
  116. )?;
  117. Ok(())
  118. }
  119. pub fn close_open_orders<'info>(
  120. ctx: CpiContext<'_, '_, '_, 'info, CloseOpenOrders<'info>>,
  121. ) -> ProgramResult {
  122. let ix = serum_dex::instruction::close_open_orders(
  123. &ID,
  124. ctx.accounts.open_orders.key,
  125. ctx.accounts.authority.key,
  126. ctx.accounts.destination.key,
  127. ctx.accounts.market.key,
  128. )?;
  129. solana_program::program::invoke_signed(
  130. &ix,
  131. &ToAccountInfos::to_account_infos(&ctx),
  132. ctx.signer_seeds,
  133. )?;
  134. Ok(())
  135. }
  136. pub fn sweep_fees<'info>(ctx: CpiContext<'_, '_, '_, 'info, SweepFees<'info>>) -> ProgramResult {
  137. let ix = serum_dex::instruction::sweep_fees(
  138. &ID,
  139. ctx.accounts.market.key,
  140. ctx.accounts.pc_vault.key,
  141. ctx.accounts.sweep_authority.key,
  142. ctx.accounts.sweep_receiver.key,
  143. ctx.accounts.vault_signer.key,
  144. ctx.accounts.token_program.key,
  145. )?;
  146. solana_program::program::invoke_signed(
  147. &ix,
  148. &ToAccountInfos::to_account_infos(&ctx),
  149. ctx.signer_seeds,
  150. )?;
  151. Ok(())
  152. }
  153. #[derive(Accounts)]
  154. pub struct NewOrderV3<'info> {
  155. pub market: AccountInfo<'info>,
  156. pub open_orders: AccountInfo<'info>,
  157. pub request_queue: AccountInfo<'info>,
  158. pub event_queue: AccountInfo<'info>,
  159. pub market_bids: AccountInfo<'info>,
  160. pub market_asks: AccountInfo<'info>,
  161. // Token account where funds are transferred from for the order. If
  162. // posting a bid market A/B, then this is the SPL token account for B.
  163. pub order_payer_token_account: AccountInfo<'info>,
  164. pub open_orders_authority: AccountInfo<'info>,
  165. // Also known as the "base" currency. For a given A/B market,
  166. // this is the vault for the A mint.
  167. pub coin_vault: AccountInfo<'info>,
  168. // Also known as the "quote" currency. For a given A/B market,
  169. // this is the vault for the B mint.
  170. pub pc_vault: AccountInfo<'info>,
  171. pub token_program: AccountInfo<'info>,
  172. pub rent: AccountInfo<'info>,
  173. }
  174. #[derive(Accounts)]
  175. pub struct CancelOrderV2<'info> {
  176. pub market: AccountInfo<'info>,
  177. pub market_bids: AccountInfo<'info>,
  178. pub market_asks: AccountInfo<'info>,
  179. pub open_orders: AccountInfo<'info>,
  180. pub open_orders_authority: AccountInfo<'info>,
  181. pub event_queue: AccountInfo<'info>,
  182. }
  183. #[derive(Accounts)]
  184. pub struct SettleFunds<'info> {
  185. pub market: AccountInfo<'info>,
  186. pub open_orders: AccountInfo<'info>,
  187. pub open_orders_authority: AccountInfo<'info>,
  188. pub coin_vault: AccountInfo<'info>,
  189. pub pc_vault: AccountInfo<'info>,
  190. pub coin_wallet: AccountInfo<'info>,
  191. pub pc_wallet: AccountInfo<'info>,
  192. pub vault_signer: AccountInfo<'info>,
  193. pub token_program: AccountInfo<'info>,
  194. }
  195. #[derive(Accounts)]
  196. pub struct InitOpenOrders<'info> {
  197. pub open_orders: AccountInfo<'info>,
  198. pub authority: AccountInfo<'info>,
  199. pub market: AccountInfo<'info>,
  200. pub rent: AccountInfo<'info>,
  201. }
  202. #[derive(Accounts)]
  203. pub struct CloseOpenOrders<'info> {
  204. pub open_orders: AccountInfo<'info>,
  205. pub authority: AccountInfo<'info>,
  206. pub destination: AccountInfo<'info>,
  207. pub market: AccountInfo<'info>,
  208. }
  209. #[derive(Accounts)]
  210. pub struct SweepFees<'info> {
  211. pub market: AccountInfo<'info>,
  212. pub pc_vault: AccountInfo<'info>,
  213. pub sweep_authority: AccountInfo<'info>,
  214. pub sweep_receiver: AccountInfo<'info>,
  215. pub vault_signer: AccountInfo<'info>,
  216. pub token_program: AccountInfo<'info>,
  217. }