keycard-redeem/contracts/ERC20Bucket.sol

59 lines
1.8 KiB
Solidity
Raw Normal View History

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