coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.36k stars 1.25k forks source link

`the trait 'anchor_client::anchor_lang::ToAccountMetas' is not implemented` when using declare_program! with anchor_client #2991

Closed esolskjaer closed 1 month ago

esolskjaer commented 1 month ago

I tried to create a transaction using declare_program! for accounts and args and anchor_client for building the transaction:

use anchor_lang::prelude::*;
declare_program!(my_program);
use my_program::client::accounts::MyInstruction as MyInstructionAccs;
use my_program::client::args::MyInstruction as MyInstructionArgs;
use anchor_client::Program;
use anchor_lang::prelude::*;
use solana_sdk::signer::Signer;

pub fn send_startup_tx<C: Deref<Target = impl Signer> + Clone>(
    p: Program<C>,
    acc: Pubkey,
    param: u32,
) {
    let accs = MyInstructionAccs {
        acc1: acc,
    };
    let args = MyInstructionArgs { 
        arg1: param,
    };
    let a = p.request().accounts(accs).args(args)
}

This unfortunately doesn't build, with the following error

the trait bound `__client_accounts_my_instruction::MyInstruction: anchor_client::anchor_lang::ToAccountMetas` is not satisfied

If I do cargo expand, I can also see why: declare_program! generates an impl for anchor_lang::ToAccountMetas, but not for anchor_client::anchor_lang::ToAccountMetas even though these two are the exact same in practice.

I've tried switching my import at the top to use anchor_client::anchor_lang::declare_program; but it still gives the same error. Any idea on how to fix this?

acheroncrypto commented 1 month ago

This could happen if you use different versions of anchor-client and anchor-lang, otherwise it should resolve to the same type since anchor-client just re-exports anchor-lang:

https://github.com/coral-xyz/anchor/blob/bcf3862ef758755e4a93bc381227bc97fe5afbf9/client/src/lib.rs#L105

esolskjaer commented 1 month ago

Wow, I could've sworn I was using the same version for both, but turns out I wasn't. Thanks for the quick answer & sorry about this stupid question 🫠