Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A simple implementation #39

Open
xinbenlv opened this issue May 9, 2021 · 0 comments
Open

A simple implementation #39

xinbenlv opened this issue May 9, 2021 · 0 comments

Comments

@xinbenlv
Copy link
Owner

xinbenlv commented May 9, 2021

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;


/** A reference implementation of EIP-2135
 *
 * For simplicity, this reference implementation creates a super simple `issueTicket` function without
 * restriction on who can issue new tickets and under what condition a ticket can be issued.
 */
contract Ticket /* is ERC2135, ERC721, ERC173, ERC165 */ {

  address private issuer;

  mapping(uint256 => uint256) private ticketStates; /** 0 = unissued, 1 = issued, unconsumed, 2 = consumed); */
  mapping(uint256 => address) private ticketHolders;
  
  constructor() {
    issuer = msg.sender;
  }
  
  /** ERC 2135 */
  event OnConsumption(uint256 _tockenId, address _consumer);
  
  /** ERC 721 */
  event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
 
  function transferFrom(address _from, address _to, uint256 _tokenId) external payable {
    require(_from == msg.sender, "The sender must be the source of transfer.");
    require(_from == ticketHolders[_tokenId], "The sender must hold the ticket.");
    require(1 == ticketStates[_tokenId], "The ticket must be issued but not consumed.");
    ticketHolders[_tokenId] = _to;
    emit Transfer(_from, _to, _tokenId);
    return;
  }

  /**
   * ERC 2135
   */
  function issueTicket(uint256 _ticketId, address _receiver) public returns (bool success) {
    require (msg.sender == issuer, "Only ticket issuer can issue ticket.");
    require (ticketStates[_ticketId] == 0, "The ticket address has been issued and not consumed yet, it cannot be issued again."); // ticket needs to be not issued yet;
    ticketStates[_ticketId] = 1;
    ticketHolders[_ticketId] = _receiver;
    return true;
  }

  /**
   * ERC 2135
   */
  function consume(uint256 _ticketId) public returns (bool success) {
    require (ticketHolders[_ticketId] == msg.sender, "Only the current ticket holder can request to consume a ticket");
    require (ticketStates[_ticketId] == 1, "The ticket needs to be issued but not consumed yet.");
    ticketStates[_ticketId] = 2;
    emit OnConsumption(_ticketId, msg.sender);
    return true;
  }

  /**
   * ERC 2135
   */
  function isConsumable(uint256 _ticketId) public view returns (bool consumable) {
    return ticketStates[_ticketId] == 1;
  }
  
  
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant