Questions tagged [require]
The require tag has no usage guidance.
80
questions
1
vote
0
answers
24
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 ...
0
votes
2
answers
16
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
39
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 ...
0
votes
1
answer
166
views
How to make sure that a token is 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
37
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
113
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 ...
1
vote
3
answers
250
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
137
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?
0
votes
0
answers
20
views
ganache not failing transaction on failed require statement
I am running ganache 2.5.4 and am deploying contracts built with solc 0.8.11.
I have some constraint checking in my smart contract that checks values in a require statement at the beginning of the ...
0
votes
0
answers
143
views
Comparing msg.value with 'require' can be really confusing
First of all, I am using BSC, the solidity is ^0.8.0. The contract is simple enough, there are only 2 functions here, one is using require, another is not. The mission of this contract is to test msg....
1
vote
1
answer
52
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
21
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 ...
0
votes
2
answers
103
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
20
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 ...
0
votes
0
answers
82
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
20
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
704
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
76
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.
...
0
votes
1
answer
28
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
49
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
2
answers
633
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
13
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
24
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 ...
0
votes
2
answers
837
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 ...
5
votes
2
answers
128
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
410
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 ...
6
votes
2
answers
11k
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
77
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
155
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
126
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 ...
3
votes
2
answers
1k
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
281
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
724
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) {
...
1
vote
2
answers
5k
views
How to diagnose a "Error: Transaction has been reverted by the EVM"?
This the error intercepted from my javascript, using web3.js
Error: Transaction has been reverted by the EVM:
{
"blockHash": "...
1
vote
0
answers
74
views
Require not working on geth private node
I have created this contract to manage a votation:
pragma solidity ^0.5.7;
contract Votacion{
mapping(uint => address[]) private votosCandidatos;
uint[] listaCandidatos;
bool abierta;
address ...
2
votes
1
answer
90
views
A Way Around Restricted Getters
Consider a getter function with a restriction on who can read it, like
function getData()
public
view
returns (uint x)
{
require(msg.sender = '0x123..123');
x = data;
}
This ...
0
votes
2
answers
504
views
Check if contract exists in list
I've got a contract which creates another contracts and stores them in a list. How can I check that contract with specific index already exists?
import "./User.sol";
contract Main is Ownable {
...
0
votes
1
answer
159
views
How do I check If a map entry doesn't exist for a given sender?
I have a map such as:
mapping (address => bytes32) private myMap;
Later in the code, in a function, I want to ensure my sender already has an entry in that given map.
Can I do this?
require(!...
2
votes
3
answers
1k
views
Should I use require in view functions?
When I call a solidity function to get a struct element, is it a best practice to use require and check if the element exists or should I just return the empty values?
Without require:
function ...
0
votes
1
answer
229
views
Geth Node doesn't revert a transaction not satisfying a require
i'm testing my smart contract on remix with local ganache cli, when i try to test case that doesn't satisfy a require on the SC i correctly receive message "transact to Contract.function errored: VM ...
2
votes
1
answer
877
views
How to handle require/revert/assert errors properly
I'm trying to find an easy pattern to revert/rollback a write transaction.
The first idea was to log a GenericErrorEvent but then I noticed that it will never be emited if any require/revert/assert ...
1
vote
1
answer
525
views
How to get require() to accept decimal value of ether in payable functions in Solidity?
On using,
require(msg.value == registerFeeMap[msg.sender] /_currentRate)
(fractional ETH value calculated)
require(msg.value == registerFeeMap[msg.sender]*10**18/ _currentRate)
(ETH converted to ...
1
vote
2
answers
269
views
How to use function arguments of payable function for require statement?
function createCampaign(uint deposit, address ll) public payable{
dpst = deposit;
require(msg.value > (40*dpst)/100 wei);
address newCampaign = address((new Campaign).value(msg.value)(...
2
votes
2
answers
103
views
Validate bytes16 argument size
I have a function which accepts one bytes16 argument.
function start(
bytes16 reference
)
I have some events which I use to track behaviour, using reference as the index. Everything works ...
2
votes
1
answer
643
views
VM Exception while processing transaction: revert message
When a solidity smart contract reverts with a message due to a failure on a require statement, the browser calling the smart contract returns "Error: VM Exception while processing transaction: revert"....
0
votes
1
answer
106
views
Is there a way to know the REAL msg.sender?
I want to know the real msg.sender of the transaction. Which this means is, if I use truffle console I can change the msg.sender by using {from: another address}. So let's say my address is A, and ...
1
vote
1
answer
121
views
Why does this function not need a require statement?
Why does the following function not need a require statment?
function transferFrom(address from, address to, uint256 value) public returns (bool) {
_allowed[from][msg.sender] = _allowed[from][...
1
vote
1
answer
1k
views
Emit event while require statement fails
I would like to be able to log a result of a condition:
if(time > 5) {
emit Result("time is larger than 5");
} else {
emit Result("time is less than 5");
}
This will work just fine, but ...
2
votes
2
answers
88
views
Does the require function need more gas when called with a message?
If I add a message to require as a second parameter, will it increase gas consumption?