Logging in OP_NET Smart Contracts

When developing smart contracts on the OP_NET metaprotocol, understanding how to use logs within your contract code is essential for debugging and gaining insights into the contract's behavior during transactions. This guide will show you how to use the Blockchain.log() method inside your contracts to output relevant information during execution.


How Blockchain.log() Works

Blockchain.log() is a utility provided within the OP_NET runtime library that allows you to print information directly from your smart contract. Unlike traditional debugging methods, Blockchain.log() will output logs to the transaction execution environment, making it visible when a transaction is executed or tested. This is particularly useful for tracking interactions and identifying potential issues in real-time.


Important: Blockchain.log() should only be used in regtest environments. If you use it in mainnet, the transaction will revert due to the restrictions on logging in production environments.


Example with airdrop Method

The example below demonstrates how Blockchain.log() is used within the airdrop() method to track important events and actions during the method's execution:

private airdrop(calldata: Calldata): BytesWriter {
    Blockchain.log(Blockchain.tx.sender + ' is calling airdrop');

    this.onlyOwner(Blockchain.tx.sender);

    const drops: Map<Address, u256> = calldata.readAddressValueTuple();

    const addresses: Address[] = drops.keys();
    for (let i: i32 = 0; i < addresses.length; i++) {
        const address = addresses[i];
        const amount = drops.get(address);

        Blockchain.log('Airdropping ' + amount.toString() + ' to ' + address);

        this._mint(address, amount, false);
    }

    const writer: BytesWriter = new BytesWriter(1);
    writer.writeBoolean(true);

    return writer;
}

Understanding the Example

The code utilizes Blockchain.log() to log relevant information during the execution of the smart contract. Here’s how it works:

  • Logging the Sender: The first log captures the address initiating the method call using Blockchain.tx.sender. This is useful for verifying that the correct entity is interacting with the contract.

  • Logging Each Airdrop: During the iteration over the addresses array, the contract logs the amount and recipient of each airdrop. This ensures that every airdrop action is tracked and visible for auditing or debugging.

Debugging with Blockchain.log()

If a certain condition fails or the method doesn’t behave as expected, adding Blockchain.log() statements before and after critical operations helps pinpoint where the contract might be encountering issues.

Running and Viewing Logs

To see the logs in action, deploy and interact with the contract in a regtest environment or use the opnet-unit-test repository for unit testing (GitHub repository). When a transaction calls these methods, the logs will be displayed in the transaction result, making it easy to trace the flow and understand what’s happening inside the contract.

Do not use Blockchain.log() in a mainnet environment, as it will cause the transaction to revert. Keep logging for testing and development only.

Last updated