Skip to content

Commit

Permalink
feat(env): Replace panic with panic_str and add abort method (#492)
Browse files Browse the repository at this point in the history
* feat(env): Replace panic with panic_str and add abort method

* replace usages within codebase

* fmt

* remove unnecessary references from codegen'

* update wasm blobs
  • Loading branch information
austinabell committed Aug 3, 2021
1 parent 7c6641c commit 1859ce4
Show file tree
Hide file tree
Showing 34 changed files with 124 additions and 104 deletions.
4 changes: 2 additions & 2 deletions HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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!");
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
...
}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified examples/fungible-token/res/defi.wasm
Binary file not shown.
Binary file modified examples/fungible-token/res/fungible_token.wasm
Binary file not shown.
Binary file not shown.
20 changes: 10 additions & 10 deletions examples/lockable-fungible-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -100,22 +100,22 @@ 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;

// If locking by escrow, need to check and update the allowance.
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);
}
Expand All @@ -136,15 +136,15 @@ 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);

// 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);

Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -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);
}
Expand Down
Binary file modified examples/mission-control/res/mission_control.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/approval_receiver.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/non_fungible_token.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/token_receiver.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/non-fungible-token/test-token-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl NonFungibleTokenReceiver for TokenReceiver {
)
.into()
}
_ => env::panic(b"unsupported msg"),
_ => env::panic_str("unsupported msg"),
}
}
}
Expand Down
Binary file not shown.
Binary file modified examples/status-message/res/status_message.wasm
Binary file not shown.
Binary file modified examples/test-contract/res/test_contract.wasm
Binary file not shown.
10 changes: 6 additions & 4 deletions near-contract-standards/src/fungible_token/core_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions near-contract-standards/src/fungible_token/storage_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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(),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl NonFungibleTokenApproval for NonFungibleToken {
) -> Option<Promise> {
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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,15 @@ 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.
let actual_approval_id = approved_account_ids.as_ref().unwrap().get(sender_id);

// 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
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions near-contract-standards/src/upgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ 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);
}
}
};
let is_private_check = if *is_private {
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 {
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 1859ce4

Please sign in to comment.