keycard-redeem/contracts/NFTBucket.sol

64 lines
2.3 KiB
Solidity
Raw Normal View History

pragma solidity ^0.6.1;
2020-04-09 08:13:16 +00:00
pragma experimental ABIEncoderV2;
2020-04-24 16:59:08 +00:00
import "./Bucket.sol";
2020-04-09 08:13:16 +00:00
import "./erc721/IERC721.sol";
import "@openzeppelin/contracts-ethereum-package/contracts/introspection/IERC165.sol";
import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC721/IERC721Receiver.sol";
import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC20/IERC20.sol";
2020-04-09 08:13:16 +00:00
2020-04-24 16:59:08 +00:00
contract NFTBucket is Bucket, IERC165, IERC721Receiver {
2020-04-14 06:31:53 +00:00
bytes4 private constant _ERC721_RECEIVED = 0x150b7a02; //bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))
2020-04-09 08:13:16 +00:00
2020-04-24 16:59:08 +00:00
constructor(
address _tokenAddress,
uint256 _startTime,
uint256 _expirationTime,
uint256 _maxTxDelayInBlocks) Bucket("KeycardNFTBucket", _tokenAddress, _startTime, _expirationTime, _maxTxDelayInBlocks) public {}
2020-04-09 08:13:16 +00:00
function transferRedeemable(uint256 data, Redeem memory redeem) internal override {
2020-04-24 16:59:08 +00:00
IERC721(tokenAddress).safeTransferFrom(address(this), redeem.receiver, data);
2020-04-09 08:13:16 +00:00
}
function transferRedeemablesToOwner() internal override {
2020-10-01 07:51:33 +00:00
address owner = owner();
2020-04-24 16:59:08 +00:00
IERC721(tokenAddress).setApprovalForAll(owner, true);
assert(IERC721(tokenAddress).isApprovedForAll(address(this), owner));
2020-04-09 08:13:16 +00:00
}
function bucketType() external override returns (uint256) {
2020-04-29 10:50:25 +00:00
return 721;
}
function supportsInterface(bytes4 interfaceID) external override(IERC165) view returns (bool) {
2020-04-09 08:13:16 +00:00
return interfaceID == _ERC721_RECEIVED;
}
function onERC721Received(address _operator, address _from, uint256 _tokenID, bytes calldata _data) external override(IERC721Receiver) returns(bytes4) {
2020-04-24 16:59:08 +00:00
require(msg.sender == tokenAddress, "only the NFT contract can call this");
2020-10-01 07:51:33 +00:00
require((_operator == owner()) || (_from == owner()), "only the owner can create redeemables");
2020-04-10 10:14:50 +00:00
require(_data.length == 52, "invalid data field");
2020-04-09 08:13:16 +00:00
bytes memory d = _data;
bytes32 tmp;
bytes32 code;
assembly {
// tmp is 12 bytes of padding (taken from the array length) + 20 bytes of address
tmp := mload(add(d, 20))
code := mload(add(d, 52))
}
address recipient = address(uint160(uint256(tmp)));
2020-04-28 09:36:34 +00:00
Redeemable storage redeemable = redeemables[recipient];
require(redeemable.recipient == address(0), "recipient already used");
2020-04-09 08:13:16 +00:00
2020-04-28 09:36:34 +00:00
redeemable.recipient = recipient;
redeemable.code = code;
redeemable.data = _tokenID;
2020-04-09 08:13:16 +00:00
2020-04-14 06:31:53 +00:00
return _ERC721_RECEIVED;
2020-04-09 08:13:16 +00:00
}
}