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

idl: Fix using Pubkey constants with seeds::program #3559

Conversation

acheroncrypto
Copy link
Collaborator

Problem

Using Pubkey constants with seeds::program:

const PUBKEY_CONST: Pubkey = pubkey!("4LVUJzLugULF1PemZ1StknKJEEtJM6rJZaGijpNqCouG");

#[derive(Accounts)]
pub struct PubkeyConst<'info> {
    #[account(
        seeds = [],
        seeds::program = PUBKEY_CONST,
        bump
    )]
    pub acc: UncheckedAccount<'info>,
}

results in the following compile error:

error[E0277]: the trait bound `std::vec::Vec<u8>: From<anchor_lang::prelude::Pubkey>` is not satisfied
   --> programs/pda-derivation/src/lib.rs:233:10
    |
233 | #[derive(Accounts)]
    |          ^^^^^^^^ the trait `From<anchor_lang::prelude::Pubkey>` is not implemented for `std::vec::Vec<u8>`
    |
    = help: the following other types implement trait `From<T>`:
              `std::vec::Vec<u8>` implements `From<&str>`
              `std::vec::Vec<u8>` implements `From<ByteString>`
              `std::vec::Vec<u8>` implements `From<CString>`
              `std::vec::Vec<u8>` implements `From<std::string::String>`
    = note: required for `anchor_lang::prelude::Pubkey` to implement `Into<std::vec::Vec<u8>>`
    = note: this error originates in the derive macro `Accounts` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `pda-derivation` (lib test) due to 1 previous error
Error: Building IDL failed. Run `ANCHOR_LOG=true anchor idl build` to see the logs.

This is because we don't have a reliable way to decide the seed expression's actual type inside macros. As a workaround, the IDL generation assumes the type to be Pubkey if the identifier ends with ID:

// Not all types can be converted to `Vec<u8>` with `.into` call e.g. `Pubkey`.
// This is problematic for `seeds::program` but a hacky way to handle this
// scenerio is to check whether the last segment of the path ends with `ID`.
let seed = path
.path
.segments
.last()
.filter(|seg| seg.ident.to_string().ends_with("ID"))
.map(|_| quote! { #seed.as_ref() })
.unwrap_or_else(|| quote! { #seed });

However, this means if people don't append the ID suffix, it likely won't compile (e.g. #3558).

Summary of changes

Fix using Pubkey constants with seeds::program in IDL generation by using AsRef::<[u8]>::as_ref before using Into::into.

Fixes #3558

Copy link

vercel bot commented Feb 13, 2025

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

A member of the Team first needs to authorize it.

@acheroncrypto acheroncrypto added idl related to the IDL, either program or client side fix Bug fix PR labels Feb 13, 2025
@acheroncrypto acheroncrypto merged commit 7ab0bec into coral-xyz:master Feb 13, 2025
0 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix PR idl related to the IDL, either program or client side
Projects
None yet
Development

Successfully merging this pull request may close these issues.

seeds::program no longer works with constants
1 participant