diff --git a/HELP.md b/HELP.md index 7cdf3986d..d6e370599 100644 --- a/HELP.md +++ b/HELP.md @@ -194,7 +194,7 @@ This is equivalent to: impl Contract { pub fn resolve_transfer(&mut self) { if near_sdk::env::current_account_id() != near_sdk::env::predecessor_account_id() { - near_sdk::env::panic(b"Method resolve_transfer is private"); + near_sdk::env::panic_str("Method resolve_transfer is private"); } env::log_str("This is a callback"); } @@ -368,7 +368,7 @@ impl Contract { pub fn do_not_take_my_money(&mut self) { if near_sdk::env::attached_deposit() != 0 { - near_sdk::env::panic(b"Method do_not_take_my_money doesn't accept deposit"); + near_sdk::env::panic_str("Method do_not_take_my_money doesn't accept deposit"); } env::log_str("Thanks!"); } diff --git a/README.md b/README.md index 60557dd45..27dd864b8 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,7 @@ pub fn my_method(&mut self) { pub fn my_method(&mut self ) { if near_sdk::env::current_account_id() != near_sdk::env::predecessor_account_id() { - near_sdk::env::panic("Method method is private".as_bytes()); + near_sdk::env::panic_str("Method method is private"); } ... } diff --git a/examples/cross-contract-high-level/res/cross_contract_high_level.wasm b/examples/cross-contract-high-level/res/cross_contract_high_level.wasm index 9258f84c4..98d52eda1 100755 Binary files a/examples/cross-contract-high-level/res/cross_contract_high_level.wasm and b/examples/cross-contract-high-level/res/cross_contract_high_level.wasm differ diff --git a/examples/cross-contract-low-level/res/cross_contract_low_level.wasm b/examples/cross-contract-low-level/res/cross_contract_low_level.wasm index a500478eb..fd5c522ca 100755 Binary files a/examples/cross-contract-low-level/res/cross_contract_low_level.wasm and b/examples/cross-contract-low-level/res/cross_contract_low_level.wasm differ diff --git a/examples/fungible-token/res/defi.wasm b/examples/fungible-token/res/defi.wasm index b6515c3d4..f965df022 100755 Binary files a/examples/fungible-token/res/defi.wasm and b/examples/fungible-token/res/defi.wasm differ diff --git a/examples/fungible-token/res/fungible_token.wasm b/examples/fungible-token/res/fungible_token.wasm index d421fccce..32f7c5b05 100755 Binary files a/examples/fungible-token/res/fungible_token.wasm and b/examples/fungible-token/res/fungible_token.wasm differ diff --git a/examples/lockable-fungible-token/res/lockable_fungible_token.wasm b/examples/lockable-fungible-token/res/lockable_fungible_token.wasm index 12c091bba..959b5d179 100755 Binary files a/examples/lockable-fungible-token/res/lockable_fungible_token.wasm and b/examples/lockable-fungible-token/res/lockable_fungible_token.wasm differ diff --git a/examples/lockable-fungible-token/src/lib.rs b/examples/lockable-fungible-token/src/lib.rs index 3f469eaab..37cbe71fc 100644 --- a/examples/lockable-fungible-token/src/lib.rs +++ b/examples/lockable-fungible-token/src/lib.rs @@ -80,12 +80,12 @@ impl FunToken { let allowance = u128::from_str(&allowance).expect("Failed to parse allowance"); let owner_id = env::predecessor_account_id(); if escrow_account_id == owner_id { - env::panic(b"Can't set allowance for yourself"); + env::panic_str("Can't set allowance for yourself"); } let mut account = self.get_account(&owner_id); let locked_balance = account.get_locked_balance(&escrow_account_id); if locked_balance > allowance { - env::panic(b"The new allowance can't be less than the amount of locked tokens"); + env::panic_str("The new allowance can't be less than the amount of locked tokens"); } account.set_allowance(&escrow_account_id, allowance - locked_balance); @@ -100,14 +100,14 @@ impl FunToken { pub fn lock(&mut self, owner_id: AccountId, lock_amount: String) { let lock_amount = u128::from_str(&lock_amount).expect("Failed to parse allow lock_amount"); if lock_amount == 0 { - env::panic(b"Can't lock 0 tokens"); + env::panic_str("Can't lock 0 tokens"); } let escrow_account_id = env::predecessor_account_id(); let mut account = self.get_account(&owner_id); // Checking and updating unlocked balance if account.balance < lock_amount { - env::panic(b"Not enough unlocked balance"); + env::panic_str("Not enough unlocked balance"); } account.balance -= lock_amount; @@ -115,7 +115,7 @@ impl FunToken { if escrow_account_id != owner_id { let allowance = account.get_allowance(&escrow_account_id); if allowance < lock_amount { - env::panic(b"Not enough allowance"); + env::panic_str("Not enough allowance"); } account.set_allowance(&escrow_account_id, allowance - lock_amount); } @@ -136,7 +136,7 @@ impl FunToken { let unlock_amount = u128::from_str(&unlock_amount).expect("Failed to parse allow unlock_amount"); if unlock_amount == 0 { - env::panic(b"Can't unlock 0 tokens"); + env::panic_str("Can't unlock 0 tokens"); } let escrow_account_id = env::predecessor_account_id(); let mut account = self.get_account(&owner_id); @@ -144,7 +144,7 @@ impl FunToken { // Checking and updating locked balance let locked_balance = account.get_locked_balance(&escrow_account_id); if locked_balance < unlock_amount { - env::panic(b"Not enough locked tokens"); + env::panic_str("Not enough locked tokens"); } account.set_locked_balance(&escrow_account_id, locked_balance - unlock_amount); @@ -172,7 +172,7 @@ impl FunToken { pub fn transfer_from(&mut self, owner_id: AccountId, new_owner_id: AccountId, amount: String) { let amount = u128::from_str(&amount).expect("Failed to parse allow amount"); if amount == 0 { - env::panic(b"Can't transfer 0 tokens"); + env::panic_str("Can't transfer 0 tokens"); } let escrow_account_id = env::predecessor_account_id(); let mut account = self.get_account(&owner_id); @@ -191,7 +191,7 @@ impl FunToken { if remaining_amount > 0 { // Checking and updating unlocked balance if account.balance < remaining_amount { - env::panic(b"Not enough unlocked balance"); + env::panic_str("Not enough unlocked balance"); } account.balance -= remaining_amount; @@ -200,7 +200,7 @@ impl FunToken { let allowance = account.get_allowance(&escrow_account_id); // Checking and updating unlocked balance if allowance < remaining_amount { - env::panic(b"Not enough allowance"); + env::panic_str("Not enough allowance"); } account.set_allowance(&escrow_account_id, allowance - remaining_amount); } diff --git a/examples/mission-control/res/mission_control.wasm b/examples/mission-control/res/mission_control.wasm index d7fc476b6..e4011aaec 100755 Binary files a/examples/mission-control/res/mission_control.wasm and b/examples/mission-control/res/mission_control.wasm differ diff --git a/examples/non-fungible-token/res/approval_receiver.wasm b/examples/non-fungible-token/res/approval_receiver.wasm index 5e9d95917..f32ac8f54 100755 Binary files a/examples/non-fungible-token/res/approval_receiver.wasm and b/examples/non-fungible-token/res/approval_receiver.wasm differ diff --git a/examples/non-fungible-token/res/non_fungible_token.wasm b/examples/non-fungible-token/res/non_fungible_token.wasm index 2da852902..53ad15e9a 100755 Binary files a/examples/non-fungible-token/res/non_fungible_token.wasm and b/examples/non-fungible-token/res/non_fungible_token.wasm differ diff --git a/examples/non-fungible-token/res/token_receiver.wasm b/examples/non-fungible-token/res/token_receiver.wasm index 7e262bfcd..5ec7272a6 100755 Binary files a/examples/non-fungible-token/res/token_receiver.wasm and b/examples/non-fungible-token/res/token_receiver.wasm differ diff --git a/examples/non-fungible-token/test-token-receiver/src/lib.rs b/examples/non-fungible-token/test-token-receiver/src/lib.rs index 0d7cf2b10..fea7d6bb5 100644 --- a/examples/non-fungible-token/test-token-receiver/src/lib.rs +++ b/examples/non-fungible-token/test-token-receiver/src/lib.rs @@ -94,7 +94,7 @@ impl NonFungibleTokenReceiver for TokenReceiver { ) .into() } - _ => env::panic(b"unsupported msg"), + _ => env::panic_str("unsupported msg"), } } } diff --git a/examples/status-message-collections/res/status_message_collections.wasm b/examples/status-message-collections/res/status_message_collections.wasm index 0d4ef9766..cdc3b6538 100755 Binary files a/examples/status-message-collections/res/status_message_collections.wasm and b/examples/status-message-collections/res/status_message_collections.wasm differ diff --git a/examples/status-message/res/status_message.wasm b/examples/status-message/res/status_message.wasm index c8ee55da7..476e49570 100755 Binary files a/examples/status-message/res/status_message.wasm and b/examples/status-message/res/status_message.wasm differ diff --git a/examples/test-contract/res/test_contract.wasm b/examples/test-contract/res/test_contract.wasm index eb8dc8ff7..2be64087d 100755 Binary files a/examples/test-contract/res/test_contract.wasm and b/examples/test-contract/res/test_contract.wasm differ diff --git a/near-contract-standards/src/fungible_token/core_impl.rs b/near-contract-standards/src/fungible_token/core_impl.rs index 637f7047a..af0348992 100644 --- a/near-contract-standards/src/fungible_token/core_impl.rs +++ b/near-contract-standards/src/fungible_token/core_impl.rs @@ -95,7 +95,9 @@ impl FungibleToken { pub fn internal_unwrap_balance_of(&self, account_id: &AccountId) -> Balance { match self.accounts.get(account_id) { Some(balance) => balance, - None => env::panic(format!("The account {} is not registered", &account_id).as_bytes()), + None => { + env::panic_str(format!("The account {} is not registered", &account_id).as_str()) + } } } @@ -106,7 +108,7 @@ impl FungibleToken { self.total_supply = self.total_supply.checked_add(amount).expect("Total supply overflow"); } else { - env::panic(b"Balance overflow"); + env::panic_str("Balance overflow"); } } @@ -117,7 +119,7 @@ impl FungibleToken { self.total_supply = self.total_supply.checked_sub(amount).expect("Total supply overflow"); } else { - env::panic(b"The account doesn't have enough balance"); + env::panic_str("The account doesn't have enough balance"); } } @@ -140,7 +142,7 @@ impl FungibleToken { pub fn internal_register_account(&mut self, account_id: &AccountId) { if self.accounts.insert(account_id, &0).is_some() { - env::panic(b"The account is already registered"); + env::panic_str("The account is already registered"); } } } diff --git a/near-contract-standards/src/fungible_token/storage_impl.rs b/near-contract-standards/src/fungible_token/storage_impl.rs index 97f25f52b..03cedc2d8 100644 --- a/near-contract-standards/src/fungible_token/storage_impl.rs +++ b/near-contract-standards/src/fungible_token/storage_impl.rs @@ -20,7 +20,9 @@ impl FungibleToken { Promise::new(account_id.clone()).transfer(self.storage_balance_bounds().min.0 + 1); Some((account_id, balance)) } else { - env::panic(b"Can't unregister the account with the positive balance without force") + env::panic_str( + "Can't unregister the account with the positive balance without force", + ) } } else { log!("The account {} is not registered", &account_id); @@ -55,7 +57,7 @@ impl StorageManagement for FungibleToken { } else { let min_balance = self.storage_balance_bounds().min.0; if amount < min_balance { - env::panic(b"The attached deposit is less than the minimum storage balance"); + env::panic_str("The attached deposit is less than the minimum storage balance"); } self.internal_register_account(&account_id); @@ -79,13 +81,13 @@ impl StorageManagement for FungibleToken { if let Some(storage_balance) = self.internal_storage_balance_of(&predecessor_account_id) { match amount { Some(amount) if amount.0 > 0 => { - env::panic(b"The amount is greater than the available storage balance"); + env::panic_str("The amount is greater than the available storage balance"); } _ => storage_balance, } } else { - env::panic( - format!("The account {} is not registered", &predecessor_account_id).as_bytes(), + env::panic_str( + format!("The account {} is not registered", &predecessor_account_id).as_str(), ); } } diff --git a/near-contract-standards/src/non_fungible_token/approval/approval_impl.rs b/near-contract-standards/src/non_fungible_token/approval/approval_impl.rs index a13a7dcc1..0ca52b104 100644 --- a/near-contract-standards/src/non_fungible_token/approval/approval_impl.rs +++ b/near-contract-standards/src/non_fungible_token/approval/approval_impl.rs @@ -32,7 +32,7 @@ impl NonFungibleTokenApproval for NonFungibleToken { ) -> Option { assert_at_least_one_yocto(); if self.approvals_by_id.is_none() { - env::panic(b"NFT does not support Approval Management"); + env::panic_str("NFT does not support Approval Management"); } let owner_id = self.owner_by_id.get(&token_id).expect("Token not found"); @@ -78,7 +78,7 @@ impl NonFungibleTokenApproval for NonFungibleToken { fn nft_revoke(&mut self, token_id: TokenId, account_id: AccountId) { assert_one_yocto(); if self.approvals_by_id.is_none() { - env::panic(b"NFT does not support Approval Management"); + env::panic_str("NFT does not support Approval Management"); } let owner_id = self.owner_by_id.get(&token_id).expect("Token not found"); @@ -110,7 +110,7 @@ impl NonFungibleTokenApproval for NonFungibleToken { fn nft_revoke_all(&mut self, token_id: TokenId) { assert_one_yocto(); if self.approvals_by_id.is_none() { - env::panic(b"NFT does not support Approval Management"); + env::panic_str("NFT does not support Approval Management"); } let owner_id = self.owner_by_id.get(&token_id).expect("Token not found"); diff --git a/near-contract-standards/src/non_fungible_token/core/core_impl.rs b/near-contract-standards/src/non_fungible_token/core/core_impl.rs index 3c12f9665..4b4492b62 100644 --- a/near-contract-standards/src/non_fungible_token/core/core_impl.rs +++ b/near-contract-standards/src/non_fungible_token/core/core_impl.rs @@ -246,7 +246,7 @@ impl NonFungibleToken { if sender_id != &owner_id { // if approval extension is NOT being used, or if token has no approved accounts if approved_account_ids.is_none() { - env::panic(b"Unauthorized") + env::panic_str("Unauthorized") } // Approval extension is being used; get approval_id for sender. @@ -254,7 +254,7 @@ impl NonFungibleToken { // Panic if sender not approved at all if actual_approval_id.is_none() { - env::panic(b"Sender not approved"); + env::panic_str("Sender not approved"); } // If approval_id included, check that it matches @@ -348,10 +348,10 @@ impl NonFungibleTokenCore for NonFungibleToken { let initial_storage_usage = env::storage_usage(); assert_eq!(env::predecessor_account_id(), self.owner_id, "Unauthorized"); if self.token_metadata_by_id.is_some() && token_metadata.is_none() { - env::panic(b"Must provide metadata"); + env::panic_str("Must provide metadata"); } if self.owner_by_id.get(&token_id).is_some() { - env::panic(b"token_id must be unique"); + env::panic_str("token_id must be unique"); } let owner_id: AccountId = token_owner_id; diff --git a/near-contract-standards/src/upgrade/mod.rs b/near-contract-standards/src/upgrade/mod.rs index 99d7e16c0..7fb4c5047 100644 --- a/near-contract-standards/src/upgrade/mod.rs +++ b/near-contract-standards/src/upgrade/mod.rs @@ -67,12 +67,12 @@ impl Upgradable for Upgrade { fn deploy_code(&mut self) -> Promise { if self.staging_timestamp < env::block_timestamp() { - env::panic( - &format!( + env::panic_str( + format!( "Deploy code too early: staging ends on {}", self.staging_timestamp + self.staging_duration ) - .into_bytes(), + .as_str(), ); } let code = env::storage_read(b"upgrade").expect("No upgrade code available"); diff --git a/near-sdk-macros/src/core_impl/code_generator/impl_item_method_info.rs b/near-sdk-macros/src/core_impl/code_generator/impl_item_method_info.rs index 22e354574..c7966aca9 100644 --- a/near-sdk-macros/src/core_impl/code_generator/impl_item_method_info.rs +++ b/near-sdk-macros/src/core_impl/code_generator/impl_item_method_info.rs @@ -63,7 +63,7 @@ impl ImplItemMethodInfo { let error = format!("Method {} doesn't accept deposit", ident.to_string()); quote! { if near_sdk::env::attached_deposit() != 0 { - near_sdk::env::panic(#error.as_bytes()); + near_sdk::env::panic_str(#error); } } }; @@ -71,7 +71,7 @@ impl ImplItemMethodInfo { let error = format!("Method {} is private", ident.to_string()); quote! { if near_sdk::env::current_account_id() != near_sdk::env::predecessor_account_id() { - near_sdk::env::panic(#error.as_bytes()); + near_sdk::env::panic_str(#error); } } } else { @@ -80,7 +80,7 @@ impl ImplItemMethodInfo { let body = if matches!(method_type, &MethodType::Init) { quote! { if near_sdk::env::state_exists() { - near_sdk::env::panic(b"The contract has already been initialized"); + near_sdk::env::panic_str("The contract has already been initialized"); } let contract = #struct_type::#ident(#arg_list); near_sdk::env::state_write(&contract); diff --git a/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs b/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs index 47b9890c3..72e156459 100644 --- a/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +++ b/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs @@ -93,7 +93,7 @@ mod tests { pub extern "C" fn method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::attached_deposit() != 0 { - near_sdk::env::panic("Method method doesn't accept deposit".as_bytes()); + near_sdk::env::panic_str("Method method doesn't accept deposit"); } let mut contract: Hello = near_sdk::env::state_read().unwrap_or_default(); contract.method(); @@ -143,7 +143,7 @@ mod tests { pub extern "C" fn method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::attached_deposit() != 0 { - near_sdk::env::panic("Method method doesn't accept deposit".as_bytes()); + near_sdk::env::panic_str("Method method doesn't accept deposit"); } #[derive(near_sdk :: serde :: Deserialize)] #[serde(crate = "near_sdk::serde")] @@ -176,7 +176,7 @@ mod tests { pub extern "C" fn method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::attached_deposit() != 0 { - near_sdk::env::panic("Method method doesn't accept deposit".as_bytes()); + near_sdk::env::panic_str("Method method doesn't accept deposit"); } #[derive(near_sdk :: serde :: Deserialize)] #[serde(crate = "near_sdk::serde")] @@ -290,7 +290,7 @@ mod tests { pub extern "C" fn method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::current_account_id() != near_sdk::env::predecessor_account_id() { - near_sdk::env::panic("Method method is private".as_bytes()); + near_sdk::env::panic_str("Method method is private"); } #[derive(near_sdk :: serde :: Deserialize)] #[serde(crate = "near_sdk::serde")] @@ -334,7 +334,7 @@ mod tests { pub extern "C" fn method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::current_account_id() != near_sdk::env::predecessor_account_id() { - near_sdk::env::panic("Method method is private".as_bytes()); + near_sdk::env::panic_str("Method method is private"); } let data: Vec = match near_sdk::env::promise_result(0u64) { near_sdk::PromiseResult::Successful(x) => x, @@ -370,7 +370,7 @@ mod tests { pub extern "C" fn method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::current_account_id() != near_sdk::env::predecessor_account_id() { - near_sdk::env::panic("Method method is private".as_bytes()); + near_sdk::env::panic_str("Method method is private"); } #[derive(near_sdk :: serde :: Deserialize)] #[serde(crate = "near_sdk::serde")] @@ -412,7 +412,7 @@ mod tests { pub extern "C" fn method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::attached_deposit() != 0 { - near_sdk::env::panic("Method method doesn't accept deposit".as_bytes()); + near_sdk::env::panic_str("Method method doesn't accept deposit"); } #[derive(near_sdk :: serde :: Deserialize)] #[serde(crate = "near_sdk::serde")] @@ -424,7 +424,7 @@ mod tests { ) .expect("Failed to deserialize input from JSON."); if near_sdk::env::state_exists() { - near_sdk::env::panic(b"The contract has already been initialized"); + near_sdk::env::panic_str("The contract has already been initialized"); } let contract = Hello::method(&mut k,); near_sdk::env::state_write(&contract); @@ -448,7 +448,7 @@ mod tests { pub extern "C" fn method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::attached_deposit() != 0 { - near_sdk::env::panic("Method method doesn't accept deposit".as_bytes()); + near_sdk::env::panic_str("Method method doesn't accept deposit"); } #[derive(near_sdk :: serde :: Deserialize)] #[serde(crate = "near_sdk::serde")] @@ -491,7 +491,7 @@ mod tests { ) .expect("Failed to deserialize input from JSON."); if near_sdk::env::state_exists() { - near_sdk::env::panic(b"The contract has already been initialized"); + near_sdk::env::panic_str("The contract has already been initialized"); } let contract = Hello::method(&mut k,); near_sdk::env::state_write(&contract); @@ -515,7 +515,7 @@ mod tests { pub extern "C" fn method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::attached_deposit() != 0 { - near_sdk::env::panic("Method method doesn't accept deposit".as_bytes()); + near_sdk::env::panic_str("Method method doesn't accept deposit"); } #[derive(near_sdk :: borsh :: BorshDeserialize)] struct Input { @@ -551,7 +551,7 @@ mod tests { pub extern "C" fn method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::current_account_id() != near_sdk::env::predecessor_account_id() { - near_sdk::env::panic("Method method is private".as_bytes()); + near_sdk::env::panic_str("Method method is private"); } #[derive(near_sdk :: borsh :: BorshDeserialize)] struct Input { @@ -611,10 +611,10 @@ mod tests { pub extern "C" fn private_method() { near_sdk::env::setup_panic_hook(); if near_sdk::env::current_account_id() != near_sdk::env::predecessor_account_id() { - near_sdk::env::panic("Method private_method is private".as_bytes()); + near_sdk::env::panic_str("Method private_method is private"); } if near_sdk::env::attached_deposit() != 0 { - near_sdk::env::panic("Method private_method doesn't accept deposit".as_bytes()); + near_sdk::env::panic_str("Method private_method doesn't accept deposit"); } let mut contract: Hello = near_sdk::env::state_read().unwrap_or_default(); contract.private_method(); diff --git a/near-sdk-macros/src/lib.rs b/near-sdk-macros/src/lib.rs index 0539ff602..bac9ce127 100644 --- a/near-sdk-macros/src/lib.rs +++ b/near-sdk-macros/src/lib.rs @@ -147,7 +147,7 @@ pub fn derive_no_default(item: TokenStream) -> TokenStream { TokenStream::from(quote! { impl Default for #name { fn default() -> Self { - near_sdk::env::panic(b"The contract is not initialized"); + near_sdk::env::panic_str("The contract is not initialized"); } } }) diff --git a/near-sdk/src/collections/lazy_option.rs b/near-sdk/src/collections/lazy_option.rs index 0967de2cd..68d5355a3 100644 --- a/near-sdk/src/collections/lazy_option.rs +++ b/near-sdk/src/collections/lazy_option.rs @@ -10,8 +10,8 @@ use borsh::{BorshDeserialize, BorshSerialize}; use crate::env; use crate::IntoStorageKey; -const ERR_VALUE_SERIALIZATION: &[u8] = b"Cannot serialize value with Borsh"; -const ERR_VALUE_DESERIALIZATION: &[u8] = b"Cannot deserialize value with Borsh"; +const ERR_VALUE_SERIALIZATION: &str = "Cannot serialize value with Borsh"; +const ERR_VALUE_DESERIALIZATION: &str = "Cannot deserialize value with Borsh"; /// An persistent lazy option, that stores a value in the storage. #[derive(BorshSerialize, BorshDeserialize)] @@ -84,14 +84,14 @@ where fn serialize_value(value: &T) -> Vec { match value.try_to_vec() { Ok(x) => x, - Err(_) => env::panic(ERR_VALUE_SERIALIZATION), + Err(_) => env::panic_str(ERR_VALUE_SERIALIZATION), } } fn deserialize_value(raw_value: &[u8]) -> T { match T::try_from_slice(raw_value) { Ok(x) => x, - Err(_) => env::panic(ERR_VALUE_DESERIALIZATION), + Err(_) => env::panic_str(ERR_VALUE_DESERIALIZATION), } } diff --git a/near-sdk/src/collections/lookup_map.rs b/near-sdk/src/collections/lookup_map.rs index 0aba0ba21..3a493dd58 100644 --- a/near-sdk/src/collections/lookup_map.rs +++ b/near-sdk/src/collections/lookup_map.rs @@ -8,9 +8,9 @@ use borsh::{BorshDeserialize, BorshSerialize}; use crate::collections::append_slice; use crate::{env, IntoStorageKey}; -const ERR_KEY_SERIALIZATION: &[u8] = b"Cannot serialize key with Borsh"; -const ERR_VALUE_DESERIALIZATION: &[u8] = b"Cannot deserialize value with Borsh"; -const ERR_VALUE_SERIALIZATION: &[u8] = b"Cannot serialize value with Borsh"; +const ERR_KEY_SERIALIZATION: &str = "Cannot serialize key with Borsh"; +const ERR_VALUE_DESERIALIZATION: &str = "Cannot deserialize value with Borsh"; +const ERR_VALUE_SERIALIZATION: &str = "Cannot serialize value with Borsh"; /// An non-iterable implementation of a map that stores its content directly on the trie. #[derive(BorshSerialize, BorshDeserialize)] @@ -78,21 +78,21 @@ where fn serialize_key(key: &K) -> Vec { match key.try_to_vec() { Ok(x) => x, - Err(_) => env::panic(ERR_KEY_SERIALIZATION), + Err(_) => env::panic_str(ERR_KEY_SERIALIZATION), } } fn deserialize_value(raw_value: &[u8]) -> V { match V::try_from_slice(raw_value) { Ok(x) => x, - Err(_) => env::panic(ERR_VALUE_DESERIALIZATION), + Err(_) => env::panic_str(ERR_VALUE_DESERIALIZATION), } } fn serialize_value(value: &V) -> Vec { match value.try_to_vec() { Ok(x) => x, - Err(_) => env::panic(ERR_VALUE_SERIALIZATION), + Err(_) => env::panic_str(ERR_VALUE_SERIALIZATION), } } diff --git a/near-sdk/src/collections/lookup_set.rs b/near-sdk/src/collections/lookup_set.rs index 84fe27728..d32bd63fe 100644 --- a/near-sdk/src/collections/lookup_set.rs +++ b/near-sdk/src/collections/lookup_set.rs @@ -8,7 +8,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; use crate::collections::append_slice; use crate::{env, IntoStorageKey}; -const ERR_ELEMENT_SERIALIZATION: &[u8] = b"Cannot serialize element with Borsh"; +const ERR_ELEMENT_SERIALIZATION: &str = "Cannot serialize element with Borsh"; /// An non-iterable implementation of a set that stores its content directly on the trie. #[derive(BorshSerialize, BorshDeserialize)] @@ -60,7 +60,7 @@ where fn serialize_element(element: &T) -> Vec { match element.try_to_vec() { Ok(x) => x, - Err(_) => env::panic(ERR_ELEMENT_SERIALIZATION), + Err(_) => env::panic_str(ERR_ELEMENT_SERIALIZATION), } } diff --git a/near-sdk/src/collections/mod.rs b/near-sdk/src/collections/mod.rs index 7db2ec559..af7c9e014 100644 --- a/near-sdk/src/collections/mod.rs +++ b/near-sdk/src/collections/mod.rs @@ -59,9 +59,9 @@ pub use lazy_option::LazyOption; mod tree_map; pub use tree_map::TreeMap; -pub const ERR_INCONSISTENT_STATE: &[u8] = b"The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"; -pub const ERR_ELEMENT_SERIALIZATION: &[u8] = b"Cannot serialize element with Borsh."; -pub const ERR_ELEMENT_DESERIALIZATION: &[u8] = b"Cannot deserialize element with Borsh."; +pub const ERR_INCONSISTENT_STATE: &str = "The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"; +pub const ERR_ELEMENT_SERIALIZATION: &str = "Cannot serialize element with Borsh."; +pub const ERR_ELEMENT_DESERIALIZATION: &str = "Cannot deserialize element with Borsh."; pub(crate) fn append(id: &[u8], chr: u8) -> Vec { append_slice(id, &[chr]) diff --git a/near-sdk/src/collections/unordered_map.rs b/near-sdk/src/collections/unordered_map.rs index 62535a605..c9c0032cd 100644 --- a/near-sdk/src/collections/unordered_map.rs +++ b/near-sdk/src/collections/unordered_map.rs @@ -5,10 +5,10 @@ use crate::{env, IntoStorageKey}; use borsh::{BorshDeserialize, BorshSerialize}; use std::mem::size_of; -const ERR_INCONSISTENT_STATE: &[u8] = b"The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"; -const ERR_KEY_SERIALIZATION: &[u8] = b"Cannot serialize key with Borsh"; -const ERR_VALUE_DESERIALIZATION: &[u8] = b"Cannot deserialize value with Borsh"; -const ERR_VALUE_SERIALIZATION: &[u8] = b"Cannot serialize value with Borsh"; +const ERR_INCONSISTENT_STATE: &str = "The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"; +const ERR_KEY_SERIALIZATION: &str = "Cannot serialize key with Borsh"; +const ERR_VALUE_DESERIALIZATION: &str = "Cannot deserialize value with Borsh"; +const ERR_VALUE_SERIALIZATION: &str = "Cannot serialize value with Borsh"; /// An iterable implementation of a map that stores its content directly on the trie. #[derive(BorshSerialize, BorshDeserialize)] @@ -24,7 +24,7 @@ impl UnorderedMap { let keys_len = self.keys.len(); let values_len = self.values.len(); if keys_len != values_len { - env::panic(ERR_INCONSISTENT_STATE) + env::panic_str(ERR_INCONSISTENT_STATE) } else { keys_len } @@ -35,7 +35,7 @@ impl UnorderedMap { let keys_is_empty = self.keys.is_empty(); let values_is_empty = self.values.is_empty(); if keys_is_empty != values_is_empty { - env::panic(ERR_INCONSISTENT_STATE) + env::panic_str(ERR_INCONSISTENT_STATE) } else { keys_is_empty } @@ -82,7 +82,7 @@ impl UnorderedMap { fn get_raw(&self, key_raw: &[u8]) -> Option> { self.get_index_raw(key_raw).map(|index| match self.values.get_raw(index) { Some(x) => x, - None => env::panic(ERR_INCONSISTENT_STATE), + None => env::panic_str(ERR_INCONSISTENT_STATE), }) } @@ -126,7 +126,7 @@ impl UnorderedMap { // element. let last_key_raw = match self.keys.get_raw(self.len() - 1) { Some(x) => x, - None => env::panic(ERR_INCONSISTENT_STATE), + None => env::panic_str(ERR_INCONSISTENT_STATE), }; env::storage_remove(&index_lookup); // If the removed element was the last element from keys, then we don't need to @@ -153,21 +153,21 @@ where fn serialize_key(key: &K) -> Vec { match key.try_to_vec() { Ok(x) => x, - Err(_) => env::panic(ERR_KEY_SERIALIZATION), + Err(_) => env::panic_str(ERR_KEY_SERIALIZATION), } } fn deserialize_value(raw_value: &[u8]) -> V { match V::try_from_slice(raw_value) { Ok(x) => x, - Err(_) => env::panic(ERR_VALUE_DESERIALIZATION), + Err(_) => env::panic_str(ERR_VALUE_DESERIALIZATION), } } fn serialize_value(value: &V) -> Vec { match value.try_to_vec() { Ok(x) => x, - Err(_) => env::panic(ERR_VALUE_SERIALIZATION), + Err(_) => env::panic_str(ERR_VALUE_SERIALIZATION), } } diff --git a/near-sdk/src/collections/unordered_set.rs b/near-sdk/src/collections/unordered_set.rs index 562cec31d..7b338533c 100644 --- a/near-sdk/src/collections/unordered_set.rs +++ b/near-sdk/src/collections/unordered_set.rs @@ -5,8 +5,8 @@ use crate::{env, IntoStorageKey}; use borsh::{BorshDeserialize, BorshSerialize}; use std::mem::size_of; -const ERR_INCONSISTENT_STATE: &[u8] = b"The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"; -const ERR_ELEMENT_SERIALIZATION: &[u8] = b"Cannot serialize element with Borsh"; +const ERR_INCONSISTENT_STATE: &str = "The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"; +const ERR_ELEMENT_SERIALIZATION: &str = "Cannot serialize element with Borsh"; /// An iterable implementation of a set that stores its content directly on the trie. #[derive(BorshSerialize, BorshDeserialize)] @@ -91,7 +91,7 @@ impl UnorderedSet { // element. let last_element_raw = match self.elements.get_raw(self.len() - 1) { Some(x) => x, - None => env::panic(ERR_INCONSISTENT_STATE), + None => env::panic_str(ERR_INCONSISTENT_STATE), }; env::storage_remove(&index_lookup); // If the removed element was the last element from keys, then we don't need to @@ -118,7 +118,7 @@ where fn serialize_element(element: &T) -> Vec { match element.try_to_vec() { Ok(x) => x, - Err(_) => env::panic(ERR_ELEMENT_SERIALIZATION), + Err(_) => env::panic_str(ERR_ELEMENT_SERIALIZATION), } } diff --git a/near-sdk/src/collections/vector.rs b/near-sdk/src/collections/vector.rs index 9223b605a..42719ebaa 100644 --- a/near-sdk/src/collections/vector.rs +++ b/near-sdk/src/collections/vector.rs @@ -7,13 +7,13 @@ use borsh::{BorshDeserialize, BorshSerialize}; use crate::collections::append_slice; use crate::{env, IntoStorageKey}; -const ERR_INCONSISTENT_STATE: &[u8] = b"The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"; -const ERR_ELEMENT_DESERIALIZATION: &[u8] = b"Cannot deserialize element"; -const ERR_ELEMENT_SERIALIZATION: &[u8] = b"Cannot serialize element"; -const ERR_INDEX_OUT_OF_BOUNDS: &[u8] = b"Index out of bounds"; +const ERR_INCONSISTENT_STATE: &str = "The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"; +const ERR_ELEMENT_DESERIALIZATION: &str = "Cannot deserialize element"; +const ERR_ELEMENT_SERIALIZATION: &str = "Cannot serialize element"; +const ERR_INDEX_OUT_OF_BOUNDS: &str = "Index out of bounds"; fn expect_consistent_state(val: Option) -> T { - val.unwrap_or_else(|| env::panic(ERR_INCONSISTENT_STATE)) + val.unwrap_or_else(|| env::panic_str(ERR_INCONSISTENT_STATE)) } /// An iterable implementation of vector that stores its content on the trie. @@ -68,7 +68,7 @@ impl Vector { /// Panics if `index` is out of bounds. pub fn swap_remove_raw(&mut self, index: u64) -> Vec { if index >= self.len { - env::panic(ERR_INDEX_OUT_OF_BOUNDS) + env::panic_str(ERR_INDEX_OUT_OF_BOUNDS) } else if index + 1 == self.len { expect_consistent_state(self.pop_raw()) } else { @@ -77,7 +77,7 @@ impl Vector { if env::storage_write(&lookup_key, &raw_last_value) { expect_consistent_state(env::storage_get_evicted()) } else { - env::panic(ERR_INCONSISTENT_STATE) + env::panic_str(ERR_INCONSISTENT_STATE) } } } @@ -101,7 +101,7 @@ impl Vector { let raw_last_value = if env::storage_remove(&last_lookup_key) { expect_consistent_state(env::storage_get_evicted()) } else { - env::panic(ERR_INCONSISTENT_STATE) + env::panic_str(ERR_INCONSISTENT_STATE) }; Some(raw_last_value) } @@ -114,13 +114,13 @@ impl Vector { /// If `index` is out of bounds. pub fn replace_raw(&mut self, index: u64, raw_element: &[u8]) -> Vec { if index >= self.len { - env::panic(ERR_INDEX_OUT_OF_BOUNDS) + env::panic_str(ERR_INDEX_OUT_OF_BOUNDS) } else { let lookup_key = self.index_to_lookup_key(index); if env::storage_write(&lookup_key, raw_element) { expect_consistent_state(env::storage_get_evicted()) } else { - env::panic(ERR_INCONSISTENT_STATE); + env::panic_str(ERR_INCONSISTENT_STATE); } } } @@ -157,7 +157,7 @@ where T: BorshSerialize, { fn serialize_element(element: &T) -> Vec { - element.try_to_vec().unwrap_or_else(|_| env::panic(ERR_ELEMENT_SERIALIZATION)) + element.try_to_vec().unwrap_or_else(|_| env::panic_str(ERR_ELEMENT_SERIALIZATION)) } /// Appends an element to the back of the collection. @@ -179,7 +179,8 @@ where T: BorshDeserialize, { fn deserialize_element(raw_element: &[u8]) -> T { - T::try_from_slice(raw_element).unwrap_or_else(|_| env::panic(ERR_ELEMENT_DESERIALIZATION)) + T::try_from_slice(raw_element) + .unwrap_or_else(|_| env::panic_str(ERR_ELEMENT_DESERIALIZATION)) } /// Returns the element by index or `None` if it is not present. diff --git a/near-sdk/src/environment/env.rs b/near-sdk/src/environment/env.rs index f8ab7e6e1..480a1dfcd 100644 --- a/near-sdk/src/environment/env.rs +++ b/near-sdk/src/environment/env.rs @@ -87,7 +87,7 @@ pub fn set_blockchain_interface(blockchain_interface: MockedBlockchain) { /// Implements panic hook that converts `PanicInfo` into a string and provides it through the /// blockchain interface. fn panic_hook_impl(info: &std_panic::PanicInfo) { - panic(info.to_string().as_bytes()); + panic_str(info.to_string().as_str()); } /// Setups panic hook to expose error info to the blockchain. @@ -488,9 +488,23 @@ pub fn value_return(value: &[u8]) { unsafe { sys::value_return(value.len() as _, value.as_ptr() as _) } } /// Terminates the execution of the program with the UTF-8 encoded message. +/// [`panic_str`] should be used as the bytes are required to be UTF-8 +#[deprecated(since = "4.0.0", note = "Use env::panic_str to panic with a message.")] pub fn panic(message: &[u8]) -> ! { unsafe { sys::panic_utf8(message.len() as _, message.as_ptr() as _) } } + +/// Terminates the execution of the program with the UTF-8 encoded message. +pub fn panic_str(message: &str) -> ! { + unsafe { sys::panic_utf8(message.len() as _, message.as_ptr() as _) } +} + +/// Aborts the current contract execution without a custom message. +/// To include a message, use [`panic_str`]. +pub fn abort() -> ! { + unsafe { sys::panic() } +} + /// Logs the string message message. This message is stored on chain. pub fn log_str(message: &str) { #[cfg(all(debug_assertions, not(target_arch = "wasm32")))] diff --git a/near-sdk/src/store/lazy/mod.rs b/near-sdk/src/store/lazy/mod.rs index 8fa190321..154798947 100644 --- a/near-sdk/src/store/lazy/mod.rs +++ b/near-sdk/src/store/lazy/mod.rs @@ -13,18 +13,18 @@ use crate::env; use crate::utils::{CacheEntry, EntryState}; use crate::IntoStorageKey; -const ERR_VALUE_SERIALIZATION: &[u8] = b"Cannot serialize value with Borsh"; -const ERR_VALUE_DESERIALIZATION: &[u8] = b"Cannot deserialize value with Borsh"; -const ERR_NOT_FOUND: &[u8] = b"No value found for the given key"; -const ERR_DELETED: &[u8] = b"The Lazy cell's value has been deleted. Verify the key has not been\ +const ERR_VALUE_SERIALIZATION: &str = "Cannot serialize value with Borsh"; +const ERR_VALUE_DESERIALIZATION: &str = "Cannot deserialize value with Borsh"; +const ERR_NOT_FOUND: &str = "No value found for the given key"; +const ERR_DELETED: &str = "The Lazy cell's value has been deleted. Verify the key has not been\ deleted manually."; fn expect_key_exists(val: Option) -> T { - val.unwrap_or_else(|| env::panic(ERR_NOT_FOUND)) + val.unwrap_or_else(|| env::panic_str(ERR_NOT_FOUND)) } fn expect_consistent_state(val: Option) -> T { - val.unwrap_or_else(|| env::panic(ERR_DELETED)) + val.unwrap_or_else(|| env::panic_str(ERR_DELETED)) } pub(crate) fn load_and_deserialize(key: &[u8]) -> CacheEntry @@ -32,7 +32,8 @@ where T: BorshDeserialize, { let bytes = expect_key_exists(env::storage_read(key)); - let val = T::try_from_slice(&bytes).unwrap_or_else(|_| env::panic(ERR_VALUE_DESERIALIZATION)); + let val = + T::try_from_slice(&bytes).unwrap_or_else(|_| env::panic_str(ERR_VALUE_DESERIALIZATION)); CacheEntry::new_cached(Some(val)) } @@ -40,7 +41,7 @@ pub(crate) fn serialize_and_store(key: &[u8], value: &T) where T: BorshSerialize, { - let serialized = value.try_to_vec().unwrap_or_else(|_| env::panic(ERR_VALUE_SERIALIZATION)); + let serialized = value.try_to_vec().unwrap_or_else(|_| env::panic_str(ERR_VALUE_SERIALIZATION)); env::storage_write(&key, &serialized); } @@ -93,7 +94,7 @@ where } else { self.cache .set(CacheEntry::new_modified(Some(value))) - .unwrap_or_else(|_| env::panic(b"cache is checked to not be filled above")) + .unwrap_or_else(|_| env::panic_str("cache is checked to not be filled above")) } } diff --git a/near-sdk/src/store/lazy_option/mod.rs b/near-sdk/src/store/lazy_option/mod.rs index c00c8203c..a34031cb9 100644 --- a/near-sdk/src/store/lazy_option/mod.rs +++ b/near-sdk/src/store/lazy_option/mod.rs @@ -72,7 +72,7 @@ where } else { self.cache .set(CacheEntry::new_modified(value)) - .unwrap_or_else(|_| env::panic(b"cache is checked to not be filled above")); + .unwrap_or_else(|_| env::panic_str("cache is checked to not be filled above")); } }