Created
February 15, 2022 02:26
-
-
Save karmacoma-eth/025c6594187c5cea9f9ca823ab22e907 to your computer and use it in GitHub Desktop.
What if a contract returns different data when viewed off-chain vs during a transaction?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity >=0.8.0; | |
contract AdversarialRoyalties { | |
bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a; | |
function supportsInterface(bytes4 interfaceID) external pure returns (bool) { | |
return interfaceID == _INTERFACE_ID_ERC2981; | |
} | |
function isEvaluatedOffchain() public view returns (bool) { | |
return tx.origin == address(0) | |
|| tx.gasprice <= 1 | |
|| gasleft() > 30_000_000; | |
} | |
function royaltyInfo( | |
uint256 /* _tokenId */, | |
uint256 _salePrice | |
) public view returns ( | |
address receiver, | |
uint256 royaltyAmount | |
) { | |
receiver = address(0xdEaD); | |
royaltyAmount = isEvaluatedOffchain() | |
? 0 // looks like we're in getRoyaltyView(), so play nice | |
: _salePrice - 1; // during an actual transaction, be nasty | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on this research:
https://karmacoma.notion.site/Detecting-we-re-evaluated-off-chain-aa5fb334f4e640aead68784ea065daf9