Skip to content

Commit 8625084

Browse files
authored
Merge pull request #71 from staratlasmeta/stegaBOB/feat/idl-wip2
Feat: Idl part 2
2 parents 5fd1bad + 90527b4 commit 8625084

File tree

29 files changed

+877
-325
lines changed

29 files changed

+877
-325
lines changed

framework/example_programs/counter/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use star_frame::__private::macro_prelude::IdlAccountSetDef;
12
use star_frame::anyhow::bail;
23
use star_frame::borsh::{BorshDeserialize, BorshSerialize};
34
use star_frame::derive_more::{Deref, DerefMut};
45
use star_frame::prelude::*;
56
use star_frame::solana_program::pubkey::Pubkey;
7+
use star_frame::star_frame_idl::IdlDefinition;
68

79
#[derive(Align1, Pod, Zeroable, Copy, Clone, Debug, Eq, PartialEq, ProgramAccount)]
810
#[program_account(seeds = CounterAccountSeeds)]
@@ -16,6 +18,7 @@ pub struct CounterAccount {
1618
}
1719

1820
#[derive(AccountSet, Deref, DerefMut, Debug)]
21+
#[account_set(skip_default_idl)]
1922
#[cleanup(generics = [<A> where DataAccount<'info, CounterAccount>: AccountSetCleanup<'info, A>], arg = A)]
2023
#[validate(generics = [<A> where DataAccount<'info, CounterAccount>: AccountSetValidate<'info, A>], arg = A)]
2124
pub struct WrappedCounter<'info>(
@@ -25,8 +28,17 @@ pub struct WrappedCounter<'info>(
2528
DataAccount<'info, CounterAccount>,
2629
);
2730

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+
}
38+
}
39+
2840
#[derive(Debug, GetSeeds, Clone)]
29-
#[seed_const(b"COUNTER")]
41+
#[get_seeds(seed_const = b"COUNTER")]
3042
pub struct CounterAccountSeeds {
3143
pub owner: Pubkey,
3244
}
@@ -45,6 +57,7 @@ pub struct CreateCounterAccounts<'info> {
4557
CreateIfNeeded(()),
4658
Seeds(CounterAccountSeeds { owner: *self.owner.key(), }),
4759
))]
60+
#[idl(arg = Seeds(FindCounterAccountSeeds { owner: seed_path("owner") }))]
4861
pub counter: Init<Seeded<WrappedCounter<'info>>>,
4962
#[account_set(system_program)]
5063
pub system_program: Program<'info, SystemProgram>,

framework/example_programs/faction_enlistment/src/lib.rs

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ use star_frame::borsh;
55
use star_frame::borsh::{BorshDeserialize, BorshSerialize};
66
use star_frame::prelude::*;
77

8-
use star_frame::star_frame_idl::ty::{IdlEnumVariant, IdlType, IdlTypeDef, IdlTypeId};
9-
use star_frame::star_frame_idl::{item_source, IdlDefinition, ItemInfo};
10-
118
#[derive(StarFrameProgram)]
129
#[program(
1310
instruction_set = FactionEnlistmentInstructionSet
@@ -81,6 +78,11 @@ pub struct ProcessEnlistPlayer<'info> {
8178
Seeds(PlayerFactionAccountSeeds {
8279
player_account: *self.player_account.key()
8380
})))]
81+
#[idl(
82+
arg = Seeds(FindPlayerFactionAccountSeeds {
83+
player_account: seed_path("player_account")
84+
})
85+
)]
8486
pub player_faction_account: Init<Seeded<DataAccount<'info, PlayerFactionData>>>,
8587
/// The player account
8688
#[account_set(funder)]
@@ -114,8 +116,9 @@ pub struct PlayerFactionData {
114116
Eq,
115117
PartialEq,
116118
Default,
119+
TypeToIdl,
117120
)]
118-
#[borsh(crate = "borsh")]
121+
#[borsh(crate = "borsh", use_discriminant = true)]
119122
#[repr(u8)]
120123
pub enum FactionId {
121124
#[default]
@@ -124,70 +127,10 @@ pub enum FactionId {
124127
Ustur,
125128
}
126129

127-
impl TypeToIdl for FactionId {
128-
type AssociatedProgram = crate::StarFrameDeclaredProgram;
129-
fn type_to_idl(idl_definition: &mut IdlDefinition) -> Result<IdlTypeDef> {
130-
let source = item_source::<Self>();
131-
let type_def = IdlTypeDef::Enum(vec![
132-
IdlEnumVariant {
133-
name: "MUD".to_string(),
134-
discriminant: vec![0],
135-
type_def: None,
136-
},
137-
IdlEnumVariant {
138-
name: "ONI".to_string(),
139-
discriminant: vec![1],
140-
type_def: None,
141-
},
142-
IdlEnumVariant {
143-
name: "USTUR".to_string(),
144-
discriminant: vec![2],
145-
type_def: None,
146-
},
147-
]);
148-
let idl_type = IdlType {
149-
info: ItemInfo {
150-
name: "FactionId".to_string(),
151-
description: vec![],
152-
source: source.clone(),
153-
},
154-
type_def,
155-
generics: vec![],
156-
};
157-
let namespace = idl_definition.add_type(idl_type, Self::AssociatedProgram::PROGRAM_ID);
158-
Ok(IdlTypeDef::Defined(IdlTypeId {
159-
namespace,
160-
source,
161-
provided_generics: vec![],
162-
}))
163-
}
164-
}
165-
166130
unsafe impl Zeroable for FactionId {}
167131

168-
// impl AccountToIdl for PlayerFactionData {
169-
// fn account_to_idl(idl_definition: &mut IdlDefinition) -> Result<IdlAccountId> {
170-
// let source = item_source::<Self>();
171-
// let idl_account = IdlAccount {
172-
// discriminant: Self::discriminant_bytes(),
173-
// seeds: Some(IdlSeeds(vec![
174-
// IdlSeed::Const(b"FACTION_ENLISTMENT".into()),
175-
// IdlSeed::Variable(IdlVariableSeed {
176-
// name: "player_account".to_string(),
177-
// description: vec![],
178-
// ty: <Pubkey>::type_to_idl(idl_definition)?,
179-
// }),
180-
// ])),
181-
// type_def: <PlayerFactionData>::type_to_idl(idl_definition)?,
182-
// };
183-
// let namespace =
184-
// idl_definition.add_account(idl_account, Self::AssociatedProgram::PROGRAM_ID)?;
185-
// Ok(IdlAccountId { namespace, source })
186-
// }
187-
// }
188-
189132
#[derive(Debug, GetSeeds, Clone)]
190-
#[seed_const(b"FACTION_ENLISTMENT")]
133+
#[get_seeds(seed_const = b"FACTION_ENLISTMENT")]
191134
pub struct PlayerFactionAccountSeeds {
192135
player_account: Pubkey,
193136
}

framework/star_frame/src/__private/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub mod macro_prelude {
22
pub use crate::account_set::{
3-
AccountSet, CanInitAccount, CanSetSeeds, HasOwnerProgram, HasProgramAccount, HasSeeds,
4-
ProgramAccount, SignedAccount, SingleAccountSet, WritableAccount,
3+
AccountSet, CanInitAccount, CanSetSeeds, GetSeeds, HasOwnerProgram, HasProgramAccount,
4+
HasSeeds, ProgramAccount, Seed, SignedAccount, SingleAccountSet, WritableAccount,
55
};
66
pub use crate::instruction::{
77
Instruction, InstructionDiscriminant, InstructionSet, StarFrameInstruction,
@@ -18,8 +18,8 @@ pub mod macro_prelude {
1818

1919
#[cfg(feature = "idl")]
2020
pub use crate::idl::{
21-
AccountSetToIdl, AccountToIdl, InstructionSetToIdl, InstructionToIdl, ProgramToIdl,
22-
TypeToIdl,
21+
seed_const, seed_path, AccountSetToIdl, AccountToIdl, FindIdlSeeds, FindSeed,
22+
InstructionSetToIdl, InstructionToIdl, ProgramToIdl, SeedsToIdl, TypeToIdl,
2323
};
2424

2525
#[cfg(feature = "idl")]
@@ -28,6 +28,7 @@ pub mod macro_prelude {
2828
account_set::{IdlAccountSet, IdlAccountSetDef, IdlAccountSetId, IdlAccountSetStructField},
2929
instruction::{IdlInstruction, IdlInstructionDef},
3030
item_source,
31+
seeds::{IdlFindSeed, IdlFindSeeds, IdlSeed, IdlSeeds},
3132
ty::{IdlEnumVariant, IdlStructField, IdlType, IdlTypeDef, IdlTypeId},
3233
IdlDefinition, IdlDefinitionReference, ItemInfo, Version,
3334
};

framework/star_frame/src/account_set/data_account.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,11 @@ mod idl_impl {
126126
idl_definition: &mut IdlDefinition,
127127
arg: A,
128128
) -> Result<IdlAccountSetDef> {
129-
Ok(IdlAccountSetDef::ProgramAccount {
130-
account_set: Box::new(<AccountInfo<'info>>::account_set_to_idl(
131-
idl_definition,
132-
arg,
133-
)?),
134-
account_id: T::account_to_idl(idl_definition)?,
135-
})
129+
let mut set = <AccountInfo<'info>>::account_set_to_idl(idl_definition, arg)?;
130+
set.single()?
131+
.program_accounts
132+
.push(T::account_to_idl(idl_definition)?);
133+
Ok(set)
136134
}
137135
}
138136
}

framework/star_frame/src/account_set/impls/account_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub mod idl_impl {
220220
_idl_definition: &mut IdlDefinition,
221221
_arg: (),
222222
) -> Result<IdlAccountSetDef> {
223-
Ok(IdlAccountSetDef::SingleAccount(IdlSingleAccountSet {
223+
Ok(IdlAccountSetDef::Single(IdlSingleAccountSet {
224224
program_accounts: vec![],
225225
seeds: None,
226226
address: None,

framework/star_frame/src/account_set/impls/option.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ mod idl_impl {
203203
use star_frame_idl::account_set::IdlAccountSetDef;
204204
use star_frame_idl::IdlDefinition;
205205

206+
// todo: figure out our optionals for IDLs. Thinking we should remove our separate decode
207+
// strategies and just use the program id method. This would make using option much simpler on
208+
// arg side and be more in line with how the rest of the ecosystem handles optionals.
206209
impl<'info, A, Arg> AccountSetToIdl<'info, Arg> for Option<A>
207210
where
208211
A: AccountSetToIdl<'info, Arg>,
@@ -211,10 +214,14 @@ mod idl_impl {
211214
idl_definition: &mut IdlDefinition,
212215
arg: Arg,
213216
) -> Result<IdlAccountSetDef> {
214-
let inner = A::account_set_to_idl(idl_definition, arg)?;
217+
let mut set = A::account_set_to_idl(idl_definition, arg)?;
218+
if let Ok(inner) = set.single() {
219+
inner.optional = true;
220+
return Ok(set);
221+
}
215222
Ok(IdlAccountSetDef::Or(vec![
216-
IdlAccountSetDef::Struct(vec![]),
217-
inner,
223+
set,
224+
IdlAccountSetDef::empty_struct(),
218225
]))
219226
}
220227
}

framework/star_frame/src/account_set/modifiers/mutable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ mod idl_impl {
3737
idl_definition: &mut IdlDefinition,
3838
arg: A,
3939
) -> Result<IdlAccountSetDef> {
40-
T::account_set_to_idl(idl_definition, arg)
41-
.map(Box::new)
42-
.map(IdlAccountSetDef::Writable)
40+
let mut set = T::account_set_to_idl(idl_definition, arg)?;
41+
set.single()?.writable = true;
42+
Ok(set)
4343
}
4444
}
4545
}

0 commit comments

Comments
 (0)