lib.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. use anchor_lang::{prelude::*, solana_program::clock::UnixTimestamp};
  2. declare_id!("Newid11111111111111111111111111111111111111");
  3. #[program]
  4. pub mod new_idl {
  5. use super::*;
  6. pub fn no_case_conversion(ctx: Context<NoCaseConversion>, field_name: u8) -> Result<()> {
  7. ctx.accounts.case_conversion_account.field_name = field_name;
  8. emit!(SimpleEvent { field_name });
  9. Ok(())
  10. }
  11. pub fn empty(_ctx: Context<Empty>) -> Result<()> {
  12. Ok(())
  13. }
  14. pub fn primitive_types(
  15. ctx: Context<PrimitiveTypes>,
  16. bool: bool,
  17. i8: i8,
  18. i16: i16,
  19. i32: i32,
  20. i64: i64,
  21. i128: i128,
  22. u8: u8,
  23. u16: u16,
  24. u32: u32,
  25. u64: u64,
  26. u128: u128,
  27. f32: f32,
  28. f64: f64,
  29. pubkey: Pubkey,
  30. ) -> Result<()> {
  31. ctx.accounts.account.bool = bool;
  32. ctx.accounts.account.i8 = i8;
  33. ctx.accounts.account.i16 = i16;
  34. ctx.accounts.account.i32 = i32;
  35. ctx.accounts.account.i64 = i64;
  36. ctx.accounts.account.i128 = i128;
  37. ctx.accounts.account.u8 = u8;
  38. ctx.accounts.account.u16 = u16;
  39. ctx.accounts.account.u32 = u32;
  40. ctx.accounts.account.u64 = u64;
  41. ctx.accounts.account.u128 = u128;
  42. ctx.accounts.account.f32 = f32;
  43. ctx.accounts.account.f64 = f64;
  44. ctx.accounts.account.pubkey = pubkey;
  45. Ok(())
  46. }
  47. pub fn unsized_types(ctx: Context<UnsizedTypes>, string: String, bytes: Vec<u8>) -> Result<()> {
  48. ctx.accounts.account.string = string;
  49. ctx.accounts.account.bytes = bytes;
  50. Ok(())
  51. }
  52. pub fn strct(
  53. ctx: Context<Struct>,
  54. unit: UnitStruct,
  55. named: NamedStruct,
  56. tuple: TupleStruct,
  57. ) -> Result<()> {
  58. ctx.accounts.account.unit = unit;
  59. ctx.accounts.account.named = named;
  60. ctx.accounts.account.tuple = tuple;
  61. Ok(())
  62. }
  63. pub fn enm(ctx: Context<Enum>, full_enum: FullEnum) -> Result<()> {
  64. ctx.accounts.account.full_enum = full_enum;
  65. Ok(())
  66. }
  67. pub fn type_alias(
  68. ctx: Context<TypeAlias>,
  69. alias_u8: AliasU8,
  70. alias_u8_array: AliasU8Array,
  71. alias_struct: AliasStruct,
  72. alias_vec_string: AliasVec<String>,
  73. alias_option_vec_pubkey: AliasOptionVec<Pubkey>,
  74. alias_generic_const: AliasGenericConst<4>,
  75. alias_multiple_generics_mixed: AliasMultipleGenericMixed<bool, 2>,
  76. alias_external: UnixTimestamp,
  77. ) -> Result<()> {
  78. ctx.accounts.account.alias_u8 = alias_u8;
  79. ctx.accounts.account.alias_u8_array = alias_u8_array;
  80. ctx.accounts.account.alias_struct = alias_struct;
  81. ctx.accounts.account.alias_vec_string = alias_vec_string;
  82. ctx.accounts.account.alias_option_vec_pubkey = alias_option_vec_pubkey;
  83. ctx.accounts.account.alias_generic_const = alias_generic_const;
  84. ctx.accounts.account.alias_multiple_generics_mixed = alias_multiple_generics_mixed;
  85. ctx.accounts.account.alias_external = alias_external;
  86. Ok(())
  87. }
  88. pub fn account_and_event_arg_and_field(
  89. ctx: Context<AccountAndEventArgAndField>,
  90. account: AccountAndEventFieldAccount,
  91. ) -> Result<()> {
  92. *ctx.accounts.account = account;
  93. Ok(())
  94. }
  95. pub fn generic(ctx: Context<Generic>, generic_arg: GenericStruct<u16, 4>) -> Result<()> {
  96. ctx.accounts.my_account.field = generic_arg;
  97. Ok(())
  98. }
  99. pub fn generic_custom_struct(
  100. ctx: Context<GenericCustomStruct>,
  101. generic_arg: GenericStruct<SomeStruct, 4>,
  102. ) -> Result<()> {
  103. ctx.accounts.my_account.field = generic_arg;
  104. Ok(())
  105. }
  106. pub fn full_path(
  107. ctx: Context<FullPath>,
  108. named_struct: NamedStruct,
  109. some_module_named_struct: some_module::NamedStruct,
  110. ) -> Result<()> {
  111. ctx.accounts.account.named_struct = named_struct;
  112. ctx.accounts.account.some_module_named_struct = some_module_named_struct;
  113. Ok(())
  114. }
  115. pub fn external(ctx: Context<External>, my_struct: external::MyStruct) -> Result<()> {
  116. ctx.accounts.account.my_struct = my_struct;
  117. Ok(())
  118. }
  119. pub fn external_non_anchor(
  120. ctx: Context<ExternalNonAnchor>,
  121. feature: wrapped::Feature,
  122. ) -> Result<()> {
  123. ctx.accounts.account.feature = feature;
  124. Ok(())
  125. }
  126. }
  127. #[account]
  128. #[derive(InitSpace)]
  129. pub struct SimpleAccount {
  130. pub field_name: u8,
  131. }
  132. #[event]
  133. #[derive(Clone)]
  134. pub struct SimpleEvent {
  135. pub field_name: u8,
  136. }
  137. #[derive(Accounts)]
  138. pub struct NoCaseConversion<'info> {
  139. #[account(init, payer = payer, space = 8 + SimpleAccount::INIT_SPACE)]
  140. pub case_conversion_account: Account<'info, SimpleAccount>,
  141. #[account(mut)]
  142. pub payer: Signer<'info>,
  143. pub system_program: Program<'info, System>,
  144. }
  145. #[derive(Accounts)]
  146. pub struct Empty {}
  147. #[derive(Accounts)]
  148. pub struct PrimitiveTypes<'info> {
  149. #[account(zero)]
  150. pub account: Account<'info, PrimitiveAccount>,
  151. }
  152. #[account]
  153. pub struct PrimitiveAccount {
  154. pub bool: bool,
  155. pub i8: i8,
  156. pub i16: i16,
  157. pub i32: i32,
  158. pub i64: i64,
  159. pub i128: i128,
  160. pub u8: u8,
  161. pub u16: u16,
  162. pub u32: u32,
  163. pub u64: u64,
  164. pub u128: u128,
  165. pub f32: f32,
  166. pub f64: f64,
  167. pub pubkey: Pubkey,
  168. }
  169. #[derive(Accounts)]
  170. pub struct UnsizedTypes<'info> {
  171. #[account(zero)]
  172. pub account: Account<'info, UnsizedAccount>,
  173. }
  174. #[account]
  175. pub struct UnsizedAccount {
  176. pub string: String,
  177. pub bytes: Vec<u8>,
  178. }
  179. #[derive(Accounts)]
  180. pub struct Struct<'info> {
  181. #[account(zero)]
  182. pub account: Account<'info, StructAccount>,
  183. }
  184. #[account]
  185. pub struct StructAccount {
  186. pub unit: UnitStruct,
  187. pub named: NamedStruct,
  188. pub tuple: TupleStruct,
  189. }
  190. #[derive(AnchorDeserialize, AnchorSerialize, Clone)]
  191. pub struct UnitStruct;
  192. #[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug, Eq, PartialEq)]
  193. pub struct NamedStruct {
  194. pub u8: u8,
  195. pub u16: u16,
  196. pub u32: u32,
  197. pub u64: u64,
  198. }
  199. #[derive(AnchorDeserialize, AnchorSerialize, Clone)]
  200. pub struct TupleStruct(u64, String);
  201. #[derive(Accounts)]
  202. pub struct Enum<'info> {
  203. #[account(zero)]
  204. pub account: Account<'info, EnumAccount>,
  205. }
  206. #[account]
  207. pub struct EnumAccount {
  208. pub full_enum: FullEnum,
  209. }
  210. #[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug, Eq, PartialEq)]
  211. pub enum FullEnum {
  212. Unit,
  213. Named { point_x: u64, point_y: u64 },
  214. Unnamed(u8, u8, u16, u16),
  215. UnnamedStruct(NamedStruct),
  216. }
  217. #[derive(Accounts)]
  218. pub struct TypeAlias<'info> {
  219. #[account(zero)]
  220. pub account: Account<'info, AliasAccount>,
  221. }
  222. #[account]
  223. pub struct AliasAccount {
  224. pub alias_u8: AliasU8,
  225. pub alias_u8_array: AliasU8Array,
  226. pub alias_struct: AliasStruct,
  227. pub alias_vec_string: AliasVec<String>,
  228. pub alias_option_vec_pubkey: AliasOptionVec<Pubkey>,
  229. pub alias_generic_const: AliasGenericConst<4>,
  230. pub alias_multiple_generics_mixed: AliasMultipleGenericMixed<bool, 2>,
  231. pub alias_external: UnixTimestamp,
  232. }
  233. pub type AliasU8 = u8;
  234. pub type AliasU8Array = [AliasU8; 8];
  235. pub type AliasStruct = NamedStruct;
  236. pub type AliasVec<T> = Vec<T>;
  237. pub type AliasOptionVec<T> = Vec<Option<T>>;
  238. pub type AliasGenericConst<const N: usize> = [u32; N];
  239. pub type AliasMultipleGenericMixed<T, const N: usize> = Vec<[T; N]>;
  240. #[derive(Accounts)]
  241. pub struct AccountAndEventArgAndField<'info> {
  242. #[account(zero)]
  243. pub account: Account<'info, AccountAndEventFieldAccount>,
  244. }
  245. #[account]
  246. pub struct AccountAndEventFieldAccount {
  247. pub simple_account: SimpleAccount,
  248. pub simple_event: SimpleEvent,
  249. }
  250. #[derive(Accounts)]
  251. pub struct FullPath<'info> {
  252. #[account(zero)]
  253. pub account: Account<'info, FullPathAccount>,
  254. pub external_program: Program<'info, external::program::External>,
  255. }
  256. #[account]
  257. pub struct FullPathAccount {
  258. pub named_struct: NamedStruct,
  259. pub some_module_named_struct: some_module::NamedStruct,
  260. }
  261. mod some_module {
  262. use super::*;
  263. #[derive(AnchorSerialize, AnchorDeserialize, Clone)]
  264. pub struct NamedStruct {
  265. pub data: u8,
  266. }
  267. }
  268. #[derive(Accounts)]
  269. pub struct Generic<'info> {
  270. #[account(mut)]
  271. pub signer: Signer<'info>,
  272. #[account(
  273. init,
  274. payer = signer,
  275. space = 1024,
  276. seeds = [b"generic", signer.key.as_ref()],
  277. bump
  278. )]
  279. pub my_account: Account<'info, GenericAccount>,
  280. pub system_program: Program<'info, System>,
  281. }
  282. #[derive(Accounts)]
  283. pub struct GenericCustomStruct<'info> {
  284. #[account(mut)]
  285. pub signer: Signer<'info>,
  286. #[account(
  287. init,
  288. payer = signer,
  289. space = 1024,
  290. seeds = [b"genericCustomStruct", signer.key.as_ref()],
  291. bump
  292. )]
  293. pub my_account: Account<'info, GenericAccountCustomStruct>,
  294. pub system_program: Program<'info, System>,
  295. }
  296. #[account]
  297. pub struct GenericAccount {
  298. pub field: GenericStruct<u16, 4>,
  299. }
  300. #[account]
  301. pub struct GenericAccountCustomStruct {
  302. pub field: GenericStruct<SomeStruct, 4>,
  303. }
  304. #[derive(AnchorSerialize, AnchorDeserialize, Clone)]
  305. pub struct SomeStruct {
  306. pub field: u16,
  307. }
  308. #[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
  309. pub struct GenericStruct<T, const N: usize> {
  310. arr: [T; N],
  311. sub_field: SubGenericStruct<8, T, Vec<Option<T>>>,
  312. }
  313. #[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
  314. pub struct SubGenericStruct<const N: usize, T, U> {
  315. sub_arr: [T; N],
  316. another: U,
  317. }
  318. #[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
  319. pub enum GenericEnum<T> {
  320. Unit,
  321. Named { x: T },
  322. Tuple(Vec<T>),
  323. }
  324. #[derive(Accounts)]
  325. pub struct External<'info> {
  326. #[account(zero)]
  327. pub account: Account<'info, AccountWithExternalField>,
  328. }
  329. #[account]
  330. pub struct AccountWithExternalField {
  331. pub my_struct: external::MyStruct,
  332. }
  333. #[derive(Accounts)]
  334. pub struct ExternalNonAnchor<'info> {
  335. #[account(zero)]
  336. pub account: Account<'info, AccountWithNonAnchorExternalField>,
  337. }
  338. #[account]
  339. pub struct AccountWithNonAnchorExternalField {
  340. pub feature: wrapped::Feature,
  341. }
  342. /// An example of wrapping a non-Anchor external type in order to include it in the IDL
  343. mod wrapped {
  344. use super::*;
  345. #[cfg(feature = "idl-build")]
  346. use anchor_lang::idl::types::*;
  347. pub struct Feature(anchor_lang::solana_program::feature::Feature);
  348. impl AnchorSerialize for Feature {
  349. fn serialize<W: std::io::prelude::Write>(&self, writer: &mut W) -> std::io::Result<()> {
  350. self.0.activated_at.serialize(writer)?;
  351. Ok(())
  352. }
  353. }
  354. impl AnchorDeserialize for Feature {
  355. fn deserialize_reader<R: std::io::prelude::Read>(reader: &mut R) -> std::io::Result<Self> {
  356. Ok(Self(anchor_lang::solana_program::feature::Feature {
  357. activated_at: AnchorDeserialize::deserialize_reader(reader)?,
  358. }))
  359. }
  360. }
  361. impl Clone for Feature {
  362. fn clone(&self) -> Self {
  363. Self(anchor_lang::solana_program::feature::Feature {
  364. activated_at: self.0.activated_at.clone(),
  365. })
  366. }
  367. }
  368. #[cfg(feature = "idl-build")]
  369. impl IdlBuild for Feature {
  370. fn create_type() -> Option<IdlTypeDef> {
  371. Some(IdlTypeDef {
  372. name: "Feature".into(),
  373. ty: IdlTypeDefTy::Struct {
  374. fields: Some(IdlDefinedFields::Named(vec![IdlField {
  375. name: "activated_at".into(),
  376. ty: IdlType::Option(Box::new(IdlType::U64)),
  377. docs: Default::default(),
  378. }])),
  379. },
  380. docs: Default::default(),
  381. generics: Default::default(),
  382. serialization: Default::default(),
  383. repr: Default::default(),
  384. })
  385. }
  386. }
  387. }