keycard-redeem/contracts/NFTBucket.sol

57 lines
1.9 KiB
Solidity
Raw Normal View History

2020-04-09 08:13:16 +00:00
pragma solidity ^0.6.1;
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,
uint256 _expirationTime) Bucket("KeycardNFTGift", _tokenAddress, _startTime, _expirationTime) public {}
2020-04-09 08:13:16 +00:00
2020-04-24 16:59:08 +00:00
function transferRedeemable(uint256 data, Redeem memory redeem) override internal {
IERC721(tokenAddress).safeTransferFrom(address(this), redeem.receiver, data);
2020-04-09 08:13:16 +00:00
}
2020-04-24 16:59:08 +00:00
function transferRedeemablesToOwner() override internal {
IERC721(tokenAddress).setApprovalForAll(owner, true);
assert(IERC721(tokenAddress).isApprovedForAll(address(this), owner));
2020-04-09 08:13:16 +00:00
}
function supportsInterface(bytes4 interfaceID) external override(IERC165) view returns (bool) {
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-04-09 08:13:16 +00:00
require((_operator == owner) || (_from == owner), "only the owner can create gifts");
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)));
Gift storage gift = gifts[recipient];
require(gift.recipient == address(0), "recipient already used");
gift.recipient = recipient;
gift.code = code;
2020-04-24 16:59:08 +00:00
gift.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
}
}