small fixes

This commit is contained in:
Ricardo Guilherme Schmidt 2019-01-16 08:55:44 -02:00
parent 9f2756383d
commit ceaa21f484
No known key found for this signature in database
GPG Key ID: BFB3F5C8ED618A94
3 changed files with 35 additions and 12 deletions

View File

@ -15,9 +15,9 @@ contract Sticker is Controlled, UnfungibleToken {
mint(_owner, tokenId);
}
function destroyToken(address _owner, uint256 _tokenId) external onlyController {
function destroyToken(uint256 _tokenId) external onlyController {
delete dataHash[_tokenId];
burn(_owner, _tokenId);
burn(_tokenId);
}
}

View File

@ -18,9 +18,10 @@ contract StickerMarket is Controlled, ApproveAndCallFallBack {
uint256 price;
address owner;
}
StickerPack public stickerPack = new StickerPack();
StickerPack public stickerPack = new StickerPack();
ERC20Token public snt;
mapping(uint256 => Pack) public packs;
mapping(bytes32 => uint256) public packIds;
uint256 public nextId;
/**
@ -35,8 +36,8 @@ contract StickerMarket is Controlled, ApproveAndCallFallBack {
snt = _snt;
}
function buy(uint256 _packId) external {
_buy(msg.sender, packs[_packId]);
function buy(uint256 _packId) external returns (uint256 tokenId) {
return _buy(msg.sender, packs[_packId]);
}
function receiveApproval(address _from, uint256 _amount, address _token, bytes calldata _data) external {
@ -49,13 +50,22 @@ contract StickerMarket is Controlled, ApproveAndCallFallBack {
_buy(_from, pack);
}
function transfer(uint256 _packId, address _to) external {
require(packs[_packId].owner == msg.sender);
packs[_packId].owner = _to;
}
function register(bytes32 _dataHash, uint256 _price, address _owner) external onlyController {
require(packs[packIds[_dataHash]].dataHash != _dataHash, "Duplicated");
uint256 packId = nextId++;
packs[packId] = Pack(_dataHash, _price, _owner);
packIds[_dataHash] = packId;
emit Register(packId, _dataHash, _price);
}
function unregister(uint256 _packId) external onlyController {
delete packIds[packs[_packId].dataHash];
delete packs[_packId];
emit Unregister(_packId);
}
@ -81,10 +91,22 @@ contract StickerMarket is Controlled, ApproveAndCallFallBack {
emit ClaimedTokens(_token, controller, balance);
}
function _buy(address _buyer, Pack memory _pack) internal {
function priceOf(uint256 _packId) external view returns(uint256 price){
price = packs[_packId].price;
}
function ownerOf(uint256 _packId) external view returns(address owner){
owner = packs[_packId].owner;
}
function dataOf(uint256 _packId) external view returns(bytes32 dataHash){
dataHash = packs[_packId].dataHash;
}
function _buy(address _buyer, Pack memory _pack) internal returns (uint256 tokenId){
require(_pack.dataHash != bytes32(0), "Bad pack");
require(snt.transferFrom(_buyer, _pack.owner, _pack.price), "Bad payment");
stickerPack.generateToken(_buyer, _pack.dataHash);
return stickerPack.generateToken(_buyer, _pack.dataHash);
}
/**

View File

@ -13,24 +13,24 @@ contract StickerPack is Controlled, UnfungibleToken {
mapping(uint256 => bytes32) public dataHash;
mapping(bytes32 => bool) public unpacked;
function unpack(uint256 _tokenId, bytes32[] calldata _proof, bytes32 _stickerData) external {
function unpack(uint256 _tokenId, bytes32 _stickerData, bytes32[] calldata _proof) external returns (uint256 tokenId) {
address owner = getOwner(_tokenId);
require(owner == msg.sender, "Unauthorized");
require(MerkleProof.verify(_proof, dataHash[_tokenId], _stickerData), "Invalid proof");
bytes32 unpackedHash = keccak256(abi.encodePacked(_tokenId, _stickerData));
require(!unpacked[unpackedHash], "Duplicate unpacking");
unpacked[unpackedHash] = true;
sticker.generateToken(owner, _stickerData);
return sticker.generateToken(owner, _stickerData);
}
function repack(uint256 _packId, uint256 _stickerToken) external {
address owner = getOwner(_packId);
require(owner == getOwner(_stickerToken) && owner == msg.sender, "Unauthorized");
require(owner == sticker.ownerOf(_stickerToken) && owner == msg.sender, "Unauthorized");
bytes32 stickerData = sticker.dataHash(_stickerToken);
bytes32 unpackedHash = keccak256(abi.encodePacked(_packId, stickerData));
require(unpacked[unpackedHash],"Bad operation");
delete unpacked[unpackedHash];
sticker.destroyToken(owner, _stickerToken);
sticker.destroyToken(_stickerToken);
}
function generateToken(address _owner, bytes32 _dataHash) external onlyController returns (uint256 tokenId){
@ -39,9 +39,10 @@ contract StickerPack is Controlled, UnfungibleToken {
mint(_owner, tokenId);
}
function isStickerInPack(uint256 _packId, bytes32[] memory _proof, bytes32 _stickerData) public view returns (bool){
function containsSticker(uint256 _packId, bytes32 _stickerData, bytes32[] memory _proof) public view returns (bool){
require(MerkleProof.verify(_proof, dataHash[_packId], _stickerData), "Invalid proof");
bytes32 unpackedHash = keccak256(abi.encodePacked(_packId, _stickerData));
return !unpacked[unpackedHash];
}
}