Questions tagged [require]
The require tag has no usage guidance.
96
questions
0
votes
0
answers
14
views
How to make this Swap Function only work when requirement is fullfilled
Hi I'm new in contract developing. The following snippet includes a swap funtion for a swap on uniswap. This swap should only work if the requirement require((balanceOf(to) + amountToSwap) <= ...
0
votes
1
answer
26
views
Use of { } when importing libraries?
Can anybody tell me the exact use of { } in import/require library statements?
I've seen some other questions where people said it's to import just a part of a library, let's say the 'ethers' part of ...
2
votes
2
answers
172
views
How is this solidity function calling another solidity function without referencing it?
I wanted to create an NFT that chooses between an enum of 3 different NFT types. Also I wanted this choice to be decided at random so the minter doesn't know what type of NFT they'll get. I followed ...
3
votes
1
answer
55
views
Why does Remix does not show the description of the error anymore?
I am trying to run one of the methods of my smart contract using Remix and I am getting the following:
Why is not possible anymore to see if this is failing on a require, with the right description ...
1
vote
1
answer
465
views
Is there a way to handle `abi.decode` errors?
I am trying to decode a bytes value, and while writing tests, I was trying to pass invalid values to force an error.
Although I get the error missing revert data in call exception, I cannot find a way ...
3
votes
2
answers
995
views
Yul/Inline Assembly: Revert with a custom error message
I am learning Yul for my Bachelor's thesis and I am currently stuck on understanding a small code segment with require and revert functions.
A simple require function in Solidity ...
require(...
1
vote
1
answer
83
views
How can I verify that a function (IERC777.send) is executed successfully if it doesn't return anything but only emits an event
In a solidity smart contract function, I want to verify that an IERC777.send function has executed successfully before proceeding. Unfortunately, IERC777 has no bool return that I could use in this ...
0
votes
1
answer
92
views
Why isn't 'require' working?
In the create function, I need the _title to be required and the _description to be optional. But when calling create without _title & with _description, it executes without any errors, but the ...
2
votes
2
answers
325
views
How does the EVM internally revert the contract to the initial state when exceptions are present?
In error handling, assert and require statements revert the state of the contract to the initial state and then the transaction passes or fails depending on the code. I'm trying to understand how this ...
0
votes
1
answer
21
views
How to set a require conditions for nested mapping on struct array
I have a nested mapping that has a group id and the item id so that it can cancel the order based on these id's specifically. But how do I set a condition e.g revert the error if the id doesnt exist ...
3
votes
4
answers
696
views
Is it possible to return something when the condition inside the require fails?
Is it possible to return 0 inside a function in contract when the condition inside the require results to false?
contract MyContract {
function initialize() public payable returns (uint) {
...
0
votes
1
answer
120
views
Is the all of Gas Fee refunded if the transaction fails?
function burn(uint256 _value) public returns (bool success) {
balances[msg.sender] = balances[msg.sender].sub(_value);
totalSupply = totalSupply.sub(_value);
Burn(msg.sender, _value);
...
0
votes
2
answers
520
views
Does solidity revert global variables if function call fails?
I was reading about reentrancy attacks in smart contracts from here: https://ethereum-contract-security-techniques-and-tips.readthedocs.io/en/latest/known_attacks/ . Here they suggest that in order to ...
1
vote
0
answers
57
views
Why openzeppeling use require instead of cursom errors?
As far as I know custom errors are more gas efficient but for example the openzeppelin contracts still use require instead of custom errors
ERC20.sol
function decreaseAllowance(address spender, ...
2
votes
2
answers
40
views
What does 'require()' return?
When you call require() with an argument that equals false, require will throw an error, the rest of the code will not be executed and the transaction will revert.
If you call it with an argument that ...
2
votes
2
answers
735
views
What is best practice for transferFrom out of these two ways
I want to know which among these is best practice when dealing with transferFrom. that is when one should wrap it in require and when not to.
require(token.transferFrom(msg.sender, address(0),token ...
1
vote
2
answers
174
views
Please Resolve this error
Why I am getting the error in the modifier function its showing red underline under the _amount variable name.
I also want to ask about whether the require function works inside the modifier because ...
2
votes
1
answer
115
views
Require check after math operation ^0.8
I'm going over some protocol contracts, and I've seen they check pretty obvious math operations with a require() statement. This contract uses solidity 0.8.11
uint256 id = vaultCount;
vaultCount = ...
4
votes
4
answers
334
views
Can revert context be provided to frontend application for solidity's reverts?
There is solidity code that fails here:
error InvalidCheck(uint32 _check);
modifier someCheck(uint32 _check) {
if (check != _check) revert InvalidCheck(check);
_;
}
My frontend application fails ...
1
vote
2
answers
198
views
Why and when to user require(bool) on functions
I want to understand when (and why) it's necessary to use this statement :
require(sent, "failed to send ether")
i could just do somethink like this instead:
0
votes
1
answer
242
views
concatenated string reason message in require() not displaying
I'm using Solidity 0.8.13 and Truffle 5.5.6.
I'm just trying to debug a transaction call that fails then reverts when I do truffle migration.
I temporarily changed my onlyOwner modifier to make the ...
2
votes
1
answer
2k
views
How to make sure that a token exist?
I want to sell an NFT within my smart contract. But first, I want to check with a require statement if the token (NFT) exists or not before moving on to the next steps. I have tried require(ownerOf(...
0
votes
1
answer
66
views
Requires working strangely / truffle / tests
My contract
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.13;
import '@openzeppelin/contracts/access/Ownable.sol';
contract GuessAndWin is Ownable{
string public hint;
string ...
0
votes
1
answer
447
views
failed require() causes RangeError: Maximum call stack size exceeded
This makes little sense to me:
a require() statement with no function calls seems to be causing the RangeError: Maximum call stack size exceeded error, but only when the requirements are not met.
The ...
4
votes
3
answers
3k
views
When should I use `require` vs custom `revert` errors?
This applies only to solidity ^0.8 and onward.
Let's say I have a function like this:
function withdraw() external {
(bool success, ) = payable(msg.sender).call{value: address(this).balance}("&...
0
votes
2
answers
2k
views
Returning error message with variables
Suppose I have require(..., "error") but instead, I want the error message to be "error var" where var is a variable of type string, or uint.
What should I write?
1
vote
1
answer
311
views
Is it safe to check a uint mapping's value >0 in a require?
Context:
A common access check or modifier is to use something like:
require(boolMapping[msg.sender] == true); to ensure the caller is approved to use a function.
Since mapping (address => uint256) ...
0
votes
1
answer
25
views
To use OR( || ) or to create different functions?
The question is, should i use a require statement with logical OR,.... or should i create a new function for each condition?
How much in gas does it cost to check 6x logical or conditions?
function ...
3
votes
2
answers
2k
views
Solidity 8 best practice - using require or revert?
Trying to understand what are the use cases for a "require" statement and when would it be better to revert using an error:
For example
require(msg.sender == owner);
Or
if (msg.sender != ...
0
votes
1
answer
57
views
Should I use require if I could substitute it for an if statement
I'm having a hard time defining for myself what would be the best approach for when to use require and when to use an if statement.
Let's take the following method for sake of example:
function ...
2
votes
0
answers
410
views
require error message with parameters
What is the best practise to write require error messages with parameters?
Currently I have
uint8 public constant MAX_PER_USER;
<..>
require(
balance + value <= MAX_PER_USER,
...
0
votes
1
answer
22
views
Require array lenght no working
I have the following function
function mint(uint256[] memory tokens) public payable {
require(saleIsActive, "Sale is not active");
require(tokens.length == 0, "No tokens ...
0
votes
1
answer
1k
views
web3.js:3 Uncaught ReferenceError: require is not defined
I've been confused by the web3.js all day.
I created the web.js for setting and initialize Web3, and it appears the following message:
And I install web3 by npm install web3, the version of web3 is 1....
0
votes
0
answers
188
views
Solidity: Difference between require and if, need to use address when calling function on a smart contract
I have a question if the two functions below differ as I have written approach 2 but no 1 was the solution. Earlier in the contract Dangerous d; is defined which refers to a different smart contract.
...
-1
votes
1
answer
127
views
Why check for zero-account?
I see require(account != address(0x0)); as somewhat of a boilerplate check for some functions. Is the purpose just to add a check that prevents the creation of a new contract?
0
votes
1
answer
290
views
What is the "correct" way to check if an address is zero? [duplicate]
A similar question was asked here: What is the correct way to check if an address is zero? but it was back in 2017.
What's the purpose of require(account != address(0)); and is it still relevant? I ...
0
votes
3
answers
1k
views
Require a specific amount of Ether inside the payable function
I have a payble function that has some logic. I want that logic to be triggered only if the received amount is equal to the amount specified when the contract was created.
Solidity v0.7.6
receive() ...
1
vote
0
answers
14
views
what is _; in solidity, well after every require statement this is follwed and why? [duplicate]
modifier onlyOwner() {
require(isOwner());
_;
}
what is the use of the the underscore here and why it is used after almost every require statements!
1
vote
1
answer
34
views
How can I ensure a player matches another player's bet?
First, I want someone to send a bet (money) to the contract.
Then I want someone to match the original bet.
(There are many more safety checks in the contract. I just removed them to keep this as ...
2
votes
3
answers
2k
views
how to set a require for a string to not be null
I want to write a require condition for string that string variable could not be null. How to write because string is not taking the != operator
function createOrder(address _seller, string memory ...
6
votes
2
answers
288
views
Why do Solidity audits include checks that function address arguments are not equal to 0x0?
I've recently completed an audit for some Solidity contracts, and one of the checks the auditor included for all of the contracts was to make sure that address function arguments are not equal to the ...
0
votes
0
answers
476
views
Error message shown as [object Object] when use require, how to get actual error message string in Remix IDE Console?
I have used require function as:
require(owner == msg.sender, "Sender must be owner");
But in the remix console I got,
transact to Contract.setAdmin errored: [object Object]
What should I ...
8
votes
4
answers
18k
views
How to get reason revert using web3.eth.call
I have a smart contract with required message and pretend to use the required/validation message in front end.
require(bytes(hashAlreadyRegistered[hash]).length == 0, "Hash already registered&...
0
votes
1
answer
110
views
Mapping didn't storing unique values
I wrote this piece of code with the goal to store unique documents hash. Used mapping to accomplished that.
pragma solidity ^0.5.16;
contract DocumentHash{
string public documentHash;
...
0
votes
1
answer
220
views
Could you explain what this require conditions means in this smart contract
I would like to know what exactly this require condition do in this smart contract:
function placeBet(uint betMask, uint modulo, uint commitLastBlock, uint commit, uint8 v, bytes32 r, bytes32 s) ...
0
votes
1
answer
187
views
Test for Require statement in Truffle
I wrote a function that generate a key value.
the function codes is here
mapping(string => bool) processedHash;
mapping(uint => bool) HashList;
function generateHash(uint256 ...
4
votes
2
answers
2k
views
Read message of require / revert statement in app.js
In my App.js I have called the smart contract function and the require statement in the function is not satisfied, so how do I get the message of that require statement in my App.js?
1
vote
2
answers
491
views
Correct syntax for require required
I am getting following error message with require statement:
Error: Wrong argument count for function call: 0 arguments given but
expected 1. This function requires a single bytes argument. Use ""...
1
vote
1
answer
2k
views
ERROR: Web3 not defined
I'm trying to build a browser UI to interact with my Solidity smart contract that is deployed on my local Ganache blockchain. But I can't seem to get my index.html file to connect to Metamask. I keep ...
0
votes
1
answer
757
views
Always raising "Gas required exceeds allowance or always failing transaction" when the condition of require() is not met
I tried to run the following code to check the error message when the condition of require() is not met.
pragma solidity ^0.5.16;
...
function addUser(string memory name) public returns (int) {
...