Skip to main content
8 votes
Accepted

Syntax: How do you require a string to be null (or anything but null)?

in the first case I would check the string length require(bytes(hash).length > 0); in the second case just check against the void address require(hashes[hash] == address(0x0));
qbsp's user avatar
  • 4,377
7 votes
Accepted

Solidity multiple vs single require

Comparing the bytecode generated by each approach, the second is 6 bytes longer - basically because the REVERT opcode and associate memory stuff appears 3 times instead of once. But since these ...
benjaminion's user avatar
  • 9,317
7 votes
Accepted

How to get reason revert using web3.eth.call

Try this, async function getRevertReason(txHash){ const tx = await web3.eth.getTransaction(txHash) var result = await web3.eth.call(tx, tx.blockNumber) result = result.startsWith('0x') ? ...
Meet Siraja's user avatar
7 votes

How to get reason revert using web3.eth.call

Web3 supports revert strings now: https://web3js.readthedocs.io/en/v1.2.8/web3-eth.html#handlerevert. You can activate it easily by setting web3.eth.handleRevert = true. Now when you use call or send ...
Markus - soliditydeveloper.com's user avatar
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
6 votes

Solidity 8 best practice - using require or revert?

Like you mentioned, both can be used. The gas consumed by both are the same too. (calculations using forge suggests its 17 units, but not very sure about this). require(condition) under the hood uses ...
pbsh's user avatar
  • 2,491
6 votes
Accepted

When should I use `require` vs custom `revert` errors?

Custom errors are more gas efficient than using require with a string explanation. So ideally you'd always use this over require.
Patrick Collins's user avatar
6 votes
Accepted

Yul/Inline Assembly: Revert with a custom error message

This kind of code: require(_amountToRaise > 0, "Amount to raise smaller than 0"); Produces revert with bytes equivalent to calldata of the following method: function Error(string memory ...
k06a's user avatar
  • 3,096
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
5 votes
Accepted

Custom error message in require() not showing up?

You can't currently use web3 to inspect error messages thrown from revert. In the future it may be possible though, so adding these to your smart contract helps to future proof against this ...
Adam Dossa's user avatar
  • 1,287
5 votes

Why do Solidity audits include checks that function address arguments are not equal to 0x0?

This check is considered important because the following combination applies: Address 0 is neither an externally-owned account nor a smart-contract account Address 0 is a value which can be "...
goodvibration's user avatar
5 votes

Solidity 8 best practice - using require or revert?

The docs mention that require and assert are convenience functions that can be used to check for conditions and throw an exception if the condition is not met (see https://docs.soliditylang.org/en/v0....
Richard's user avatar
  • 5,144
5 votes

What is best practice for transferFrom out of these two ways

Jeremy's answer is incorrect. While it's true that Openzeppelin's implementation does never return false, other implementations might do it. According to the EIP-20, a token can return false, and it's ...
0xSanson's user avatar
  • 3,947
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

Syntax: How do you require a string to be null (or anything but null)?

Just to add to @mirg's valid answer. As you know, in Solidity variables are never null. Instead, if they are not initialized, they contain their default value. For example for uint this is 0. So, ...
Lauri Peltonen's user avatar
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

Setting time requirements in Solidity

You're totally right! The way you do has no problem. You can make a test with small time like require (block.timestamp >= today + 60 seconds);
Tony Dang's user avatar
  • 2,161
4 votes
Accepted

Is there a way to know the REAL msg.sender?

On a real blockchain, you can't send from whatever account you want. You can only send as an address for which you know the private key. So "the real msg.sender" is just msg.sender. It cannot be ...
user19510's user avatar
  • 28.1k
4 votes
Accepted

Should I use require in view functions?

It is as a matter of style and context. It is not possible to maintain the confidentiality of information stored in the contract, so as an access-control mechanism, it is pointless. In my opinion, ...
Rob Hitchens's user avatar
  • 55.6k
4 votes

Yul/Inline Assembly: Revert with a custom error message

First I think you mixed some hexadecimal notation with some decimal ones, the correct decomposition should be : if iszero(gt(_amountToRaise, 0)) { mstore(0x80, shl(229, 4594637)) mstore(0x84, ...
hroussille's user avatar
  • 7,901
3 votes

Does the require function need more gas when called with a message?

I did small test in Remix Here is small contract contract Sharer { function sendHalf(address addr) public payable { require(msg.value % 2 == 0); } } Gas estimation without message: ...
Aquila's user avatar
  • 1,822
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

Unit testing for assert()/require() in Truffle/Solidity

Do you use TestRPC or Geth for your tests? They can behave different and for my unit testing where it should fail, I follow this instruction: When TestRPC and Geth throw, they behave in a ...
Luiz Soares's user avatar
  • 1,054
3 votes

Unit testing for assert()/require() in Truffle/Solidity

require('chai') .use(require('chai-as-promised')) .should(); const ERROR_MSG = 'VM Exception while processing transaction: revert'; ContractName = await ContractName.new(); it('should never be zero',...
Victor Baranov's user avatar
3 votes
Accepted

Correct syntax for require required

Your problem is not in the require statement, but in the addr1.call.value(addr2)() function call inside it. In solc 0.4.x, this function would take no input arguments and return one output value of ...
goodvibration's user avatar
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

When should I use `require` vs custom `revert` errors?

When reviewing the Impostors NFT contract (which exclusively uses custom errors with revert), they noted this in regards to using custom errors: It saves bytecode to revert on custom errors instead ...
luxo's user avatar
  • 228
3 votes
Accepted

What does 'require()' return?

The require function does not return anything. You can't use it as if(require(false)) {}, it will not compile. If the condition passed to the require function is true, nothing happens, and the code ...
Jeremy Then's user avatar
  • 4,649
3 votes

Is it possible to return something when the condition inside the require fails?

Well, you can try to use try-catch Currently, Solidity supports different kinds of catch blocks depending on the type of error. If the error was caused by revert("reasonString") or require(...
matank001's user avatar
  • 820
3 votes
Accepted

How can I verify that a function (IERC777.send) is executed successfully if it doesn't return anything but only emits an event

You do not need verification. It either succeeds or the whole transaction will revert. In ERC20 source code you can see it conveniently returns True everytime: GithubLink. When the ERC20Token.transfer ...
minhhn2910's user avatar
  • 1,840

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