2

I was following this guide to learn anchor: https://www.quicknode.com/guides/solana-development/anchor/transfer-tokens

When deploying the program via Solana Playground (as instructed), everything runs fine. The issue comes from trying to replicate on my local machine using anchor. The code is exactly the same; however I was running into this issue when trying to invoke the transferLamports function:

    'Program log: Instruction: TransferLamports',
    'Program log: AnchorError caused by account: to. Error Code: ConstraintMut. Error Number: 2000. Error Message: A mut constraint was violated.',

After much, much frustration I discovered that the issue is with the IDL that Anchor generates when building the program. Comparing to the one that Solana Playground generates (remember - exact same lib.rs):

Solana Playground

"accounts":[
{"name":"from","isMut":true,"isSigner":true},
{"name":"to","isMut":true,"isSigner":false},
{"name":"systemProgram","isMut":false,"isSigner":false}],
"args":[{"name":"amount","type":"u64"}]}]

Local Anchor

      "accounts": [
        {
          "name": "payer",
          "writable": true,
          "signer": true
        },
        {
          "name": "recipient",
          "writable": true
        },
        {
          "name": "system_program",
          "address": "11111111111111111111111111111111"
        }
      ],
      "args": [
        {
          "name": "amount",
          "type": "u64"
        }
      ]

Pulling down the IDL from Solana Playground and using that in my Typescript module instead allows the code to run just fine.

    const sig = program.methods.transferLamports(new BN(10000)).accounts({
        from: from.publicKey,
        to: to.publicKey,
        system_program: system_program
    })

What is causing anchor to produce a completely different IDL type that isn't compatible with anchor itself?

2
  • Would you have a link to your program at github to be possible to check the code?
    – chalda
    Commented Apr 17 at 14:20
  • The code is available in the quicknode guide in the comment, haven't uploaded it to github but it's relatively short Commented Apr 17 at 14:41

2 Answers 2

6

This is because the IDL has been rewritten, and it's incompatible with the old IDL.

There is also a new release (v0.30.0) which was released 2 days ago. Solana Playground has not yet upgraded to the latest version, so if you use the IDL generated from there (or any other place), you can use the 0.29.0 version of @coral-xyz/anchor locally (instead of 0.30.0).

Copied from this GitHub answer.

0

For those who compiles anchor project in VS Code and then uploads IDL file to the Solana Playground or vice versa - for sake of compatibility make a small hack and downgrade your anchor version in two simple steps.

In Anchor.toml set lower toolchain version.

[toolchain]
anchor_version = "0.29.0" 

In Cargo.toml comment out idl-build feature section.

[features]
#idl-build = ["anchor-lang/idl-build", "anchor-spl/idl-build"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.