lib.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // This file is autogenerated with https://github.com/acheroncrypto/native-to-anchor
  2. use anchor_lang::prelude::*;
  3. declare_id!("11111111111111111111111111111111");
  4. #[program]
  5. pub mod spl_token_swap {
  6. use super::*;
  7. pub fn initialize(ctx: Context<Initialize>, fees: Fees, swap_curve: SwapCurve) -> Result<()> {
  8. Ok(())
  9. }
  10. pub fn swap(ctx: Context<Swap>, amount_in: u64, minimum_amount_out: u64) -> Result<()> {
  11. Ok(())
  12. }
  13. pub fn deposit_all_token_types(
  14. ctx: Context<DepositAllTokenTypes>,
  15. pool_token_amount: u64,
  16. maximum_token_a_amount: u64,
  17. maximum_token_b_amount: u64,
  18. ) -> Result<()> {
  19. Ok(())
  20. }
  21. pub fn withdraw_all_token_types(
  22. ctx: Context<WithdrawAllTokenTypes>,
  23. pool_token_amount: u64,
  24. minimum_token_a_amount: u64,
  25. minimum_token_b_amount: u64,
  26. ) -> Result<()> {
  27. Ok(())
  28. }
  29. pub fn deposit_single_token_type_exact_amount_in(
  30. ctx: Context<DepositSingleTokenTypeExactAmountIn>,
  31. source_token_amount: u64,
  32. minimum_pool_token_amount: u64,
  33. ) -> Result<()> {
  34. Ok(())
  35. }
  36. pub fn withdraw_single_token_type_exact_amount_out(
  37. ctx: Context<WithdrawSingleTokenTypeExactAmountOut>,
  38. destination_token_amount: u64,
  39. maximum_pool_token_amount: u64,
  40. ) -> Result<()> {
  41. Ok(())
  42. }
  43. }
  44. #[derive(Accounts)]
  45. pub struct Initialize<'info> {
  46. #[account(mut)]
  47. swap: Signer<'info>,
  48. authority: AccountInfo<'info>,
  49. token_a: AccountInfo<'info>,
  50. token_b: AccountInfo<'info>,
  51. #[account(mut)]
  52. pool: AccountInfo<'info>,
  53. fee: AccountInfo<'info>,
  54. #[account(mut)]
  55. destination: AccountInfo<'info>,
  56. token_program: Program<'info, Token>,
  57. }
  58. #[derive(Accounts)]
  59. pub struct Swap<'info> {
  60. swap: AccountInfo<'info>,
  61. authority: AccountInfo<'info>,
  62. user_transfer_authority: Signer<'info>,
  63. #[account(mut)]
  64. source: AccountInfo<'info>,
  65. #[account(mut)]
  66. swap_source: AccountInfo<'info>,
  67. #[account(mut)]
  68. swap_destination: AccountInfo<'info>,
  69. #[account(mut)]
  70. destination: AccountInfo<'info>,
  71. #[account(mut)]
  72. pool_mint: AccountInfo<'info>,
  73. #[account(mut)]
  74. pool_fee: AccountInfo<'info>,
  75. token_program: Program<'info, Token>,
  76. // #[account(mut)]
  77. // optional_host_fee: AccountInfo<'info>,
  78. }
  79. #[derive(Accounts)]
  80. pub struct DepositAllTokenTypes<'info> {
  81. swap: AccountInfo<'info>,
  82. authority: AccountInfo<'info>,
  83. user_transfer_authority: Signer<'info>,
  84. #[account(mut)]
  85. deposit_token_a: AccountInfo<'info>,
  86. #[account(mut)]
  87. deposit_token_b: AccountInfo<'info>,
  88. #[account(mut)]
  89. swap_token_a: AccountInfo<'info>,
  90. #[account(mut)]
  91. swap_token_b: AccountInfo<'info>,
  92. #[account(mut)]
  93. pool_mint: AccountInfo<'info>,
  94. #[account(mut)]
  95. destination: AccountInfo<'info>,
  96. token_program: Program<'info, Token>,
  97. }
  98. #[derive(Accounts)]
  99. pub struct WithdrawAllTokenTypes<'info> {
  100. swap: AccountInfo<'info>,
  101. authority: AccountInfo<'info>,
  102. user_transfer_authority: Signer<'info>,
  103. #[account(mut)]
  104. pool_mint: AccountInfo<'info>,
  105. #[account(mut)]
  106. source: AccountInfo<'info>,
  107. #[account(mut)]
  108. swap_token_a: AccountInfo<'info>,
  109. #[account(mut)]
  110. swap_token_b: AccountInfo<'info>,
  111. #[account(mut)]
  112. destination_token_a: AccountInfo<'info>,
  113. #[account(mut)]
  114. destination_token_b: AccountInfo<'info>,
  115. #[account(mut)]
  116. fee_account: AccountInfo<'info>,
  117. token_program: Program<'info, Token>,
  118. }
  119. #[derive(Accounts)]
  120. pub struct DepositSingleTokenTypeExactAmountIn<'info> {
  121. swap: AccountInfo<'info>,
  122. authority: AccountInfo<'info>,
  123. user_transfer_authority: Signer<'info>,
  124. #[account(mut)]
  125. source_token: AccountInfo<'info>,
  126. #[account(mut)]
  127. swap_token_a: AccountInfo<'info>,
  128. #[account(mut)]
  129. swap_token_b: AccountInfo<'info>,
  130. #[account(mut)]
  131. pool_mint: AccountInfo<'info>,
  132. #[account(mut)]
  133. destination: AccountInfo<'info>,
  134. token_program: Program<'info, Token>,
  135. }
  136. #[derive(Accounts)]
  137. pub struct WithdrawSingleTokenTypeExactAmountOut<'info> {
  138. swap: AccountInfo<'info>,
  139. authority: AccountInfo<'info>,
  140. user_transfer_authority: Signer<'info>,
  141. #[account(mut)]
  142. pool_mint: AccountInfo<'info>,
  143. #[account(mut)]
  144. pool_token_source: AccountInfo<'info>,
  145. #[account(mut)]
  146. swap_token_a: AccountInfo<'info>,
  147. #[account(mut)]
  148. swap_token_b: AccountInfo<'info>,
  149. #[account(mut)]
  150. destination: AccountInfo<'info>,
  151. #[account(mut)]
  152. fee_account: AccountInfo<'info>,
  153. token_program: Program<'info, Token>,
  154. }
  155. #[account]
  156. pub struct Swap {
  157. // Swap version
  158. pub version: u8,
  159. /// Initialized state.
  160. pub is_initialized: bool,
  161. /// Bump seed used in program address.
  162. /// The program address is created deterministically with the bump seed,
  163. /// swap program id, and swap account pubkey. This program address has
  164. /// authority over the swap's token A account, token B account, and pool
  165. /// token mint.
  166. pub bump_seed: u8,
  167. /// Program ID of the tokens being exchanged.
  168. pub token_program_id: Pubkey,
  169. /// Token A
  170. pub token_a: Pubkey,
  171. /// Token B
  172. pub token_b: Pubkey,
  173. /// Pool tokens are issued when A or B tokens are deposited.
  174. /// Pool tokens can be withdrawn back to the original A or B token.
  175. pub pool_mint: Pubkey,
  176. /// Mint information for token A
  177. pub token_a_mint: Pubkey,
  178. /// Mint information for token B
  179. pub token_b_mint: Pubkey,
  180. /// Pool token account to receive trading and / or withdrawal fees
  181. pub pool_fee_account: Pubkey,
  182. /// All fee information
  183. pub fees: Fees,
  184. /// Swap curve parameters, to be unpacked and used by the SwapCurve, which
  185. /// calculates swaps, deposits, and withdrawals
  186. pub swap_curve: SwapCurve,
  187. }
  188. #[derive(AnchorSerialize, AnchorDeserialize)]
  189. pub struct Fees {
  190. /// Trade fees are extra token amounts that are held inside the token
  191. /// accounts during a trade, making the value of liquidity tokens rise.
  192. /// Trade fee numerator
  193. pub trade_fee_numerator: u64,
  194. /// Trade fee denominator
  195. pub trade_fee_denominator: u64,
  196. /// Owner trading fees are extra token amounts that are held inside the token
  197. /// accounts during a trade, with the equivalent in pool tokens minted to
  198. /// the owner of the program.
  199. /// Owner trade fee numerator
  200. pub owner_trade_fee_numerator: u64,
  201. /// Owner trade fee denominator
  202. pub owner_trade_fee_denominator: u64,
  203. /// Owner withdraw fees are extra liquidity pool token amounts that are
  204. /// sent to the owner on every withdrawal.
  205. /// Owner withdraw fee numerator
  206. pub owner_withdraw_fee_numerator: u64,
  207. /// Owner withdraw fee denominator
  208. pub owner_withdraw_fee_denominator: u64,
  209. /// Host fees are a proportion of the owner trading fees, sent to an
  210. /// extra account provided during the trade.
  211. /// Host trading fee numerator
  212. pub host_fee_numerator: u64,
  213. /// Host trading fee denominator
  214. pub host_fee_denominator: u64,
  215. }
  216. #[derive(AnchorSerialize, AnchorDeserialize)]
  217. pub struct SwapCurve {
  218. /// The type of curve contained in the calculator, helpful for outside
  219. /// queries
  220. pub curve_type: CurveType,
  221. /// The actual calculator, represented as a trait object to allow for many
  222. /// different types of curves
  223. // pub calculator: Arc<dyn CurveCalculator + Sync + Send>,
  224. pub calculator: [u8; 32],
  225. }
  226. #[derive(AnchorSerialize, AnchorDeserialize)]
  227. pub struct StableCurve {
  228. /// Amplifier constant
  229. pub amp: u64,
  230. }
  231. #[derive(AnchorSerialize, AnchorDeserialize)]
  232. pub struct OffsetCurve {
  233. /// Amount to offset the token B liquidity account
  234. pub token_b_offset: u64,
  235. }
  236. #[derive(AnchorSerialize, AnchorDeserialize)]
  237. pub struct ConstantProductCurve {}
  238. #[derive(AnchorSerialize, AnchorDeserialize)]
  239. pub struct ConstantPriceCurve {
  240. /// Amount of token A required to get 1 token B
  241. pub token_b_price: u64,
  242. }
  243. #[derive(AnchorSerialize, AnchorDeserialize)]
  244. pub enum CurveType {
  245. /// Uniswap-style constant product curve, invariant = token_a_amount * token_b_amount
  246. ConstantProduct,
  247. /// Flat line, always providing 1:1 from one token to another
  248. ConstantPrice,
  249. /// Stable, like uniswap, but with wide zone of 1:1 instead of one point
  250. Stable,
  251. /// Offset curve, like Uniswap, but the token B side has a faked offset
  252. Offset,
  253. }
  254. #[error_code]
  255. pub enum SwapError {
  256. // 0.
  257. /// The account cannot be initialized because it is already being used.
  258. #[msg("Swap account already in use")]
  259. AlreadyInUse,
  260. /// The program address provided doesn't match the value generated by the program.
  261. #[msg("Invalid program address generated from bump seed and key")]
  262. InvalidProgramAddress,
  263. /// The owner of the input isn't set to the program address generated by the program.
  264. #[msg("Input account owner is not the program address")]
  265. InvalidOwner,
  266. /// The owner of the pool token output is set to the program address generated by the program.
  267. #[msg("Output pool account owner cannot be the program address")]
  268. InvalidOutputOwner,
  269. /// The deserialization of the account returned something besides State::Mint.
  270. #[msg("Deserialized account is not an SPL Token mint")]
  271. ExpectedMint,
  272. // 5.
  273. /// The deserialization of the account returned something besides State::Account.
  274. #[msg("Deserialized account is not an SPL Token account")]
  275. ExpectedAccount,
  276. /// The input token account is empty.
  277. #[msg("Input token account empty")]
  278. EmptySupply,
  279. /// The pool token mint has a non-zero supply.
  280. #[msg("Pool token mint has a non-zero supply")]
  281. InvalidSupply,
  282. /// The provided token account has a delegate.
  283. #[msg("Token account has a delegate")]
  284. InvalidDelegate,
  285. /// The input token is invalid for swap.
  286. #[msg("InvalidInput")]
  287. InvalidInput,
  288. // 10.
  289. /// Address of the provided swap token account is incorrect.
  290. #[msg("Address of the provided swap token account is incorrect")]
  291. IncorrectSwapAccount,
  292. /// Address of the provided pool token mint is incorrect
  293. #[msg("Address of the provided pool token mint is incorrect")]
  294. IncorrectPoolMint,
  295. /// The output token is invalid for swap.
  296. #[msg("InvalidOutput")]
  297. InvalidOutput,
  298. /// General calculation failure due to overflow or underflow
  299. #[msg("General calculation failure due to overflow or underflow")]
  300. CalculationFailure,
  301. /// Invalid instruction number passed in.
  302. #[msg("Invalid instruction")]
  303. InvalidInstruction,
  304. // 15.
  305. /// Swap input token accounts have the same mint
  306. #[msg("Swap input token accounts have the same mint")]
  307. RepeatedMint,
  308. /// Swap instruction exceeds desired slippage limit
  309. #[msg("Swap instruction exceeds desired slippage limit")]
  310. ExceededSlippage,
  311. /// The provided token account has a close authority.
  312. #[msg("Token account has a close authority")]
  313. InvalidCloseAuthority,
  314. /// The pool token mint has a freeze authority.
  315. #[msg("Pool token mint has a freeze authority")]
  316. InvalidFreezeAuthority,
  317. /// The pool fee token account is incorrect
  318. #[msg("Pool fee token account incorrect")]
  319. IncorrectFeeAccount,
  320. // 20.
  321. /// Given pool token amount results in zero trading tokens
  322. #[msg("Given pool token amount results in zero trading tokens")]
  323. ZeroTradingTokens,
  324. /// The fee calculation failed due to overflow, underflow, or unexpected 0
  325. #[msg("Fee calculation failed due to overflow, underflow, or unexpected 0")]
  326. FeeCalculationFailure,
  327. /// ConversionFailure
  328. #[msg("Conversion to u64 failed with an overflow or underflow")]
  329. ConversionFailure,
  330. /// The provided fee does not match the program owner's constraints
  331. #[msg("The provided fee does not match the program owner's constraints")]
  332. InvalidFee,
  333. /// The provided token program does not match the token program expected by the swap
  334. #[msg("The provided token program does not match the token program expected by the swap")]
  335. IncorrectTokenProgramId,
  336. // 25.
  337. /// The provided curve type is not supported by the program owner
  338. #[msg("The provided curve type is not supported by the program owner")]
  339. UnsupportedCurveType,
  340. /// The provided curve parameters are invalid
  341. #[msg("The provided curve parameters are invalid")]
  342. InvalidCurve,
  343. /// The operation cannot be performed on the given curve
  344. #[msg("The operation cannot be performed on the given curve")]
  345. UnsupportedCurveOperation,
  346. }