Skip to content

Instantly share code, notes, and snippets.

@nazt
Forked from dievardump/README.md
Created January 23, 2022 21:43
Show Gist options
  • Save nazt/5379d82da5f71af4d9abcafaa23d48ca to your computer and use it in GitHub Desktop.
Save nazt/5379d82da5f71af4d9abcafaa23d48ca to your computer and use it in GitHub Desktop.
Base file I use for my mainnet contracts to be compatible with OpenSea

Those two files allow to have easy gas-less trading on OpenSea

OpenSea Proxyregistry addresses:

Mainnet: 0xa5409ec958c83c3f309868babaca7c86dcb077c1 Rinkeby: 0xf57b2c51ded3a29e6891aba85459d600256cf317

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @title OpenSea contract helper that defines a few things
/// @author Simon Fremaux (@dievardump)
/// @dev This is a contract used to add OpenSea's support for gas-less trading
/// by checking if operator is owner's proxy
contract BaseOpenSea {
string private _contractURI;
ProxyRegistry private _proxyRegistry;
/// @notice Returns the contract URI function. Used on OpenSea to get details
// about a contract (owner, royalties etc...)
function contractURI() public view returns (string memory) {
return _contractURI;
}
/// @notice Helper for OpenSea gas-less trading
/// @dev Allows to check if `operator` is owner's OpenSea proxy
/// @param owner the owner we check for
/// @param operator the operator (proxy) we check for
function isOwnersOpenSeaProxy(address owner, address operator)
public
view
returns (bool)
{
ProxyRegistry proxyRegistry = _proxyRegistry;
return
// we have a proxy registry address
address(proxyRegistry) != address(0) &&
// current operator is owner's proxy address
address(proxyRegistry.proxies(owner)) == operator;
}
/// @dev Internal function to set the _contractURI
/// @param contractURI_ the new contract uri
function _setContractURI(string memory contractURI_) internal {
_contractURI = contractURI_;
}
/// @dev Internal function to set the _proxyRegistry
/// @param proxyRegistryAddress the new proxy registry address
function _setOpenSeaRegistry(address proxyRegistryAddress) internal {
_proxyRegistry = ProxyRegistry(proxyRegistryAddress);
}
}
contract OwnableDelegateProxy {}
contract ProxyRegistry {
mapping(address => OwnableDelegateProxy) public proxies;
}
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "./OpenSea/BaseOpenSea.sol";
/// @title Example of usage of BaseOpenSea
/// @author Simon Fremaux (@dievardump)
/// @notice This contract is only an example of how I implement BaseOpenSea
contract MyNFT is
Ownable,
ERC721,
BaseOpenSea
{
/// @notice constructor
/// @param openSeaProxyRegistry - the address of OpenSea Proxy Registry
/// @param contractURI_ - the contract metadata URI
constructor(address openSeaProxyRegistry, string memory contractURI_) ERC721('MyNFT', 'NFT') {
if (openSeaProxyRegistry != address(0)) {
_setOpenSeaRegistry(openSeaProxyRegistry);
}
if (bytes(contractURI_).length > 0) {
_setContractURI(contractURI_);
}
}
/// @notice Allows gas-less trading on OpenSea by safelisting the ProxyRegistry of the user
/// @dev Override isApprovedForAll to check first if current operator is owner's OpenSea proxy
/// @inheritdoc ERC721
function isApprovedForAll(address owner, address operator)
public
view
override
returns (bool)
{
// allows gas less trading on OpenSea
return super.isApprovedForAll(owner, operator) || isOwnersOpenSeaProxy(owner, operator);
}
/// @notice Helper for the owner of the contract to set the new contract URI
/// @dev caller needs to be owner
/// @param contractURI_ new contract URI
function setContractURI(string memory contractURI_) external onlyOwner {
_setContractURI(contractURI_);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment