0

I got started with writing smart contracts 2 days ago so please bear with me. The function emit_event emits an event. I want to call the function via a CPI form another program. I have written code for the CPI as well. When I do anchor test, I'm getting the error
IdlError: Type not found: {"name":"revent","type":{"defined":"RoosterEvent"}}

I checked the generated IDL and it has the revent type is present in there.

Following is my code for logger/lib.rs. This has the exposed function.

use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};

declare_id!("ENdMF5CR3qvWj2L6xZWofrYiYaQVV3dM94ghtbrvJ9QN");

#[program]
pub mod logger {
    use super::*;

    pub fn emit_event(_ctx: Context<SetRoosterEvent>, data: String, label: String) -> Result<()> {

        let m_data = data;
        let m_label = label;

        emit!(RoosterEvent {
            data: m_data.to_string(),
            label: m_label.to_string(),
        });

        msg!("HAHA");
        msg!(m_data.as_str());
        msg!(m_label.as_str());
        msg!("HAHA");
        
        Ok(())
    }
}

#[derive(Accounts, AnchorDeserialize, AnchorSerialize)]
pub struct SetRoosterEvent<'info> {
    #[account(mut)]
    pub event: Account<'info, TestEvent>,
}

#[account]
pub struct TestEvent {
    pub revent: RoosterEvent,
}

#[event]
#[derive(Clone, Debug, Default)]
pub struct RoosterEvent {
    pub data: String,
    pub label: String,
}

Following is my consumer/lib.rs that does the CPI call

use anchor_lang::prelude::*;
use logger::cpi::accounts::SetRoosterEvent;
use logger::program::Logger;
use logger::{self, TestEvent};

declare_id!("5M8M5VCtD3cDkqJJj3NmfgVwnrVj1t9NSpmdSB3XGKR6");

#[program]
pub mod consumer {
    use super::*;

    pub fn do_something(ctx: Context<DoSomething>) -> Result<()> {
        let cpi_program = ctx.accounts.logger_program.to_account_info().clone();
        let cpi_accounts = SetRoosterEvent {
            event: ctx.accounts.event.to_account_info().clone(),
        };
        let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
        logger::cpi::emit_event(cpi_ctx, "data".to_string(), "label".to_string())?;
        Ok(())
    }

}

#[derive(Accounts)]
pub struct DoSomething<'info> {
    #[account(mut)]
    pub event: Account<'info, TestEvent>,
    pub logger_program: Program<'info, Logger>,
}

Following is my test file

import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Logger } from "../target/types/logger";
import { Consumer } from "../target/types/consumer";

describe("logger", () => {
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);



  const program = anchor.workspace.Logger as Program<Logger>;
  const consumer = anchor.workspace.Consumer as Program<Consumer>;
  


  it("Does CPI", async()=>{

    await consumer.methods.doSomething().rpc();
    

  })

});

Edit: Here's a gist of the code.

3 Answers 3

2

When defining the struct, you need to use #[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)] so the struct can be properly deserialized and serialized to be used in the anchor account macro

0

The problem is that you are passing an event (RoosterEvent) to be held in an account (TestEvent). It seems that Anchor doesn't cover this corner case, because in the IDL RoosterEvent is defined in events but Anchor is looking for it in types within the IDL.

If you simply remove the #[event] macro attribute from RoosterEvent this error will be gone.

So to fix this, simply don't store an event (in this case RoosterEvent) in an account. Instantiate your event only in the code where you're actually emit!ing it, and pass the data you need to log in a different struct that is not an event.

8
  • I changed the TestEvent to #[account] pub struct TestEvent {pub ignore: u64,} and still the same error IdlError: Type not found: {"name":"event","type":{"defined":"Account<'info,TestEvent>"}} Commented Aug 22, 2023 at 17:07
  • RoosterEvent now only has #[event] and is only used in the emit! Commented Aug 22, 2023 at 17:09
  • Add a link with the code, on gist.github.com for example
    – Serban
    Commented Aug 22, 2023 at 19:46
  • Here's the gist gist.github.com/WilfredAlmeida/93cb327e55dbe6aff1fa05b416cb5961 Commented Aug 24, 2023 at 6:01
  • lib.rs is a ts file and SetRoosterEvent and RoosterEvent are missing
    – Serban
    Commented Aug 24, 2023 at 7:10
0

I had updated and made changes to using the new IDL spec from anchor but yarn was still using the older cached version of anchor 0.29.0

solutions that might help

yarn upgrade --latest

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.