Assignment ERC721

Create the truffle directory
Implement the IERC721 into your contracts directory.

Implementing IERC721.
Functions created!

pragma solidity >=0.6.0 <0.8.0;

import "./IERC721.sol";
import "../node_modules/@openzeppelin/contracts/utils/math/SafeMath.sol";

contract Kittycontract is IERC721 {

    using SafeMath for uint256;

    mapping(address=>uint256) private balances;
    uint256 private totalTokens;
    string private ticker;
    string private tickerName;

    struct tokenOwnersDetails {
        address owner;
        bool created;
    }
    mapping(uint256=>tokenOwnersDetails) private tokenOwners;

    function balanceOf(address owner) external override view returns (uint256 balance) {
        balance = balances[owner];
    }

    function totalSupply() external override view returns (uint256 total) {
        total = totalTokens;
    }

    function name() external override view returns (string memory tokenName) {
        tokenName = tickerName;
    }

    function symbol() external override view returns (string memory tokenSymbol) {  
        tokenSymbol = ticker;
    }

    function ownerOf(uint256 tokenId) external override view returns (address owner) {
        require(tokenOwners[tokenId].created, "Token does not exist");

        owner = tokenOwners[tokenId].owner;
    }

    function transfer(address to, uint256 tokenId) external override {
        require(to != address(0), "TO address must be defined.");
        require(to != address(this), "Cannot transfer to the contract itself");
        require(tokenOwners[tokenId].created, "Token ID does not exist.");
        require(tokenOwners[tokenId].owner == msg.sender, "Only the Token owner can transfer it.");

        tokenOwners[tokenId].owner = to;

        balances[to] = balances[to].add(1);
        balances[msg.sender] = balances[to].sub(1);

        emit Transfer(msg.sender, to, tokenId);
    }

}
1 Like
ERC721 Assignment code
pragma solidity ^0.5.12;

import "./IERC721.sol";

contract CryptoBears is IERC721 {

    string public constant bearTokenName = "CryptoBears";   //Token Name
    string public constant bearTokenSymbol = "CB";          //Token Symbol
    
    struct Bear {
        uint256 genes;
        uint64 birthTime;
        uint32 mumId;
        uint32 dadId;
        uint16 generation;
    }

    Bear[] bears;

    
    mapping(address => uint256) tokenOwnershipCount; //Token owners balance
    mapping(uint256 => address) public tokenIdOwnerMapping; //Maps TokenId to owners address

    

     function balanceOf(address owner) external view returns (uint256 balance){
         return tokenOwnershipCount[owner];
     }

      function totalSupply() public view returns (uint256 total){
          return bears.length;
      }

      function name() external view returns (string memory tokenName){
          return bearTokenName;
      }

    function symbol() external view returns (string memory tokenSymbol){
        return bearTokenSymbol;
    }

     function ownerOf(uint256 tokenId) external view returns (address owner){
        return tokenIdOwnerMapping[tokenId];
     }

     function transfer(address to, uint256 _tokenId) external {
         require(_owns(msg.sender, _tokenId), "You are not the owner of this token!");
         require(to != address(0), "You cannot send tokens to this address!");
         require(to != address(this), "You cannot send tokens to this address!");

         _transfer(msg.sender, to, _tokenId);

         
     }

     function _transfer(address _from, address _to, uint256 _tokenId) internal {
            tokenOwnershipCount[_to]++;

            tokenIdOwnerMapping[_tokenId] = _to;

            if(_from != address(0)){
                tokenOwnershipCount[_from]--;
            }

            emit Transfer(_from, _to, _tokenId);
     }

     function _owns(address _claimant, uint256 _tokenId) internal view returns(bool) {
         return tokenIdOwnerMapping[_tokenId] == _claimant;
     }
}



1 Like
ERC721 First Assignment
pragma solidity 0.8.3;

import "./IERC721.sol";

contract catContract is IERC721 {

    mapping (address => uint256) private _balances;

    mapping (uint256 => address) private _ownerOf;

    uint256 _totalSupply = 100;

    string private _name = "Gato";

    string private _symbol = "GAT";
 
     
    function balanceOf(address owner) external override view returns (uint256 balance){
        return _balances[msg.sender];
    }

     
    function totalSupply() external override view returns (uint256 total){
        return _totalSupply;
    }

    
    function name() external view override returns (string memory tokenName){
        return _name;
    }

    
    function symbol() external view override returns (string memory tokenSymbol){
        return _symbol;
    }

    function ownerOf(uint256 tokenId) external view override returns (address owner){
        return _ownerOf[tokenId]; 
    }

/* @dev Transfers `tokenId` token from `msg.sender` to `to`.
     *
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - `to` can not be the contract address.
     * - `tokenId` token must be owned by `msg.sender`.
     *
     * Emits a {Transfer} event.
     */

    function transfer(address to, uint256 tokenId)override external{
        require(to != address(0), "Don't send it to 0");
        require(to != address(this),"Don't send to the contract");
        require(_ownerOf[tokenId] == msg.sender, "must be the owner");
        event Transfer(msg.sender, to, tokenId);
    }

}

Just watched Philips video, will fix the totalSupply function and the state variable as well :frowning: