简体   繁体   中英

Error loading workspace IDL for Solana Anchor Testing

For some reason, my mocha test is unable to find my IDL. I am getting the error below:

name@Mabels-MacBook-Pro solana-anchor-reactjs-payment % anchor test
BPF SDK: /Users/name/.local/share/solana/install/releases/1.9.2/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
    Finished release [optimized] target(s) in 0.84s
cargo-build-bpf child: /Users/name/.local/share/solana/install/releases/1.9.2/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /Users/name/Solana/solana-anchor-reactjs-payment/target/deploy/solana_anchor_reactjs_payment.so

To deploy this program:
  $ solana program deploy /Users/name/Solana/solana-anchor-reactjs-payment/target/deploy/solana_anchor_reactjs_payment.so
The program address will default to this keypair (override with --program-id):
  /Users/name/Solana/solana-anchor-reactjs-payment/target/deploy/solana_anchor_reactjs_payment-keypair.json

Error: Error loading workspace IDL for solana_anchor_reactjs_payment
    at /Users/name/Solana/solana-anchor-reactjs-payment/node_modules/@project-serum/anchor/src/workspace.ts:101:13
    at Array.forEach (<anonymous>)
    at attachWorkspaceOverride (/Users/name/Solana/solana-anchor-reactjs-payment/node_modules/@project-serum/anchor/src/workspace.ts:90:31)
    at Object.get (/Users/name/Solana/solana-anchor-reactjs-payment/node_modules/@project-serum/anchor/src/workspace.ts:71:9)
    at Suite.<anonymous> (/Users/name/Solana/solana-anchor-reactjs-payment/tests/payment-test.ts:12:38)
    at Object.create (/Users/name/Solana/solana-anchor-reactjs-payment/node_modules/mocha/lib/interfaces/common.js:148:19)
    at context.describe.context.context (/Users/name/Solana/solana-anchor-reactjs-payment/node_modules/mocha/lib/interfaces/bdd.js:42:27)
    at Object.<anonymous> (/Users/name/Solana/solana-anchor-reactjs-payment/tests/payment-test.ts:7:1)

It was properly produced and saved in my target IDL folder (../solana-anchor-reactjs-payment/target/idl/payment.json), and below are the contents of the IDL file:

{
  "version": "0.0.0",
  "name": "payment",
  "instructions": [
    {
      "name": "sendPayment",
      "accounts": [
        {
          "name": "paymentState",
          "isMut": true,
          "isSigner": true
        },
        {
          "name": "sender",
          "isMut": true,
          "isSigner": true
        },
        {
          "name": "systemProgram",
          "isMut": false,
          "isSigner": false
        }
      ],
      "args": [
        {
          "name": "amount",
          "type": "u64"
        },
        {
          "name": "note",
          "type": "string"
        }
      ]
    }
  ],
  "accounts": [
    {
      "name": "PaymentState",
      "type": {
        "kind": "struct",
        "fields": [
          {
            "name": "timestamp",
            "type": "i64"
          },
          {
            "name": "sender",
            "type": "publicKey"
          },
          {
            "name": "receiver",
            "type": "publicKey"
          },
          {
            "name": "amount",
            "type": "u64"
          },
          {
            "name": "note",
            "type": "string"
          }
        ]
      }
    }
  ],
  "errors": [
    {
      "code": 300,
      "name": "WalletToWithdrawFromInvalid",
      "msg": "Wallet withdrawn from is not owned by the owner"
    },
    {
      "code": 301,
      "name": "AmountExceeded",
      "msg": "This program doesn't transfer over 99 sol's"
    },
    {
      "code": 302,
      "name": "NoteTooLong",
      "msg": "The note is exceeds 280 characters"
    }
  ],
  "metadata": {
    "address": "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
  }
}

The test that's calling this IDL is the test below (../solana-anchor-reactjs-payment/tests/payment-test.ts):

import * as anchor from '@project-serum/anchor';
import { Program } from '@project-serum/anchor';
import {Payment} from "../target/types/payment";
import * as assert from "assert";


describe('send payment message', () => {
    // Configure the client to use the local cluster.
    anchor.setProvider(anchor.Provider.env());


    const program = anchor.workspace.Payment as Program<Payment>;

    it('can send a new payment', async () => {
        // Call the "SendTweet" instruction.
        const payment = anchor.web3.Keypair.generate();
        await program.rpc.sendPayment('veganism', {
            accounts: {
                paymentState: payment.publicKey,
                sender: program.provider.wallet.publicKey,
                systemProgram: anchor.web3.SystemProgram.programId,
            },
            signers: [payment],
        });

        //Fetch the account details of the payment sender
        const senderAccount = await program.account.paymentState
            .fetch(payment.publicKey);

        assert.ok(senderAccount.timestamp);
        assert.equal(senderAccount.sender.toBase58(), program.provider.wallet.publicKey.toBase58())
        assert.ok(senderAccount.note, 'Starbucks Coffee');
        assert.ok(senderAccount.amount, '8');
        assert.fail(senderAccount.amount, 200);
    });
});

It could be possible that the test is unable to access the folder where the IDL resides. So I even physically copied it into the same folder as the test, but that still didn't work out. Any help on this would be appreciated, thank you in advance.

Update This isn't ideal but you can get around this by parsing the idl json file and then passing it in as an object in the program. You would add the following lines to setup the program:

// Read the generated IDL.
const idl = JSON.parse(
    require("fs").readFileSync("./target/idl/payment.json", "utf8")
);

//Address of the deployed program
const programId = new anchor.web3.PublicKey("8BBGEacFKQ1dYDPF39HstjAC2195iV1ta9scv1WxtJfT");

//Generate the program client from IDL
const program = new anchor.Program(idl, programId);

I had a similar issue as well, you need to go through and make sure you are naming and referencing everything correctly. For me the issue was in the Cargo file for the program, specifically I was naming the library wrong, it needed to have the same name as the program and it had the wrong capitalization

[package]
name = "program-name"
version = "0.1.0"
description = "Created with Anchor"
edition = "2018"

[lib]
crate-type = ["cdylib", "lib"]
name = "program_name". //Here was the issue needed to have the same name as the program itself

[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = "0.21.0"
anchor-spl = "0.21.0"

Here is an example of the lib.rs with the program

#[program]
pub mod program_name {
...
}

I had a similar problem, when running the test, it worked, but it gave me the Error: Metadata property not found in IDL of program, in this case it was not the same error exposed, it was the version of Anchor in the Cargo.toml, the I updated and everything ok

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM