The smart contract will be deployed juste one time on the Polygon Network. After that, the lottery starts and renews automatically with each winner draw
ERC721A is popular for reduce gas price then you mint multiple items
Chainlink contracts are tools to generate a random number in a decentralized way via a chainlink oracle.
All the Constants
///parameters allowing the counting of nft already minted and still available
uint public supplyLottery = 2;
uint public alreadySupply = 0;
///Max number of NFT that 1 wallet is allow to mint
uint public max_mint_allowed = 50;
///The price of 1 NFT in $MATIC
uint public priceSale = 0.01 ether;
///We "define" the baseURI
string public baseURI;
///we "set" the extension to .json
string public baseExtension = ".json";
///Boolean to set if lottery is open/closed
bool public lotteryClosed;
Constructor
This is the constructor of the contract, with ERC721A and Chainlink.
The constructor has some parameters:
-string memory _theBaseURI --> To link the IPFS to the collection.
ERC721A:
-DeFiLottery --> The name of the token (NFT).
-DFL --> The symbol of the tokens
Line 1: team = payable(_team);--> Address for the team payment
Line 2: COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator); --> Address of the Chainlik vrfCoordinator
Line 3: s_owner; --> A function to transfer the Ownership,from the Owner to the new Owner,if somebody is going wrong with our smart contract deployer...
Line 4: s_subscriptionId = subscriptionId; --> The Id of subscription on Chainlink services
Line 5: baseURI = theBaseURI;--> To redefines the name of theBaseURI to baseURI (wich is use to link the IPFS to the collection)
Functions
In the smart contract,we are using some functions to make different actions...
The Mint
function enter(address _account, uint256 _ammount) external payable nonReentrant {
///To change the maxMint allowed
function changeMaxMintAllowed(uint _maxMintAllowed) external onlyOwner {
max_mint_allowed = _maxMintAllowed;
}
///To change the price sale (if the market is too volatile)
function changePriceSale(uint _priceSale) external onlyOwner {
priceSale = _priceSale;
}
///To change the BaseURI
function setBaseUri(string memory _newBaseURI) external onlyOwner {
baseURI = _newBaseURI;
}
///Return the BaseURI
function _baseURI() internal view virtual override returns (string memory) {
return baseURI;
}
///To set the base extension
function setBaseExtension(string memory _baseExtension) external onlyOwner {
baseExtension = _baseExtension;
}
///Return the token URI from the token ID
function tokenURI(uint _tokenId) public view virtual override returns (string memory) {
require(_exists(_tokenId), "URI query for nonexistent token");
return string(abi.encodePacked(baseURI, _tokenId.toString(), ".json"));
}
///Return the balance of the contract
function getBalance() public view returns(uint) {
return address(this).balance;
}
///Define the number of available tickets for the lottery
function changeSupplyLottery(uint _supplyLottery) external onlyOwner {
supplyLottery = _supplyLottery;
}
///For update the number of tickets already sell
function changeAlreadySupply(uint _alreadySupply) external onlyOwner {
alreadySupply = _alreadySupply;
}
///Return the winner address for selected lottery
function getWinner(uint _idLottery) external view returns(address) {
return winners[_idLottery];
}
function requestRandomWords() external onlyOwner {
require(lotteryClosed, "Lottery is not closed");
s_requestId = COORDINATOR.requestRandomWords(
keyHash,
s_subscriptionId,
requestConfirmations,
callbackGasLimit,
numWords
);
}