Skip to main content

Module 0x2::tx_context

use 0x1::option;

Struct TxContext

Information about the transaction currently being executed. This cannot be constructed by a transaction--it is a privileged object created by the VM and passed in to the entrypoint of the transaction as &mut TxContext.

struct TxContext has drop
Click to open
Fields
sender: address
The address of the user that signed the current transaction
tx_hash: vector<u8>
Hash of the current transaction
epoch: u64
The current epoch number
epoch_timestamp_ms: u64
Timestamp that the epoch started at
ids_created: u64
Counter recording the number of fresh id's created while executing this transaction. Always 0 at the start of a transaction

Constants

const EUnsupportedFunction: u64 = 2;

const NATIVE_CONTEXT: bool = false;

Function sender

Return the address of the user that signed the current transaction

public fun sender(self: &tx_context::TxContext): address
Click to open
Implementation
public fun sender(self: &TxContext): address {
    if (NATIVE_CONTEXT) {
        native_sender()
    } else {
        self.sender
    }
}

Function digest

Return the transaction digest (hash of transaction inputs). Please do not use as a source of randomness.

public fun digest(self: &tx_context::TxContext): &vector<u8>
Click to open
Implementation
public fun digest(self: &TxContext): &vector<u8> {
    if (NATIVE_CONTEXT) {
        native_digest()
    } else {
        &self.tx_hash
    }
}

Function epoch

Return the current epoch

public fun epoch(self: &tx_context::TxContext): u64
Click to open
Implementation
public fun epoch(self: &TxContext): u64 {
    if (NATIVE_CONTEXT) {
        native_epoch()
    } else {
        self.epoch
    }
}

Function epoch_timestamp_ms

Return the epoch start time as a unix timestamp in milliseconds.

public fun epoch_timestamp_ms(self: &tx_context::TxContext): u64
Click to open
Implementation
public fun epoch_timestamp_ms(self: &TxContext): u64 {
    if (NATIVE_CONTEXT) {
        native_epoch_timestamp_ms()
    } else {
        self.epoch_timestamp_ms
    }
}

Function sponsor

public fun sponsor(_self: &tx_context::TxContext): option::Option<address>
Click to open
Implementation
public fun sponsor(_self: &TxContext): Option<address> {
    assert!(NATIVE_CONTEXT, EUnsupportedFunction);
    native_sponsor()
}

Function fresh_object_address

Create an address that has not been used. As it is an object address, it will never occur as the address for a user. In other words, the generated address is a globally unique object ID.

public fun fresh_object_address(ctx: &mut tx_context::TxContext): address
Click to open
Implementation
public fun fresh_object_address(ctx: &mut TxContext): address {
    let ids_created = ctx.ids_created();
    let id = derive_id(*ctx.digest(), ids_created);
    ctx.increment_ids_created();
    id
}

Function increment_ids_created

fun increment_ids_created(self: &mut tx_context::TxContext)
Click to open
Implementation
fun increment_ids_created(self: &mut TxContext) {
    if (NATIVE_CONTEXT) {
        self.native_inc_ids_created()
    } else {
        self.ids_created = self.ids_created + 1
    }
}

Function ids_created

Return the number of id's created by the current transaction. Hidden for now, but may expose later

fun ids_created(self: &tx_context::TxContext): u64
Click to open
Implementation
fun ids_created(self: &TxContext): u64 {
    if (NATIVE_CONTEXT) {
        native_ids_created()
    } else {
        self.ids_created
    }
}

Function derive_id

Native function for deriving an ID via hash(tx_hash || ids_created)

fun derive_id(tx_hash: vector<u8>, ids_created: u64): address
Click to open
Implementation
native fun derive_id(tx_hash: vector<u8>, ids_created: u64): address;

Function native_sender

fun native_sender(): address
Click to open
Implementation
native fun native_sender(): address;

Function native_digest

fun native_digest(): &vector<u8>
Click to open
Implementation
native fun native_digest(): &vector<u8>;

Function native_epoch

fun native_epoch(): u64
Click to open
Implementation
native fun native_epoch(): u64;

Function native_epoch_timestamp_ms

fun native_epoch_timestamp_ms(): u64
Click to open
Implementation

Function native_sponsor

fun native_sponsor(): option::Option<address>
Click to open
Implementation
native fun native_sponsor(): Option<address>;

Function native_ids_created

fun native_ids_created(): u64
Click to open
Implementation
native fun native_ids_created(): u64;

Function native_inc_ids_created

fun native_inc_ids_created(self: &mut tx_context::TxContext)
Click to open
Implementation
native fun native_inc_ids_created(self: &mut TxContext);