Verified contractExactMIT
PerfectloopGiveaway
Address0xb3b355a797b23baee5b26ca0b1f29da69eb2247c
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.30
- Same code
Only this contract
Details
- Compiler
- solc 0.8.30+commit.73712a01
- Language
- solidity
- Optimization
- Yes, 10,000 runs
- EVM version
- osaka
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,349,488
- Creation transaction
- 0x967d6faabd…4c25c2fe37
5 files
- src/PerfectloopGiveaway.solmain5.42 KB
- dependencies/@openzeppelin-contracts-5.6.1/access/Ownable.sol3.1 KB
- dependencies/@openzeppelin-contracts-5.6.1/token/ERC721/IERC721.sol4.93 KB
- dependencies/@openzeppelin-contracts-5.6.1/utils/Context.sol963 B
- dependencies/@openzeppelin-contracts-5.6.1/utils/introspection/IERC165.sol871 B
src/PerfectloopGiveaway.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;
import {Ownable} from "@openzeppelin-contracts-5.6.1/access/Ownable.sol";
import {IERC721} from "@openzeppelin-contracts-5.6.1/token/ERC721/IERC721.sol";
/// @title PerfectloopGiveaway
/// @notice Gives away a single NFT to one address from a fixed list of holders.
/// @dev Flow:
/// 1. Owner deploys with the holder list and approves this contract for `tokenId`.
/// 2. Owner calls `chooseSeedBlock`, committing to the hash of the *next* block (unknown at commit time).
/// 3. Anyone calls `storeSeedHash` once that block is mined (must be within 256 blocks, the `blockhash` window).
/// 4. Owner calls `giveaway`, which picks `holders[seedHash % holders.length]` and transfers the NFT from the owner.
contract PerfectloopGiveaway is Ownable {
/////////////////////////////////////////////////////////////////////
// STORAGE
/////////////////////////////////////////////////////////////////////
/// @dev `blockhash` only returns non-zero values for the most recent 256 blocks
uint256 public constant BLOCKHASH_WINDOW = 256;
address public immutable nftAddress;
uint256 public immutable tokenId;
uint256 public seedBlockNumber;
bytes32 public seedHash;
address public winner;
address[] public w0rmHolders;
/////////////////////////////////////////////////////////////////////
// EVENTS
/////////////////////////////////////////////////////////////////////
event SeedBlockChosen(uint256 indexed seedBlockNumber);
event SeedHashStored(uint256 indexed seedBlockNumber, bytes32 seedHash);
event Giveaway(address indexed winner, uint256 indexed selectedIndex, address indexed nftAddress, uint256 tokenId);
/////////////////////////////////////////////////////////////////////
// ERRORS
/////////////////////////////////////////////////////////////////////
error NoHoldersAdded();
error SeedBlockAlreadyChosen();
error SeedBlockNotChosen();
error SeedBlockNotReached();
error SeedBlockExpired();
error SeedHashAlreadyStored();
error SeedHashNotStored();
error AlreadyGivenAway();
/////////////////////////////////////////////////////////////////////
// CONSTRUCTOR
/////////////////////////////////////////////////////////////////////
constructor(address initOwner, address nftAddress_, uint256 tokenId_, address[] memory w0rmHolders_)
Ownable(initOwner)
{
if (w0rmHolders_.length == 0) revert NoHoldersAdded();
nftAddress = nftAddress_;
tokenId = tokenId_;
w0rmHolders = w0rmHolders_;
}
/////////////////////////////////////////////////////////////////////
// FUNCTIONS
/////////////////////////////////////////////////////////////////////
/// @notice Commits to the hash of the next block as the randomness seed
/// @dev Can only be called once, unless the previous seed block expired (>256 blocks) without its hash being stored
function chooseSeedBlock() external onlyOwner {
if (seedBlockNumber != 0) {
if (seedHash != bytes32(0) || block.number <= seedBlockNumber + BLOCKHASH_WINDOW) {
revert SeedBlockAlreadyChosen();
}
}
// the hash of the current block is not available during execution and the
// next block's hash is unknown at this point, so commit to the next block
seedBlockNumber = block.number + 1;
emit SeedBlockChosen(seedBlockNumber);
}
/// @notice Stores the hash of the seed block. Callable by anyone so the result can be locked in publicly.
function storeSeedHash() external {
if (seedBlockNumber == 0) revert SeedBlockNotChosen();
if (seedHash != bytes32(0)) revert SeedHashAlreadyStored();
if (block.number <= seedBlockNumber) revert SeedBlockNotReached();
bytes32 hash = blockhash(seedBlockNumber);
if (hash == bytes32(0)) revert SeedBlockExpired();
seedHash = hash;
emit SeedHashStored(seedBlockNumber, hash);
}
/// @notice Selects the winner from the seed hash and transfers the NFT from the owner to them
/// @dev Owner must hold `tokenId` and have approved this contract (or set approval for all)
function giveaway() external onlyOwner {
if (seedBlockNumber == 0) revert SeedBlockNotChosen();
if (seedHash == bytes32(0)) revert SeedHashNotStored();
if (winner != address(0)) revert AlreadyGivenAway();
// store index based on seedhash and modulo of holders array
uint256 selectedIndex = uint256(seedHash) % w0rmHolders.length;
address holder = w0rmHolders[selectedIndex];
winner = holder;
// transfer nft to the selected holder
// `transferFrom` (not `safeTransferFrom`) so a holder contract without `onERC721Received` can't brick the giveaway
IERC721(nftAddress).transferFrom(owner(), holder, tokenId);
emit Giveaway(holder, selectedIndex, nftAddress, tokenId);
}
/////////////////////////////////////////////////////////////////////
// VIEWS
/////////////////////////////////////////////////////////////////////
function numHolders() external view returns (uint256) {
return w0rmHolders.length;
}
function getHolders() external view returns (address[] memory) {
return w0rmHolders;
}
}
dependencies/@openzeppelin-contracts-5.6.1/access/Ownable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
dependencies/@openzeppelin-contracts-5.6.1/token/ERC721/IERC721.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC721/IERC721.sol)
pragma solidity >=0.6.2;
import {IERC165} from "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC-721 compliant contract.
*/
interface IERC721 is IERC165 {
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/
function balanceOf(address owner) external view returns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function ownerOf(uint256 tokenId) external view returns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC-721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must have been allowed to move this token by either {approve} or
* {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC-721
* or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
* understand this adds an external call which potentially creates a reentrancy vulnerability.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/
function approve(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the address zero.
*
* Emits an {ApprovalForAll} event.
*/
function setApprovalForAll(address operator, bool approved) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function getApproved(uint256 tokenId) external view returns (address operator);
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/
function isApprovedForAll(address owner, address operator) external view returns (bool);
}
dependencies/@openzeppelin-contracts-5.6.1/utils/Context.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
dependencies/@openzeppelin-contracts-5.6.1/utils/introspection/IERC165.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (utils/introspection/IERC165.sol)
pragma solidity >=0.4.16;
/**
* @dev Interface of the ERC-165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[ERC].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 10000,
"enabled": true
},
"evmVersion": "osaka",
"remappings": [
"@openzeppelin-contracts-5.6.1/=dependencies/@openzeppelin-contracts-5.6.1/",
"@openzeppelin/contracts/=dependencies/@openzeppelin-contracts-5.6.1/",
"forge-std-1.14.0/=dependencies/forge-std-1.14.0/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.