Skip to main content

Debugging Contracts on OP_NET

Debugging smart contracts on OP_NET is essential for ensuring functionality and identifying issues during development. OP_NET provides tools like Blockchain.log for contract-level logging, specifically designed for development environments.

Key Features
  • Simple and Intuitive: Easily log strings or computed values within the contract.
  • Real-Time Output: Logs are accessible during contract execution, providing immediate feedback.
  • Development-Only: Logging is only available in development environments, ensuring that production contracts are not affected.

Method

Blockchain.log(data: string): void;
  • Parameters:
    • data: string: The message or value to log within the contract.

How to Use Blockchain.log

import { Blockchain, OP_NET } from "@btc-vision/btc-runtime/runtime";

class MyContract extends OP_NET {
public someFunction(): void {
Blockchain.log("Function execution started.");

const someValue = 42;
Blockchain.log(`Computed value: ${someValue}`);

// Add more logs as needed
Blockchain.log("Function execution completed.");
}
}

Example Usage

  1. Tracking Execution Flow:
Blockchain.log("Step 1: Initializing variables...");
Blockchain.log("Step 2: Performing calculations...");
Blockchain.log("Step 3: Finalizing results...");
  1. Debugging Contract State:
const currentBalance = 100n;
Blockchain.log(`Current balance: ${currentBalance}`);

Best Practices

  • Only use Blockchain.log for debugging and tracking purposes in development environments.
  • Use logging strategically to capture critical points in your contract's execution.
  • Ensure logs are meaningful and provide enough context for debugging.