由于某种原因,我的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所在的文件夹。所以我甚至把它物理复制到测试的同一个文件夹中,但仍然没有成功。如有任何帮助,我们将不胜感激,并提前表示感谢。
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);
我也遇到了类似的问题,您需要仔细检查并确保正确命名和引用了所有内容。对我来说,这个问题出现在程序的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 {
...
}
我也遇到了类似的问题,在运行测试时,它起了作用,但它给了我错误:在程序的IDL中找不到元数据属性,在这种情况下,它不是暴露的相同错误,它是Cargo.toml中的Anchor版本,我更新了,一切正常
如果您正在使用Anchor,请检查您的Anchor.toml文件,可能存在程序名称拼写错误。