metaplex-foundation / shank

Extracts IDL from Solana Rust contracts
https://docs.rs/crate/shank_macro/latest
116 stars 23 forks source link

[feat]: Add #[idl_instruction] annotation to allow Shank programs to export shank'd IDL on-chain #42

Open ngundotra opened 1 year ago

ngundotra commented 1 year ago

I think Shank would just need to feature flag the following code from anchor:

https://github.com/coral-xyz/anchor/blob/fa1249836e2b8a73d3cb9fb525634e789290f251/lang/syn/src/codegen/program/handlers.rs#L19

ngundotra commented 1 year ago

Ideally, shank would expose a macro that allowed me to redeploy my program with instructions to manage an on-chain IDL.

#[derive(ShankInstruction, ShankIdlDeploy)]
pub enum MyProgramInstruction {
  ...
}
// now if you fail to handle ShankIdlDeployParse -> fail in compiler
//  ShankIdlDeployParse

// now you add the following...
processor::parse_shank_idl_parse(program_id, &accounts, program_data)

// which then does the following
pub fn ....() {
let disc: ShankIdlInnerInstruction = parse(data)?;
match disc {
  ShankIdlInnerInstruction::CreateIdl =>
  shank::idl::create(program_id, &accounts, program_data)?
  ShankIdlInnerInstruction::CreateBuffer =>
  shank::idl::create_buffer(program_id, &accounts, program_data)?
  ShankIdlInnerInstruction::WriteIdl =>
  shank::idl::write(program_id, &accounts, program_data)?
  ShankIdlInnerInstruction::SetAuthority =>
  shank::idl::set_authority(program_id, &accounts, program_data)?
  ShankIdlInnerInstruction::SetBuffer =>
  shank::idl::set_buffer(program_id, &accounts, program_data)?
}

}

Anchor idl account is is derived via

const [key, bump] = PublicKey.findProgramAddressSync([], PROGRAM_ID);
PublicKey.createWithSeed(key, 'anchor:idl')

Here is the proof: part one and part two