lib.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. Ok(())
  8. }
  9. pub fn apply(_ctx: Context<Apply>, _args: Vec<u8>) -> Result<()> {
  10. Ok(())
  11. }
  12. #[derive(Accounts)]
  13. pub struct Apply<'info> {
  14. #[account(mut)]
  15. pub bolt_component: Account<'info, ComponentData>,
  16. /// CHECK: The system can modify the data of the component
  17. pub bolt_system: UncheckedAccount<'info>,
  18. }
  19. impl<'info> Apply<'info> {
  20. pub fn set_data_ctx(
  21. &self,
  22. ) -> CpiContext<'_, '_, '_, 'info, bolt_system::cpi::accounts::SetData<'info>> {
  23. let cpi_program = self.bolt_system.to_account_info();
  24. let cpi_accounts = bolt_system::cpi::accounts::SetData {
  25. component: self.bolt_component.to_account_info().clone(),
  26. };
  27. CpiContext::new(cpi_program, cpi_accounts)
  28. }
  29. }
  30. pub fn update(_ctx: Context<Update>, _data: Vec<u8>) -> Result<()> {
  31. Ok(())
  32. }
  33. #[derive(Accounts)]
  34. pub struct Update<'info> {
  35. #[account(mut)]
  36. pub bolt_component: Account<'info, ComponentData>,
  37. }
  38. }
  39. #[derive(Accounts)]
  40. pub struct Initialize<'info> {
  41. #[account(mut)]
  42. pub payer: Signer<'info>,
  43. #[account(init_if_needed, payer = payer, space = ComponentData::size(), seeds = [ComponentData::seed(), entity.key().as_ref()], bump)]
  44. pub data: Account<'info, ComponentData>,
  45. #[account()]
  46. /// CHECK: A generic entity account
  47. pub entity: UncheckedAccount<'info>,
  48. pub system_program: Program<'info, System>,
  49. }
  50. // Component data
  51. #[account]
  52. #[derive(InitSpace, Default, Copy)]
  53. pub struct ComponentData {
  54. pub id: u64,
  55. pub position: Position,
  56. }
  57. impl ComponentData {
  58. pub fn size() -> usize {
  59. 8 + ComponentData::INIT_SPACE
  60. }
  61. pub fn seed() -> &'static [u8] {
  62. b"origin-component"
  63. }
  64. }
  65. #[derive(InitSpace, AnchorSerialize, AnchorDeserialize, Default, Copy, Clone)]
  66. pub struct Position {
  67. pub x: i64,
  68. pub y: i64,
  69. pub z: i64,
  70. }