Questions tagged [modifiers]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
66
votes
2answers
24k views

Solidity: What is the difference between `view` and `constant`?

Solidity 0.4.16 introduced the view and constant function modifiers. The documentation says: constant for functions: Same as view. Does this mean view is just an alias for constant? If so, why do ...
42
votes
1answer
11k views

Are underscores `_` in modifiers code or are they just meant to look cool?

I often see _ in modifiers modifier onlyOwner() { if (msg.sender != owner) throw; _ } Does it execute any code or is it meant to make the code easier to read ?
18
votes
2answers
6k views

Can Solidity function modifiers access function arguments?

I would like to have a modifier that looks like this: modifier restrictTargetType(uint contractType) { if (contractTypes[target] != contractType) { throw; } _ } Where target is an address ...
14
votes
1answer
1k views

Bad Jump Destination error

I wrote what I thought was a dead simple contract - a saving jar... you add value ("save") into the jar and when the balance of the contract reaches a certain amount ("target"), then I am able to ...
9
votes
1answer
6k views

calldata keyword as parameter in solidity v0.5.0 function?

What purpose does the "calldata" serve in this parameter _owners? Is it a new keyword? function setup(address[] calldata _owners, uint256 _threshold, address to, bytes calldata data) Taken from ...
8
votes
2answers
3k views

What's the order of execution for multiple function modifiers? [duplicate]

I don't know why nobody is asking this question (I've done my homework and did tons of Googling for an answer) but I am having hard time understanding how function modifiers actually work. Sure it's ...
7
votes
2answers
132 views

Is there a widely-accepted convention for when to use a modifier and when to use a simple assertion?

I've read this answer, which is good in general, but it doesn't quite tell me when to use a modifier (at the function declaration) and when to use assert/require (inside the function). I do ...
6
votes
1answer
660 views

Why use throw in modifiers instead of conditional entrance?

Almost every tutorial advises a code like this for a permission modifier. modifier isAdmin() { if(msg.sender != owner) { throw; } _ } But wouldn't it be simpler to use it without ...
5
votes
2answers
4k views

Using require or modifier?

If you create a function that you only want the owner to interact with would you use require(msg.sender == owner) or modifier onlyOwner() { require(msg.sender == owner); _; } Which ...
5
votes
1answer
3k views

Override parent method and modifiers

For example, we have a contract: contract A { modifier checkCaller() { require(msg.sender == 0x123); _; } function doSmth() checkCaller {} } We make child contract and ...
5
votes
2answers
318 views

Solidity: Can modifiers be marked payable?

Can I have a modifier that enforces the amount sent by a user in a call to a function, and if it is below a certain amount, throw? I want to do something like the following: uint256 n; // Set some ...
4
votes
1answer
524 views

Will payable modifier throw if no ether is sent to it?

Should I add a check to make sure Ether was sent along with the transaction or is that redundant?
4
votes
1answer
239 views

Call parent class modifier possible?

