javascript 测试失败->无法读取未定义的属性(阅读“rpc”)

q3aa0525  于 2022-11-27  发布在  Java
关注(0)|答案(2)|浏览(103)

我正在用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。有人能帮忙吗?

hmmo2u0o

hmmo2u0o1#

program未定义。这意味着此行代码存在问题

const program = Anchor.workspace.calculator;

锚将合约名称大写,因此应为“Calculator”

const program = Anchor.workspace.Calculator;
roejwanj

roejwanj2#

从@project-serum/anchor导入AnchorProvider并将提供程序分配给它,如下所示:

const provider = AnchorProvider.local()

我认为这应该可以解决问题。如果你的systemProgram有错误,用类似的方式从锚导入web 3。

相关问题