2020-02-18 19:23:31 +00:00
|
|
|
pragma solidity ^0.6.1;
|
|
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
|
2020-03-30 13:43:39 +00:00
|
|
|
import "./IERC20.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-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;
|
|
|
|
|
|
|
|
struct Redeem {
|
2020-04-02 11:28:41 +00:00
|
|
|
uint256 blockNumber;
|
|
|
|
bytes32 blockHash;
|
2020-02-18 19:23:31 +00:00
|
|
|
address receiver;
|
|
|
|
bytes32 code;
|
|
|
|
}
|
|
|
|
|
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)");
|
2020-04-02 11:28:41 +00:00
|
|
|
bytes32 constant REDEEM_TYPEHASH = keccak256("Redeem(uint256 blockNumber,bytes32 blockHash,address receiver,bytes32 code)");
|
2020-02-18 19:23:31 +00:00
|
|
|
bytes32 DOMAIN_SEPARATOR;
|
|
|
|
|
|
|
|
modifier onlyOwner() {
|
|
|
|
require(msg.sender == owner, "owner required");
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(address _tokenAddress, uint256 _expirationTime) public {
|
2020-02-21 18:55:53 +00:00
|
|
|
initialize(_tokenAddress, _expirationTime, msg.sender);
|
|
|
|
}
|
|
|
|
|
|
|
|
function initialize(address _tokenAddress, uint256 _expirationTime, address _owner) public {
|
|
|
|
require(initialized == false, "already initialized");
|
|
|
|
|
2020-02-21 15:09:42 +00:00
|
|
|
require(_expirationTime > block.timestamp, "expiration can't be in the past");
|
|
|
|
|
2020-03-30 13:43:39 +00:00
|
|
|
tokenContract = IERC20(_tokenAddress);
|
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-02-21 15:09:42 +00:00
|
|
|
_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 15:09:42 +00:00
|
|
|
function _getChainID() internal pure returns (uint256) {
|
2020-02-18 19:23:31 +00:00
|
|
|
uint256 id;
|
|
|
|
assembly {
|
|
|
|
id := chainid()
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
require(gift.amount == 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
|
|
|
}
|
|
|
|
|
|
|
|
function redeem(Redeem calldata _redeem, bytes calldata sig) external {
|
2020-04-02 11:28:41 +00:00
|
|
|
require(_redeem.blockNumber < block.number, "transaction cannot be in the future");
|
|
|
|
require(_redeem.blockNumber >= (block.number - maxTxDelayInBlocks), "transaction too old");
|
|
|
|
require(_redeem.blockHash == blockhash(_redeem.blockNumber), "invalid block hash");
|
|
|
|
|
2020-02-18 19:23:31 +00:00
|
|
|
require(block.timestamp < expirationTime, "expired gift");
|
|
|
|
|
2020-03-25 08:43:52 +00:00
|
|
|
address recipient = recoverSigner(_redeem, sig);
|
2020-02-18 19:23:31 +00:00
|
|
|
|
2020-03-25 08:23:22 +00:00
|
|
|
Gift storage gift = gifts[recipient];
|
|
|
|
require(gift.amount > 0, "not found");
|
2020-02-21 15:57:23 +00:00
|
|
|
|
2020-02-18 19:23:31 +00:00
|
|
|
bytes32 codeHash = keccak256(abi.encodePacked(_redeem.code));
|
|
|
|
require(codeHash == gift.code, "invalid code");
|
|
|
|
|
2020-02-21 15:57:23 +00:00
|
|
|
uint256 amount = gift.amount;
|
|
|
|
require(redeemableSupply >= amount, "not enough redeemable supply");
|
|
|
|
|
|
|
|
gift.amount = 0;
|
|
|
|
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 {
|
|
|
|
require(block.timestamp >= expirationTime, "not expired yet");
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hashRedeem(Redeem memory _redeem) internal pure returns (bytes32) {
|
|
|
|
return keccak256(abi.encode(
|
|
|
|
REDEEM_TYPEHASH,
|
2020-04-02 11:28:41 +00:00
|
|
|
_redeem.blockNumber,
|
|
|
|
_redeem.blockHash,
|
2020-02-18 19:23:31 +00:00
|
|
|
_redeem.receiver,
|
|
|
|
_redeem.code
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-03-25 08:43:52 +00:00
|
|
|
function recoverSigner(Redeem memory _redeem, bytes memory sig) internal view returns(address) {
|
2020-02-18 19:23:31 +00:00
|
|
|
require(sig.length == 65, "bad signature length");
|
|
|
|
|
|
|
|
bytes32 r;
|
|
|
|
bytes32 s;
|
|
|
|
uint8 v;
|
|
|
|
|
|
|
|
assembly {
|
|
|
|
r := mload(add(sig, 32))
|
|
|
|
s := mload(add(sig, 64))
|
|
|
|
v := byte(0, mload(add(sig, 96)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v < 27) {
|
|
|
|
v += 27;
|
|
|
|
}
|
|
|
|
|
|
|
|
require(v == 27 || v == 28, "signature version doesn't match");
|
|
|
|
|
|
|
|
bytes32 digest = keccak256(abi.encodePacked(
|
|
|
|
"\x19\x01",
|
|
|
|
DOMAIN_SEPARATOR,
|
|
|
|
hashRedeem(_redeem)
|
|
|
|
));
|
|
|
|
|
2020-03-25 08:23:22 +00:00
|
|
|
return ecrecover(digest, v, r, s);
|
2020-02-18 19:23:31 +00:00
|
|
|
}
|
|
|
|
}
|