⌘K

Icon SunFilledIcon MoonStars
Debugging With Scripts

Icon LinkDebugging with Scripts

In every aspect of development, trade-offs are inevitable. As previously mentioned, logging is not feasible when dealing with predicates, since predicates are required to be pure. This raises an important question: how do we debug predicates?

Sway, a programming language, categorizes programs into four types, with scripts being one of them. Unlike predicates, scripts allow for shared logic.

Let's move outside our MultiSig project

cd ../..

and create a separate project called predicate-script-logging.

forc new --predicate predicate-script-logging

Copy and paste this new predicate in your src/main.sw. Attempting to build this predicate will result in an error, indicating that logging is an invalid operation.

predicate;
 
use std::{
    logging::log,
};
 
configurable {
    SECRET_NUMBER: u64 = 777
}
 
fn main() -> bool {
    log(SECRET_NUMBER);
    return true;
}

However, let's try switching the program type from a predicate to a script.

script;

Your code should now look like this:

script;
 
use std::{
    logging::log,
};
 
configurable {
    SECRET_NUMBER: u64 = 777
}
 
fn main() -> bool {
    log(SECRET_NUMBER);
    return true;
}

Now, if we attempt to build our script, it should compile without any issues.

forc build

Next, we'll generate a Rust template to see it in action!