Icon SunFilledIcon MoonStars

Icon LinkWorking with Contract Logs

When you log a value within a contract method, it generates a log entry that is added to the log receipt, and the variable type is recorded in the contract's ABI. The SDK enables you to parse these values into TypeScript types.

Consider the following example contract:

contract;
 
use std::logging::log;
 
abi LogValues {
fn log_values(val1: u64, val2: b256, val3: str[4], val4: [u8; 3]);
}
 
impl LogValues for Contract {
fn log_values(val1: u64, val2: b256, val3: str[4], val4: [u8; 3]) {
	log(val1);
	log(val2);
	log(val3);
	log(val4);
}
}

To access the logged values in TypeScript, use the logs property in the FunctionInvocationResult of a contract call result. The logs data will be stored in an Array<any>:

const value1 = 500;
const value2 = '0xef86afa9696cf0dc6385e2c407a6e159a1103cefb7e2ae0636fb33d3cb2a9e4a';
const value3 = 'Fuel';
const value4 = [1, 2, 3];
 
const { logs } = await contract.functions.log_values(value1, value2, value3, value4).call();
 
expect(new BN(logs[0]).toNumber()).toBe(value1);
expect(logs[1]).toBe(value2);
expect(logs[2]).toBe(value3);
expect(logs[3]).toEqual(value4);

This approach allows you to work seamlessly with logged values in your contract, making it easier to understand and debug the contract's behavior.

Was this page helpful?