Skip to content

Commit 095e864

Browse files
authored
Merge pull request #67 from staratlasmeta/stegaBOB/feat/single-account-set-macro
Feat: macro single account set + propagate marker traits
2 parents 3626998 + a32544d commit 095e864

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+3237
-4131
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ syn = { version = "2.0.40", features = ["extra-traits"] }
5252
typenum = "1.17.0"
5353
sha2 = "0.10.8"
5454
pretty_assertions = "1.4.0"
55+
semver = { version = "1.0.23", features = ["serde"] }
5556

5657
# version locks
5758
bumpalo = "~3.14.0"

framework/example_programs/counter/src/lib.rs

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
use star_frame::__private::macro_prelude::IdlAccountSetDef;
12
use star_frame::anyhow::bail;
23
use star_frame::borsh::{BorshDeserialize, BorshSerialize};
4+
use star_frame::derive_more::{Deref, DerefMut};
35
use star_frame::prelude::*;
46
use star_frame::solana_program::pubkey::Pubkey;
7+
use star_frame::star_frame_idl::IdlDefinition;
58

6-
#[derive(Align1, Copy, Clone, Debug, Eq, PartialEq, Pod, Zeroable)]
9+
#[derive(Align1, Pod, Zeroable, Copy, Clone, Debug, Eq, PartialEq, ProgramAccount)]
10+
#[program_account(seeds = CounterAccountSeeds)]
711
#[repr(C, packed)]
812
pub struct CounterAccount {
913
pub version: u8,
@@ -13,36 +17,48 @@ pub struct CounterAccount {
1317
pub bump: u8,
1418
}
1519

16-
impl ProgramAccount for CounterAccount {
17-
type OwnerProgram = CounterProgram;
18-
const DISCRIMINANT: <Self::OwnerProgram as StarFrameProgram>::AccountDiscriminant = [0; 8];
19-
}
20-
21-
impl HasSeeds for CounterAccount {
22-
type Seeds = CounterAccountSeeds;
20+
#[derive(AccountSet, Deref, DerefMut, Debug)]
21+
#[account_set(skip_default_idl)]
22+
#[cleanup(generics = [<A> where DataAccount<'info, CounterAccount>: AccountSetCleanup<'info, A>], arg = A)]
23+
#[validate(generics = [<A> where DataAccount<'info, CounterAccount>: AccountSetValidate<'info, A>], arg = A)]
24+
pub struct WrappedCounter<'info>(
25+
#[cleanup(arg = arg)]
26+
#[validate(arg = arg)]
27+
#[single_account_set]
28+
DataAccount<'info, CounterAccount>,
29+
);
30+
31+
impl<'info, A> AccountSetToIdl<'info, A> for WrappedCounter<'info>
32+
where
33+
DataAccount<'info, CounterAccount>: AccountSetToIdl<'info, A>,
34+
{
35+
fn account_set_to_idl(idl_definition: &mut IdlDefinition, arg: A) -> Result<IdlAccountSetDef> {
36+
<DataAccount<'info, CounterAccount>>::account_set_to_idl(idl_definition, arg)
37+
}
2338
}
2439

2540
#[derive(Debug, GetSeeds, Clone)]
26-
#[seed_const(b"COUNTER")]
41+
#[get_seeds(seed_const = b"COUNTER")]
2742
pub struct CounterAccountSeeds {
2843
pub owner: Pubkey,
2944
}
3045

31-
#[derive(BorshSerialize, BorshDeserialize, Debug)]
46+
#[derive(BorshSerialize, BorshDeserialize, Debug, InstructionToIdl)]
3247
pub struct CreateCounterIx {
3348
pub start_at: Option<u64>,
3449
}
3550

3651
#[derive(AccountSet)]
3752
pub struct CreateCounterAccounts<'info> {
3853
#[account_set(funder)]
39-
pub funder: Signer<Writable<SystemAccount<'info>>>,
54+
pub funder: Signer<Mut<SystemAccount<'info>>>,
4055
pub owner: SystemAccount<'info>,
4156
#[validate(arg = (
4257
CreateIfNeeded(()),
4358
Seeds(CounterAccountSeeds { owner: *self.owner.key(), }),
4459
))]
45-
pub counter: Init<Seeded<DataAccount<'info, CounterAccount>>>,
60+
#[idl(arg = Seeds(FindCounterAccountSeeds { owner: seed_path("owner") }))]
61+
pub counter: Init<Seeded<WrappedCounter<'info>>>,
4662
#[account_set(system_program)]
4763
pub system_program: Program<'info, SystemProgram>,
4864
}
@@ -81,15 +97,15 @@ impl StarFrameInstruction for CreateCounterIx {
8197
}
8298
}
8399

