anchor: Anchor IDL different (incorrect?) from Solana Playground IDL generated

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?

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 18

Most upvoted comments

All you need is to cast the idl.json’s type to the generated camelCased IDL type in target/types. For example:

import * as idl from "./idl/spl_staking.json";
import type { SplStaking } from "./types/spl_staking";

const program = new anchor.Program(idl as unknown as SplStaking, provider);

or

import type { SplStaking } from "./types/spl_staking";

const idl: SplStaking = require("idl/spl_staking.json");
const program = new anchor.Program(idl, provider);

same problem, but i need use new anchor version but want to use solanaFM local idl (to help better debugging), so im try to make tools to convert my new idl to old idl, so now can compatible to use in solanaFM

https://github.com/viandwi24/anchor-idl-converter

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).

@acheroncrypto Yep. Got what I was doing wrong.

It’s the same if it says missing name and version properties. You’d get this if you used @coral-xyz/anchor 0.29.0 with a new IDL for example.

But eventually decided to revert back to the version of 0.29 for both anchor-lang and anchor-spl crates and it seemed to work perfectly fine.

The old IDL was better😔.

Before I go messing around with all my configuration, can you clarify for me if there is a version of the ‘npm’ anchor package which can correctly invoke program functions with this new IDL format? Any quick references on syntax?

It will work as long as you use the same Anchor version for all tooling (crates, packages, CLI).

See https://www.anchor-lang.com/release-notes/0.30.0#type-script