我正在用TypeScript编写一个用于将两个数字相加的测试。我收到以下错误:
Adds two numbers:
TypeError: Cannot read properties of undefined (reading 'rpc')
at Context.<anonymous> (tests/calculator.ts:33:19)
at Generator.next (<anonymous>)
at /mnt/e/solana/calculator/tests/calculator.ts:7:71
at new Promise (<anonymous>)
at __awaiter (tests/calculator.ts:3:12)
at Context.<anonymous> (tests/calculator.ts:35:16)
at processImmediate (node:internal/timers:473:21)
下面是代码:
const Anchor = require('@project-serum/anchor');
describe('calculator',() => {
const provider = Anchor.Provider.local();
Anchor.setProvider(provider);
const calculator = Anchor.web3.Keypair.generate();
const program = Anchor.workspace.calculator;
var _calculator;
it('Creates a calculator', async() => {
await program.rpc.create("Welcome to My calculator",{
accounts: {
calculator: calculator.publicKey,
user: provider.wallet.publicKey,
SystemProgram: SystemProgram.programId,
},
signers: [calculator]
});
const account = await
program.account.calculator.fetch(calculator.publicKey);
assert.ok(account.greeting === "Welcome to My calculator");
_calculator = calculator;
});
这是在Solana区块链上使用anchor-cli。有人能帮忙吗?
2条答案
按热度按时间hmmo2u0o1#
program
未定义。这意味着此行代码存在问题锚将合约名称大写,因此应为“Calculator”
roejwanj2#
从@project-serum/anchor导入AnchorProvider并将提供程序分配给它,如下所示:
我认为这应该可以解决问题。如果你的systemProgram有错误,用类似的方式从锚导入web 3。