2020-09-30 10:17:50 +00:00
|
|
|
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 "./erc721/IERC721Receiver.sol";
|
|
|
|
import "./erc721/IERC165.sol";
|
|
|
|
|
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,
|
2020-05-04 06:04:30 +00:00
|
|
|
uint256 _expirationTime,
|
|
|
|
uint256 _maxTxDelayInBlocks) Bucket("KeycardNFTBucket", _tokenAddress, _startTime, _expirationTime, _maxTxDelayInBlocks) public {}
|
2020-04-09 08:13:16 +00:00
|
|
|
|
2020-09-30 10:17:50 +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
|
|
|
}
|
|
|
|
|
2020-09-30 10:17:50 +00:00
|
|
|
function transferRedeemablesToOwner() internal override {
|
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
|
|
|
}
|
|
|
|
|
2020-09-30 10:17:50 +00:00
|
|
|
function bucketType() external override returns (uint256) {
|
2020-04-29 10:50:25 +00:00
|
|
|
return 721;
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:17:50 +00:00
|
|
|
function supportsInterface(bytes4 interfaceID) external override(IERC165) view returns (bool) {
|
2020-04-09 08:13:16 +00:00
|
|
|
return interfaceID == _ERC721_RECEIVED;
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:17:50 +00:00
|
|
|
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-04-28 09:36:34 +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
|
|
|
}
|
|
|
|
}
|