lib.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. pub bolt_system: UncheckedAccount<'info>,
  28. pub authority: Signer<'info>,
  29. }
  30. impl<'info> Apply<'info> {
  31. pub fn set_data_ctx(
  32. &self,
  33. ) -> CpiContext<'_, '_, '_, 'info, bolt_system::cpi::accounts::SetData<'info>> {
  34. let cpi_program = self.bolt_system.to_account_info();
  35. let cpi_accounts = bolt_system::cpi::accounts::SetData {
  36. component: self.bolt_component.to_account_info().clone(),
  37. authority: self.authority.to_account_info(),
  38. };
  39. CpiContext::new(cpi_program, cpi_accounts)
  40. }
  41. }
  42. pub fn update(ctx: Context<Update>, _data: Vec<u8>) -> Result<()> {
  43. let instruction =
  44. anchor_lang::solana_program::sysvar::instructions::get_instruction_relative(
  45. 0,
  46. &ctx.accounts.instruction_sysvar_account.to_account_info(),
  47. )
  48. .unwrap();
  49. if instruction.program_id == id() {
  50. panic!("The instruction must be called from a CPI");
  51. }
  52. Ok(())
  53. }
  54. #[derive(Accounts)]
  55. pub struct Update<'info> {
  56. #[account(mut)]
  57. pub bolt_component: Account<'info, Component>,
  58. #[account()]
  59. /// CHECK: The authority of the component
  60. pub authority: AccountInfo<'info>,
  61. #[account(address = anchor_lang::solana_program::sysvar::instructions::id())]
  62. /// CHECK: The instruction sysvar
  63. pub instruction_sysvar_account: AccountInfo<'info>,
  64. }
  65. }
  66. #[derive(Accounts)]
  67. pub struct Initialize<'info> {
  68. #[account(mut)]
  69. pub payer: Signer<'info>,
  70. #[account(init_if_needed, payer = payer, space = Component::size(), seeds = [Component::seed(), entity.key().as_ref()], bump)]
  71. pub data: Account<'info, Component>,
  72. #[account()]
  73. /// CHECK: A generic entity account
  74. pub entity: AccountInfo<'info>,
  75. #[account()]
  76. /// CHECK: The authority of the component
  77. pub authority: AccountInfo<'info>,
  78. #[account(address = anchor_lang::solana_program::sysvar::instructions::id())]
  79. /// CHECK: The instruction sysvar
  80. pub instruction_sysvar_account: AccountInfo<'info>,
  81. pub system_program: Program<'info, System>,
  82. }
  83. // Component data
  84. #[account]
  85. #[derive(InitSpace, Default, Copy)]
  86. pub struct Component {
  87. pub position: Position,
  88. pub bolt_metadata: BoltMetadata,
  89. }
  90. impl Component {
  91. pub fn size() -> usize {
  92. 8 + Component::INIT_SPACE
  93. }
  94. pub fn seed() -> &'static [u8] {
  95. b"origin-component"
  96. }
  97. }
  98. #[derive(InitSpace, AnchorSerialize, AnchorDeserialize, Default, Copy, Clone)]
  99. pub struct Position {
  100. pub x: i64,
  101. pub y: i64,
  102. pub z: i64,
  103. }
  104. #[derive(InitSpace, AnchorSerialize, AnchorDeserialize, Default, Copy, Clone)]
  105. pub struct BoltMetadata {
  106. pub authority: Pubkey,
  107. }