lib.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. use anchor_lang::prelude::*;
  2. declare_id!("CmP2djJgABZ4cRokm4ndxuq6LerqpNHLBsaUv2XKEJua");
  3. #[program]
  4. pub mod bolt_component {
  5. use super::*;
  6. pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
  7. let instruction =
  8. anchor_lang::solana_program::sysvar::instructions::get_instruction_relative(
  9. 0,
  10. &ctx.accounts.instruction_sysvar_account.to_account_info(),
  11. )
  12. .unwrap();
  13. if instruction.program_id == id() {
  14. panic!("The instruction must be called from a CPI");
  15. }
  16. ctx.accounts.data.bolt_metadata.authority = *ctx.accounts.authority.key;
  17. Ok(())
  18. }
  19. pub fn apply(_ctx: Context<Apply>, _args: Vec<u8>) -> Result<()> {
  20. Ok(())
  21. }
  22. #[derive(Accounts)]
  23. pub struct Apply<'info> {
  24. #[account(mut)]
  25. pub bolt_component: Account<'info, Component>,
  26. /// CHECK: The system can modify the data of the component
  27. #[account()]
  28. pub bolt_system: UncheckedAccount<'info>,
  29. #[account()]
  30. pub authority: Signer<'info>,
  31. }
  32. pub fn update(ctx: Context<Update>, _data: Vec<u8>) -> Result<()> {
  33. let instruction =
  34. anchor_lang::solana_program::sysvar::instructions::get_instruction_relative(
  35. 0,
  36. &ctx.accounts.instruction_sysvar_account.to_account_info(),
  37. )
  38. .unwrap();
  39. if instruction.program_id == id() {
  40. panic!("The instruction must be called from a CPI");
  41. }
  42. Ok(())
  43. }
  44. #[derive(Accounts)]
  45. pub struct Update<'info> {
  46. #[account(mut)]
  47. pub bolt_component: Account<'info, Component>,
  48. #[account()]
  49. /// CHECK: The authority of the component
  50. pub authority: AccountInfo<'info>,
  51. #[account(address = anchor_lang::solana_program::sysvar::instructions::id())]
  52. /// CHECK: The instruction sysvar
  53. pub instruction_sysvar_account: AccountInfo<'info>,
  54. }
  55. }
  56. #[derive(Accounts)]
  57. pub struct Initialize<'info> {
  58. #[account(mut)]
  59. pub payer: Signer<'info>,
  60. #[account(init_if_needed, payer = payer, space = Component::size(), seeds = [Component::seed(), entity.key().as_ref()], bump)]
  61. pub data: Account<'info, Component>,
  62. #[account()]
  63. /// CHECK: A generic entity account
  64. pub entity: AccountInfo<'info>,
  65. #[account()]
  66. /// CHECK: The authority of the component
  67. pub authority: AccountInfo<'info>,
  68. #[account(address = anchor_lang::solana_program::sysvar::instructions::id())]
  69. /// CHECK: The instruction sysvar
  70. pub instruction_sysvar_account: AccountInfo<'info>,
  71. pub system_program: Program<'info, System>,
  72. }
  73. // Component data
  74. #[account]
  75. #[derive(InitSpace, Default, Copy)]
  76. pub struct Component {
  77. pub position: Position,
  78. pub bolt_metadata: BoltMetadata,
  79. }
  80. impl Component {
  81. pub fn size() -> usize {
  82. 8 + Component::INIT_SPACE
  83. }
  84. pub fn seed() -> &'static [u8] {
  85. b"origin-component"
  86. }
  87. }
  88. #[derive(InitSpace, AnchorSerialize, AnchorDeserialize, Default, Copy, Clone)]
  89. pub struct Position {
  90. pub x: i64,
  91. pub y: i64,
  92. pub z: i64,
  93. }
  94. #[derive(InitSpace, AnchorSerialize, AnchorDeserialize, Default, Copy, Clone)]
  95. pub struct BoltMetadata {
  96. pub authority: Pubkey,
  97. }