Skip to main content

op_context

The op_context namespace provides information about the current operation context, including block information, transaction details, and signer verification. For complete API documentation, see the op_context reference in the Standard Library.

note

You can only use op_context in an operation or a function called from an operation, but not in a query. Use op_context.exists to check if the code is running in an operation context.

Overview

The op_context namespace includes:

  • Block informationblock_height, last_block_time for accessing blockchain state
  • Transaction detailstransaction, op_index for information about the current transaction and operation
  • Signer verificationget_signers, is_signer for checking transaction signatures
  • Operation accessget_all_operations, get_current_operation for inspecting transaction operations
  • Event emissionemit_event for communication with Postchain components

Key functions and fields

Example: Verify transaction signer

operation transfer(to: text, amount: integer) {
val sender_pubkey = x'036d6caac248af96f6afa7f904f550253a0f3ef3f5aa2fe6838a95b216691468e2';

// Check if the expected public key signed this transaction
require(op_context.is_signer(sender_pubkey), "Unauthorized: sender must sign the transaction");

// Perform transfer logic
// ...
}

Example: Access block and operation information

operation log_operation() {
print("Block height: %d".format(op_context.block_height));
print("Operation index: %d".format(op_context.op_index));
print("Number of signers: %d".format(op_context.get_signers().size()));
print("Last block time: %d".format(op_context.last_block_time));
}

Example: Get all operations in a transaction

operation process_batch() {
val all_ops = op_context.get_all_operations();

for (op in all_ops) {
print("Operation: %s".format(op.name));
}
}
warning

You must not use op_context.transaction.block because, except block_height, its attributes are null, so reading them gives a run-time error. Use op_context.block_height and op_context.last_block_time instead.