ETH商品合约

pragma solidity ^0.4.22;
/**
* @dev 可归属合同具有所有者地址,并提供基本授权控制
*/
contract Ownable {
  address public owner;

  event OwnershipRenounced(address indexed previousOwner);
  event OwnershipTransferred(
    address indexed previousOwner,
    address indexed newOwner
  );

  /**
   * @dev 可拥有的构造函数将合同的原始“所有者”设置为发送者
   * account.
   */
  constructor() public {
    owner = msg.sender;
  }

  /**
   * @dev 如果由所有者以外的任何帐户调用,则抛出
   */
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }

  /**
   * @dev 允许业主放弃合同的控制权.
   */
  function renounceOwnership() public onlyOwner {
    emit OwnershipRenounced(owner);
    owner = address(0);
  }

  /**
   * @dev 允许当前所有者将合同的控制转移给新所有者.
   */
  function transferOwnership(address _newOwner) public onlyOwner {
    _transferOwnership(_newOwner);
  }

  /**
   * @dev 将合同的控制权移交给新所有者.
   */
  function _transferOwnership(address _newOwner) internal {
    require(_newOwner != address(0));
    emit OwnershipTransferred(owner, _newOwner);
    owner = _newOwner;
  }
}

/**
 * @dev  ERC165
 */
interface ERC165 {
    /// @notice 查询合同是否实现接口
    /// @dev 接口标识在ERC-165中指定。此功能使用少于30,000的气体.
    function supportsInterface(bytes4 interfaceID) external view returns (bool);
}

/**
 * @title ECR721非可互换令牌基本标准
 * @dev  ERC721 Non-Fungible Token
 */
contract ERC721Basic is ERC165 {
    
  bytes4 internal constant InterfaceId_ERC721 = 0x80ac58cd;

  bytes4 internal constant InterfaceId_ERC721Enumerable = 0x780e9d63;

  bytes4 internal constant InterfaceId_ERC721Metadata = 0x5b5e139f;
  
  event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
  event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);
  event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);

  //返回由_owner 持有的NFTs的数量
  function balanceOf(address _owner) public view returns (uint256);
  
  //返回tokenId代币持有者的地址
  function ownerOf(uint256 _tokenId) public view returns (address);

  //授予地址_to具有_tokenId的控制权,方法成功后需触发Approval 事件
  function approve(address _approved, uint256 _tokenId) public;

  //用来查询授权
  function getApproved(uint256 _tokenId) public view returns (address);
  
  //授予地址_operator具有所有NFTs的控制权,成功后需触发ApprovalForAll事件
  function setApprovalForAll(address _operator, bool _approved) public;
  
  //用来查询授权
  function isApprovedForAll(address _owner, address _operator) public view returns (bool);
  
  //用来转移NFTs, 方法成功后需触发Transfer事件。调用者自己确认_to地址能正常接收NFT,否则将丢失此NFT
  function transferFrom(address _from, address _to, uint256 _tokenId) public;
  
  //转移NFT所有权,一次成功的转移操作必须发起 Transer 事件
  function safeTransferFrom(address _from, address _to, uint256 _tokenId) public;
  function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) public;
}
/**
 * @dev 对于ERC-721智能合约,枚举扩展是可选的(参见下面的“警告”)。
 * 这允许您的合同公布其完整的NFT列表并使其可被发现
 */
contract ERC721Enumerable is ERC721Basic {
  //总量 
  function totalSupply() public view returns (uint256);
  function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint256);
  function tokenByIndex(uint256 _index) public view returns (uint256);
}
/**
 * @dev 对于ERC-721智能合约,元数据扩展是可选的。
 * 这样可以查询您的智能合约的名称以及您的NFT所代表的资产的详细信息
 */
contract ERC721Metadata is ERC721Basic {
  //本合同中的NFTS集合的描述性名称
  function name() external view returns (string _name);
  //本合同中的NFTS缩写名称
  function symbol() external view returns (string _symbol);
  //给定资产的一个不同的统一资源标识符(URI)URI可以指向符合“Erc721”的JSON文件。
  //元数据JSON模式
  function tokenURI(uint256 _tokenId) public view returns (string);
}

contract ERC721Receiver {
  /**
   * @dev NFT成功接收后返回的值
   *  Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`,
   *  which can be also obtained as `ERC721Receiver(0).onERC721Received.selector`
   */
  bytes4 internal constant ERC721_RECEIVED = 0x150b7a02;
  bytes4 retval;
  bool reverts;

  constructor(bytes4 _retval, bool _reverts) public {
    retval = _retval;
    reverts = _reverts;
  }

  event Received(address _operator,address _from,uint256 _tokenId,bytes _data,uint256 _gas);

  function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data )public returns(bytes4)
  {
    require(!reverts);
    emit Received( _operator, _from, _tokenId, _data, gasleft() );
    return retval;
  }
}
/**
 * @title SafeMath
 * @dev 使用安全检查返回错误的数学运算
 */
library SafeMath {

  /**
  * @dev 乘以两个数字,恢复溢出
  */
  function mul(uint256 _a, uint256 _b) internal pure returns (uint256) {
    if (_a == 0) {
      return 0;
    }
    uint256 c = _a * _b;
    require(c / _a == _b);
    return c;
  }

  /**
  * @dev 两个数截去商的整数除法,归零除法.
  */
  function div(uint256 _a, uint256 _b) internal pure returns (uint256) {
    require(_b > 0); // Solidity only automatically asserts when dividing by 0
    uint256 c = _a / _b;
    return c;
  }

  /**
  * @dev 减去两个数字,恢复溢出(即如果减数大于被减数
  */
  function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
    require(_b <= _a);
    uint256 c = _a - _b;

    return c;
  }

  /**
  * @dev 添加两个数字,恢复溢出
  */
  function add(uint256 _a, uint256 _b) internal pure returns (uint256) {
    uint256 c = _a + _b;
    require(c >= _a);

    return c;
  }

  /**
  * @dev 除以两个数并返回余数(无符号整数模.
  */
  function mod(uint256 a, uint256 b) internal pure returns (uint256) {
    require(b != 0);
    return a % b;
  }
}


/**
 * 地址内联函数实用库
 */
