-
-
Save flyq/cc38f1ca7d5ad032444211c810cc97fe to your computer and use it in GitHub Desktop.
Convert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.21; | |
contract Convert { | |
address owner; | |
address public fromContractAddr; | |
address public toContractAddr; | |
mapping (uint => bool) public isConvert; | |
modifier onlyOwner { | |
require(msg.sender == owner); | |
_; | |
} | |
function Convert() public { | |
owner = msg.sender; | |
} | |
function setFromContractAddr(address _addr) public onlyOwner { | |
fromContractAddr = _addr; | |
} | |
function setToContractAddr(address _addr) public onlyOwner { | |
toContractAddr = _addr; | |
} | |
function getNewToken(uint _tokenId) public { | |
IFrom ifrom = IFrom(fromContractAddr); | |
require(ifrom.ownerOf(_tokenId) == msg.sender); | |
require(isConvert[_tokenId] == false); | |
isConvert[_tokenId] = true; | |
ITo ito = ITo(toContractAddr); | |
ito.issueTokenAndTransfer(1, msg.sender); | |
} | |
// 721token拥有者已经协商好,把各自的某个token相互交换。 | |
// _tokenId_1是ifrom合约里面的,_tokenId_2是ito合约里面的 | |
function approveToken(uint _tokenId_1, uint _tokenId_2) public { | |
IFrom ifrom = IFrom(fromContractAddr); | |
ITo ito = ITo(toContractAddr); | |
address ownerOfToken1 = ifrom.ownerOf(_tokenId_1); | |
address ownerOfToken2 = ito.ownerOf(_tokenId_2); | |
require(msg.sender == ownerOfToken1); | |
ifrom.approve(this, _tokenId_1); | |
if (ito.approvedFor(_tokenId_2) == this) { | |
ito.transferFrom(ownerOfToken2, ownerOfToken1, _tokenId_2); | |
ifrom.transferFrom(ownerOfToken1, ownerOfToken2, _tokenId_1); | |
} | |
/* only read */ | |
} | |
interface IFrom { | |
function ownerOf (uint256 _itemId) public view returns (address _owner); | |
function approve(address _to, uint256 _tokenId) public; | |
function transferFrom(address _from, address _to, uint256 _tokenId); | |
} | |
interface ITo { | |
function issueTokenAndTransfer(uint256 _count, address to) public; | |
function approvedFor(uint256 _tokenId) public view returns (address _approved); | |
function transferFrom(address _from, address _to, uint256 _tokenId) public; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment