keycard-redeem/contracts/ERC20Bucket.sol

58 lines
1.8 KiB
Solidity
Raw Normal View History

2020-02-18 19:23:31 +00:00
pragma solidity ^0.6.1;
pragma experimental ABIEncoderV2;
2020-04-24 16:59:08 +00:00
import "./Bucket.sol";
2020-04-03 12:19:40 +00:00
import "./erc20/IERC20.sol";
2020-02-18 19:23:31 +00:00
2020-04-28 09:36:34 +00:00
contract ERC20Bucket is Bucket {
uint256 public redeemableSupply;
2020-04-24 16:59:08 +00:00
constructor(
address _tokenAddress,
uint256 _startTime,
2020-04-28 09:36:34 +00:00
uint256 _expirationTime) Bucket("KeycardERC20Bucket", _tokenAddress, _startTime, _expirationTime) public {}
2020-02-18 19:23:31 +00:00
2020-02-21 18:55:53 +00:00
function totalSupply() public view returns(uint256) {
2020-04-24 16:59:08 +00:00
return IERC20(tokenAddress).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-04-28 09:36:34 +00:00
function createRedeemable(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-04-28 09:36:34 +00:00
Redeemable storage redeemable = redeemables[recipient];
require(redeemable.recipient == address(0), "recipient already used");
2020-02-18 19:23:31 +00:00
2020-04-28 09:36:34 +00:00
redeemable.recipient = recipient;
redeemable.code = code;
redeemable.data = amount;
2020-02-18 19:23:31 +00:00
require(redeemableSupply + amount > redeemableSupply, "addition overflow");
redeemableSupply += amount;
2020-02-18 19:23:31 +00:00
}
2020-04-28 09:36:34 +00:00
function transferRedeemable(uint256 data, Redeem memory redeem) internal override {
2020-04-24 16:59:08 +00:00
require(redeemableSupply >= data, "not enough redeemable supply");
redeemableSupply -= data;
IERC20(tokenAddress).transfer(redeem.receiver, data);
2020-02-18 19:23:31 +00:00
}
2020-04-28 09:36:34 +00:00
function transferRedeemablesToOwner() internal override {
2020-04-24 16:59:08 +00:00
bool success = IERC20(tokenAddress).transfer(owner, this.totalSupply());
2020-02-18 19:23:31 +00:00
assert(success);
}
2020-04-29 10:50:25 +00:00
function bucketType() external override returns (uint256) {
return 20;
}
2020-02-18 19:23:31 +00:00
}