Created
April 7, 2022 02:40
-
-
Save kevupton/9b85312e06bcbf90a992099b4cadde39 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=false&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
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | |
contract PooNami is ERC20 { | |
constructor() ERC20('PooNami', 'POO') { | |
_mint(msg.sender, 1e24); | |
} | |
} | |
contract AirGrap { | |
event GrabCreated(Grab, address indexed sender); | |
event GrabClaimed(uint256 indexed grabId, address indexed sender); | |
struct Grab { | |
IERC20 token; | |
uint256 totalAmount; | |
uint256 amountPerGrab; | |
uint256 totalGrabsPossible; | |
uint256 currentGrabCount; | |
} | |
Grab[] private _grabs; | |
// grabId => walletAddress => hasGrabbed | |
mapping(uint256 => mapping(address => bool)) public hasGrabbed; | |
function getGrab(uint256 grabId) external view returns (Grab memory) { | |
return _grabs[grabId]; | |
} | |
// deposit an amount of a token, and how much per user someone can grab | |
function depositToken(IERC20 token, uint256 totalAmount, uint256 totalGrabsPossible) external returns (uint256 grabId) { | |
require(totalGrabsPossible > 0, 'No grabs specified'); | |
uint256 amountPerGrab = totalAmount / totalGrabsPossible; | |
require(amountPerGrab > 0, 'Requires amount per grab more than 0'); | |
// transfer token to this contract | |
token.transferFrom(msg.sender, address(this), totalAmount); | |
grabId = _grabs.length; | |
Grab memory grab = Grab( | |
token, | |
totalAmount, | |
amountPerGrab, | |
totalGrabsPossible, | |
0 | |
); | |
// save the grab as a new grab | |
_grabs.push(grab); | |
emit GrabCreated(grab, msg.sender); | |
} | |
// claim function -> receive x tokens | |
function claim(uint256 grabId) external { | |
// make sure that the grab id exists | |
require(grabId < _grabs.length, 'Invalid grab id'); | |
// only allow one grab per wallet | |
require(!hasGrabbed[grabId][msg.sender], 'You have already grabbed'); | |
// only allow so many grabs | |
require(_grabs[grabId].currentGrabCount < _grabs[grabId].totalGrabsPossible, 'No more grabs to be claimed'); | |
_grabs[grabId].token.transfer(msg.sender, _grabs[grabId].amountPerGrab); | |
// makes sure they cannot do it again | |
hasGrabbed[grabId][msg.sender] = true; | |
_grabs[grabId].currentGrabCount++; | |
emit GrabClaimed(grabId, msg.sender); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment