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

feat: declare program allow overriding address #3435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Arrowana
Copy link
Contributor

@Arrowana Arrowana commented Dec 17, 2024

Problem:

A given application might need to target multiple environments with the same program interface, right now to do so it would need to rewrite the entire IDL editing the address field

Solution:
Implement address = "..." to override the IDL address

Potential issue, if any pda based on the program id is hardcoded at compilation time elsewhere it will not be compatible

Copy link

vercel bot commented Dec 17, 2024

@Arrowana is attempting to deploy a commit to the coral-xyz Team on Vercel.

A member of the Team first needs to authorize it.

@Arrowana Arrowana force-pushed the feat/declare-program-override-address branch from bc11aa1 to 5e8ddce Compare December 17, 2024 22:38
@Arrowana Arrowana force-pushed the feat/declare-program-override-address branch from 5e8ddce to cec825e Compare December 18, 2024 00:31
Copy link
Collaborator

@acheroncrypto acheroncrypto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should support this. However, I think we should avoid adding new arguments to the declare_program macro to keep things simple and make things work out of the box.

This is what I had in mind when adding the idl.metadata.deployments field, which is an optional field that stores the program ids in different clusters:

pub struct IdlDeployments {
pub mainnet: Option<String>,
pub testnet: Option<String>,
pub devnet: Option<String>,
pub localnet: Option<String>,
}

So we could use that information in declare_program! with feature names as the field names, e.g. if the program gets compiled with devnet feature activated, declare_program! impl would use the program id from idl.metadata.deployments.devnet.

This would also avoid having multiple declare_program! calls with different feature configurations, as this would be handled inside the macro impl automatically.

Wdyt?

@Arrowana
Copy link
Contributor Author

Yeah, we should support this. However, I think we should avoid adding new arguments to the declare_program macro to keep things simple and make things work out of the box.

This is what I had in mind when adding the idl.metadata.deployments field, which is an optional field that stores the program ids in different clusters:

pub struct IdlDeployments {
pub mainnet: Option<String>,
pub testnet: Option<String>,
pub devnet: Option<String>,
pub localnet: Option<String>,
}

So we could use that information in declare_program! with feature names as the field names, e.g. if the program gets compiled with devnet feature activated, declare_program! impl would use the program id from idl.metadata.deployments.devnet.

This would also avoid having multiple declare_program! calls with different feature configurations, as this would be handled inside the macro impl automatically.

Wdyt?

This works but is a bit rigid, what if there are 2 contracts on mainnet, production and staging? That's a likely case.

@acheroncrypto
Copy link
Collaborator

Each program has its own instance, so their IDLs are not guaranteed to be the same. You'll be able to achieve what you want with the help of the as keyword (will add):

#[cfg(feature = "production")]
declare_program!(name);
#[cfg(feature = "staging")]
declare_program!(name_staging as name);

Imo adding overrides should be used as the last resort, only if there are no other options available.

@acheroncrypto
Copy link
Collaborator

You can also create a common module to achieve the same thing:

mod name {
    #[cfg(feature = "production")]
    anchor_lang::declare_program!(name);
    #[cfg(feature = "staging")]
    anchor_lang::declare_program!(name_staging);

    #[cfg(feature = "production")]
    pub use name::*;
    #[cfg(feature = "staging")]
    pub use name_staging::*;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants