Skip to main content
127 votes
Accepted

Difference between require and assert and the difference between revert and throw

edit 2022-12-08: As of Solidity 0.8.0 this answer is no longer accurate with respect to opcode and gas usage. Furthermore, internal errors like division by zero, assertion failure and others do not ...
maurelian's user avatar
  • 3,251
25 votes

Difference between require and assert and the difference between revert and throw

I'm using require for input validation as it's a little more efficient that if/throw. function foo(uint amount) { require(amount < totalAmount); ... } Where as assert should be used more ...
o0ragman0o's user avatar
  • 4,330
19 votes

Difference between require and assert and the difference between revert and throw

I think none of the answers is correct. assert is reserved for conditions in which it is expected that static code analysis tools (maybe Solidity compiler in future versions) will be able to detect ...
earizon's user avatar
  • 606
6 votes

Why using assert, since it would consume all gas

You can use assert (over require) to indicate your intention to source code analyzers such as oyente. While require is good to validate input, it can sometimes be false because the user did something ...
Rob Hitchens's user avatar
  • 55.6k
5 votes
Accepted

Why not use require() in place of assert()?

require is for making sure that an illegal input has not been passed to the function. assert is for making sure that something which is never supposed to happen - does not happen. In other words, ...
goodvibration's user avatar
4 votes

Why using assert, since it would consume all gas

If you want to punish the naughty users who supply bad arguments to your functions, you should use up all their gas :-)
Jesbus's user avatar
  • 10.5k
4 votes

Why not use require() in place of assert()?

Prior to the Byzantium network upgrade, require() and assert() actually behave identically, but their bytecode output is slightly different. assert() uses the 0xfe opcode to cause an error condition ...
hellohihihi's user avatar
4 votes
Accepted

assertEq is showing error when written in the test file foundry

You need to inherit the test.sol in your test contract. Like this : contract FundMeTest is Test { FundMe fundme; function setup() external { fundme = new FundMe(); } function ...
Zartaj Afser's user avatar
  • 5,320
3 votes

Read message of require / revert statement in app.js

Web3 1.2.5 supports the revert messages now, activated by setting web3.eth.handleRevert = true. You can use TestContract.methods.myMethod(myParam).call().catch(console.log) to receive the message ...
Markus - soliditydeveloper.com's user avatar
3 votes
Accepted

Better ways to test bytes32 type using truffle testing framework

The values you're showing are 32 bytes long (256 bits), not 64 bits. They're that length because the type is bytes32. The number 1 is different from 10, and the same holds in hexadecimal. You can't ...
user19510's user avatar
  • 28.1k
3 votes
Accepted

How to use require() with stings

There's no built-in equality test for strings (or other arrays), so a common technique is to compare the hashes of the strings instead: function feedData(string _result) public { // Compare ...
user19510's user avatar
  • 28.1k
3 votes

Difference between require and assert and the difference between revert and throw

Assert is suitable for checking conditions which are not meant to occur but do. Require is suitable for checking for unwanted conditions which can occur.
nonseodion's user avatar
2 votes
Accepted

Control structure with truffle (assert and require)

The code works perfectly on my machine. I'm able to migrate and compile the given code. The truffle version I'm using is - 3.2.5 Upgrade to this version, shall fix your issue.
Sanchit's user avatar
  • 3,512
2 votes
Accepted

Remix Warning : Assert

This is a misuse of assert because, depending on input, the expressions may evaluate to false. Use require() instead. assert() is meant to check for logical errors in the contract itself. Asserts ...
Rob Hitchens's user avatar
  • 55.6k
2 votes

Why using assert, since it would consume all gas

Here's a good article: https://codeforgeek.com/assert-vs-require-in-solidity/ Philosophically, they are intended to mean two very different things. require is "user should have done this before ...
ori's user avatar
  • 346
2 votes

Truffle script: how to run multiple assertions without stopping the program?

To begin with, you are not running your script as an unit test, so I guess it isn't meant to be one. If it's not meant to be a unit test, it probably shouldn't have asserts. You should run unit test ...
Lauri Peltonen's user avatar
2 votes

assert function

Yes. It is guarding against underflow and overflow, which is not necessary with the Solidity version that contract is using (^0.8.16). So, it's weird that that contract is using that guard since it's ...
Jeremy Then's user avatar
  • 4,649
1 vote
Accepted

Test with foundry using "vm.prank()" failing

It's happening because vm.prank is setting msg.sender to PLAYER address only for the getEntranceFee() call, after that it's getting reset to the "foundry default caller". As, you can read in ...
SYED ASAD KAZMI's user avatar
1 vote
Accepted

Testing address(this) generates exception, how to obtain the correct value for address(this)

address(this) will return the address of the FundingTest contract, so of course it will be different from the Funding contract. The test should pass if you use address(funding), assuming your address ...
Alex Otsu's user avatar
  • 478
1 vote
Accepted

Fail with error 'TransferHelper: TRANSFER_FROM_FAILED', all gas used up

You did not approve USDT to the router address before attempting the swap. The USDT contract uses assert instead of revert or throw to cancel the transaction because of the lack of approval. Using ...
Undead8's user avatar
  • 3,630
1 vote
Accepted

Truffle script: how to run multiple assertions without stopping the program?

In case of multiple assertions, if one assertion fails then we will come out of the script. This problem can be solved by handling assertions using try-catch block. Zulfi.
zak100's user avatar
  • 1,416
1 vote
Accepted

Truffle console: assert.equal, how to use exception for testing purposes?

assert.equal will throw an exception when the values are not equal. assert.equal("foo", "bar", "Must be equal"); // AssertionError: Must be equal If it does not throw ...
Morten's user avatar
  • 6,082
1 vote

How to handle require/revert/assert errors properly

You can throw custom error messages using require() || revert() Example: require(CONDITION, "ERROR_MSG"); revert("ERROR_MSG"); I wouldn't use assert() as they spend all the left over gas. revert() |...
Matt Swezey's user avatar
  • 1,256
1 vote
Accepted

Access require message

You can modify this helper a little bit to check error.message should contain your message on the require https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/test/helpers/assertRevert....
Tony Dang's user avatar
  • 2,161
1 vote

Contract design by contract -> correctness and maximum gas consumption

I fear that there may not be a fool-proof answer to this, for the simple reason that over the course of forks, gas costs can change for certain op codes (as was done to stop spam attacks some time ago)...
Raghav Sood's user avatar
  • 4,060
1 vote
Accepted

Call a zero-initialized variable of internal function type

Variables can contain function pointers. If you leave them uninitialized and then try to call them, the call will fail because the variable was zero-initialized and therefore it does not contain a ...
Jesbus's user avatar
  • 10.5k
1 vote
Accepted

'Assert' and 'require' atomicity while internally calling another contract

First, if you are interested in checking the return value from ether transferring operation you should use send method instead of transfer which is meant to throw an exception on failure. assert(...
Jakub Wojciechowski's user avatar
1 vote

Difference between require and assert and the difference between revert and throw

Solidity has an SMTChecker which makes using assert very cool because it can prove that your invariants are true: Solidity implements a formal verification approach based on SMT (Satisfiability ...
eth's user avatar
  • 86.5k

Only top scored, non community-wiki answers of a minimum length are eligible