Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevupton/9b85312e06bcbf90a992099b4cadde39 to your computer and use it in GitHub Desktop.
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=
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