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));
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 ...
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') ? ...
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 ...
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 ...
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 ...
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.
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 ...
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, ...
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 ...
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 "...
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....
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 ...
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
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, ...
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
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);
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 ...
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, ...
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, ...
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:
...
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
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 ...
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',...
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 ...
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
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 ...
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 ...
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(...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
require × 98solidity × 64
contract-development × 12
assert × 11
error × 10
web3js × 9
truffle × 7
remix × 6
mapping × 6
payable × 5
transactions × 4
contract-design × 4
javascript × 4
revert-opcode × 4
custom-errors × 4
revert × 4
go-ethereum × 3
erc-20 × 3
ether × 3
gas × 3
dapps × 3
string × 3
solidity-0.8.x × 3
tokens × 2
events × 2