Skip to content

Commit d265288

Browse files
authored
Merge pull request #74 from staratlasmeta/stegaBOB/feat/idl-finishing-touches
Feat: idl finishing touches
2 parents 2889ebe + d6f38d6 commit d265288

File tree

20 files changed

+658
-58
lines changed

20 files changed

+658
-58
lines changed

Cargo.lock

Lines changed: 11 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
@@ -18,6 +18,7 @@ star_frame_idl = { path = "framework/star_frame_idl", features = ["verifier"] }
1818
star_frame_proc = { path = "framework/star_frame_proc" }
1919
advance = { path = "advance", features = ["solana"] }
2020
anyhow = "1.0.75"
21+
anchor-lang-idl-spec = "0.1.0"
2122
array-init = "2.1.0"
2223
bincode = { version = "1.3.3", features = ["i128"] }
2324
borsh = "1.3.1"

framework/star_frame/src/__private/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ pub mod macro_prelude {
3030
item_source,
3131
seeds::{IdlFindSeed, IdlFindSeeds, IdlSeed, IdlSeeds},
3232
ty::{IdlEnumVariant, IdlStructField, IdlType, IdlTypeDef, IdlTypeId},
33-
IdlDefinition, IdlDefinitionReference, ItemInfo, Version,
33+
CrateMetadata, IdlDefinition, IdlDefinitionReference, ItemInfo, Version,
3434
};
3535
}

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

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,18 @@ impl<'a, 'info> AccountSetCleanup<'info, ()> for &'a AccountInfo<'info> {
183183
#[cfg(feature = "idl")]
184184
pub mod idl_impl {
185185
use super::*;
186-
use crate::idl::AccountSetToIdl;
186+
use crate::idl::{AccountSetToIdl, FindIdlSeeds};
187+
use crate::prelude::Seeds;
187188
use star_frame_idl::account_set::{IdlAccountSetDef, IdlSingleAccountSet};
189+
use star_frame_idl::seeds::IdlFindSeeds;
188190
use star_frame_idl::IdlDefinition;
189191

190192
impl<'info> AccountSetToIdl<'info, ()> for AccountInfo<'info> {
191193
fn account_set_to_idl(
192194
_idl_definition: &mut IdlDefinition,
193195
_arg: (),
194196
) -> Result<IdlAccountSetDef> {
195-
Ok(IdlAccountSetDef::Single(IdlSingleAccountSet {
196-
program_accounts: vec![],
197-
seeds: None,
198-
address: None,
199-
writable: false,
200-
signer: false,
201-
optional: false,
202-
is_init: false,
203-
}))
197+
Ok(IdlAccountSetDef::Single(IdlSingleAccountSet::default()))
204198
}
205199
}
206200
impl<'info> AccountSetToIdl<'info, Pubkey> for AccountInfo<'info> {
@@ -209,16 +203,49 @@ pub mod idl_impl {
209203
arg: Pubkey,
210204
) -> Result<IdlAccountSetDef> {
211205
Ok(IdlAccountSetDef::Single(IdlSingleAccountSet {
212-
program_accounts: vec![],
213-
seeds: None,
214206
address: Some(arg),
215-
writable: false,
216-
signer: false,
217-
optional: false,
218-
is_init: false,
207+
..Default::default()
208+
}))
209+
}
210+
}
211+
212+
impl<'info, T> AccountSetToIdl<'info, Seeds<(T, Pubkey)>> for AccountInfo<'info>
213+
where
214+
T: FindIdlSeeds,
215+
{
216+
fn account_set_to_idl(
217+
_idl_definition: &mut IdlDefinition,
218+
arg: Seeds<(T, Pubkey)>,
219+
) -> Result<IdlAccountSetDef> {
220+
let (seeds, program) = arg.0;
221+
Ok(IdlAccountSetDef::Single(IdlSingleAccountSet {
222+
seeds: Some(IdlFindSeeds {
223+
seeds: T::find_seeds(&seeds)?,
224+
program: Some(program),
225+
}),
226+
..Default::default()
219227
}))
220228
}
221229
}
230+
231+
impl<'info, T> AccountSetToIdl<'info, Seeds<T>> for AccountInfo<'info>
232+
where
233+
T: FindIdlSeeds,
234+
{
235+
fn account_set_to_idl(
236+
_idl_definition: &mut IdlDefinition,
237+
arg: Seeds<T>,
238+
) -> Result<IdlAccountSetDef> {
239+
Ok(IdlAccountSetDef::Single(IdlSingleAccountSet {
240+
seeds: Some(IdlFindSeeds {
241+
seeds: T::find_seeds(&arg.0)?,
242+
program: None,
243+
}),
244+
..Default::default()
245+
}))
246+
}
247+
}
248+
222249
impl<'a, 'info, A> AccountSetToIdl<'info, A> for &'a AccountInfo<'info>
223250
where
224251
AccountInfo<'info>: AccountSetToIdl<'info, A>,

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ pub use mutable::*;
1111
pub use seeded::*;
1212
pub use signer::*;
1313

