Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Dynamic types resolver #24

Merged
merged 4 commits into from
Mar 9, 2024
Merged

Conversation

GabrielePicco
Copy link
Contributor

@GabrielePicco GabrielePicco commented Mar 8, 2024

Dynamic Components Resolver

Status Type ⚠️ Core Change Issue
Ready Feature Yes #23

Motivation

Composability on Solana is currently limited by the fact that typically crates must be available in order to interact with a program. Although Rust crates can be generated automatically if the IDL is published, this process is usually time-consuming and error-prone, as the crates could become outdated. Furthermore, the ECS pattern offers a way to structure logic in a very decoupled way, where common data structures (components) can be reused within or outside a project. This also allows anyone to write or propose logic (system) very easily. Therefore, having a simple way to use components/accounts in a new program is essential to promote composability and reuse.

Solution

Introduce a built-in mechanism in Bolt to automatically generate Rust types for the components, using the IDL published on-chain. Automatically build the types when needed in a system.

Specification

  • Systems can specify within the struct, marked with #[system_input], the bundle of input components.
  • Components can use types that are dynamically generated, using the IDL retrieved from the component ID, via the component_id macro.
use bolt_lang::*;

declare_id!("FSa6qoJXFBR3a7ThQkTAMrC15p6NkchPEjBdd4n6dXxA");

#[system]
pub mod system_simple_movement {

    pub fn execute(ctx: Context<Components>, args_p: Vec<u8>) -> Result<Components> {
        ctx.accounts.position.x += 1;
        Ok(ctx.accounts)
    }

    #[system_input]
    pub struct Components {
        #[component_id("Fn1JzzEdyb55fsyduWS94mYHizGhJZuhvjX6DVvrmGbQ")]
        pub position: Position,
    }
}

Generation

  • The bolt build includes a pre-step that automatically parses all systems, identifies the missing types, and autogenerates the crates.
  • Running bolt build will use the default cluster (as specified in the Anchor.toml) to retrieve the components IDL, parse it and generate the rust code.
  • Running bolt build --provider.cluster devnet will use the specified cluster to retrieve the IDL.
  • Once types are generated, the crates will be available under crates/types. The crate dependency is automatically added to the systems that need it.
Screenshot 2024-03-08 at 15 50 32
  • Use bolt build --rebuild-types to force regeneration of the types (in case the IDL is updated on-chain).
Screenshot 2024-03-08 at 15 49 51
  • Autogenerated types can be used to operate on the component and define the system logic.
Screenshot 2024-03-08 at 15 53 38

Issues

Closes #23

@GabrielePicco GabrielePicco self-assigned this Mar 8, 2024
@GabrielePicco GabrielePicco added the enhancement New feature or request label Mar 8, 2024
@GabrielePicco GabrielePicco merged commit e802f53 into main Mar 9, 2024
4 checks passed
@GabrielePicco GabrielePicco deleted the feat/dynamic-types-resolver branch March 9, 2024 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

On-Chain IDL Integration for Component Usage Without Dependency on Crates
1 participant