简体   繁体   中英

What's the preferred way to stop execution in NEAR Rust smart contracts?

There are at least 3 methods to stop program execution

  1. panic!
  2. assert! (and it's all siblings)
  3. env::panic

How to properly stop smart contract execution?

Is there any preferred way? When someone should use env::panic ?

They all will eventually call env::panic . From the docsTerminates the execution of the program with the UTF-8 encoded message. . It is a wrapper around a host function imported to the contract.

As for the other two, assert! checks a boolean and calls panic! with a message. They both support the fmt::Display trait, which means you can have string interpolation with "{}" marking where a passed string will go in the order of their appearance.

eg

assert!(b, "{}", "oops");
/// is
if (b) {
  panic!("{}", "oops");
}
/// is equivalent to 
if (b) {
  env::panic(format!("{}", "oops"));
}

So you can use any one as you see fit. A great place to see examples of them in the near-sdk-rs/examples like the fungible token contract .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM