Skip to content

Commit

Permalink
add ETH cashback
Browse files Browse the repository at this point in the history
  • Loading branch information
ZumZoom committed Oct 19, 2021
1 parent 9faa2f3 commit 07c7fa5
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion contracts/MerkleDrop128.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ pragma abicoder v1;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import "./interfaces/IMerkleDrop128.sol";

contract MerkleDrop128 is IMerkleDrop128 {
contract MerkleDrop128 is IMerkleDrop128, Ownable {
using Address for address payable;
using SafeERC20 for IERC20;

address public immutable override token;
Expand All @@ -22,6 +25,10 @@ contract MerkleDrop128 is IMerkleDrop128 {
// This is a packed array of booleans.
mapping(uint256 => uint256) private _claimedBitMap;

uint256 private constant _CLAIM_GAS_COST = 60000;

receive() external payable {} // solhint-disable-line no-empty-blocks

constructor(address token_, bytes16 merkleRoot_, uint256 depth_) {
token = token_;
merkleRoot = merkleRoot_;
Expand All @@ -37,6 +44,7 @@ contract MerkleDrop128 is IMerkleDrop128 {
require(valid, "MD: Invalid proof");
_invalidate(index);
IERC20(token).safeTransfer(receiver, amount);
_cashback();
}

function verify(bytes calldata proof, bytes16 root, bytes16 leaf) external view returns (bool valid, uint256 index) {
Expand All @@ -51,6 +59,14 @@ contract MerkleDrop128 is IMerkleDrop128 {
return claimedWord & mask == mask;
}

function _cashback() private {
uint256 balance = address(this).balance;
if (balance > 0) {
// solhint-disable-next-line avoid-tx-origin
payable(tx.origin).sendValue(Math.min(block.basefee * _CLAIM_GAS_COST, balance));
}
}

function _invalidate(uint256 index) private {
uint256 claimedWordIndex = index >> 8;
uint256 claimedBitIndex = index & 0xff;
Expand Down Expand Up @@ -92,4 +108,12 @@ contract MerkleDrop128 is IMerkleDrop128 {
index <<= depth - proof.length / 16;
}
}

function rescueFunds(address token_, uint256 amount) external onlyOwner {
if (token_ == address(0)) {
payable(msg.sender).sendValue(amount);
} else {
IERC20(token_).safeTransfer(msg.sender, amount);
}
}
}

0 comments on commit 07c7fa5

Please sign in to comment.