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 information –
block_height,last_block_timefor accessing blockchain state - Transaction details –
transaction,op_indexfor information about the current transaction and operation - Signer verification –
get_signers,is_signerfor checking transaction signatures - Operation access –
get_all_operations,get_current_operationfor inspecting transaction operations - Event emission –
emit_eventfor communication with Postchain components
Key functions and fields
op_context.block_height– Height of the block being builtop_context.last_block_time– Timestamp of the last block in millisecondsop_context.transaction– The transaction currently being builtop_context.exists– Check if the code is running in an operation contextop_context.op_index– Index of the operation within the transactionop_context.get_signers– Get all public keys that signed the transactionop_context.is_signer– Check if a specific public key signed the transactionop_context.get_all_operations– Get all operations in the current transactionop_context.get_current_operation– Get the current operationop_context.emit_event– Emit an event to Postchain components
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.