Assignment - Get Kitty

Create a getKitty function that will return all properties of the cat.

1 Like
 function getBear(uint256 _tokenId) public view returns(uint256 genes, uint64 birthTime, uint32 mumId, uint32 dadId, uint16 generation) {
        return (
                bears[_tokenId].genes, 
                bears[_tokenId].birthTime, 
                bears[_tokenId].mumId, 
                bears[_tokenId].dadId, 
                bears[_tokenId].generation
                );
    }
1 Like
function getKitty(uint256 tokenId)public view returns(uint256, uint64, uint32, uint32, uint16){
        return (kitties[tokenId].genes, kitties[tokenId].birthTime, kitties[tokenId].mumId, kitties[tokenId].dadId, kitties[tokenId].generation);
    }

I just watched filips video, i learned like 3 new tricks, with that video *o *, will update my code accordingly.

1 Like

Hey guys, there is an error on line 59 that i am not sure how to rectify. Could it be a new syntax that has been implemented by a recent update?

Also my getCat function is on line 65.

Screenshot 2021-04-16 at 14.20.49

2 Likes

Hello @ol_frank. You need a space from cats.push(_cats) -1 the -1 separated.
If you still getting the error, please paste full contract code here, so I can find out what is missing.

1 Like

The space doesn’t resolve the issue unfortunately. @kenn.eth

pragma solidity ^0.8.3;

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


contract Catcontract is IERC721 {
using SafeMath for uint256;

string private  constant Name = "Purrfect";
string private constant Symbol = "PURR";
uint16 private constant Creation_Limit_Gen0 = 10;
event Birth(address owner, uint256 catId, uint256 mumId, uint256 dadId, uint256 genes);

modifier onlyOwner{
    require(msg.sender == address(this));
    _;
}

struct Cat {
    uint256 genes;
    uint64 birthTime;
    uint32 mumId;
    uint32 dadId;
    uint16 generation;
}
Cat[] cats;
//      owner      amount
mapping(address => uint256) ownerTokenBalance;

//      tokenId    owner of cat
mapping(uint256 => address) public catIndexToOwner;

uint256 public gen0Counter;

function createCatGen0(uint256 _genes)public onlyOwner{
    require(gen0Counter < Creation_Limit_Gen0);
    gen0Counter++;

    _createCat(_genes, 0, 0, 0, msg.sender);
}

function _createCat(
    uint256 _genes,
    uint256 _mumId,
    uint256 _dadId,
    uint256 _generation,
    address _owner
)internal returns(uint256){
    Cat memory _cat = Cat({
        genes: uint64(_genes),
        birthTime: uint64(block.timestamp),
        mumId: uint32(_mumId),
        dadId: uint32(_dadId),
        generation: uint16(_generation)
    });


    uint256 newCatId = cats.push(_cat) -1;

    emit Birth(_owner, newCatId, _mumId, _dadId, _genes);
    _transfer(address(0), _owner, newCatId);
    return newCatId;
}
    function getCat(uint256 _tokenId) public returns(uint256, uint64, uint32, uint32, uint16){
        return(cats[_tokenId].genes,
                cats[_tokenId].birthTime,
                cats[_tokenId].mumId,
                cats[_tokenId].dadId,
                cats[_tokenId].generation
                );
    }
    function balanceOf(address owner) external override view returns (uint256 balance){
        balance = ownerTokenBalance[owner];
        return balance;
    }

    function totalSupply() external override view returns (uint256 total){
        total = cats.length;
        return total; 
    }

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

    function transfer(address to, uint256 tokenId) external override{
        require(to != address(0), "Cannot Send Tokens To This Address");
        require(to != address(this), "Cannot Send Tokens To This Address");
        require(owns(msg.sender, tokenId), "You Must Own The Token You Are Sending");
        _transfer(msg.sender, to, tokenId);
        // ownerTokenBalance[to] = ownerTokenBalance[to].add(1);
        // ownerTokenBalance[msg.sender] = ownerTokenBalance[msg.sender].sub(1);
    }

    function _transfer(address from, address _to, uint256 _tokenId) internal {
        ownerTokenBalance[_to]++;
        catIndexToOwner[_tokenId] = _to;

        //(edge case senario) when we mint new cats we dont want to decrease the token count
        if(from != address (0)) {
            ownerTokenBalance[from]--;
        }
        emit Transfer(from, _to, _tokenId);
    }

    function owns(address claimant, uint256 _tokenId) internal view returns (bool){
        return catIndexToOwner[_tokenId] == claimant;
    }

}

This looks like is your solidity version. I just make a test of your contract and is sending same error. But when I switch to 0.5 is gone. Always try to use same version like the courses. The rest of the code looks good. Try and let me know

1 Like

Ahh okay thanks for your help @kenn.eth

Hey @kenn.eth , I tried changing it to the version on the course but it keeps on saying that my compiler version is still 0.8.3 even though i changed it to 0.5.12 in the truffle config.js file. So now my IERC721 and catContract has an error on the version declaration
Screenshot 2021-04-20 at 11.49.23 Screenshot 2021-04-20 at 13.44.42
Screenshot 2021-04-20 at 13.45.27

I used the following:

function getKitty(uint256 _kittyID) public view returns (Kitty memory){

        return kitties[_kittyID];

    }
1 Like

Oh this message is not an error. This is a message of the compiler of visual code sniped.
You can just ignore it. Change version and run it in Remix.
This is the code from you that I just modify and try it.

pragma solidity ^0.5.3;

contract Catcontract {

string private  constant Name = "Purrfect";
string private constant Symbol = "PURR";
uint16 private constant Creation_Limit_Gen0 = 10;
event Birth(address owner, uint256 catId, uint256 mumId, uint256 dadId, uint256 genes);

modifier onlyOwner{
    require(msg.sender == address(this));
    _;
}

struct Cat {
    uint256 genes;
    uint64 birthTime;
    uint32 mumId;
    uint32 dadId;
    uint16 generation;
}
Cat[] cats;
//      owner      amount
mapping(address => uint256) ownerTokenBalance;

//      tokenId    owner of cat
mapping(uint256 => address) public catIndexToOwner;

uint256 public gen0Counter;

function createCatGen0(uint256 _genes)public onlyOwner{
    require(gen0Counter < Creation_Limit_Gen0);
    gen0Counter++;

    _createCat(_genes, 0, 0, 0, msg.sender);
}

function _createCat(
    uint256 _genes,
    uint256 _mumId,
    uint256 _dadId,
    uint256 _generation,
    address _owner
)internal returns(uint256){
    Cat memory _cat = Cat({
        genes: uint64(_genes),
        birthTime: uint64(block.timestamp),
        mumId: uint32(_mumId),
        dadId: uint32(_dadId),
        generation: uint16(_generation)
    }); 

    uint newCatId = cats.push(_cat) -1;

    emit Birth(_owner, newCatId, _mumId, _dadId, _genes);

    return newCatId;
}
    function getCat(uint256 _tokenId) public returns(uint256, uint64, uint32, uint32, uint16){
        return(cats[_tokenId].genes,
                cats[_tokenId].birthTime,
                cats[_tokenId].mumId,
                cats[_tokenId].dadId,
                cats[_tokenId].generation
                );
    }
    
    function balanceOf(address owner) external  view returns (uint256 balance){
        balance = ownerTokenBalance[owner];
        return balance;
    }

    function totalSupply() external  view returns (uint256 total){
        total = cats.length;
        return total; 
    }

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

}

2021-04-21_00h48_36

1 Like

Nice one! Even though it underlined red its not a serious error? Im guess it won’t effect my Cat dapp further down the line then?

Thanks for your help by the way @kenn.eth appreciate it!

What version of Ownable are you using? The below one comes with error when I tried to inherited to Kittycontract. (Error: Identified not found or not unique)

`Pragma solidity >=0.5.0;

contract Ownable {
address internal owner;

modifier onlyOwner {
    require(msg.sender == owner);
    _;
}

constructor (){
    owner = msg.sender;
}

} `

  function getDoraemon(uint256 _tokenId) external view returns(
    uint256 genes,
    uint64 birthTime,
    uint32 mumId,
    uint32 dadId,
    uint16 generation,
    address owner
  ){
    genes = allTokens[_tokenId].genes;
    birthTime = allTokens[_tokenId].birthTime;
    mumId = allTokens[_tokenId].mumId;
    dadId = allTokens[_tokenId].dadId;
    generation = allTokens[_tokenId].generation;
    owner = tokenToOwner[_tokenId];
  }
1 Like

Hey @Haidar_Baqir wellcome! You have to use same version of the other contracs. Post all the code so i can test it in case not work for you.

My code to solve this

function getKitty(uint256 kittyId) public view returns(uint256, uint64, uint32, uint32, uint16){
             return (kitties[kittyId].genes, kitties[kittyId].birthTime, kitties[kittyId].mumId,
             kitties[kittyId].dadId, kitties[kittyId].generation); 
    }

test

truffle(develop)> let instance = await KittyContract.deployed()
undefined
truffle(develop)> instance.createKittyGen0(0, {value: '10000', from:accounts[0]})
{
  tx: '0xd5585084ba2e898f8035bf9ae6bb41aad95121b2e0648b16e6a713db38ddc1fa',
  receipt: {
    transactionHash: '0xd5585084ba2e898f8035bf9ae6bb41aad95121b2e0648b16e6a713db38ddc1fa',
    transactionIndex: 0,
    blockHash: '0x33e5af288409bf56ab07beae96c0fef0e713ab9411561caafccd39c631d264eb',
    blockNumber: 7,
    from: '0x3dd4fa6633b917895e583608b554a7d7f94ad545',
    to: '0x56ab74239f45b65a26ec3a2226ddef195227f53e',
    gasUsed: 118419,
    cumulativeGasUsed: 118419,
    contractAddress: null,
    logs: [ [Object], [Object] ],
    status: true,
    logsBloom: '0x00000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000010010000020040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000010000000000000000002000000000000000000000020000000040100000000000000000000000000000000000000000000000000000000',
    rawLogs: [ [Object], [Object] ]
  },
  logs: [
    {
      logIndex: 0,
      transactionIndex: 0,
      transactionHash: '0xd5585084ba2e898f8035bf9ae6bb41aad95121b2e0648b16e6a713db38ddc1fa',
      blockHash: '0x33e5af288409bf56ab07beae96c0fef0e713ab9411561caafccd39c631d264eb',
      blockNumber: 7,
      address: '0x56aB74239F45B65a26Ec3a2226Ddef195227f53e',
      type: 'mined',
      id: 'log_2c562b51',
      event: 'Birth',
      args: [Result]
    },
    {
      logIndex: 1,
      transactionIndex: 0,
      transactionHash: '0xd5585084ba2e898f8035bf9ae6bb41aad95121b2e0648b16e6a713db38ddc1fa',
      blockHash: '0x33e5af288409bf56ab07beae96c0fef0e713ab9411561caafccd39c631d264eb',
      blockNumber: 7,
      address: '0x56aB74239F45B65a26Ec3a2226Ddef195227f53e',
      type: 'mined',
      id: 'log_516d2ef4',
      event: 'Transfer',
      args: [Result]
    }
  ]
}
truffle(develop)> instance.getKitty(0)
Result {
  '0': BN {
    negative: 0,
    words: [ 0, <1 empty item> ],
    length: 1,
    red: null
  },
  '1': BN {
    negative: 0,
    words: [ 11112594, 24, <1 empty item> ],
    length: 2,
    red: null
  },
  '2': BN {
    negative: 0,
    words: [ 0, <1 empty item> ],
    length: 1,
    red: null
  },
  '3': BN {
    negative: 0,
    words: [ 0, <1 empty item> ],
    length: 1,
    red: null
  },
  '4': BN {
    negative: 0,
    words: [ 0, <1 empty item> ],
    length: 1,
    red: null
  }
}
1 Like

Hey man, I had the same error(solidity ^0.8.0) but you can solve it by splitting it into two operations:

Try this if it works for you:

cats.push(_cat);
uint256 newCatId = cats.length - 1;
2 Likes

Assignment here:

    event Breed(address owner, uint256 kittenId, uint256 mumId, uint256 dadId, uint256 genes, uint256 generation);

    function getKitty(uint256 _tokenId) external view returns(uint256, uint64, uint32, uint32, uint16) {
        return (
            kitties[_tokenId].genes,
            kitties[_tokenId].birthTime,
            kitties[_tokenId].mumId,
            kitties[_tokenId].dadId,
            kitties[_tokenId].generation
        );
    }

Questions: Where should I put the emit Breed (event)? Should I call _createKitty in this function as well? OR This is not yet the breeding part?

Hey, thanks @CryptoXyz, i managed to find the same solution too.

1 Like
    function getOrder(uint256 _id) external view returns(
        uint256 id,
        uint256 cost,
        string memory productName,
        string memory description,
        uint256 leadTime_in_days
    ) {
        return (
            orderVolume[_id].id,
            orderVolume[_id].cost,
            orderVolume[_id].productName,
            orderVolume[_id].description,
            orderVolume[_id].leadTime_in_days
        );
    } 

1 Like