I want to do the following: contract Base { bool public active; modifier canSetMetadata() { require(active == false); _; } } contract BaseWithMetadataControl is Base { ...
4
votes
1answer
302 views

Use modifier function to create heirachy of smart contracts

I have 3 contracts that I want to implement. The contracts are master , admin and customer. So the master contract is initiated by a single person, the owner of the private chain (based on geth). The ...
3
votes
1answer
156 views

How to create modifers for permissioned access

I have a citizenship persona contract which acts as a personal identity file which is under the control of the user. Personas can make other personas in the system into trustees or allies. As part of ...
3
votes
2answers
2k views

How the contract storage data saved in blockchain?

contract { mapping (bytes32 => uint256) private balances; function setValue(uint256 a) { uint index = sha3(msg.sender); balances[index] = a; } } I put a "private" before balances , ...
3
votes
1answer
238 views

error: invalid opcode using modifier

Im getting the following error when trying to run my js test file using truffle: Error: VM Exception while executing eth_call: invalid opcode The js test file is the following: var DebitService = ...
2
votes
3answers
336 views

Why cannot I get uint returns without 'view' modifier?

I have very simple contract as.. contract myContract { function foo() public returns(uint) { return 10; } function foo1() public view returns(uint) { return 10; } ...
2
votes
2answers
385 views

How does the re-entrancy guard modifier work?

Following is code for a re-entrancy guard, but not sure if I understand it properly: contract Test { using SafeMath for uint256; uint256 private counter = 1; modifier entrancyGuard() { ...
2
votes
2answers
622 views

Is it possible to use a modifier from a different contract?

Is there a way to use modifiers from a different contract? My attempt did not work. contract con1{ modifier supermod(uint _value){ require(_value == 1); _; } } contract con2{...
2
votes
1answer
180 views

Are Solidity modifiers functionally equivalent to Python decorators?

I understand that in Solidity if a function has a modifier, this modifier runs first, prior the the logic within the function running. This is done for checks of ownership, checks of state, etc. In ...
2
votes
2answers
366 views

Solidity modifiers: good or bad?

I've heard arguments both for and against Solidity modifiers. On the one hand, they make the code very readable, and the intents behind the calls are very clear. On the other, they increase the ...
2
votes
2answers
270 views

Modifiers causing internal compilation error

My modifiers are throwing internal compilation errors: Internal compiler error: Identifier type not expected in expression context. modifier critical { if (mutex) throw; mutex = true; _; ...
2
votes
1answer
132 views

Is it possible to use a modifier so that certain functions can only be executed by the contract, itself?

How can you program a contract so certain functions can only be called by other functions within the contract? Would you use a modifier? In the example below I'd want a user to have to call ...
2
votes
1answer
526 views

Can't call function with modifier from another contract even if the owner himself calls

So I have multiple contracts that use the same modifier. contract Token is ERC20Basic { using SafeMath for uint256; mapping(address => uint256) balances; Owned private owned; /// @dev `owner` ...
2
votes
1answer
610 views

Solidity store storage pointer in storage variable for accessing it from distinct functions

In a contract, using Solidity, is it possible to store a pointer to a storage variable in a storage variable (or somewhere else that may be suitable) and access it from distinct functions? Consider ...
2
votes
2answers
702 views

Could not compile source code. Expected primary expression in modifier onlyAdmin

Why do i get this error message? Code: pragma solidity 0.4.8; contract Adminstrator { address public admin; function Adminstrator() { admin = msg.sender; modifier onlyAdmin() { ...
2
votes
0answers
83 views

UnhandledPromiseRejectionWarning: Error: nonce generation function failed or private key is invalid in Ganache

In Go I am using go-ethereum to read data from the ethereum smart contract. My smart contract is maintained with truffle. I am trying to fetch candidate information based on candidate id. I have a ...
2
votes
2answers
261 views

Can we apply modifier to an event in solidity just like function?

Let's say as per business I want to restrict certain users(addresses) from seeing the logs for a contract. I know there is "modifier" which can be set to function, to give access rights but Can we do ...
1
vote
2answers
138 views

Should I use the 'external' modifier to return a large array?

If I need to access a large array from my contract... say for my front end. Should I be using external to make it cheaper for myself? A comment by William Entriken on this question is what got me ...
1
vote
1answer
756 views

Is it Okay to use if else in modifiers?

In solidity use of require and assert is recommended than using if else conditions but in every solidity example where modifiers are used instead of require if else is used. So is it Okay to use if ...
1
vote
2answers
168 views

Function modifier seems to be not working

I have a simple contract as below. pragma solidity ^0.4.9; /* Contract accepting ethers during 10 minutes */ contract SimpleBidding { address receivingAccount; uint deadline; struct ...
1
vote
1answer
131 views

Solidity: call from function to modifier with array [closed]

I have the following problem, I'm trying to call a function from Solidity function to Solidity modifier: contract B is A function checkIfIsValid() external pure onlyValidAddresses([manually put ...
1
vote
1answer
233 views

Calling onlyowner functions using owner's private key

I need to call a function with onlyOwner modifier in my smart contract via web3.js. I want a user of my site to be able to send their information to a white list, but it sends the transaction from ...
1
vote
2answers
300 views

modifier consumes all gas

Given a modifier: modifier yetNotCalculated() { require(calculated == false); _; } and then using it in my function: function calculateResult() public yetNotCalculated { calculate(); ...
1
vote
1answer
481 views

how to use multiple modofier to form combinations of boolean conditions?

I have read this article and this SO. And I understand the functionality of modifier. When modifiers are placed on a function with a spacing they will be evaluated in the order of placing thus ...
1
vote
1answer
132 views

Splitter contact: is payable modifier missing?

I am using the geth javascript console, trying to split pre-fork ETH using the ReplaySafeSplit v2 contract 0xaBbb6bEbFA05aA13e908EaA492Bd7a8343760477 (code here). I am doing this as follows: ...
1
vote
0answers
287 views

Modifier to only allow access via staticcall

I'm trying to create a modifier that only allows a function to be called using staticcall. Using the view declaration in Solidity 0.5.0 guarantees that a function won't change state, but it still ...
1
vote
1answer
571 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 ...
1
vote
0answers
188 views

Modifiers: can I pass an array or specify that not all of them have to pass, but one is enough (OR functionality instead of AND)

Say I want to restrict a function to only authors and editors. And later on, I want to restrict another function to authors and contributors only. So ideally, I would add a modifier like this: ...
1
vote
0answers
22 views

Can't check if time is over using NOW

I'm coding a smart contract for an ICO and I have a problem. I have a modifier wich checks if NOW > timestamp, but it's not working for some reason. Here is the function: function DeleteModerator(...
1
vote
2answers
642 views

function () public payable modifier can trigger actions when sending tokens or only works when sending ether?

Modifier payable triggers an action when an address sends ether to a contract. For example: function () public payable { require(msg.data.length == 0); DoSomething(); } Is this ...
1
vote
1answer
217 views

Contract Security - When to include modifiers

Just some pretty high level questions here. When is it best to include modifiers? Are there any safeties needed to be placed on the initial contract creation function? In the last function of my ...
1
vote
1answer
447 views

Solidity: Invert modifers?

I can already implement a basic onlyOwner modifier. But what if I have an onlyOwner modifer already in my function and I want to specify that a function can be called by anyone other than the owner in ...
0
votes
2answers
32 views

Can you ! a modifier?

I have a modifier for a few functions that check if a mapping exists, for one function I need the modifier to verify the mapping doesn't exists, is there any way to !modifier as you would a function ...
0
votes
1answer
528 views

Is it possible to send Ether using delegatecall?

I tested this in Remix and it seems that the ether remains in the delegator contract and doesn't get forwarded. Using the code below to test: contract SomeContract { address public sender; ...
0
votes
1answer
40 views

When are modifiers evaluated?

I have a code like modifier inTime { require(now <= starttime + duration);//easy peasy _; } applied to a fallback function like this function() payable inTime{ depositDone(msg.sender,...
0
votes
1answer
385 views

Solidity modifiers in library?

Isn't it possible to define modifiers in libraries? I'm really asking myself why, because thats some functionality you would need over and over again. If I would like to hypothetically implement some ...
0
votes
2answers
130 views

Is it safe to remove the “view” modifier from the function “getTokenAmount” in the OpenZeppelin crowdsale contract?

This function from the openzeppelin crowdsale sample is marked view as seen here: // Override this method to have a way to add business logic to your crowdsale when buying function getTokenAmount(...
0
votes
1answer
41 views

Read storage struct from a modifier

If i read a struct(storage) Human storage human = humans[2]; in my modifier is there a possibility that the function that uses this modifier can use the "human"? Otherwise i have to scope the storage ...