【问题标题】:Error loading workspace IDL for Solana Anchor Testing为 Solana 锚测试加载工作区 IDL 时出错
【发布时间】:2022-07-22 00:44:05
【问题描述】:

由于某种原因,我的 mocha 测试无法找到我的 IDL。我收到以下错误:

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)

它已正确生成并保存在我的目标 IDL 文件夹 (../solana-anchor-reactjs-payment/target/idl/payment.json) 中,以下是 IDL 文件的内容:

{
  "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"
  }
}

调用此 IDL 的测试是以下测试 (../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);
    });
});

测试可能无法访问 IDL 所在的文件夹。所以我什至将它物理复制到与测试相同的文件夹中,但这仍然没有成功。对此的任何帮助将不胜感激,在此先感谢您。

更新 这并不理想,但您可以通过解析 idl json 文件然后将其作为程序中的对象传递来解决此问题。您将添加以下行来设置程序:

// 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);

【问题讨论】:

    标签: typescript solana solana-web3js anchor-solana


    【解决方案1】:

    我也遇到了类似的问题,您需要仔细检查并确保正确命名和引用所有内容。对我来说,问题出在程序的 Cargo 文件中,特别是我将库命名错误,它需要与程序同名,并且大小写错误

    [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"
    

    这是一个带有程序的 lib.rs 示例

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

    【讨论】:

    • 感谢队友这有帮助!我的情况也一样。
    【解决方案2】:

    我有一个类似的问题,在运行测试时,它工作了,但它给了我错误:在程序的 IDL 中找不到元数据属性,在这种情况下,它不是暴露的相同错误,它是 Anchor 的版本在 Cargo.toml 中,我更新了,一切正常

    【讨论】:

      猜你喜欢
      • 2022-07-08
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 2022-01-06
      • 2021-05-02
      • 2023-01-31
      • 2018-09-30
      • 1970-01-01
      相关资源
      最近更新 更多