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 ...
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 ...
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 ...
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 ...
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, ...
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 :-)
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
...
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 ...
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 ...
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 ...
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 ...
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.
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.
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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.
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 ...
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() |...
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....
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)...
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 ...
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(...
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♦
- 86.5k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
assert × 29solidity × 15
require × 11
web3js × 3
contract-development × 3
truffle × 3
remix × 3
testing × 3
revert-opcode × 3
contract-design × 2
javascript × 2
evm × 2
error × 2
truffle-test × 2
exceptions × 2
truffle-console × 2
chai × 2
foundry × 2
transactions × 1
gas × 1
dapp-development × 1
events × 1
security × 1
contract-debugging × 1
ethereumjs × 1