0

I have code that's used both in my program and off-chain. In the code, I use msg!. If that msg! line is run off-chain, I get this error:

Invoke context not set!

What do I need to do to make msg! print to stdout instead of throwing this error?

I want to re-use my code. Duplicating it and modifying the new copy is unacceptable.

2 Answers 2

1

Depending on what you mean by "off-chain", ie. in a test or an application, you can set your own override for the syscall at the start of the program. Start by defining a type that implements the syscall stubs:

struct SyscallStubs {}
impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
    fn sol_log(&self, message: &str) {
        println!("{message}");
    }
}

And then at the start of your test / program, you can have your program use those stubs by invoking:

solana_sdk::program_stubs::set_syscall_stubs(Box::new(SyscallStubs {}));
0

I ended up defining this macro and using it in place of msg!:

#[macro_export]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
macro_rules! msg_or_println {                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
    ($($arg:tt)*) => {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]                                                                                                                                                                                                                                                                                                                                                                                                                                      
        {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
            println!($($arg)*);                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
        }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
        #[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]                                                                                                                                                                                                                                                                                                                                                                                                                                 
        {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
            msg!($($arg)*);                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
        }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
    };                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
}

Additionally, I had to suppress warnings about msg being an unused import. Unfortunately, this is required, since the warning will appear when compiling off-chain programs (a test, for example) yet the import is necessary when compiling on-chain programs.

#[allow(unused_imports)]
use solana_program::msg;

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.