2020-02-18 19:23:31 +00:00
|
|
|
pragma solidity ^0.6.1;
|
|
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
|
2020-04-03 12:19:40 +00:00
|
|
|
import "./erc20/IERC20.sol";
|
2020-04-21 10:39:40 +00:00
|
|
|
import "./RedeemUtil.sol";
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-02-21 11:41:03 +00:00
|
|
|
contract GiftBucket {
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-02-21 18:55:53 +00:00
|
|
|
bool initialized;
|
|
|
|
|
2020-02-18 19:23:31 +00:00
|
|
|
address payable public owner;
|
|
|
|
|
2020-03-30 13:43:39 +00:00
|
|
|
IERC20 public tokenContract;
|
2020-02-18 19:23:31 +00:00
|
|
|
|
|
|
|
uint256 public expirationTime;
|
2020-04-23 12:51:05 +00:00
|
|
|
uint256 public startTime;
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-04-02 11:28:41 +00:00
|
|
|
uint256 constant maxTxDelayInBlocks = 10;
|
|
|
|
|
2020-02-18 19:23:31 +00:00
|
|
|
struct Gift {
|
2020-02-21 18:55:53 +00:00
|
|
|
address recipient;
|
2020-02-18 19:23:31 +00:00
|
|
|
uint256 amount;
|
|
|
|
bytes32 code;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapping(address => Gift) public gifts;
|
|
|
|
|
2020-02-21 15:09:42 +00:00
|
|
|
uint256 public redeemableSupply;
|
|
|
|
|
2020-02-18 19:23:31 +00:00
|
|
|
bytes32 constant EIP712DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
|
|
|
|
bytes32 DOMAIN_SEPARATOR;
|
|
|
|
|
|
|
|
modifier onlyOwner() {
|
|
|
|
require(msg.sender == owner, "owner required");
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2020-04-23 12:51:05 +00:00
|
|
|
constructor(address _tokenAddress, uint256 _startTime, uint256 _expirationTime) public {
|
|
|
|
initialize(_tokenAddress, _startTime, _expirationTime, msg.sender);
|
2020-02-21 18:55:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 12:51:05 +00:00
|
|
|
function initialize(address _tokenAddress, uint256 _startTime, uint256 _expirationTime, address _owner) public {
|
2020-02-21 18:55:53 +00:00
|
|
|
require(initialized == false, "already initialized");
|
|
|
|
|
2020-04-21 10:39:40 +00:00
|
|
|
RedeemUtil.validateExpiryDate(_expirationTime);
|
2020-02-21 15:09:42 +00:00
|
|
|
|
2020-03-30 13:43:39 +00:00
|
|
|
tokenContract = IERC20(_tokenAddress);
|
2020-04-23 12:51:05 +00:00
|
|
|
startTime = _startTime;
|
2020-02-18 19:23:31 +00:00
|
|
|
expirationTime = _expirationTime;
|
2020-02-21 18:55:53 +00:00
|
|
|
owner = payable(_owner);
|
|
|
|
|
2020-02-18 19:23:31 +00:00
|
|
|
DOMAIN_SEPARATOR = keccak256(abi.encode(
|
|
|
|
EIP712DOMAIN_TYPEHASH,
|
|
|
|
keccak256("KeycardGift"),
|
|
|
|
keccak256("1"),
|
2020-04-21 10:39:40 +00:00
|
|
|
RedeemUtil.getChainID(),
|
2020-02-18 19:23:31 +00:00
|
|
|
address(this)
|
|
|
|
));
|
2020-02-21 18:55:53 +00:00
|
|
|
|
|
|
|
initialized = true;
|
2020-02-18 19:23:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:55:53 +00:00
|
|
|
function totalSupply() public view returns(uint256) {
|
2020-02-21 15:09:42 +00:00
|
|
|
return tokenContract.balanceOf(address(this));
|
|
|
|
}
|
|
|
|
|
2020-02-21 18:55:53 +00:00
|
|
|
function availableSupply() public view returns(uint256) {
|
2020-02-21 15:09:42 +00:00
|
|
|
uint256 _totalSupply = this.totalSupply();
|
2020-02-21 15:57:23 +00:00
|
|
|
require(_totalSupply >= redeemableSupply, "redeemableSupply is greater than redeemableSupply");
|
|
|
|
|
2020-02-21 15:09:42 +00:00
|
|
|
return _totalSupply - redeemableSupply;
|
2020-02-18 19:23:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:55:53 +00:00
|
|
|
function createGift(address recipient, uint256 amount, bytes32 code) external onlyOwner {
|
2020-02-18 19:23:31 +00:00
|
|
|
require(amount > 0, "invalid amount");
|
2020-02-21 15:09:42 +00:00
|
|
|
|
|
|
|
uint256 _availableSupply = this.availableSupply();
|
|
|
|
require(_availableSupply >= amount, "low supply");
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-02-21 18:55:53 +00:00
|
|
|
Gift storage gift = gifts[recipient];
|
2020-04-08 11:00:16 +00:00
|
|
|
require(gift.recipient == address(0), "recipient already used");
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-02-21 18:55:53 +00:00
|
|
|
gift.recipient = recipient;
|
2020-02-18 19:23:31 +00:00
|
|
|
gift.amount = amount;
|
|
|
|
gift.code = code;
|
|
|
|
|
2020-02-21 15:57:23 +00:00
|
|
|
require(redeemableSupply + amount > redeemableSupply, "addition overflow");
|
2020-02-21 15:09:42 +00:00
|
|
|
redeemableSupply += amount;
|
2020-02-18 19:23:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 10:39:40 +00:00
|
|
|
function redeem(RedeemUtil.Redeem calldata _redeem, bytes calldata _sig) external {
|
2020-04-23 12:51:05 +00:00
|
|
|
RedeemUtil.validateRedeem(_redeem, maxTxDelayInBlocks, expirationTime, startTime);
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-04-21 10:39:40 +00:00
|
|
|
address recipient = RedeemUtil.recoverSigner(DOMAIN_SEPARATOR, _redeem, _sig);
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-03-25 08:23:22 +00:00
|
|
|
Gift storage gift = gifts[recipient];
|
2020-04-08 11:00:16 +00:00
|
|
|
require(gift.recipient == recipient, "not found");
|
2020-02-21 15:57:23 +00:00
|
|
|
|
2020-04-21 10:39:40 +00:00
|
|
|
RedeemUtil.validateCode(_redeem, gift.code);
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-02-21 15:57:23 +00:00
|
|
|
uint256 amount = gift.amount;
|
|
|
|
require(redeemableSupply >= amount, "not enough redeemable supply");
|
|
|
|
|
2020-04-23 11:44:04 +00:00
|
|
|
gift.recipient = address(0);
|
2020-02-21 15:57:23 +00:00
|
|
|
gift.amount = 0;
|
2020-04-23 11:44:04 +00:00
|
|
|
gift.code = 0;
|
|
|
|
|
2020-02-21 15:57:23 +00:00
|
|
|
redeemableSupply -= amount;
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-02-21 15:57:23 +00:00
|
|
|
tokenContract.transfer(_redeem.receiver, amount);
|
2020-02-18 19:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function kill() external onlyOwner {
|
2020-04-21 10:39:40 +00:00
|
|
|
RedeemUtil.validateExpired(expirationTime);
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-02-21 15:09:42 +00:00
|
|
|
bool success = tokenContract.transfer(owner, this.totalSupply());
|
2020-02-18 19:23:31 +00:00
|
|
|
assert(success);
|
|
|
|
|
|
|
|
selfdestruct(owner);
|
|
|
|
}
|
|
|
|
}
|