Created
April 1, 2022 08:54
-
-
Save korrio/f16c09affa48c822df310ab0c068a9d9 to your computer and use it in GitHub Desktop.
WavePortalWIthERC20.sol
This file contains hidden or 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.0; | |
// import 'hardhat/console.sol'; | |
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
contract WavePortal is Ownable { | |
using SafeERC20 for IERC20; | |
uint totalWaves; | |
uint private seed; | |
event NewWave(address indexed from, uint timestamp, string message); | |
struct Wave { | |
address waver; | |
string message; | |
uint timestamp; | |
} | |
Wave[] waves; | |
mapping(address => uint) public lastWavedAt; | |
address public token; | |
constructor(address _token) { | |
token = _token; | |
// console.log('Yo yo, I am a contract and I am smart'); | |
} | |
function setToken(address newToken) public onlyOwner { | |
token = newToken; | |
} | |
function depositToken(uint tokenAmount) public onlyOwner { | |
IERC20(token).approve(address(this),tokenAmount); | |
IERC20(token).safeTransferFrom(msg.sender,address(this),tokenAmount); | |
} | |
function wave(string memory _message) public { | |
// require(lastWavedAt[msg.sender] + 15 minutes < block.timestamp, "Wait 15 minutes"); | |
lastWavedAt[msg.sender] = block.timestamp; | |
totalWaves += 1; | |
// console.log('%s is waved with %s', msg.sender, _message); | |
waves.push(Wave(msg.sender, _message, block.timestamp)); | |
// uint tokenAmount = 100 * 10 ** 18; | |
uint tokenAmount = 100 ether; | |
IERC20(token).safeTransfer(msg.sender, tokenAmount); | |
// uint randomNumber = (block.difficulty + block.timestamp + seed) % 100; | |
// console.log("Random # generated: %s", randomNumber); | |
// seed = randomNumber; | |
// Give ether to lucky waver! | |
// if(randomNumber == 42) { | |
// uint prizeAmount = 0.0001 ether; | |
// // console.log("%s won 0.0001 ether!", msg.sender); | |
// require(prizeAmount <= address(this).balance, 'Trying to withraw more money than the contract has.'); | |
// (bool success,) = (msg.sender).call{value: prizeAmount}(''); | |
// require(success, 'Failed to withraw money from contract.'); | |
// } | |
emit NewWave(msg.sender, block.timestamp, _message); | |
} | |
function getAllWaves() view public returns (Wave[] memory) { | |
return waves; | |
} | |
function getTotalWaves() view public returns (uint) { | |
// console.log('We have %d total waves', totalWaves); | |
return totalWaves; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment