typescript 加载Solana锚测试的工作空间IDL时出错

57hvy0tb  于 2023-02-13  发布在  TypeScript
关注(0)|答案(3)|浏览(162)

由于某种原因,我的摩卡测试无法找到我的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所在的文件夹。所以我甚至物理地将其复制到与测试相同的文件夹中,但这仍然没有成功。对此的任何帮助都将不胜感激,提前感谢。

Update这并不理想,但你可以通过解析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);
4xy9mtcn

4xy9mtcn1#

我也遇到过类似的问题,你需要仔细检查并确保你的命名和引用都是正确的。对我来说,这个问题是在程序的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 {
...
}
uyto3xhc

uyto3xhc2#

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

bq3bfh9z

bq3bfh9z3#

如果您使用锚检查您的锚。toml文件可能存在拼写错误的程序名称。

相关问题