14-
// TODO: Add macros to make propagating the marker traits easier.
15-
1614
/// A marker trait that indicates the underlying account is a signer
1715
pub trait SignedAccount<'info>: SingleAccountSet<'info> {
1816
/// Gets the seeds of the account if it is seeded.

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::account_set::{AccountSet, SignedAccount, SingleAccountSet};
1+
use crate::account_set::{AccountSet, CanSetSeeds, SignedAccount, SingleAccountSet};
22

3+
use crate::prelude::SyscallInvoke;
34
use crate::Result;
45
use derive_more::{Deref, DerefMut};
56
use solana_program::account_info::AccountInfo;
@@ -11,7 +12,7 @@ use std::fmt::Debug;
1112
#[validate(
1213
extra_validation = self.check_signer(),
1314
)]
14-
pub struct Signer<T>(#[single_account_set(skip_signed_account)] pub(crate) T);
15+
pub struct Signer<T>(#[single_account_set(skip_signed_account, skip_can_set_seeds)] pub(crate) T);
1516

1617
pub type SignerInfo<'info> = Signer<AccountInfo<'info>>;
1718

@@ -24,6 +25,16 @@ where
2425
}
2526
}
2627

28+
// CanSetSeeds on Signer is a no-op
29+
impl<'info, T> CanSetSeeds<'info, ()> for Signer<T>
30+
where
31+
Self: SingleAccountSet<'info>,
32+
{
33+
fn set_seeds(&mut self, _arg: &(), _syscalls: &mut impl SyscallInvoke<'info>) -> Result<()> {
34+
Ok(())
35+
}
36+
}
37+
2738
#[cfg(feature = "idl")]
2839
mod idl_impl {
2940
use super::*;

framework/star_frame/src/idl/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use star_frame_idl::account_set::IdlAccountSetDef;
44
use star_frame_idl::instruction::IdlInstructionDef;
55
use star_frame_idl::seeds::IdlSeeds;
66
use star_frame_idl::ty::IdlTypeDef;
7-
use star_frame_idl::{IdlDefinition, IdlMetadata, Version};
7+
use star_frame_idl::{CrateMetadata, IdlDefinition, IdlMetadata};
88
pub use star_frame_proc::{InstructionToIdl, TypeToIdl};
99

1010
mod find_seeds;
@@ -41,7 +41,7 @@ pub trait SeedsToIdl: GetSeeds {
4141
}
4242

4343
pub trait ProgramToIdl: StarFrameProgram {
44-
fn version() -> Version;
44+
fn crate_metadata() -> CrateMetadata;
4545

4646
fn program_to_idl() -> Result<IdlDefinition>
4747
where
@@ -50,7 +50,7 @@ pub trait ProgramToIdl: StarFrameProgram {
5050
let mut out = IdlDefinition {
5151
address: Self::PROGRAM_ID,
5252
metadata: IdlMetadata {
53-
version: Self::version(),
53+
crate_metadata: Self::crate_metadata(),
5454
..Default::default()
5555
},
5656
..Default::default()

framework/star_frame/src/program/system_program.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::prelude::*;
22
use borsh::BorshDeserialize;
33
use solana_program::system_program;
4+
use star_frame_idl::CrateMetadata;
45
use system_instruction_impl::SystemInstructionSet;
56

67
/// Solana's system program.
@@ -15,8 +16,16 @@ impl StarFrameProgram for SystemProgram {
1516

1617
#[cfg(feature = "idl")]
1718
impl ProgramToIdl for SystemProgram {
18-
fn version() -> star_frame_idl::Version {
19-
star_frame_idl::Version::new(1, 18, 10)
19+
fn crate_metadata() -> CrateMetadata {
20+
CrateMetadata {
21+
version: star_frame_idl::Version::new(1, 18, 10),
22+
name: "system_program".to_string(),
23+
docs: vec![],
24+
description: None,
25+
homepage: None,
26+
license: None,
27+
repository: None,
28+
}
2029
}
2130
}
2231

framework/star_frame_idl/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ edition.workspace = true
55
publish.workspace = true
66

77
[features]
8-
default = []
8+
default = ["anchor"]
99
verifier = ["anyhow"]
10+
anchor = ["anchor-lang-idl-spec"]
1011

1112
[dependencies]
1213
# verifier
14+
anchor-lang-idl-spec = { workspace = true, optional = true }
1315
anyhow = { workspace = true, optional = true }
1416
bincode = { workspace = true }
1517
derivative = { workspace = true }

framework/star_frame_idl/src/account_set.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::account::IdlAccountId;
22
use crate::seeds::IdlFindSeeds;
3-
use crate::serde_base58_pubkey_option;
43
use crate::ty::IdlTypeDef;
4+
use crate::{serde_base58_pubkey_option, IdlDefinition};
55
use crate::{IdlGeneric, ItemInfo};
66
use crate::{ItemDescription, ItemSource};
77
use anyhow::bail;
@@ -58,6 +58,24 @@ pub enum IdlAccountSetDef {
5858
}
5959

6060
impl IdlAccountSetDef {
61+
pub fn assert_defined(&self) -> anyhow::Result<&IdlAccountSetId> {
62+
match self {
63+
IdlAccountSetDef::Defined(id) => Ok(id),
64+
_ => bail!("Expected defined account set, found {:?}", self),
65+
}
66+
}
67+
68+
pub fn get_defined<'a>(
69+
&self,
70+
idl_definition: &'a IdlDefinition,
71+
) -> anyhow::Result<&'a IdlAccountSet> {
72+
let source = &self.assert_defined()?.source;
73+
idl_definition
74+
.account_sets
75+
.get(source)
76+
.ok_or_else(|| anyhow::anyhow!("Account set `{source}` not found in definition"))
77+
}
78+
6179
pub fn empty_struct() -> Self {
6280
IdlAccountSetDef::Struct(vec![])
6381
}

0 commit comments

Comments
 (0)