“需要”的区别是什么并“如果”坚固的智能合同中的声明

发布于 2025-01-21 07:31:25 字数 111 浏览 0 评论 0 原文

要求是一个错误处理全局功能的错误,基本上是按照要求的方式运行的,如果要求内部的条件是正确的,那么编译器将执行其下面写的代码。 但是,一般的逻辑语句如果在坚固性内也具有相同的目的, 因此,想知道这两个都有差异。

Require is an error handling global function in solidity which is basically operates in the manner that if the condition within require comes out to be true then the compiler will execute the piece of code written beneath it.
However the general logical statement if also serves the same purpose within solidity,
so wanted to know if there is a difference b/w both of these.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〗斷ホ乔殘χμё〖 2025-01-28 07:31:25

区别在于 require()是您所说的错误处理语句,但是如果此语句失败,则交易将恢复。鉴于您有如果其他语句,则必须确保自己还原交易。

The difference is that require() is an error handling statement like you had stated, but if this statement fails, the transaction is reverted. Whereas if you had an if else statement, you would have to make sure to revert the transaction yourself.

×眷恋的温暖 2025-01-28 07:31:25

需要

恢复失败,用于错误处理。
时呼叫要求(x)

  ```if (amount > msg.value / 2 ether)
        revert("Not enough Ether provided.");```

当x = false替代方法执行此操作

    ```require(
        amount <= msg.value / 2 ether,
        "Not enough Ether provided."
    );```

https://docs.solitylang.org/en/v0.8.8.8.23/control-structures.html#error andring-error handling-sast-resert-require-require-require-require-require-require-require-require-require-require-revert-and-ceptions

如果语句

如果false继续执行。
不恢复。

require

Reverts on failure,use for error handling.
Calling require(x) when x=false

  ```if (amount > msg.value / 2 ether)
        revert("Not enough Ether provided.");```

Alternative way to do it:

    ```require(
        amount <= msg.value / 2 ether,
        "Not enough Ether provided."
    );```

https://docs.soliditylang.org/en/v0.8.23/control-structures.html#error-handling-assert-require-revert-and-exceptions

if statement

if false then continues execution.
Does not revert.

https://docs.soliditylang.org/en/v0.8.23/grammar.html#a4.SolidityParser.ifStatement

影子是时光的心 2025-01-28 07:31:25

最好使用,因为它是气体效率的。

It is preferable to use If because it is gas efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文