How can I let my smart contract receive any erc-20 tokens and pay them out when the paywinner function is called?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
contract Ownable_Page_Lottery is VRFConsumerBase {
address public owner;
address payable[] public players;
uint public lotteryId;
mapping (uint => address payable) public lotteryHistory;
bytes32 internal keyHash; // identifies which Chainlink oracle to use
uint internal fee; // fee to get random number
uint public randomResult;
constructor()
VRFConsumerBase(
0xb3dCcb4Cf7a26f6cf6B120Cf5A73875B7BBc655B, // VRF coordinator
0x01BE23585060835E02B77ef475b0Cc51aA1e0709 // LINK token address
) {
keyHash = 0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311;
fee = 0.1 * 10 ** 18; // 0.1 LINK
owner = msg.sender;
lotteryId = 1;
}
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK in contract");
return requestRandomness(keyHash, fee);
}
function fulfillRandomness(bytes32 requestId, uint randomness) internal override {
randomResult = randomness;
payWinner();
}
function getWinnerByLottery(uint lottery) public view returns (address payable) {
return lotteryHistory[lottery];
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
function getPlayers() public view returns (address payable[] memory) {
return players;
}
function enter() public payable {
require(msg.value > 1 ether);
// address of player entering lottery
players.push(payable(msg.sender));
}
function pickWinner() public onlyowner {
getRandomNumber();
}
function payWinner() public {
uint index = randomResult % players.length;
players[index].transfer(address(this).balance);
lotteryHistory[lotteryId] = players[index];
lotteryId++;
// reset the state of the contract
players = new address payable[](0);
}
modifier onlyowner() {
require(msg.sender == owner);
_;
}
receive () external payable {}
}
I would also love for this to be able to receive 721 tokens and pay them out using the pay winner function also but erc20 is a good start. I appreciate even any advise on where to look.
Thank you!