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

✨ Macro to define and access extra accounts #26

Merged
merged 5 commits into from
Mar 14, 2024

Conversation

GabrielePicco
Copy link
Contributor

@GabrielePicco GabrielePicco commented Mar 13, 2024

✨ Macro to define and access extra accounts

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

Problem

See #7 for details

Solution

Example

The example below show a system which defines 3 extra accounts:

#[system]
pub mod system_apply_velocity {
    pub fn execute(ctx: Context<Components>, _args: Vec<u8>) -> Result<Components>  {
        // ...
        Ok(ctx.accounts)
    }


    #[system_input]
    pub struct Components {
        pub velocity: Velocity,
        pub position: Position,
    }

    #[extra_accounts]
    pub struct ExtraAccounts {
        #[account(address = bolt_lang::solana_program::sysvar::clock::id())]
        pub sysvar_clock: AccountInfo,
        #[account(address = pubkey!("6LHhFVwif6N9Po3jHtSmMVtPjF6zRfL3xMosSzcrQAS8"))]
        pub some_extra_account: AccountInfo,
        #[account(address = Metadata::id())]
        pub program_metadata: Program<Metadata>,
    }
}

The system can define the logic, using the injected helper functions to access the extra accounts:

#[system]
pub mod system_apply_velocity {
    pub fn execute(ctx: Context<Components>, _args: Vec<u8>) -> Result<Components>  {
         // Access the clock account
         let sysvar_clock_account_info = ctx.sysvar_clock()?;
         clock = Clock::from_account_info(sysvar_clock_account_info)?;
        ...
        // Access an extra account
        let extra_account = ctx.some_extra_account()?;
        ...
        Ok(ctx.accounts)
    }
}

The IDL of the system will contain the extra accounts and their address

Comments

  • Clients can use the IDL to automatically retrieve the extra accounts, similarly to tlv-account-resolution for token extensions
...
"accounts": [
        {
          "name": "sysvarClock",
          "address": "SysvarC1ock11111111111111111111111111111111"
        },
        {
          "name": "someExtraAccount",
          "address": "6LHhFVwif6N9Po3jHtSmMVtPjF6zRfL3xMosSzcrQAS8"
        },
        {
          "name": "programMetadata",
          "address": "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
        }
 ],
 ...
  • Additionally, needed AccountMetas could be saved onchain (in a PDA), following the token extensions standard

Issues

Closes #7

@GabrielePicco GabrielePicco merged commit 68b3c0a into main Mar 14, 2024
4 checks passed
@GabrielePicco GabrielePicco deleted the feat/propagate-remaining-accounts branch March 14, 2024 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Propagate accounts to the systems
1 participant