question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a modifier to prevent calling from another contract.

See original GitHub issue

🧐 Motivation

A lots of contract security issues need to be exploited by calling from another contract, so we often protect our contract by preventing contract calling some critical functions.

šŸ“ Details

We can provide a modifier to do this check, although address.code.length == 0 do the same effect, that cost more gas.

modifier callerIsUser() {
     require(tx.origin == msg.sender, 'The caller is another contract.');
      _;
}

function mint() public callerIsUser {
  ...
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Amxxcommented, Mar 1, 2022

Some users don’t use EOA. They use Argent, Gnosis, or any other smart-contract-based wallets. By forcing tx.origin == msg.sender you are preventing these users for interacting with you. I would not call that fair.

I am yet to see a case where using such a modifier was really needed. All usages I’ve seen so far were trying to mitigate ā€œbadā€ design decisions.

0reactions
themezcommented, Mar 2, 2022

I got your points now, thanks @Amxx @frangio I might use tx.origin == msg.sender to exclude contract user or even real smart wallet users sometimes, for example in some case I’ll just use block.timestamp as random seed instead of VRF which is expensive. But I understand your concern for including such a modifier in library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can you call a function with a modifier in another contract?
I am getting an error Error: Undeclared identifier: getModified for getModified in Remix. I tried reading the docs but they also dont mentionĀ ......
Read more >
Solidity Tutorial: all about Modifiers | by Jean Cvllr | Coinmonks
In Solidity, Modifiers express what actions are occurring in a declarative ... As a result, this prevent other smart contracts to call theĀ ......
Read more >
Access Restriction | solidity-patterns - GitHub Pages
This is a good example for the various possibilities modifiers can provide. Combined with the previous modifier the contract can only be bought...
Read more >
Solidity: call modifiers from other smart contracts
My current workaround is the following: - In my token smart contract, I added an "intermediate" function. That function is directly calledĀ ...
Read more >
Contracts — Solidity 0.8.17 documentation
Calling a function on a different contract (instance) will perform an EVM function call and ... but the latter provides an external view...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found