Skip to content

Commit d238b45

Browse files
authored
feat(solana): auto init space calulcation (#825)
1 parent a025498 commit d238b45

File tree

3 files changed

+5
-20
lines changed

3 files changed

+5
-20
lines changed

programs/solana/programs/gmp-counter-app/src/instructions/counter_ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct IncrementCounter<'info> {
1717
#[account(
1818
init_if_needed,
1919
payer = payer,
20-
space = UserCounter::INIT_SPACE,
20+
space = 8 + UserCounter::INIT_SPACE,
2121
seeds = [UserCounter::SEED, user_authority.key().as_ref()],
2222
bump
2323
)]

programs/solana/programs/gmp-counter-app/src/state.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anchor_lang::prelude::*;
22

33
/// Global counter app state
44
#[account]
5+
#[derive(InitSpace)]
56
pub struct CounterAppState {
67
/// Authority that can manage the app
78
pub authority: Pubkey,
@@ -15,15 +16,11 @@ pub struct CounterAppState {
1516

1617
impl CounterAppState {
1718
pub const SEED: &'static [u8] = b"counter_app_state";
18-
pub const INIT_SPACE: usize = 8 + // discriminator
19-
32 + // authority
20-
8 + // total_counters
21-
8 + // total_gmp_calls
22-
1; // bump
2319
}
2420

2521
/// Per-user counter state
2622
#[account]
23+
#[derive(InitSpace)]
2724
pub struct UserCounter {
2825
/// User's public key
2926
pub user: Pubkey,
@@ -41,13 +38,6 @@ pub struct UserCounter {
4138

4239
impl UserCounter {
4340
pub const SEED: &'static [u8] = b"user_counter";
44-
pub const INIT_SPACE: usize = 8 + // discriminator
45-
32 + // user
46-
8 + // count
47-
8 + // increments
48-
8 + // decrements
49-
8 + // last_updated
50-
1; // bump
5141

5242
pub fn increment(&mut self, amount: u64, current_time: i64) -> Result<()> {
5343
self.count = self
@@ -72,6 +62,7 @@ impl UserCounter {
7262

7363
/// GMP callback data for tracking calls
7464
#[account]
65+
#[derive(InitSpace)]
7566
pub struct GMPCallState {
7667
/// User who initiated the call
7768
pub user: Pubkey,
@@ -87,10 +78,4 @@ pub struct GMPCallState {
8778

8879
impl GMPCallState {
8980
pub const SEED: &'static [u8] = b"gmp_call_state";
90-
pub const INIT_SPACE: usize = 8 + // discriminator
91-
32 + // user
92-
32 + // payload_hash
93-
8 + // timestamp
94-
1 + // success
95-
1; // bump
9681
}

programs/solana/programs/gmp-counter-app/tests/counter_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anchor_lang::{AnchorSerialize, Discriminator, InstructionData};
1+
use anchor_lang::{AnchorSerialize, Discriminator, InstructionData, Space};
22
use gmp_counter_app::{state::*, ID};
33
use mollusk_svm::Mollusk;
44
use solana_sdk::{

0 commit comments

Comments
 (0)