4

由于某种原因,我的 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);
4

0 回答 0