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
//! Cross-contract call utilities for contract composability.
//!
//! This module provides functions to invoke other contracts from within your contract,
//! enabling composability and modular contract design.
//!
//! # Features
//!
//! - **Legacy calls**: Basic cross-contract invocation without value transfer
//! - **Value transfer**: Call contracts while transferring native tokens
//! - **Return data**: Capture and process return values from called contracts
//!
//! # Example
//!
//! ```ignore
//! use truthlinked_sdk::call;
//!
//! // Call another contract
//! let token_contract = [0x12; 32];
//! let calldata = encode_transfer_call(recipient, amount);
//! let result = call::call_contract(&token_contract, &calldata)?;
//!
//! // Call with value transfer
//! let result = call::call_contract_with_value(
//! &contract_id,
//! &calldata,
//! 1000, // Transfer 1000 base units
//! )?;
//! ```
extern crate alloc;
use vec;
use Vec;
use crateenv;
use crateResult;
/// Type alias for 32-byte account identifiers.
///
/// Account IDs in TruthLinked are 32-byte arrays, typically BLAKE3 hashes
/// of public keys or deterministic contract addresses.
pub type AccountId = ;
/// Invokes another contract without transferring value.
///
/// This is the legacy cross-contract call mechanism. The called contract
/// executes with the same caller context (the original transaction sender).
///
/// # Arguments
///
/// * `contract_id` - The 32-byte address of the contract to call
/// * `calldata` - Encoded function selector and arguments
///
/// # Returns
///
/// * `Ok(Vec<u8>)` - The return data from the called contract
/// * `Err(Error)` - If the call fails or the contract doesn't exist
///
/// # Gas
///
/// The called contract shares the remaining gas budget of the current execution.
///
/// # Example
///
/// ```ignore
/// use truthlinked_sdk::call;
///
/// let token_contract = [0x12; 32];
/// let calldata = encode_balance_of(account);
/// let balance_bytes = call::call_contract(&token_contract, &calldata)?;
/// let balance = decode_u128(&balance_bytes);
/// ```
/// Invokes another contract while transferring native tokens.
///
/// This is the v2 cross-contract call mechanism that supports value transfer.
/// The specified amount of native tokens is transferred from the calling contract
/// to the called contract before execution.
///
/// # Arguments
///
/// * `contract_id` - The 32-byte address of the contract to call
/// * `calldata` - Encoded function selector and arguments
/// * `value` - Amount of native tokens to transfer (in base units)
///
/// # Returns
///
/// * `Ok(Vec<u8>)` - The return data from the called contract
/// * `Err(Error)` - If the call fails, contract doesn't exist, or insufficient balance
///
/// # Errors
///
/// This function will fail if:
/// - The calling contract has insufficient balance
/// - The called contract doesn't exist
/// - The called contract reverts
/// - Gas is exhausted
///
/// # Example
///
/// ```ignore
/// use truthlinked_sdk::call;
///
/// // Call a payable function with 1000 tokens
/// let result = call::call_contract_with_value(
/// &contract_id,
/// &calldata,
/// 1000,
/// )?;
/// ```