Created
May 1, 2022 17:21
-
-
Save dexXxed/f74068168071570fb4df3896d5f23f84 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=true&runs=200&gist=
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.7; | |
import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol"; | |
import "erc721a/contracts/ERC721A.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/utils/Strings.sol"; | |
import "@openzeppelin/contracts/utils/Counters.sol"; | |
contract RandomNumberLotteryNFTCollection is VRFConsumerBase, Ownable { | |
bytes32 internal keyHash; | |
uint256 internal fee; | |
using Counters for Counters.Counter; | |
Counters.Counter private _lottery_id_number; | |
address public approved_contract; | |
/** | |
* Constructor inherits VRFConsumerBase | |
* | |
* Network: Matic | |
* Chainlink VRF Coordinator address: 0x8C7382F9D8f56b33781fE506E897a4F1e2d17255 | |
* LINK token address: 0x326C977E6efc84E512bB9C30f76E30c160eD06FB | |
* Key Hash: 0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4 | |
*/ | |
constructor() | |
VRFConsumerBase( | |
0x8C7382F9D8f56b33781fE506E897a4F1e2d17255, // VRF Coordinator | |
0x326C977E6efc84E512bB9C30f76E30c160eD06FB // LINK Token | |
) | |
{ | |
keyHash = 0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4; | |
fee = 0.0001 * 10 ** 18; // 0.0001 LINK | |
} | |
function addApprovedContract(address _approved_contract) public onlyOwner { | |
approved_contract = _approved_contract; | |
} | |
mapping(bytes32 => uint256) public requestIdToRandomNumber; // by default all numbers are 0 | |
mapping(uint256 => uint256) public LotteryIdToRandomNumber; // by default all numbers are 0 (0-14 by ids) | |
function getRandomNumber() public returns (bytes32 requestId) { | |
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract!"); | |
require(approved_contract != address(0), "No approved_contract specified!"); | |
require(msg.sender == approved_contract, "Can be triggered only by approved NFT contract!"); | |
return requestRandomness(keyHash, fee); | |
} | |
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override { | |
LotteryIdToRandomNumber[_lottery_id_number.current()] = (randomness % 10) + 1; // generate from 1 to 10 (then minus 1 in NFT contract, because we will get people by their ids from 0 to 9 in ranges 10) | |
_lottery_id_number.increment(); // increment index for the mapping LotteryIdToRandomNumber | |
} | |
} | |
contract NFT_LOTTERY is ERC721A, Ownable { | |
uint256 public MAX_MINTS = 3; | |
uint256 public MAX_SUPPLY = 300; | |
uint256 public mintRate = 0.001 ether; // to adjust | |
uint256 public prize = 0.005 ether; // to adjust (NOTE: x5 from mintRate) | |
address private developer_1 = 0x44b5dAD1188F06AF0fDd00Bca807c71C8BC84d81; // to adjust | |
address private developer_2 = 0x26c3Cc2d99d91D55f2a02aDcF280F71b6bEA6190; // to adjust | |
string public baseURI = "ipfs://test/"; | |
RandomNumberLotteryNFTCollection public random_number_contract = RandomNumberLotteryNFTCollection(0x5b6De89AC1cc928B5c23B042b1e7047a45a2c67e); // to adjust | |
mapping (uint256 => bool) public got_the_prize; | |
constructor() ERC721A("NFT Lottery", "NFTL") {} | |
function purchase(uint256 quantity) external payable { | |
require(msg.sender != owner(), "Owner cannot mint!"); | |
require(quantity + _numberMinted(msg.sender) <= MAX_MINTS, "Exceeded the limit!"); | |
require(totalSupply() + quantity <= MAX_SUPPLY, "Not enough NFT Lottery tokens left!"); | |
require(msg.value >= (mintRate * quantity), "Not enough MATIC sent"); | |
_safeMint(msg.sender, quantity); | |
if (totalSupply() % 10 == 0 || got_the_prize[uint256(totalSupply() / 10)] == false) { | |
random_number_contract.getRandomNumber(); | |
} | |
} | |
function tokenURI(uint256 tokenId) public view override returns (string memory) { | |
return string(abi.encodePacked(baseURI, Strings.toString(tokenId), ".json")); | |
} | |
function check_and_get_lottery_reward() public payable returns (bool) { | |
require(balanceOf(msg.sender) != 0, "You have no \"NFT Lottery\" items!"); | |
uint256 index_to_iterate_from; | |
for (uint256 index = 0; index < 14; index++) { | |
if (got_the_prize[index] == false) { | |
index_to_iterate_from = index; | |
break; | |
} | |
} | |
if (index_to_iterate_from == uint256(totalSupply() / 10)) { | |
return false; // "Results aren't ready!" | |
} | |
// Get values if they are generated already | |
for (uint256 i = index_to_iterate_from; i < uint256(totalSupply() / 10); i++) { | |
if (random_number_contract.LotteryIdToRandomNumber(i) == 0){ | |
return false; // "Results aren't ready!" | |
} | |
if (ownerOf(i * 10 + random_number_contract.LotteryIdToRandomNumber(i) - 1) == msg.sender) { | |
got_the_prize[i] = true; | |
(bool lottery_prise, ) = address(msg.sender).call{value: prize * 10 ** 18}(""); // Matic win amount | |
require(lottery_prise, "Transfer for lucky address failed."); | |
(bool lottery_admin_payback_developer_1, ) = developer_1.call{value: (prize * 10 ** 18) / 2}(""); // To Dev 1 | |
require(lottery_admin_payback_developer_1, "Transfer for developer_1 wallet failed."); | |
(bool lottery_admin_payback_developer_2, ) = developer_2.call{value: (prize * 10 ** 18) / 2}(""); // To Dev 2 | |
require(lottery_admin_payback_developer_2, "Transfer for developer_2 wallet failed."); | |
return true; // "You won Matic!" | |
} | |
} | |
return false; | |
} | |
function _baseURI() internal view override returns (string memory) { | |
return baseURI; | |
} | |
function contractURI() public pure returns (string memory) { | |
return "ipfs://tests"; // to adjust metadata | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment