lib.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use anchor_lang::prelude::*;
  2. use std::str::FromStr;
  3. declare_id!("CmP2djJgABZ4cRokm4ndxuq6LerqpNHLBsaUv2XKEJua");
  4. #[program]
  5. pub mod bolt_component {
  6. use super::*;
  7. pub fn initialize(_ctx: Context<Initialize>) -> Result<Vec<u8>> {
  8. let mut component = Component::default();
  9. component.bolt_metadata.authority = Pubkey::from_str("WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n").unwrap();
  10. let mut serialized_data = Vec::new();
  11. anchor_lang::AccountSerialize::try_serialize(&component, &mut serialized_data).expect("Failed to serialize");
  12. //component.serialize(&mut serialized_data).expect("Failed to serialize");
  13. Ok(serialized_data)
  14. }
  15. pub fn apply(_ctx: Context<Apply>, _args: Vec<u8>) -> Result<()> {
  16. Ok(())
  17. }
  18. #[derive(Accounts)]
  19. pub struct Apply<'info> {
  20. #[account(mut)]
  21. pub bolt_component: Account<'info, Component>,
  22. /// CHECK: The system can modify the data of the component
  23. pub bolt_system: UncheckedAccount<'info>,
  24. }
  25. impl<'info> Apply<'info> {
  26. pub fn set_data_ctx(
  27. &self,
  28. ) -> CpiContext<'_, '_, '_, 'info, bolt_system::cpi::accounts::SetData<'info>> {
  29. let cpi_program = self.bolt_system.to_account_info();
  30. let cpi_accounts = bolt_system::cpi::accounts::SetData {
  31. component: self.bolt_component.to_account_info().clone(),
  32. };
  33. CpiContext::new(cpi_program, cpi_accounts)
  34. }
  35. }
  36. pub fn update(_ctx: Context<Update>, _data: Vec<u8>) -> Result<()> {
  37. Ok(())
  38. }
  39. #[derive(Accounts)]
  40. pub struct Update<'info> {
  41. #[account(mut)]
  42. pub bolt_component: Account<'info, Component>,
  43. }
  44. }
  45. #[derive(Accounts)]
  46. pub struct Initialize<'info> {
  47. #[account(mut)]
  48. pub payer: Signer<'info>,
  49. #[account(init_if_needed, owner = Pubkey::from_str("WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n").unwrap(), payer = payer, space = Component::size(), seeds = [Component::seed(), entity.key().as_ref()], bump)]
  50. pub data: AccountInfo<'info>,
  51. #[account()]
  52. /// CHECK: A generic entity account
  53. pub entity: AccountInfo<'info>,
  54. #[account()]
  55. /// CHECK: The authority of the component
  56. pub authority: Option<AccountInfo<'info>>,
  57. pub system_program: Program<'info, System>,
  58. }
  59. // Component data
  60. #[account]
  61. #[derive(InitSpace, Default, Copy)]
  62. pub struct Component {
  63. pub position: Position,
  64. pub bolt_metadata: BoltMetadata,
  65. }
  66. impl Component {
  67. pub fn size() -> usize {
  68. 8 + Component::INIT_SPACE
  69. }
  70. pub fn seed() -> &'static [u8] {
  71. b"origin-component"
  72. }
  73. }
  74. #[derive(InitSpace, AnchorSerialize, AnchorDeserialize, Default, Copy, Clone)]
  75. pub struct Position {
  76. pub x: i64,
  77. pub y: i64,
  78. pub z: i64,
  79. }
  80. #[derive(InitSpace, AnchorSerialize, AnchorDeserialize, Default, Copy, Clone)]
  81. pub struct BoltMetadata {
  82. pub authority: Pubkey
  83. }