keycard-redeem/contracts/GiftBucket.sol

122 lines
3.2 KiB
Solidity
Raw Normal View History

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;
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;
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);
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) {
return tokenContract.balanceOf(address(this));
}
2020-02-21 18:55:53 +00:00
function availableSupply() public view returns(uint256) {
uint256 _totalSupply = this.totalSupply();
require(_totalSupply >= redeemableSupply, "redeemableSupply is greater than redeemableSupply");
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");
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;
require(redeemableSupply + amount > redeemableSupply, "addition overflow");
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-04-21 10:39:40 +00:00
RedeemUtil.validateCode(_redeem, gift.code);
2020-02-18 19:23:31 +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);
gift.amount = 0;
2020-04-23 11:44:04 +00:00
gift.code = 0;
redeemableSupply -= amount;
2020-02-18 19:23:31 +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
bool success = tokenContract.transfer(owner, this.totalSupply());
2020-02-18 19:23:31 +00:00
assert(success);
selfdestruct(owner);
}
}