1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! Execution context accessors for contract runtime information.
//!
//! This module provides functions to query the current execution context,
//! including caller information, block data, and call parameters.
//!
//! # Available Context
//!
//! - **Caller**: Who invoked this contract
//! - **Owner**: Who deployed/owns this contract
//! - **Contract ID**: This contract's address
//! - **Block data**: Current height and timestamp
//! - **Call value**: Native tokens sent with the call
//! - **Calldata**: Function selector and arguments
//!
//! # Example
//!
//! ```ignore
//! use truthlinked_sdk::context;
//!
//! fn handle_execute() -> Result<()> {
//! let caller = context::caller()?;
//! let owner = context::owner()?;
//!
//! // Only owner can call this function
//! if caller != owner {
//! return Err(Error::new(ERR_UNAUTHORIZED));
//! }
//!
//! let height = context::block_height()?;
//! let value = context::call_value()?;
//!
//! // Process transaction...
//! Ok(())
//! }
//! ```
extern crate alloc;
use vec;
use Vec;
use crateenv;
use crateResult;
/// Type alias for 32-byte account identifiers.
pub type AccountId = ;
/// Returns the account ID of the transaction sender.
///
/// This is the original caller who initiated the transaction, not necessarily
/// the immediate caller in case of cross-contract calls.
///
/// # Returns
///
/// The 32-byte account ID of the caller.
///
/// # Example
///
/// ```ignore
/// let caller = context::caller()?;
/// if caller == owner {
/// // Caller is the owner
/// }
/// ```
/// Returns the account ID of the contract owner.
///
/// The owner is set at deployment time and can be transferred via ownership
/// transfer mechanisms. Typically used for access control.
///
/// # Returns
///
/// The 32-byte account ID of the contract owner.
///
/// # Example
///
/// ```ignore
/// let owner = context::owner()?;
/// let caller = context::caller()?;
///
/// if caller != owner {
/// return Err(Error::new(ERR_NOT_OWNER));
/// }
/// ```
/// Returns this contract's account ID.
///
/// Useful for:
/// - Self-referential operations
/// - Checking if caller is this contract (for callbacks)
/// - Logging contract address
///
/// # Returns
///
/// The 32-byte account ID of the current contract.
///
/// # Example
///
/// ```ignore
/// let my_address = context::contract_id()?;
/// log::emit_event(&ContractDeployed { address: my_address })?;
/// ```
/// Returns the current block height.
///
/// Block height increments with each finalized batch (typically every 200ms).
/// Useful for time-based logic, expiration checks, and replay protection.
///
/// # Returns
///
/// The current block height as a `u64`.
///
/// # Example
///
/// ```ignore
/// let current_height = context::block_height()?;
/// let expiration = storage.read_expiration()?;
///
/// if current_height > expiration {
/// return Err(Error::new(ERR_EXPIRED));
/// }
/// ```
/// Returns the current block timestamp (Unix seconds).
///
/// This is the timestamp of the current batch being executed.
/// Useful for time-based logic, but note that it updates in ~200ms intervals.
///
/// # Returns
///
/// Unix timestamp in seconds as a `u64`.
///
/// # Example
///
/// ```ignore
/// let now = context::block_timestamp()?;
/// let deadline = storage.read_deadline()?;
///
/// if now > deadline {
/// return Err(Error::new(ERR_DEADLINE_PASSED));
/// }
/// ```
/// Returns the amount of native tokens sent with this call.
///
/// Only non-zero when called via `call_contract_with_value()`.
/// The value is transferred before contract execution begins.
///
/// # Returns
///
/// The amount of native tokens (in base units) sent with the call.
///
/// # Example
///
/// ```ignore
/// let value = context::call_value()?;
///
/// if value < MINIMUM_DEPOSIT {
/// return Err(Error::new(ERR_INSUFFICIENT_VALUE));
/// }
///
/// // Credit the caller's balance
/// balances.insert(&caller, &value)?;
/// ```
/// Returns the raw calldata (function selector + arguments).
///
/// Calldata format:
/// - Bytes 0-3: Function selector (4 bytes)
/// - Bytes 4+: Encoded arguments
///
/// # Returns
///
/// A `Vec<u8>` containing the complete calldata.
///
/// # Example
///
/// ```ignore
/// let calldata = context::calldata()?;
/// let selector = abi::selector(&calldata)?;
///
/// if selector == abi::selector_of("transfer") {
/// let to = abi::read_account(&calldata, 4)?;
/// let amount = abi::read_u64(&calldata, 36)?;
/// // Handle transfer...
/// }
/// ```
/// Sets the return data for this contract call.
///
/// The return data is sent back to the caller and can be decoded by the client.
/// Typically used to return function results (balances, status codes, etc.).
///
/// # Arguments
///
/// * `data` - The bytes to return to the caller
///
/// # Example
///
/// ```ignore
/// // Return a u64 balance
/// let balance = 1000u64;
/// context::set_return_data(&balance.to_le_bytes())?;
///
/// // Return multiple values (encode them first)
/// let mut encoder = Encoder::new();
/// encoder.push_u64(balance);
/// encoder.push_bool(is_active);
/// context::set_return_data(encoder.as_slice())?;
/// ```