84-
#[derive(BorshSerialize, BorshDeserialize, Debug)]
85-
pub struct UpdateCounterSignerIx {}
100+
#[derive(BorshSerialize, BorshDeserialize, Debug, InstructionToIdl)]
101+
pub struct UpdateCounterSignerIx;
86102

87103
#[derive(AccountSet, Debug)]
88104
#[validate(extra_validation = self.validate())]
89105
pub struct UpdateCounterSignerAccounts<'info> {
90106
pub signer: Signer<SystemAccount<'info>>,
91107
pub new_signer: SystemAccount<'info>,
92-
pub counter: Writable<DataAccount<'info, CounterAccount>>,
108+
pub counter: Mut<DataAccount<'info, CounterAccount>>,
93109
}
94110

95111
impl<'info> UpdateCounterSignerAccounts<'info> {
@@ -127,7 +143,7 @@ impl StarFrameInstruction for UpdateCounterSignerIx {
127143
}
128144
}
129145

130-
#[derive(BorshSerialize, BorshDeserialize, Debug)]
146+
#[derive(BorshSerialize, BorshDeserialize, Debug, InstructionToIdl)]
131147
pub struct CountIx {
132148
pub amount: u64,
133149
pub subtract: bool,
@@ -137,7 +153,7 @@ pub struct CountIx {
137153
#[validate(extra_validation = self.validate())]
138154
pub struct CountAccounts<'info> {
139155
pub owner: Signer<SystemAccount<'info>>,
140-
pub counter: Writable<DataAccount<'info, CounterAccount>>,
156+
pub counter: Mut<DataAccount<'info, CounterAccount>>,
141157
}
142158

143159
impl<'info> CountAccounts<'info> {
@@ -178,17 +194,17 @@ impl StarFrameInstruction for CountIx {
178194
}
179195
}
180196

181-
#[derive(BorshSerialize, BorshDeserialize, Debug)]
182-
pub struct CloseCounterIx {}
197+
#[derive(BorshSerialize, BorshDeserialize, Debug, InstructionToIdl)]
198+
pub struct CloseCounterIx;
183199

184200
#[derive(AccountSet, Debug)]
185201
pub struct CloseCounterAccounts<'info> {
186202
#[validate(arg = &self.counter.data()?.signer)]
187203
pub signer: Signer<SystemAccount<'info>>,
188204
#[account_set(recipient)]
189-
pub funds_to: Writable<SystemAccount<'info>>,
205+
pub funds_to: Mut<SystemAccount<'info>>,
190206
#[cleanup(arg = CloseAccountAuto)]
191-
pub counter: Writable<DataAccount<'info, CounterAccount>>,
207+
pub counter: Mut<WrappedCounter<'info>>,
192208
}
193209

194210
impl StarFrameInstruction for CloseCounterIx {
@@ -212,7 +228,7 @@ impl StarFrameInstruction for CloseCounterIx {
212228
}
213229
}
214230

215-
#[star_frame_instruction_set]
231+
#[derive(InstructionSet)]
216232
pub enum CounterInstructionSet {
217233
CreateCounter(CreateCounterIx),
218234
UpdateSigner(UpdateCounterSignerIx),
@@ -222,10 +238,10 @@ pub enum CounterInstructionSet {
222238

223239
#[derive(StarFrameProgram)]
224240
#[program(
225-
instruction_set = CounterInstructionSet<'static>,
226-
id = "Coux9zxTFKZpRdFpE4F7Fs5RZ6FdaURdckwS61BUTMG",
241+
instruction_set = CounterInstructionSet,
242+
id = "Coux9zxTFKZpRdFpE4F7Fs5RZ6FdaURdckwS61BUTMG",
227243
)]
228-
pub struct CounterProgram {}
244+
pub struct CounterProgram;
229245

230246
#[cfg(test)]
231247
mod tests {
@@ -238,8 +254,15 @@ mod tests {
238254
use solana_sdk::system_program;
239255
use solana_sdk::transaction::Transaction;
240256
use star_frame::itertools::Itertools;
257+
use star_frame::serde_json;
241258
use star_frame::solana_program::instruction::AccountMeta;
242259

260+
#[test]
261+
fn idl_test() {
262+
let idl = CounterProgram::program_to_idl().unwrap();
263+
println!("{}", serde_json::to_string_pretty(&idl).unwrap());
264+
}
265+
243266
#[tokio::test]
244267
async fn test_that_it_works() {
245268
let program_test = ProgramTest::new(

0 commit comments

Comments
 (0)