diff --git a/sway-lib-std/src/tx.sw b/sway-lib-std/src/tx.sw index 08e42bd259b..f6fa6c74fce 100644 --- a/sway-lib-std/src/tx.sw +++ b/sway-lib-std/src/tx.sw @@ -85,8 +85,8 @@ pub fn tx_type() -> Transaction { } const TIP_POLICY: u32 = 1u32 << 0; -const MATURITY_POLICY: u32 = 1u32 << 1; -const WITNESS_LIMIT_POLICY: u32 = 1u32 << 2; +const WITNESS_LIMIT_POLICY: u32 = 1u32 << 1; +const MATURITY_POLICY: u32 = 1u32 << 2; const MAX_FEE_POLICY: u32 = 1u32 << 3; /// Returns policies bits. It can be used to identify which policies are set. @@ -151,14 +151,14 @@ pub fn script_gas_limit() -> u64 { /// use std::tx::tx_maturity; /// /// fn foo() { -/// let maturity = tx_maturity(); +/// let maturity = tx_maturity().unwrap(); /// log(maturity); /// } /// ``` -pub fn tx_maturity() -> Option { +pub fn tx_maturity() -> Option { let bits = policies(); if bits & MATURITY_POLICY > 0 { - Some(__gtf::(0, GTF_POLICY_MATURITY)) + Some(__gtf::(0, GTF_POLICY_MATURITY)) } else { None } @@ -218,11 +218,7 @@ pub fn tx_max_fee() -> Option { /// /// # Returns /// -/// * [u64] - The script length for the transaction. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The script length for the transaction. /// /// # Examples /// @@ -230,14 +226,14 @@ pub fn tx_max_fee() -> Option { /// use std::tx::tx_script_length; /// /// fn foo() { -/// let script_length = tx_script_length(); +/// let script_length = tx_script_length().unwrap(); /// assert(script_length > 0); /// } /// ``` -pub fn tx_script_length() -> u64 { +pub fn tx_script_length() -> Option { match tx_type() { - Transaction::Script => __gtf::(0, GTF_SCRIPT_SCRIPT_LENGTH), - Transaction::Create => revert(0), + Transaction::Script => Some(__gtf::(0, GTF_SCRIPT_SCRIPT_LENGTH)), + Transaction::Create => None, } } @@ -247,24 +243,20 @@ pub fn tx_script_length() -> u64 { /// /// * [u64] - The script data length for the transaction. /// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. -/// /// # Examples /// /// ```sway /// use std::tx::tx_script_data_length; /// /// fn foo() { -/// let script_data_length = tx_script_data_length(); +/// let script_data_length = tx_script_data_length().unwrap(); /// assert(script_data_length > 0); /// } /// ``` -pub fn tx_script_data_length() -> u64 { +pub fn tx_script_data_length() -> Option { match tx_type() { - Transaction::Script => __gtf::(0, GTF_SCRIPT_SCRIPT_DATA_LENGTH), - Transaction::Create => revert(0), + Transaction::Script => Some(__gtf::(0, GTF_SCRIPT_SCRIPT_DATA_LENGTH)), + Transaction::Create => None, } } @@ -299,7 +291,7 @@ pub fn tx_witnesses_count() -> u64 { /// /// # Returns /// -/// * [u64] - The pointer to the witness at index `index`. +/// * [Option] - The pointer to the witness at index `index`. /// /// # Examples /// @@ -307,14 +299,17 @@ pub fn tx_witnesses_count() -> u64 { /// use std::tx::tx_witness_pointer; /// /// fn foo() { -/// let witness_pointer = tx_witness_pointer(0); -/// log(witness_pointer); +/// let witness_pointer = tx_witness_pointer(0).unwrap(); /// } /// ``` -pub fn tx_witness_pointer(index: u64) -> u64 { +pub fn tx_witness_pointer(index: u64) -> Option { + if index >= tx_witnesses_count() { + return None + } + match tx_type() { - Transaction::Script => __gtf::(index, GTF_SCRIPT_WITNESS_AT_INDEX), - Transaction::Create => __gtf::(index, GTF_CREATE_WITNESS_AT_INDEX), + Transaction::Script => Some(__gtf::(index, GTF_SCRIPT_WITNESS_AT_INDEX)), + Transaction::Create => Some(__gtf::(index, GTF_CREATE_WITNESS_AT_INDEX)), } } @@ -326,7 +321,7 @@ pub fn tx_witness_pointer(index: u64) -> u64 { /// /// # Returns /// -/// * [u64] - The length of the witness data at `index`. +/// * [Option<64>] - The length of the witness data at `index`. /// /// # Examples /// @@ -334,12 +329,16 @@ pub fn tx_witness_pointer(index: u64) -> u64 { /// use std::tx::tx_witness_data_length; /// /// fn foo() { -/// let witness_data_length = tx_witness_data_length(0); +/// let witness_data_length = tx_witness_data_length(0).unwrap(); /// log(witness_data_length); /// } /// ``` -pub fn tx_witness_data_length(index: u64) -> u64 { - __gtf::(index, GTF_WITNESS_DATA_LENGTH) +pub fn tx_witness_data_length(index: u64) -> Option { + if index >= tx_witnesses_count() { + return None + } + + Some(__gtf::(index, GTF_WITNESS_DATA_LENGTH)) } /// Get the witness data at `index`. @@ -350,7 +349,7 @@ pub fn tx_witness_data_length(index: u64) -> u64 { /// /// # Returns /// -/// * [T] - The witness data at `index`. +/// * [Option] - The witness data at `index`. /// /// # Examples /// @@ -358,15 +357,19 @@ pub fn tx_witness_data_length(index: u64) -> u64 { /// use std::tx::tx_witness_data; /// /// fn foo() { -/// let witness_data: u64 = tx_witness_data(0); +/// let witness_data: u64 = tx_witness_data(0).unwrap(); /// log(witness_data); /// } /// ``` -pub fn tx_witness_data(index: u64) -> T { +pub fn tx_witness_data(index: u64) -> Option { + if index >= tx_witnesses_count() { + return None + } + if __size_of::() == 1 { - __gtf::(index, GTF_WITNESS_DATA).add::(7).read::() + Some(__gtf::(index, GTF_WITNESS_DATA).add::(7).read::()) } else { - __gtf::(index, GTF_WITNESS_DATA).read::() + Some(__gtf::(index, GTF_WITNESS_DATA).read::()) } } @@ -374,11 +377,7 @@ pub fn tx_witness_data(index: u64) -> T { /// /// # Returns /// -/// * [raw_ptr] - The transaction script start pointer. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The transaction script start pointer. /// /// # Examples /// @@ -386,14 +385,14 @@ pub fn tx_witness_data(index: u64) -> T { /// use std::tx::tx_script_start_pointer; /// /// fn foo() { -/// let script_start_pointer = tx_script_start_pointer(); +/// let script_start_pointer = tx_script_start_pointer().unwrap(); /// log(script_start_pointer); /// } /// ``` -pub fn tx_script_start_pointer() -> raw_ptr { +pub fn tx_script_start_pointer() -> Option { match tx_type() { - Transaction::Script => __gtf::(0, GTF_SCRIPT_SCRIPT), - _ => revert(0), + Transaction::Script => Some(__gtf::(0, GTF_SCRIPT_SCRIPT)), + _ => None, } } @@ -401,11 +400,7 @@ pub fn tx_script_start_pointer() -> raw_ptr { /// /// # Returns /// -/// * [raw_ptr] - The transaction script data start pointer. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The transaction script data start pointer. /// /// # Examples /// @@ -413,17 +408,14 @@ pub fn tx_script_start_pointer() -> raw_ptr { /// use std::tx::tx_script_data_start_pointer; /// /// fn foo() { -/// let script_data_start_pointer = tx_script_data_start_pointer(); +/// let script_data_start_pointer = tx_script_data_start_pointer().unwrap(); /// log(script_data_start_pointer); /// } /// ``` -pub fn tx_script_data_start_pointer() -> raw_ptr { +pub fn tx_script_data_start_pointer() -> Option { match tx_type() { - Transaction::Script => __gtf::(0, GTF_SCRIPT_SCRIPT_DATA), - _ => { - // transaction-create has no script data length - revert(0); - } + Transaction::Script => Some(__gtf::(0, GTF_SCRIPT_SCRIPT_DATA)), + _ => None, } } @@ -436,11 +428,7 @@ pub fn tx_script_data_start_pointer() -> raw_ptr { /// /// # Returns /// -/// * [T] - The script data, typed. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The script data, typed. /// /// # Examples /// @@ -452,10 +440,14 @@ pub fn tx_script_data_start_pointer() -> raw_ptr { /// log(script_data); /// } /// ``` -pub fn tx_script_data() -> T { +pub fn tx_script_data() -> Option { let ptr = tx_script_data_start_pointer(); + if ptr.is_none() { + return None + } + // TODO some safety checks on the input data? We are going to assume it is the right type for now. - ptr.read::() + Some(ptr.unwrap().read::()) } /// Get the script bytecode. @@ -467,11 +459,7 @@ pub fn tx_script_data() -> T { /// /// # Returns /// -/// * [T] - The script bytecode. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The script bytecode. /// /// # Examples /// @@ -479,12 +467,15 @@ pub fn tx_script_data() -> T { /// use std::tx::tx_script_bytecode; /// /// fn foo() { -/// let script_bytecode: [u64; 64] = tx_script_bytecode(); +/// let script_bytecode: [u64; 64] = tx_script_bytecode().unwrap(); /// log(script_bytecode); /// } /// ``` -pub fn tx_script_bytecode() -> T { - tx_script_start_pointer().read::() +pub fn tx_script_bytecode() -> Option { + match tx_type() { + Transaction::Script => Some(tx_script_start_pointer().unwrap().read::()), + _ => None, + } } /// Get the hash of the script bytecode. @@ -492,11 +483,7 @@ pub fn tx_script_bytecode() -> T { /// /// # Returns /// -/// * [b256] - The hash of the script bytecode. -/// -/// # Reverts -/// -/// * When the transaction type is of type `Transaction::Create`. +/// * [Option] - The hash of the script bytecode. /// /// # Examples /// @@ -504,25 +491,25 @@ pub fn tx_script_bytecode() -> T { /// use std::tx::tx_script_bytecode_hash; /// /// fn foo() { -/// let script_bytecode_hash: b256 = tx_script_bytecode_hash(); +/// let script_bytecode_hash: b256 = tx_script_bytecode_hash().unwrap(); /// assert(script_bytecode_hash == 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef); /// } /// ``` -pub fn tx_script_bytecode_hash() -> b256 { +pub fn tx_script_bytecode_hash() -> Option { match tx_type() { Transaction::Script => { // Get the script memory details let mut result_buffer = b256::zero(); - let script_length = tx_script_length(); - let script_ptr = tx_script_start_pointer(); + let script_length = tx_script_length().unwrap(); + let script_ptr = tx_script_start_pointer().unwrap(); // Run the hash opcode for the script in memory - asm(hash: result_buffer, ptr: script_ptr, len: script_length) { + Some(asm(hash: result_buffer, ptr: script_ptr, len: script_length) { s256 hash ptr len; hash: b256 - } + }) }, - _ => revert(0), + _ => None, } }