0

I'm trying to call a simple hello_world method from my Solana Anchor program, but I keep getting this error:

TypeError: Cannot read properties of undefined (reading 'encode')
    at .../node_modules/@coral-xyz/anchor/dist/cjs/program/namespace/index.js:35:100

Here’s my JavaScript code:

import { Program, AnchorProvider, setProvider } from '@coral-xyz/anchor';
import { PublicKey, Connection, Keypair } from '@solana/web3.js';
import bs58 from 'bs58';
import idl from './operator.json';

const connection = new Connection("http://127.0.0.1:8899"); // Localnet
const scionx = Keypair.fromSecretKey(bs58.decode("my_secret_key"));

const provider = new AnchorProvider(connection, scionx, { commitment: "confirmed" });
setProvider(provider);

const programID = new PublicKey("CMjNnp9yvMUqKJF2jB6E5HHpktwhm1ax9rHzh5risdNK");
const program = new Program(idl, programID, provider);

const tx = await program.methods.helloWorld().accounts({
  user: scionx.publicKey
}).rpc();

console.log("Transaction signature:", tx);

Here’s the relevant part of my Rust contract:

use anchor_lang::prelude::*;

#[program]
pub mod operator {
    pub fn hello_world(_ctx: Context<HelloWorld>) -> Result<()> {
        msg!("Hello, World!");
        Ok(())
    }
}

#[derive(Accounts)]
pub struct HelloWorld<'info> {
    pub user: Signer<'info>,
}

The program deploys fine, but I get the error when trying to call hello_world. Any ideas what might be wrong? Thanks!

3 Answers 3

1
new Program(idl, programID, provider);  

===>

new Program(idl, provider);

The third parameter of Program should be a Coder, you passed your provider as a Coder.

Sign up to request clarification or add additional context in comments.

Comments

0

this line

.../node_modules/@coral-xyz/anchor/dist/cjs/program/namespace/index.js:35:100

has this code:

  const ixItem = instruction_js_1.default.build(idlIx, (ixName, ix) => coder.instruction.encode(ixName, ix), programId);

that means coder.instruction undefined.

upon checking docs

import { Idl, Coder } from "@coral-xyz/anchor";

import { SplMemoAccountsCoder } from "./accounts";
import { SplMemoEventsCoder } from "./events";
import { SplMemoInstructionCoder } from "./instructions";
import { SplMemoTypesCoder } from "./types";


export class SplMemoCoder implements Coder {
  readonly accounts: SplMemoAccountsCoder;
  readonly events: SplMemoEventsCoder;
  readonly instruction: SplMemoInstructionCoder;
  readonly types: SplMemoTypesCoder;

  constructor(idl: Idl) {
    this.accounts = new SplMemoAccountsCoder(idl);
    this.events = new SplMemoEventsCoder(idl);
    this.instruction = new SplMemoInstructionCoder(idl);
    this.types = new SplMemoTypesCoder(idl);
  }
}

you may not not be using correct idl

Comments

0

At first, second parameter for AnchorProvider is Wallet type, so replace like below.

 const provider = new AnchorProvider(connection, scionx, { commitment: "confirmed" });

=>

 const provider = new AnchorProvider(connection, new Wallet(scionx), { commitment: "confirmed" });

Second, Program has only two parameters and idl contains programid, so replace like below.

 const program = new Program(idl, programID, provider);

=>

const program = new Program(idle as Idl, provider);

In a nutshell, this is full code for your case.

import { Program, AnchorProvider, setProvider, Wallet, Idl } from '@coral-xyz/anchor';
import { Connection, Keypair } from '@solana/web3.js';
import idle from "../target/idl/test.json";

describe("test", async () => {

  it("Is initialized!", async () => {
    const secretkey=[];//"your secret key arrry";
    const connection = new Connection("https://api.devnet.solana.com"); // Devnet
    const scionx = Keypair.fromSecretKey(new Uint8Array(secretkey));
    const provider = new AnchorProvider(connection, new Wallet(scionx), { commitment: "confirmed" });
    setProvider(provider);
    const program = new Program(idle as Idl, provider);
    const tx = await program.methods.helloWorld().accounts({
      user: scionx.publicKey
    }).rpc();

    console.log("Transaction signature:", tx);
  });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.