lib.rs 2.7 KB

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