I was writing a test in TypeScript for Adding two numbers. I am receiving the following error:
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)
Following is the code:
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;
});
This is working on Solana blockchain using anchor-cli. Can someone help?
program
is undefined. That means this line of code has issue
const program = Anchor.workspace.calculator;
Anchor capitalizes the contract name. so it should be "Calculator"
const program = Anchor.workspace.Calculator;
import AnchorProvider from @project-serum/anchor and assign provider to it instead, like:
const provider = AnchorProvider.local()
I think this should solve the problem. If you have an error with systemProgram, import web3 from anchor in a similar manner.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.