library AddressUtils {

  /**
   * Returns whether the target address is a contract
   * @dev 如果在合同的构造函数中调用该函数,则此函数将返回false,
    *在构造函数完成后,代码才真正创建.
   * @param _account address of the account to check
   * @return whether the target address is a contract
   */
  function isContract(address _account) internal view returns (bool) {
    uint256 size;
    assembly { size := extcodesize(_account) }
    return size > 0;
  }

}
contract SupportsInterfaceWithLookup is ERC165 {

  bytes4 public constant InterfaceId_ERC165 = 0x01ffc9a7;
  mapping(bytes4 => bool) internal supportedInterfaces;
  constructor() public {
    _registerInterface(InterfaceId_ERC165);
  }

  /**
   * @dev 使用查找表实现支持接口
   */
  function supportsInterface(bytes4 _interfaceId) external view returns (bool) {
    return supportedInterfaces[_interfaceId];
  }
  /**
   * @dev private method for registering an interface
   */
  function _registerInterface(bytes4 _interfaceId)
    internal
  {
    require(_interfaceId != 0xffffffff);
    supportedInterfaces[_interfaceId] = true;
  }
}
contract ERC721BasicToken is SupportsInterfaceWithLookup, ERC721Basic {

  using SafeMath for uint256;
  using AddressUtils for address;

  // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
  // which can be also obtained as `ERC721Receiver(0).onERC721Received.selector`
  bytes4 private constant ERC721_RECEIVED = 0x150b7a02;

  // Mapping from token ID to owner
  mapping (uint256 => address) internal tokenOwner;

  // Mapping from token ID to approved address
  mapping (uint256 => address) internal tokenApprovals;

  // Mapping from owner to number of owned token
  mapping (address => uint256) internal ownedTokensCount;

  // Mapping from owner to operator approvals
  mapping (address => mapping (address => bool)) internal operatorApprovals;


  //tokenid属性   商户  品牌商  商品名称 规格 产地
  struct ShopInfo {
      uint256 tokenId;//tokenId
      string merchantsName;//商户 
      address merchantsAddress;//商户地址
      string brandsName;//品牌商
      string goodsName;//商品名称
      string goodsSpecifications;//规格
      string goodsAddress;//产地
  }
  mapping(uint256 =>ShopInfo) shopInfoToken;
  
  constructor()
    public
  {
    // register the supported interfaces to conform to ERC721 via ERC165
    _registerInterface(InterfaceId_ERC721);
  }

  /**
   * @dev Gets the balance of the specified address
   * @param _owner address to query the balance of
   * @return uint256 representing the amount owned by the passed address
   */
  function balanceOf(address _owner) public view returns (uint256) {
    require(_owner != address(0));
    return ownedTokensCount[_owner];
  }

  /**
   * @dev Gets the owner of the specified token ID
   * @param _tokenId uint256 ID of the token to query the owner of
   * @return owner address currently marked as the owner of the given token ID
   */
  function ownerOf(uint256 _tokenId) public view returns (address) {
    address owner = tokenOwner[_tokenId];
    require(owner != address(0));
    return owner;
  }

  /**
   * @dev Approves another address to transfer the given token ID
   * The zero address indicates there is no approved address.
   * There can only be one approved address per token at a given time.
   * Can only be called by the token owner or an approved operator.
   * @param _to address to be approved for the given token ID
   * @param _tokenId uint256 ID of the token to be approved
   */
  function approve(address _to, uint256 _tokenId) public {
    address owner = ownerOf(_tokenId);
    require(_to != owner);
    require(msg.sender == owner || isApprovedForAll(owner, msg.sender));

    tokenApprovals[_tokenId] = _to;
    emit Approval(owner, _to, _tokenId);
  }

  /**
   * @dev Gets the approved address for a token ID, or zero if no address set
   * @param _tokenId uint256 ID of the token to query the approval of
   * @return address currently approved for the given token ID
   */
  function getApproved(uint256 _tokenId) public view returns (address) {
    return tokenApprovals[_tokenId];
  }

  /**
   * @dev Sets or unsets the approval of a given operator
   * An operator is allowed to transfer all tokens of the sender on their behalf
   * @param _to operator address to set the approval
   * @param _approved representing the status of the approval to be set
   */
  function setApprovalForAll(address _to, bool _approved) public {
    require(_to != msg.sender);
    operatorApprovals[msg.sender][_to] = _approved;
    emit ApprovalForAll(msg.sender, _to, _approved);
  }

  /**
   * @dev Tells whether an operator is approved by a given owner
   * @param _owner owner address which you want to query the approval of
   * @param _operator operator address which you want to query the approval of
   * @return bool whether the given operator is approved by the given owner
   */
  function isApprovedForAll(
    address _owner,
    address _operator
  )
    public
    view
    returns (bool)
  {
    return operatorApprovals[_owner][_operator];
  }

  /**
   * @dev 将给定令牌ID的所有权转移到另一个地址使用此方法时,应尽可能使用“安全转移要求MSG发送者为所有者、批准者或操作者
   * @param _from 令牌的当前所有者
   * @param _to 接收给定令牌ID的所有权的地址
   * @param _tokenId adsfa
  */
  function transferFrom(address _from,address _to,uint256 _tokenId)public{
    require(isApprovedOrOwner(msg.sender, _tokenId));
    require(_to != address(0));

    clearApproval(_from, _tokenId);
    removeTokenFrom(_from, _tokenId);
    addTokenTo(_to, _tokenId);
    transferTokenId(_to,_tokenId);

    emit Transfer(_from, _to, _tokenId);
  }


  /**
   * @dev 安全地将给定令牌ID的所有权转移到另一个地址
   * @param _from current owner of the token
   * @param _to address to receive the ownership of the given token ID
   * @param _tokenId uint256 ID of the token to be transferred
  */
  function safeTransferFrom(address _from,address _to,uint256 _tokenId) public{
    // solium-disable-next-line arg-overflow
    safeTransferFrom(_from, _to, _tokenId, "");
  }

  /**
   * @dev Safely transfers the ownership of a given token ID to another address
   * If the target address is a contract, it must implement `onERC721Received`,
   * which is called upon a safe transfer, and return the magic value
   * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
   * the transfer is reverted.
   * Requires the msg sender to be the owner, approved, or operator
   * @param _from current owner of the token
   * @param _to address to receive the ownership of the given token ID
   * @param _tokenId uint256 ID of the token to be transferred
   * @param _data bytes data to send along with a safe transfer check
   */
  function safeTransferFrom(
    address _from,
    address _to,
    uint256 _tokenId,
    bytes _data
  )
    public
  {
    transferFrom(_from, _to, _tokenId);
    // solium-disable-next-line arg-overflow
    require(checkAndCallSafeTransfer(_from, _to, _tokenId, _data));
  }

  /**
   * @dev Returns whether the specified token exists
   * @param _tokenId uint256 ID of the token to query the existence of
   * @return whether the token exists
   */
  function _exists(uint256 _tokenId) internal view returns (bool) {
    address owner = tokenOwner[_tokenId];
    return owner != address(0);
  }

  /**
   * @dev Returns whether the given spender can transfer a given token ID
   * @param _spender address of the spender to query
   * @param _tokenId uint256 ID of the token to be transferred
   * @return bool whether the msg.sender is approved for the given token ID,
   *  is an operator of the owner, or is the owner of the token
   */
  function isApprovedOrOwner(
    address _spender,
    uint256 _tokenId
  )
    internal
    view
    returns (bool)
  {
    address owner = ownerOf(_tokenId);
    // Disable solium check because of
    // https://github.com/duaraghav8/Solium/issues/175
    // solium-disable-next-line operator-whitespace
    return (
      _spender == owner ||
      getApproved(_tokenId) == _spender ||
      isApprovedForAll(owner, _spender)
    );
  }

  /**
   * @dev Internal function to mint a new token
   * Reverts if the given token ID already exists
   * @param _to The address that will own the minted token
   * @param _tokenId uint256 ID of the token to be minted by the msg.sender
   */
  function _mint(address _to, uint256 _tokenId) internal {
    require(_to != address(0));
    addTokenTo(_to, _tokenId);
    emit Transfer(address(0), _to, _tokenId);
  }
  /**
   * @dev Internal function to burn a specific token
   * Reverts if the token does not exist
   * @param _tokenId uint256 ID of the token being burned by the msg.sender
   */
  function _burn(address _owner, uint256 _tokenId) internal {
    clearApproval(_owner, _tokenId);
    removeTokenFrom(_owner, _tokenId);
    emit Transfer(_owner, address(0), _tokenId);
  }

  /**
   * @dev Internal function to clear current approval of a given token ID
   * Reverts if the given address is not indeed the owner of the token
   * @param _owner owner of the token
   * @param _tokenId uint256 ID of the token to be transferred
   */
  function clearApproval(address _owner, uint256 _tokenId) internal {
    require(ownerOf(_tokenId) == _owner);
    if (tokenApprovals[_tokenId] != address(0)) {
      tokenApprovals[_tokenId] = address(0);
    }
  }

  /**
   * @dev Internal function to add a token ID to the list of a given address
   * @param _to address representing the new owner of the given token ID
   * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
   */
  function addTokenTo(address _to, uint256 _tokenId) internal {
    require(tokenOwner[_tokenId] == address(0));
    tokenOwner[_tokenId] = _to;
    ownedTokensCount[_to] = ownedTokensCount[_to].add(1);
  }

  /**
   * @dev Internal function to remove a token ID from the list of a given address
   * @param _from address representing the previous owner of the given token ID
   * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
   */
  function removeTokenFrom(address _from, uint256 _tokenId) internal {
    require(ownerOf(_tokenId) == _from);
    ownedTokensCount[_from] = ownedTokensCount[_from].sub(1);
    tokenOwner[_tokenId] = address(0);
  }

  /**
   * @dev Internal function to invoke `onERC721Received` on a target address
   * The call is not executed if the target address is not a contract
   * @param _from address representing the previous owner of the given token ID
   * @param _to target address that will receive the tokens
   * @param _tokenId uint256 ID of the token to be transferred
   * @param _data bytes optional data to send along with the call
   * @return whether the call correctly returned the expected magic value
   */
  function checkAndCallSafeTransfer(
    address _from,
    address _to,
    uint256 _tokenId,
    bytes _data
  )
    internal
    returns (bool)
  {
    if (!_to.isContract()) {
      return true;
    }
    bytes4 retval = ERC721Receiver(_to).onERC721Received(
      msg.sender, _from, _tokenId, _data);
    return (retval == ERC721_RECEIVED);
  }
  
   /**
   * 更新shop信息tokenId  
   */
   function transferTokenId(address _to, uint256 _tokenId) internal {

    shopInfoToken[_tokenId].merchantsAddress = _to;
  }
  
   /**
   *添加tokenId属性  
   */
  function _shop(uint256 _tokenId,string _merchantsName,address _merchantsAddress,string _brandsName,
            string _goodsName,string _goodsSpecifications,string _goodsAddress) internal{
      shopInfoToken[_tokenId].tokenId = _tokenId;
      shopInfoToken[_tokenId].merchantsName = _merchantsName;
      shopInfoToken[_tokenId].merchantsAddress = _merchantsAddress;
      shopInfoToken[_tokenId].brandsName = _brandsName;
      shopInfoToken[_tokenId].goodsName = _goodsName;
      shopInfoToken[_tokenId].goodsSpecifications = _goodsSpecifications;
      shopInfoToken[_tokenId].goodsAddress = _goodsAddress;
  }
   /**
    * 返回 tokenId属性 
    * 
   */
    function getShop(uint256 tokenId)public view returns(uint256 _tokenId,string _merchantsName,address _merchantsAddress,string _brandsName,
            string _goodsName,string _goodsSpecifications,string _goodsAddress){
                
         ShopInfo memory shopInfo = shopInfoToken[tokenId];
        return (shopInfo.tokenId,shopInfo.merchantsName,shopInfo.merchantsAddress,shopInfo.brandsName,
            shopInfo.goodsName,shopInfo.goodsSpecifications,shopInfo.goodsAddress);
    }
    
}
contract ERC721TokenInterface is SupportsInterfaceWithLookup, ERC721BasicToken {

  // Token name
  string internal name_;

  // Token symbol
  string internal symbol_;

  // 从所有者到拥有令牌ID的列表的映射
  mapping(address => uint256[]) internal ownedTokens;

  // 从令牌ID到所有者令牌列表索引的映射
  mapping(uint256 => uint256) internal ownedTokensIndex;

  // 具有用于枚举的所有令牌ID的数组
  uint256[] internal allTokens;

  // AltokEN数组中从令牌ID到位置的映射
  mapping(uint256 => uint256) internal allTokensIndex;

  // 可选的标记映射URI for
  mapping(uint256 => string) internal tokenURIs;

 
  /**
   * @dev Constructor function
   */
  constructor(string _name, string _symbol) public {
    name_ = _name;
    symbol_ = _symbol;

    // register the supported interfaces to conform to ERC721 via ERC165
    _registerInterface(InterfaceId_ERC721Enumerable);
    _registerInterface(InterfaceId_ERC721Metadata);
  }

  /**
   * @dev Gets the token name
   * @return string representing the token name
   */
  function name() external view returns (string) {
    return name_;
  }

  /**
   * @dev Gets the token symbol
   * @return string representing the token symbol
   */
  function symbol() external view returns (string) {
    return symbol_;
  }

  /**
   * @dev Gets the token ID at a given index of the tokens list of the requested owner
   * @param _owner address owning the tokens list to be accessed
   * @param _index uint256 representing the index to be accessed of the requested tokens list
   * @return uint256 token ID at the given index of the tokens list owned by the requested address
   */
  function tokenOfOwnerByIndex(address _owner,uint256 _index)public view returns (uint256) {
    require(_index < balanceOf(_owner));
    return ownedTokens[_owner][_index];
  }

  /**
   * 查询代币的总量
   */
  function totalSupply() public view returns (uint256) {
    return allTokens.length;
  }

  /**
   * 在令牌列表的给定索引处返回令牌ID
   */
  function tokenByIndex(uint256 _index) public view returns (uint256) {
    require(_index < totalSupply());
    return allTokens[_index];
  }

  /**
   * 用于将令牌ID添加到给定地址的列表中
   */
  function addTokenTo(address _to, uint256 _tokenId) internal {
    super.addTokenTo(_to, _tokenId);
    uint256 length = ownedTokens[_to].length;
    ownedTokens[_to].push(_tokenId);
    ownedTokensIndex[_tokenId] = length;
  }

  /**
   *发布一个新的tokenId
   */
  function _mint(address _to, uint256 _tokenId) internal {
    super._mint(_to, _tokenId);
    allTokensIndex[_tokenId] = allTokens.length;
    allTokens.push(_tokenId);
  }
 
    /**
     * 返回所有者所有的tokenID
    */ 
    function myTokens(address _owner)external view returns ( uint256[] ) {
        return ownedTokens[_owner];
    }
}


contract ERC721TokenRelease is ERC721TokenInterface,Ownable {
    
    constructor () public ERC721TokenInterface("HuRongGoodsToken", "HRGT") {
        owner = msg.sender;
    }
    
    function mint(uint256 tokenId,string merchantsName,address merchantsAddress,string brandsName,
            string goodsName,string goodsSpecifications,string goodsAddress) public onlyOwner {
     
        super._mint(merchantsAddress, tokenId);
        super._shop( tokenId, merchantsName, merchantsAddress, brandsName,
             goodsName, goodsSpecifications, goodsAddress);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值